1 /*
2  * Copyright (c) 2023 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 <ctime>
17 #include <thread>
18 #include <vector>
19 
20 #include <gtest/gtest.h>
21 #ifndef USE_SQLITE_SYMBOLS
22 #include "sqlite3.h"
23 #else
24 #include "sqlite3sym.h"
25 #endif
26 
27 #ifdef GTEST_API_
28 #define private public
29 #define protected public
30 #endif
31 
32 #include "net_stats_sqlite_statement.h"
33 #include "net_stats_database_helper.h"
34 
35 namespace OHOS {
36 namespace NetManagerStandard {
37 namespace {
38 using namespace testing::ext;
39 #define DTEST_LOG std::cout << __func__ << ":" << __LINE__ << ": "
40 constexpr int32_t SQLITE_ERRORNUM = 21;
41 } // namespace
42 
43 
44 class NetStatsSqliteStatementTest : public testing::Test {
45 public:
46     static void SetUpTestCase();
47     static void TearDownTestCase();
48     void SetUp();
49     void TearDown();
50     static inline std::shared_ptr<NetStatsSqliteStatement> instance_ = nullptr;
51 };
52 
SetUpTestCase()53 void NetStatsSqliteStatementTest::SetUpTestCase()
54 {
55     instance_ = std::make_shared<NetStatsSqliteStatement>();
56 }
57 
TearDownTestCase()58 void NetStatsSqliteStatementTest::TearDownTestCase()
59 {
60 }
61 
SetUp()62 void NetStatsSqliteStatementTest::SetUp() {}
63 
TearDown()64 void NetStatsSqliteStatementTest::TearDown() {}
65 
66 HWTEST_F(NetStatsSqliteStatementTest, PrepareTest001, TestSize.Level1)
67 {
68     const std::string testSql = "test sql";
69     auto result = instance_->Prepare(nullptr, testSql);
70     DTEST_LOG << "Prepare result: " << result << std::endl;
71     EXPECT_NE(result, SQLITE_OK);
72 }
73 
74 HWTEST_F(NetStatsSqliteStatementTest, FinalizeTest001, TestSize.Level1)
75 {
76     instance_->stmtHandle_ = nullptr;
77     instance_->Finalize();
78     EXPECT_NE(instance_, nullptr);
79 }
80 
81 HWTEST_F(NetStatsSqliteStatementTest, FinalizeTest002, TestSize.Level1)
82 {
83     instance_->stmtHandle_ = nullptr;
84     instance_->Finalize();
85     EXPECT_NE(instance_, nullptr);
86 }
87 
88 HWTEST_F(NetStatsSqliteStatementTest, ResetStatementAndClearBindingsTest001, TestSize.Level1)
89 {
90     instance_->stmtHandle_ = nullptr;
91     instance_->ResetStatementAndClearBindings();
92     EXPECT_NE(instance_, nullptr);
93 }
94 
95 HWTEST_F(NetStatsSqliteStatementTest, GetColumnStringTest001, TestSize.Level1)
96 {
97     instance_->stmtHandle_ = nullptr;
98     std::string emptyValue;
99     auto result = instance_->GetColumnString(-1, emptyValue);
100     EXPECT_EQ(result, SQLITE_ERROR);
101 }
102 
103 HWTEST_F(NetStatsSqliteStatementTest, GetColumnStringTest002, TestSize.Level1)
104 {
105     instance_->stmtHandle_ = nullptr;
106     std::string emptyValue;
107     instance_->columnCount_ = 100;
108     auto result = instance_->GetColumnString(instance_->columnCount_ + 1, emptyValue);
109     EXPECT_EQ(result, SQLITE_ERROR);
110 }
111 
112 HWTEST_F(NetStatsSqliteStatementTest, GetColumnStringTest003, TestSize.Level1)
113 {
114     instance_->stmtHandle_ = nullptr;
115     std::string emptyValue;
116     instance_->columnCount_ = 100;
117     auto result = instance_->GetColumnString(instance_->columnCount_ - 1, emptyValue);
118     EXPECT_EQ(result, SQLITE_ERROR);
119 }
120 
121 HWTEST_F(NetStatsSqliteStatementTest, GetColumnLongTest001, TestSize.Level1)
122 {
123     instance_->stmtHandle_ = nullptr;
124     uint64_t emptyValue;
125     auto result = instance_->GetColumnLong(-1, emptyValue);
126     EXPECT_EQ(result, SQLITE_ERROR);
127 }
128 
129 HWTEST_F(NetStatsSqliteStatementTest, GetColumnLongTest002, TestSize.Level1)
130 {
131     instance_->stmtHandle_ = nullptr;
132     uint64_t emptyValue;
133     instance_->columnCount_ = 100;
134     auto result = instance_->GetColumnLong(instance_->columnCount_ + 1, emptyValue);
135     EXPECT_EQ(result, SQLITE_ERROR);
136 }
137 
138 HWTEST_F(NetStatsSqliteStatementTest, GetColumnLongTest003, TestSize.Level1)
139 {
140     instance_->stmtHandle_ = nullptr;
141     uint64_t emptyValue;
142     instance_->columnCount_ = 100;
143     auto result = instance_->GetColumnLong(instance_->columnCount_ - 1, emptyValue);
144     EXPECT_EQ(result, SQLITE_ERROR);
145 }
146 
147 HWTEST_F(NetStatsSqliteStatementTest, GetColumnIntTest001, TestSize.Level1)
148 {
149     instance_->stmtHandle_ = nullptr;
150     uint32_t emptyValue;
151     auto result = instance_->GetColumnInt(-1, emptyValue);
152     EXPECT_EQ(result, SQLITE_ERROR);
153 }
154 
155 HWTEST_F(NetStatsSqliteStatementTest, GetColumnIntTest002, TestSize.Level1)
156 {
157     instance_->stmtHandle_ = nullptr;
158     uint32_t emptyValue;
159     instance_->columnCount_ = 100;
160     auto result = instance_->GetColumnInt(instance_->columnCount_ + 1, emptyValue);
161     EXPECT_EQ(result, SQLITE_ERROR);
162 }
163 
164 HWTEST_F(NetStatsSqliteStatementTest, GetColumnIntTest003, TestSize.Level1)
165 {
166     instance_->stmtHandle_ = nullptr;
167     uint32_t emptyValue;
168     instance_->columnCount_ = 100;
169     auto result = instance_->GetColumnInt(instance_->columnCount_ - 1, emptyValue);
170     EXPECT_EQ(result, SQLITE_ERROR);
171 }
172 
173 HWTEST_F(NetStatsSqliteStatementTest, BindInt32001, TestSize.Level1)
174 {
175     instance_->stmtHandle_ = nullptr;
176     int32_t emptyValue = 0;
177     auto result = instance_->BindInt32(-1, emptyValue);
178     EXPECT_EQ(result, SQLITE_ERRORNUM);
179 }
180 
181 HWTEST_F(NetStatsSqliteStatementTest, BindInt64001, TestSize.Level1)
182 {
183     instance_->stmtHandle_ = nullptr;
184     int64_t emptyValue = 0;
185     instance_->columnCount_ = 100;
186     auto result = instance_->BindInt64(-1, emptyValue);
187     EXPECT_EQ(result, SQLITE_ERRORNUM);
188 }
189 
190 HWTEST_F(NetStatsSqliteStatementTest, BindText001, TestSize.Level1)
191 {
192     instance_->stmtHandle_ = nullptr;
193     std::string emptyValue;
194     instance_->columnCount_ = 100;
195     auto result = instance_->BindText(-1, emptyValue);
196     EXPECT_EQ(result, SQLITE_ERRORNUM);
197 }
198 
199 HWTEST_F(NetStatsSqliteStatementTest, Step001, TestSize.Level1)
200 {
201     instance_->stmtHandle_ = nullptr;
202     instance_->columnCount_ = 100;
203     auto result = instance_->Step();
204     EXPECT_EQ(result, SQLITE_ERRORNUM);
205 }
206 
207 HWTEST_F(NetStatsSqliteStatementTest, GetColumnCount001, TestSize.Level1)
208 {
209     instance_->stmtHandle_ = nullptr;
210     instance_->columnCount_ = 100;
211     auto result = instance_->GetColumnCount();
212     EXPECT_EQ(result, 100);
213 }
214 } // namespace NetManagerStandard
215 } // namespace OHOS