1 /*
2 * Copyright (c) 2022 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 <gtest/gtest.h>
17
18 #include "distributeddb_tools_unit_test.h"
19 #include "native_sqlite.h"
20 #include "platform_specific.h"
21 #include "sqlite_import.h"
22
23 using namespace testing::ext;
24 using namespace DistributedDB;
25 using namespace DistributedDBUnitTest;
26 using namespace std;
27
28 namespace {
29 string g_testDir;
30 string g_dbDir;
31 sqlite3 *g_db = nullptr;
32
33 const int MAX_BLOB_READ_SIZE = 5 * 1024 * 1024; // 5M limit
34 const int MAX_TEXT_READ_SIZE = 5 * 1024 * 1024; // 5M limit
35 }
36
37 class DistributedDBSqliteUtilsTest : public testing::Test {
38 public:
39 static void SetUpTestCase(void);
40 static void TearDownTestCase(void);
41 void SetUp();
42 void TearDown();
43 };
44
SetUpTestCase(void)45 void DistributedDBSqliteUtilsTest::SetUpTestCase(void)
46 {
47 DistributedDBToolsUnitTest::TestDirInit(g_testDir);
48 LOGI("The test db is:%s", g_testDir.c_str());
49 g_dbDir = g_testDir + "/";
50 }
51
TearDownTestCase(void)52 void DistributedDBSqliteUtilsTest::TearDownTestCase(void)
53 {
54 }
55
SetUp()56 void DistributedDBSqliteUtilsTest::SetUp()
57 {
58 DistributedDBToolsUnitTest::PrintTestCaseInfo();
59
60 g_db = NativeSqlite::CreateDataBase(g_dbDir + "test.db");
61 }
62
TearDown()63 void DistributedDBSqliteUtilsTest::TearDown()
64 {
65 sqlite3_close_v2(g_db);
66 g_db = nullptr;
67 if (DistributedDBToolsUnitTest::RemoveTestDbFiles(g_testDir) != 0) {
68 LOGE("rm test db files error.");
69 }
70 }
71
72 /**
73 * @tc.name: GetBlobTest001
74 * @tc.desc: Get blob size over limit
75 * @tc.type: FUNC
76 * @tc.require:
77 * @tc.author: lianhuix
78 */
79 HWTEST_F(DistributedDBSqliteUtilsTest, GetBlobTest001, TestSize.Level1)
80 {
81 NativeSqlite::ExecSql(g_db, "CREATE TABLE IF NOT EXISTS t1 (a INT, b BLOB);");
82
83 vector<uint8_t> blob;
84 blob.resize(MAX_BLOB_READ_SIZE + 2); // 2: over limit
85 std::fill(blob.begin(), blob.end(), static_cast<uint8_t>('a'));
86
__anondfabdd510202(sqlite3_stmt *stmt) 87 NativeSqlite::ExecSql(g_db, "INSERT INTO t1 VALUES(?, ?)", [&blob](sqlite3_stmt *stmt) {
88 (void)SQLiteUtils::BindInt64ToStatement(stmt, 1, 1);
89 (void)SQLiteUtils::BindBlobToStatement(stmt, 2, blob); // 2: bind index
90 return E_OK;
91 }, nullptr);
92
__anondfabdd510302(sqlite3_stmt *stmt) 93 NativeSqlite::ExecSql(g_db, "SELECT b FROM t1", nullptr, [](sqlite3_stmt *stmt) {
94 Value val;
95 EXPECT_EQ(SQLiteUtils::GetColumnBlobValue(stmt, 0, val), E_OK);
96 EXPECT_EQ(static_cast<int>(val.size()), MAX_BLOB_READ_SIZE + 1);
97 return E_OK;
98 });
99 }
100
101 /**
102 * @tc.name: GetBlobTest002
103 * @tc.desc: Get blob size equal limit
104 * @tc.type: FUNC
105 * @tc.require:
106 * @tc.author: lianhuix
107 */
108 HWTEST_F(DistributedDBSqliteUtilsTest, GetBlobTest002, TestSize.Level1)
109 {
110 NativeSqlite::ExecSql(g_db, "CREATE TABLE IF NOT EXISTS t1 (a INT, b BLOB);");
111
112 vector<uint8_t> blob;
113 blob.resize(MAX_BLOB_READ_SIZE);
114 std::fill(blob.begin(), blob.end(), static_cast<uint8_t>('a'));
115
__anondfabdd510402(sqlite3_stmt *stmt) 116 NativeSqlite::ExecSql(g_db, "INSERT INTO t1 VALUES(?, ?)", [&blob](sqlite3_stmt *stmt) {
117 (void)SQLiteUtils::BindInt64ToStatement(stmt, 1, 1);
118 (void)SQLiteUtils::BindBlobToStatement(stmt, 2, blob); // 2: bind index
119 return E_OK;
120 }, nullptr);
121
__anondfabdd510502(sqlite3_stmt *stmt) 122 NativeSqlite::ExecSql(g_db, "SELECT b FROM t1", nullptr, [&blob](sqlite3_stmt *stmt) {
123 Value val;
124 EXPECT_EQ(SQLiteUtils::GetColumnBlobValue(stmt, 0, val), E_OK);
125 EXPECT_EQ(val, blob);
126 return E_OK;
127 });
128 }
129
130 /**
131 * @tc.name: GetBlobTest003
132 * @tc.desc: Get blob size equal zero
133 * @tc.type: FUNC
134 * @tc.require:
135 * @tc.author: lianhuix
136 */
137 HWTEST_F(DistributedDBSqliteUtilsTest, GetBlobTest003, TestSize.Level1)
138 {
139 NativeSqlite::ExecSql(g_db, "CREATE TABLE IF NOT EXISTS t1 (a INT, b BLOB);");
140
141 vector<uint8_t> blob;
142 blob.resize(0);
143
__anondfabdd510602(sqlite3_stmt *stmt) 144 NativeSqlite::ExecSql(g_db, "INSERT INTO t1 VALUES(?, ?)", [&blob](sqlite3_stmt *stmt) {
145 (void)SQLiteUtils::BindInt64ToStatement(stmt, 1, 1);
146 (void)SQLiteUtils::BindBlobToStatement(stmt, 2, blob); // 2: bind index
147 return E_OK;
148 }, nullptr);
149
__anondfabdd510702(sqlite3_stmt *stmt) 150 NativeSqlite::ExecSql(g_db, "SELECT b FROM t1", nullptr, [&blob](sqlite3_stmt *stmt) {
151 Value val;
152 EXPECT_EQ(SQLiteUtils::GetColumnBlobValue(stmt, 0, val), E_OK);
153 EXPECT_EQ(val, blob);
154 return E_OK;
155 });
156 }
157
158 /**
159 * @tc.name: GetTextTest001
160 * @tc.desc: Get text size over limit
161 * @tc.type: FUNC
162 * @tc.require:
163 * @tc.author: lianhuix
164 */
165 HWTEST_F(DistributedDBSqliteUtilsTest, GetTextTest001, TestSize.Level1)
166 {
167 NativeSqlite::ExecSql(g_db, "CREATE TABLE IF NOT EXISTS t1 (a INT, b TEXT);");
168
169 std::string text;
170 text.resize(MAX_TEXT_READ_SIZE + 2); // 2: over limit
171 std::fill(text.begin(), text.end(), 'a');
172
__anondfabdd510802(sqlite3_stmt *stmt) 173 NativeSqlite::ExecSql(g_db, "INSERT INTO t1 VALUES(?, ?)", [&text](sqlite3_stmt *stmt) {
174 (void)SQLiteUtils::BindInt64ToStatement(stmt, 1, 1);
175 (void)SQLiteUtils::BindTextToStatement(stmt, 2, text); // 2: bind index
176 return E_OK;
177 }, nullptr);
178
__anondfabdd510902(sqlite3_stmt *stmt) 179 NativeSqlite::ExecSql(g_db, "SELECT b FROM t1", nullptr, [](sqlite3_stmt *stmt) {
180 std::string val;
181 EXPECT_EQ(SQLiteUtils::GetColumnTextValue(stmt, 0, val), E_OK);
182 EXPECT_EQ(static_cast<int>(val.size()), MAX_TEXT_READ_SIZE + 1);
183 return E_OK;
184 });
185 }
186
187 /**
188 * @tc.name: GetTextTest002
189 * @tc.desc: Get text size equal limit
190 * @tc.type: FUNC
191 * @tc.require:
192 * @tc.author: lianhuix
193 */
194 HWTEST_F(DistributedDBSqliteUtilsTest, GetTextTest002, TestSize.Level1)
195 {
196 NativeSqlite::ExecSql(g_db, "CREATE TABLE IF NOT EXISTS t1 (a INT, b TEXT);");
197
198 std::string text;
199 text.resize(MAX_TEXT_READ_SIZE);
200 std::fill(text.begin(), text.end(), 'a');
201
__anondfabdd510a02(sqlite3_stmt *stmt) 202 NativeSqlite::ExecSql(g_db, "INSERT INTO t1 VALUES(?, ?)", [&text](sqlite3_stmt *stmt) {
203 (void)SQLiteUtils::BindInt64ToStatement(stmt, 1, 1);
204 (void)SQLiteUtils::BindTextToStatement(stmt, 2, text); // 2: bind index
205 return E_OK;
206 }, nullptr);
207
__anondfabdd510b02(sqlite3_stmt *stmt) 208 NativeSqlite::ExecSql(g_db, "SELECT b FROM t1", nullptr, [&text](sqlite3_stmt *stmt) {
209 std::string val;
210 EXPECT_EQ(SQLiteUtils::GetColumnTextValue(stmt, 0, val), E_OK);
211 EXPECT_EQ(val, text);
212 return E_OK;
213 });
214 }
215
216 /**
217 * @tc.name: GetTextTest003
218 * @tc.desc: Get text size equal zero
219 * @tc.type: FUNC
220 * @tc.require:
221 * @tc.author: lianhuix
222 */
223 HWTEST_F(DistributedDBSqliteUtilsTest, GetTextTest003, TestSize.Level1)
224 {
225 NativeSqlite::ExecSql(g_db, "CREATE TABLE IF NOT EXISTS t1 (a INT, b TEXT);");
226
227 std::string text;
228 text.resize(0);
229
__anondfabdd510c02(sqlite3_stmt *stmt) 230 NativeSqlite::ExecSql(g_db, "INSERT INTO t1 VALUES(?, ?)", [&text](sqlite3_stmt *stmt) {
231 (void)SQLiteUtils::BindInt64ToStatement(stmt, 1, 1);
232 (void)SQLiteUtils::BindTextToStatement(stmt, 2, text); // 2: bind index
233 return E_OK;
234 }, nullptr);
235
__anondfabdd510d02(sqlite3_stmt *stmt) 236 NativeSqlite::ExecSql(g_db, "SELECT b FROM t1", nullptr, [&text](sqlite3_stmt *stmt) {
237 std::string val;
238 EXPECT_EQ(SQLiteUtils::GetColumnTextValue(stmt, 0, val), E_OK);
239 EXPECT_EQ(val, text);
240 return E_OK;
241 });
242 }