1 /*
2 * Copyright (c) 2025 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 #include <gtest/gtest.h>
16 #include <sys/stat.h>
17 #include <sys/types.h>
18
19 #include <cstdio>
20 #include <fstream>
21 #include <string>
22 #include <unistd.h>
23 #include <vector>
24
25 #include "sqlite3sym.h"
26
27 using namespace testing::ext;
28 using namespace std;
29
30 #define TEST_DIR "./sqlitetest"
31 #define TEST_DB (TEST_DIR "/test.db")
32 #define TEST_DWR (TEST_DIR "/test.db-dwr")
33
UtSqliteLogPrint(const void * data,int err,const char * msg)34 static void UtSqliteLogPrint(const void *data, int err, const char *msg)
35 {
36 std::cout << "LibSQLiteMetaDwrTest SQLite xLog err:" << err << ", msg:" << msg << std::endl;
37 }
38
39 class LibSQLiteMetaDwrTest : public testing::Test {
40 public:
41 static void SetUpTestCase(void);
42 static void TearDownTestCase(void);
43 void SetUp();
44 void TearDown();
45 };
46
SetUpTestCase(void)47 void LibSQLiteMetaDwrTest::SetUpTestCase(void)
48 {
49 // permission 0770
50 mkdir(TEST_DIR, 0770);
51 }
52
TearDownTestCase(void)53 void LibSQLiteMetaDwrTest::TearDownTestCase(void)
54 {
55 }
56
SetUp(void)57 void LibSQLiteMetaDwrTest::SetUp(void)
58 {
59 unlink(TEST_DB);
60 unlink(TEST_DWR);
61 sqlite3_config(SQLITE_CONFIG_LOG, &UtSqliteLogPrint, NULL);
62 }
63
TearDown(void)64 void LibSQLiteMetaDwrTest::TearDown(void)
65 {
66 sqlite3_config(SQLITE_CONFIG_LOG, NULL, NULL);
67 }
68
69 /**
70 * @tc.name: Lib_SQLite_Meta_Dwr_Test_001
71 * @tc.desc: Test write meta dwr pages out of range.
72 * @tc.type: FUNC
73 */
74 HWTEST_F(LibSQLiteMetaDwrTest, Lib_SQLite_Meta_Dwr_Test_001, TestSize.Level2)
75 {
76 /**
77 * @tc.steps: step1. Open database
78 * @tc.expected: step1. Return SQLITE_OK
79 */
80 sqlite3 *db = NULL;
81 EXPECT_EQ(sqlite3_open(TEST_DB, &db), SQLITE_OK);
82 /**
83 * @tc.steps: step2. Enable meta dwr
84 * @tc.expected: step2. Execute successfully
85 */
86 EXPECT_EQ(sqlite3_exec(db, "pragma meta_double_write=enabled", NULL, NULL, NULL), SQLITE_OK);
87 /**
88 * @tc.steps: step3. Create tables to reach max meta dwr pages
89 * @tc.expected: step3. Execute successfully
90 */
91 std::string tableNamePrefix("TestTableInfoForMetaDwr");
92 // table tail 4000
93 tableNamePrefix += std::string(4000, 'a');
94 // meta dwr pages correspond to 240 table info
95 for (int i = 0; i < 240; i++) {
96 std::string cteateTable = std::string("CREATE TABLE ") + tableNamePrefix + to_string(i) +
97 "(id INTEGER PRIMARY KEY, name TEXT);";
98 EXPECT_EQ(sqlite3_exec(db, cteateTable.c_str(), NULL, NULL, NULL), SQLITE_OK);
99 }
100 sqlite3_close(db);
101 /**
102 * @tc.steps: step4. Expect meta dwr file limit size
103 * @tc.expected: step4. Execute successfully
104 */
105 struct stat statbuf;
106 EXPECT_EQ(stat(TEST_DWR, &statbuf), 0);
107 // max 1001 pages and page size 4096
108 EXPECT_EQ(statbuf.st_size, 1001 * 4096);
109 }
110