|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +#include "arrow/flight/sql/odbc/tests/odbc_test_suite.h" |
| 18 | + |
| 19 | +#ifdef _WIN32 |
| 20 | +# include <windows.h> |
| 21 | +#endif |
| 22 | + |
| 23 | +#include <sql.h> |
| 24 | +#include <sqltypes.h> |
| 25 | +#include <sqlucode.h> |
| 26 | + |
| 27 | +#include "gmock/gmock.h" |
| 28 | +#include "gtest/gtest.h" |
| 29 | + |
| 30 | +namespace arrow::flight::sql::odbc { |
| 31 | + |
| 32 | +TEST(SQLAllocHandle, TestSQLAllocHandleEnv) { |
| 33 | + // ODBC Environment |
| 34 | + SQLHENV env; |
| 35 | + |
| 36 | + // Allocate an environment handle |
| 37 | + SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env); |
| 38 | + |
| 39 | + EXPECT_THAT(env, ::testing::NotNull()); |
| 40 | +} |
| 41 | + |
| 42 | +TEST(SQLAllocEnv, TestSQLAllocEnv) { |
| 43 | + // ODBC Environment |
| 44 | + SQLHENV env; |
| 45 | + |
| 46 | + // Allocate an environment handle |
| 47 | + SQLRETURN return_value = SQLAllocEnv(&env); |
| 48 | + |
| 49 | + EXPECT_EQ(SQL_SUCCESS, return_value); |
| 50 | +} |
| 51 | + |
| 52 | +TEST(SQLAllocHandle, TestSQLAllocHandleConnect) { |
| 53 | + // ODBC Environment |
| 54 | + SQLHENV env; |
| 55 | + SQLHDBC conn; |
| 56 | + |
| 57 | + // Allocate an environment handle |
| 58 | + SQLRETURN return_value = SQLAllocEnv(&env); |
| 59 | + |
| 60 | + EXPECT_EQ(SQL_SUCCESS, return_value); |
| 61 | + |
| 62 | + // Allocate a connection using alloc handle |
| 63 | + SQLRETURN return_alloc_handle = SQLAllocHandle(SQL_HANDLE_DBC, env, &conn); |
| 64 | + |
| 65 | + EXPECT_EQ(SQL_SUCCESS, return_alloc_handle); |
| 66 | +} |
| 67 | + |
| 68 | +TEST(SQLAllocConnect, TestSQLAllocHandleConnect) { |
| 69 | + // ODBC Environment |
| 70 | + SQLHENV env; |
| 71 | + SQLHDBC conn; |
| 72 | + |
| 73 | + // Allocate an environment handle |
| 74 | + SQLRETURN return_value = SQLAllocEnv(&env); |
| 75 | + |
| 76 | + EXPECT_EQ(SQL_SUCCESS, return_value); |
| 77 | + |
| 78 | + // Allocate a connection using alloc handle |
| 79 | + SQLRETURN return_alloc_connect = SQLAllocConnect(env, &conn); |
| 80 | + |
| 81 | + EXPECT_EQ(SQL_SUCCESS, return_alloc_connect); |
| 82 | +} |
| 83 | + |
| 84 | +TEST(SQLFreeHandle, TestSQLFreeHandleEnv) { |
| 85 | + // ODBC Environment |
| 86 | + SQLHENV env; |
| 87 | + |
| 88 | + // Allocate an environment handle |
| 89 | + SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env); |
| 90 | + |
| 91 | + // Free an environment handle |
| 92 | + SQLRETURN return_value = SQLFreeHandle(SQL_HANDLE_ENV, env); |
| 93 | + |
| 94 | + EXPECT_EQ(SQL_SUCCESS, return_value); |
| 95 | +} |
| 96 | + |
| 97 | +TEST(SQLFreeEnv, TestSQLFreeEnv) { |
| 98 | + // ODBC Environment |
| 99 | + SQLHENV env; |
| 100 | + |
| 101 | + // Allocate an environment handle |
| 102 | + SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env); |
| 103 | + |
| 104 | + // Free an environment handle |
| 105 | + SQLRETURN return_value = SQLFreeEnv(env); |
| 106 | + |
| 107 | + EXPECT_EQ(SQL_SUCCESS, return_value); |
| 108 | +} |
| 109 | + |
| 110 | +TEST(SQLFreeHandle, TestSQLFreeHandleConnect) { |
| 111 | + // ODBC Environment |
| 112 | + SQLHENV env; |
| 113 | + SQLHDBC conn; |
| 114 | + |
| 115 | + // Allocate an environment handle |
| 116 | + SQLRETURN return_value = SQLAllocEnv(&env); |
| 117 | + |
| 118 | + EXPECT_EQ(SQL_SUCCESS, return_value); |
| 119 | + |
| 120 | + // Allocate a connection using alloc handle |
| 121 | + SQLRETURN return_alloc_handle = SQLAllocHandle(SQL_HANDLE_DBC, env, &conn); |
| 122 | + |
| 123 | + EXPECT_EQ(SQL_SUCCESS, return_alloc_handle); |
| 124 | + |
| 125 | + // Free the created connection using free handle |
| 126 | + SQLRETURN return_free_handle = SQLFreeHandle(SQL_HANDLE_DBC, conn); |
| 127 | + |
| 128 | + EXPECT_EQ(SQL_SUCCESS, return_free_handle); |
| 129 | +} |
| 130 | + |
| 131 | +TYPED_TEST(FlightSQLODBCTestBase, TestFreeNullHandles) { |
| 132 | + // Verifies attempt to free invalid handle does not cause segfault |
| 133 | + // Attempt to free null statement handle |
| 134 | + SQLRETURN ret = SQLFreeHandle(SQL_HANDLE_STMT, this->stmt); |
| 135 | + |
| 136 | + EXPECT_EQ(SQL_INVALID_HANDLE, ret); |
| 137 | + |
| 138 | + // Attempt to free null connection handle |
| 139 | + ret = SQLFreeHandle(SQL_HANDLE_DBC, this->conn); |
| 140 | + |
| 141 | + EXPECT_EQ(SQL_INVALID_HANDLE, ret); |
| 142 | + |
| 143 | + // Attempt to free null environment handle |
| 144 | + ret = SQLFreeHandle(SQL_HANDLE_ENV, this->env); |
| 145 | + |
| 146 | + EXPECT_EQ(SQL_INVALID_HANDLE, ret); |
| 147 | +} |
| 148 | + |
| 149 | +TEST(SQLFreeConnect, TestSQLFreeConnect) { |
| 150 | + // ODBC Environment |
| 151 | + SQLHENV env; |
| 152 | + SQLHDBC conn; |
| 153 | + |
| 154 | + // Allocate an environment handle |
| 155 | + SQLRETURN return_env = SQLAllocEnv(&env); |
| 156 | + |
| 157 | + EXPECT_EQ(SQL_SUCCESS, return_env); |
| 158 | + |
| 159 | + // Allocate a connection using alloc handle |
| 160 | + SQLRETURN return_alloc_handle = SQLAllocHandle(SQL_HANDLE_DBC, env, &conn); |
| 161 | + |
| 162 | + EXPECT_EQ(SQL_SUCCESS, return_alloc_handle); |
| 163 | + |
| 164 | + // Free the created connection using free connect |
| 165 | + SQLRETURN return_free_connect = SQLFreeConnect(conn); |
| 166 | + |
| 167 | + EXPECT_EQ(SQL_SUCCESS, return_free_connect); |
| 168 | +} |
| 169 | + |
| 170 | +} // namespace arrow::flight::sql::odbc |
0 commit comments