1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "rd_utils.h"
17
18 #include <gtest/gtest.h>
19 #include <sys/stat.h>
20 #include <sys/types.h>
21
22 #include <atomic>
23 #include <climits>
24 #include <mutex>
25 #include <string>
26 #include <unordered_set>
27
28 #include "grd_api_manager.h"
29 #include "grd_type_export.h"
30 #include "rdb_helper.h"
31 #include "task_executor.h"
32
33 using namespace testing::ext;
34 using namespace OHOS::NativeRdb;
35
36 static std::mutex g_mutex;
37 static std::unordered_set<void *> allocatedAddresses;
38 static bool g_isRecord = false;
39
operator new[](size_t size,const std::nothrow_t & tag)40 void *operator new[](size_t size, const std::nothrow_t &tag) noexcept
41 {
42 void *ptr = std::malloc(size);
43 if (g_isRecord && ptr != nullptr) {
44 std::lock_guard<std::mutex> lock(g_mutex);
45 allocatedAddresses.insert(ptr);
46 }
47 return ptr;
48 }
49
operator delete[](void * ptr)50 void operator delete[](void *ptr) noexcept
51 {
52 if (g_isRecord && ptr != nullptr) {
53 std::lock_guard<std::mutex> lock(g_mutex);
54 allocatedAddresses.erase(ptr);
55 }
56 std::free(ptr);
57 }
58
59 namespace Test {
60 class RdUtilsTest : public testing::Test {
61 public:
62 static void SetUpTestCase(void);
63 static void TearDownTestCase(void);
SetUp(void)64 void SetUp(void) {};
TearDown(void)65 void TearDown(void) {};
66 };
67
SetUpTestCase(void)68 void RdUtilsTest::SetUpTestCase(void)
69 {
70 std::lock_guard<std::mutex> lock(g_mutex);
71 g_isRecord = true;
72 }
73
TearDownTestCase(void)74 void RdUtilsTest::TearDownTestCase(void)
75 {
76 std::lock_guard<std::mutex> lock(g_mutex);
77 g_isRecord = false;
78 }
79
ScheduleMock(void * param)80 static void ScheduleMock(void *param)
81 {
82 (void)param;
83 int sleepTime = 20;
84 std::this_thread::sleep_for(std::chrono::seconds(sleepTime));
85 }
86
87 /**
88 * @tc.name: RdUtils_Test_001
89 * @tc.desc: Normal testCase of sqlite_utils for IsSpecial, if sqlType is special
90 * @tc.type: FUNC
91 */
92 HWTEST_F(RdUtilsTest, RdUtils_Test_001, TestSize.Level1)
93 {
94 EXPECT_EQ(RdUtils::TransferGrdErrno(1), 1);
95 EXPECT_EQ(RdUtils::TransferGrdErrno(0), E_OK);
96 EXPECT_EQ(RdUtils::TransferGrdErrno(-9999), E_ERROR);
97 }
98
99 HWTEST_F(RdUtilsTest, RdUtils_Test_002, TestSize.Level1)
100 {
101 EXPECT_EQ(RdUtils::TransferGrdTypeToColType(GRD_DB_DATATYPE_INTEGER), ColumnType::TYPE_INTEGER);
102 EXPECT_EQ(RdUtils::TransferGrdTypeToColType(GRD_DB_DATATYPE_FLOAT), ColumnType::TYPE_FLOAT);
103 EXPECT_EQ(RdUtils::TransferGrdTypeToColType(GRD_DB_DATATYPE_TEXT), ColumnType::TYPE_STRING);
104 EXPECT_EQ(RdUtils::TransferGrdTypeToColType(GRD_DB_DATATYPE_BLOB), ColumnType::TYPE_BLOB);
105 EXPECT_EQ(RdUtils::TransferGrdTypeToColType(GRD_DB_DATATYPE_FLOATVECTOR), ColumnType::TYPE_FLOAT32_ARRAY);
106 EXPECT_EQ(RdUtils::TransferGrdTypeToColType(GRD_DB_DATATYPE_NULL), ColumnType::TYPE_NULL);
107 }
108
109 /**
110 * @tc.name: RdUtils_Test_003
111 * @tc.desc: Test RdSqlRegistryThreadPool
112 * @tc.type: FUNC
113 */
114 HWTEST_F(RdUtilsTest, RdUtils_Test_003, TestSize.Level1)
115 {
116 if (!IsUsingArkData()) {
117 GTEST_SKIP() << "Current testcase is not compatible from current rdb";
118 }
119 std::string dbPath = "/data/test/execute_test.db";
120 std::string configStr = "{}";
121 RdbHelper::DeleteRdbStore(dbPath);
122
123 GRD_DB *db = nullptr;
124 EXPECT_EQ(RdUtils::RdDbOpen(dbPath.c_str(), configStr.c_str(), GRD_DB_OPEN_CREATE, &db), E_OK);
125 ASSERT_EQ(RdUtils::RdSqlRegistryThreadPool(db), E_OK);
126
127 ASSERT_NE(RdUtils::threadPool_.schedule, nullptr);
128 ASSERT_NE(RdUtils::threadPool_.remove, nullptr);
129
130 TaskExecutor::TaskId taskId = RdUtils::threadPool_.schedule(reinterpret_cast<void *>(ScheduleMock), nullptr);
131 ASSERT_NE(static_cast<uint64_t>(taskId), TaskExecutor::INVALID_TASK_ID);
132
133 int sleepTime = 2;
134 std::this_thread::sleep_for(std::chrono::seconds(sleepTime));
135
136 bool ret = RdUtils::threadPool_.remove(static_cast<uint64_t>(taskId), false);
137 // expect false because this task is running, will remove from exec list
138 ASSERT_FALSE(ret);
139
140 EXPECT_EQ(RdUtils::RdDbClose(db, 0), E_OK);
141 RdbHelper::DeleteRdbStore(dbPath);
142 }
143
144 /**
145 * @tc.name: RdUtils_Test_004
146 * @tc.desc: Test bind empty string
147 * @tc.type: FUNC
148 */
149 HWTEST_F(RdUtilsTest, RdUtils_Test_004, TestSize.Level1)
150 {
151 if (!IsUsingArkData()) {
152 GTEST_SKIP() << "Current testcase is not compatible from current rdb";
153 }
154 GRD_SqlStmt *stmt = nullptr;
155 uint32_t idx = 0;
156 const char *str = "";
157 EXPECT_EQ(RdUtils::RdSqlBindText(stmt, idx, str, -1, nullptr), E_INVALID_ARGS);
158 EXPECT_EQ(RdUtils::RdSqlBindText(stmt, idx, str, 0, nullptr), E_INVALID_ARGS);
159 }
160
161 /**
162 * @tc.name: RdUtils_Test_005
163 * @tc.desc: Test RdSqlBindBlob
164 * @tc.type: FUNC
165 */
166 HWTEST_F(RdUtilsTest, RdUtils_Test_005, TestSize.Level0)
167 {
168 if (!IsUsingArkData()) {
169 GTEST_SKIP() << "Current testcase is not compatible from current rdb";
170 }
171 GRD_SqlStmt *stmtHandle = nullptr;
172 const uint8_t testData[] = { 0x05, 0x06 };
173 const int32_t dataLen = sizeof(testData);
174 auto ret = RdUtils::RdSqlBindBlob(stmtHandle, 1, testData, dataLen, nullptr);
175 EXPECT_EQ(ret, E_INVALID_ARGS);
176 EXPECT_TRUE(allocatedAddresses.empty());
177 }
178
179 /**
180 * @tc.name: RdUtils_Test_006
181 * @tc.desc: Test RdSqlBindBlob
182 * @tc.type: FUNC
183 */
184 HWTEST_F(RdUtilsTest, RdUtils_Test_006, TestSize.Level0)
185 {
186 if (!IsUsingArkData()) {
187 GTEST_SKIP() << "Current testcase is not compatible from current rdb";
188 }
189 GRD_SqlStmt *stmtHandle = nullptr;
190 const char *testStr = "Test";
191 auto ret = RdUtils::RdSqlBindText(stmtHandle, 1, testStr, strlen(testStr), nullptr);
192 EXPECT_EQ(ret, E_INVALID_ARGS);
193 EXPECT_TRUE(allocatedAddresses.empty());
194 }
195
196 /**
197 * @tc.name: RdUtils_Test_007
198 * @tc.desc: Test RdSqlBindFloatVector
199 * @tc.type: FUNC
200 */
201 HWTEST_F(RdUtilsTest, RdUtils_Test_007, TestSize.Level0)
202 {
203 if (!IsUsingArkData()) {
204 GTEST_SKIP() << "Current testcase is not compatible from current rdb";
205 }
206 GRD_SqlStmt *stmtHandle = nullptr;
207 float testData[] = { 1.1f, 2.2f, 3.3f };
208 uint32_t dim = sizeof(testData) / sizeof(float);
209 auto ret = RdUtils::RdSqlBindFloatVector(stmtHandle, 2, testData, dim, nullptr);
210 EXPECT_EQ(ret, E_INVALID_ARGS);
211 EXPECT_TRUE(allocatedAddresses.empty());
212 }
213 } // namespace Test
214