• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 <gtest/gtest.h>
17 
18 #ifdef GTEST_API_
19 #define private public
20 #define protected public
21 #endif
22 
23 #include "net_stats_notification.h"
24 #include "net_stats_utils.h"
25 #include "net_stats_rdb.h"
26 
27 namespace OHOS {
28 namespace NetManagerStandard {
29 namespace {
30 using namespace testing::ext;
31 constexpr const char *KEY_MONTH_LIMIT_TEXT = "netstats_month_limit_message";
32 constexpr const char *KEY_MONTH_NOTIFY_TEXT = "netstats_month_notify_message";
33 constexpr const char *KEY_DAILY_NOTIFY_TEXT = "netstats_daily_notify_message";
34 const int32_t UNIT_CONVERT_1024 = 1024;
35 
36 class MockRdbStore : public NativeRdb::RdbStore {
37 public:
Delete(int & deletedRows,const std::string & table,const std::string & whereClause="",const Values & args={})38     int Delete(int &deletedRows, const std::string &table,
39         const std::string &whereClause = "", const Values &args = {}) override
40     {
41         return 0;
42     }
43 
QuerySql(const std::string & sql,const Values & args={})44     std::shared_ptr<NativeRdb::AbsSharedResultSet> QuerySql(const std::string &sql, const Values &args = {}) override
45     {
46         return nullptr;
47     }
48 
QueryByStep(const std::string & sql,const Values & args={},bool preCount=true)49     std::shared_ptr<NativeRdb::ResultSet> QueryByStep(const std::string &sql, const Values &args = {},
50         bool preCount = true) override
51     {
52         return nullptr;
53     }
54 
Execute(const std::string & sql,const Values & args={},int64_t trxId=0)55     std::pair<int32_t, NativeRdb::ValueObject> Execute(
56         const std::string &sql, const Values &args = {}, int64_t trxId = 0) override
57     {
58         return std::make_pair(0, NativeRdb::ValueObject());
59     }
60 
GetVersion(int & version)61     int GetVersion(int &version) override
62     {
63         return 0;
64     }
65 
SetVersion(int version)66     int SetVersion(int version) override
67     {
68         return 0;
69     }
70 };
71 }
72 
73 class NetStatsNotificationTest : public testing::Test {
74 public:
SetUpTestCase()75     static void SetUpTestCase() {}
76 
TearDownTestCase()77     static void TearDownTestCase() {}
78 
SetUp()79     void SetUp() {}
80 
TearDown()81     void TearDown() {}
82 };
83 
84 HWTEST_F(NetStatsNotificationTest, ParseJSONFileTest001, TestSize.Level1)
85 {
86     auto &notification = NetMgrNetStatsLimitNotification::GetInstance();
87     std::string filePath;
88     notification.ParseJSONFile(filePath, notification.languageMap);
89     EXPECT_FALSE(notification.languageMap.empty());
90 }
91 
92 HWTEST_F(NetStatsNotificationTest, UpdateResourceMapTest001, TestSize.Level1)
93 {
94     auto &notification = NetMgrNetStatsLimitNotification::GetInstance();
95     notification.UpdateResourceMap();
96     EXPECT_FALSE(notification.localeBaseName.empty());
97 }
98 
99 HWTEST_F(NetStatsNotificationTest, GetDayNotificationTextTest001, TestSize.Level1)
100 {
101     auto &notification = NetMgrNetStatsLimitNotification::GetInstance();
102     std::string temp = notification.resourceMap[KEY_DAILY_NOTIFY_TEXT];
103     notification.resourceMap.erase(KEY_DAILY_NOTIFY_TEXT);
104     auto ret = notification.GetDayNotificationText();
105     EXPECT_TRUE(ret.empty());
106 
107     notification.resourceMap[KEY_DAILY_NOTIFY_TEXT] = "";
108     ret = notification.GetDayNotificationText();
109     EXPECT_TRUE(ret.empty());
110     notification.resourceMap[KEY_DAILY_NOTIFY_TEXT] = temp;
111 }
112 
113 HWTEST_F(NetStatsNotificationTest, GetMonthNotificationTextTest001, TestSize.Level1)
114 {
115     auto &notification = NetMgrNetStatsLimitNotification::GetInstance();
116     std::string temp = notification.resourceMap[KEY_MONTH_NOTIFY_TEXT];
117     notification.resourceMap.erase(KEY_MONTH_NOTIFY_TEXT);
118     auto ret = notification.GetMonthNotificationText();
119     EXPECT_TRUE(ret.empty());
120 
121     notification.resourceMap[KEY_MONTH_NOTIFY_TEXT] = "";
122     ret = notification.GetMonthNotificationText();
123     EXPECT_TRUE(ret.empty());
124     notification.resourceMap[KEY_MONTH_NOTIFY_TEXT] = temp;
125     ret = notification.GetMonthNotificationText();
126     EXPECT_TRUE(ret.empty());
127 }
128 
129 HWTEST_F(NetStatsNotificationTest, GetMonthAlertTextTest001, TestSize.Level1)
130 {
131     auto &notification = NetMgrNetStatsLimitNotification::GetInstance();
132     std::string temp = notification.resourceMap[KEY_MONTH_LIMIT_TEXT];
133     notification.resourceMap.erase(KEY_MONTH_LIMIT_TEXT);
134     auto ret = notification.GetMonthAlertText();
135     EXPECT_TRUE(ret.empty());
136 
137     notification.resourceMap[KEY_MONTH_LIMIT_TEXT] = "";
138     ret = notification.GetMonthAlertText();
139     EXPECT_TRUE(ret.empty());
140     notification.resourceMap[KEY_MONTH_LIMIT_TEXT] = temp;
141 }
142 
143 HWTEST_F(NetStatsNotificationTest, SetTitleAndTextTest001, TestSize.Level1)
144 {
145     auto &notification = NetMgrNetStatsLimitNotification::GetInstance();
146     int notificationId = 0;
147     std::shared_ptr<Notification::NotificationNormalContent> content = nullptr;
148     bool isDualCard = false;
149     auto ret = notification.SetTitleAndText(notificationId, content, isDualCard);
150     EXPECT_FALSE(ret);
151 
152     content = std::make_shared<Notification::NotificationNormalContent>();
153     ret = notification.SetTitleAndText(notificationId, content, isDualCard);
154     EXPECT_FALSE(ret);
155 
156     notification.resourceMap[""] = "test";
157     ret = notification.SetTitleAndText(notificationId, content, isDualCard);
158     EXPECT_FALSE(ret);
159     notification.resourceMap.erase("");
160 }
161 
162 HWTEST_F(NetStatsNotificationTest, SetTitleAndTextTest002, TestSize.Level1)
163 {
164     auto &notification = NetMgrNetStatsLimitNotification::GetInstance();
165     int notificationId = NETMGR_STATS_LIMIT_DAY;
166     auto content = std::make_shared<Notification::NotificationNormalContent>();
167     bool isDualCard = false;
168     auto ret = notification.SetTitleAndText(notificationId, content, isDualCard);
169     EXPECT_TRUE(ret);
170 
171     notificationId = NETMGR_STATS_LIMIT_MONTH;
172     ret = notification.SetTitleAndText(notificationId, content, isDualCard);
173     EXPECT_TRUE(ret);
174 
175     isDualCard = true;
176     notificationId = NETMGR_STATS_ALERT_MONTH;
177     ret = notification.SetTitleAndText(notificationId, content, isDualCard);
178     EXPECT_TRUE(ret);
179 }
180 
181 HWTEST_F(NetStatsNotificationTest, GetPixelMapTest001, TestSize.Level1)
182 {
183     auto &notification = NetMgrNetStatsLimitNotification::GetInstance();
184     EXPECT_NE(notification.netmgrStatsLimitIconPixelMap_, nullptr);
185     notification.GetPixelMap();
186 }
187 
188 HWTEST_F(NetStatsNotificationTest, GetTrafficNumTest001, TestSize.Level1)
189 {
190     auto &notification = NetMgrNetStatsLimitNotification::GetInstance();
191     double traffic = static_cast<double>(UNIT_CONVERT_1024);
192     auto ret = notification.GetTrafficNum(traffic);
193     EXPECT_FALSE(ret.empty());
194 
195     for (int i = 0; i < 4; i++) {
196         traffic *= UNIT_CONVERT_1024;
197     }
198     ret = notification.GetTrafficNum(traffic);
199     EXPECT_FALSE(ret.empty());
200 }
201 
202 HWTEST_F(NetStatsNotificationTest, GetStartTimestampTest001, TestSize.Level1)
203 {
204     NetStatsUtils utils;
205     int32_t startdate = 32;
206     auto ret = utils.GetStartTimestamp(startdate);
207     EXPECT_NE(ret, 0);
208 }
209 
210 HWTEST_F(NetStatsNotificationTest, GetTrafficNumTest002, TestSize.Level1)
211 {
212     NetStatsUtils utils;
213     int32_t startdate = 1;
214     auto ret = utils.GetStartTimestamp(startdate);
215     EXPECT_NE(ret, 0);
216 }
217 
218 HWTEST_F(NetStatsNotificationTest, GetTodayStartTimestampTest001, TestSize.Level1)
219 {
220     NetStatsUtils utils;
221     auto ret = utils.GetTodayStartTimestamp();
222     EXPECT_NE(ret, 0);
223 }
224 
225 HWTEST_F(NetStatsNotificationTest, GetDaysInMonthTest001, TestSize.Level1)
226 {
227     NetStatsUtils utils;
228     int32_t year = 0;
229     int32_t month = 0;
230     auto ret = utils.GetDaysInMonth(year, month);
231     EXPECT_EQ(ret, -1);
232 
233     year = 2000;
234     ret = utils.GetDaysInMonth(year, month);
235     EXPECT_EQ(ret, -1);
236 
237     month = 13;
238     ret = utils.GetDaysInMonth(year, month);
239     EXPECT_EQ(ret, -1);
240 }
241 
242 HWTEST_F(NetStatsNotificationTest, GetDaysInMonthTest002, TestSize.Level1)
243 {
244     NetStatsUtils utils;
245     int32_t year = 2001;
246     int32_t month = 1;
247     auto ret = utils.GetDaysInMonth(year, month);
248     EXPECT_EQ(ret, 31);
249 
250     month = 2;
251     ret = utils.GetDaysInMonth(year, month);
252     EXPECT_EQ(ret, 28);
253 
254     year = 2000;
255     ret = utils.GetDaysInMonth(year, month);
256     EXPECT_EQ(ret, 29);
257 }
258 
259 HWTEST_F(NetStatsNotificationTest, ConvertToUint64Test001, TestSize.Level1)
260 {
261     NetStatsUtils utils;
262     std::string str;
263     uint64_t value = 0;
264     auto ret = utils.ConvertToUint64(str, value);
265     EXPECT_FALSE(ret);
266 
267     str = "test";
268     ret = utils.ConvertToUint64(str, value);
269     EXPECT_FALSE(ret);
270 
271     str = "123test";
272     ret = utils.ConvertToUint64(str, value);
273     EXPECT_FALSE(ret);
274 }
275 
276 HWTEST_F(NetStatsNotificationTest, ConvertToUint64Test002, TestSize.Level1)
277 {
278     NetStatsUtils utils;
279     std::string str = "99999999999999999999";
280     uint64_t value = 0;
281     auto ret = utils.ConvertToUint64(str, value);
282     EXPECT_TRUE(ret);
283 
284     str = "123";
285     ret = utils.ConvertToUint64(str, value);
286     EXPECT_TRUE(ret);
287 }
288 
289 HWTEST_F(NetStatsNotificationTest, ConvertToInt32Test001, TestSize.Level1)
290 {
291     NetStatsUtils utils;
292     std::string str;
293     int32_t value = 0;
294     auto ret = utils.ConvertToInt32(str, value);
295     EXPECT_FALSE(ret);
296 
297     str = "test";
298     ret = utils.ConvertToInt32(str, value);
299     EXPECT_FALSE(ret);
300 
301     str = "123test";
302     ret = utils.ConvertToInt32(str, value);
303     EXPECT_FALSE(ret);
304 }
305 
306 HWTEST_F(NetStatsNotificationTest, ConvertToInt32Test002, TestSize.Level1)
307 {
308     NetStatsUtils utils;
309     std::string str = "1e309";
310     int32_t value = 0;
311     auto ret = utils.ConvertToInt32(str, value);
312     EXPECT_TRUE(ret);
313 
314     str = "123";
315     ret = utils.ConvertToInt32(str, value);
316     EXPECT_TRUE(ret);
317 }
318 
319 HWTEST_F(NetStatsNotificationTest, OnUpgradeTest001, TestSize.Level1)
320 {
321     NetStatsRDB::RdbDataOpenCallback callback;
322     MockRdbStore store;
323     auto ret = callback.OnUpgrade(store, 0, 1);
324     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
325 }
326 
327 HWTEST_F(NetStatsNotificationTest, UpgradeDbVersionToTest001, TestSize.Level1)
328 {
329     NetStatsRDB::RdbDataOpenCallback callback;
330     MockRdbStore store;
331     auto ret = callback.OnUpgrade(store, 0, 1);
332     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
333     callback.UpgradeDbVersionTo(store, 0);
334     callback.UpgradeDbVersionTo(store, 1);
335 }
336 
337 HWTEST_F(NetStatsNotificationTest, GetRdbStoreTest001, TestSize.Level1)
338 {
339     NetStatsRDB rdb;
340     auto ret = rdb.GetRdbStore();
341     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
342 
343     EXPECT_NE(rdb.rdbStore_, nullptr);
344     ret = rdb.GetRdbStore();
345     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
346 }
347 
348 HWTEST_F(NetStatsNotificationTest, InitRdbStoreTest001, TestSize.Level1)
349 {
350     NetStatsRDB rdb;
351     auto ret = rdb.InitRdbStore();
352     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
353 }
354 
355 HWTEST_F(NetStatsNotificationTest, InsertDataTest001, TestSize.Level1)
356 {
357     NetStatsRDB rdb;
358     NetStatsData state;
359     state.simId = 0;
360     auto ret = rdb.InsertData(state);
361     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
362 
363     ret = rdb.DeleteBySimId(state.simId);
364     EXPECT_EQ(ret, 1);
365 }
366 
367 HWTEST_F(NetStatsNotificationTest, DeleteBySimIdTest001, TestSize.Level1)
368 {
369     NetStatsRDB rdb;
370     NetStatsData state;
371     auto ret = rdb.DeleteBySimId(0);
372     EXPECT_EQ(ret, 0);
373 }
374 
375 HWTEST_F(NetStatsNotificationTest, UpdateBySimIdTest001, TestSize.Level1)
376 {
377     NetStatsRDB rdb;
378     NetStatsData state;
379     auto ret = rdb.UpdateBySimId(0, state);
380     EXPECT_EQ(ret, 0);
381 }
382 
383 HWTEST_F(NetStatsNotificationTest, QueryAllTest001, TestSize.Level1)
384 {
385     NetStatsRDB rdb;
386     NetStatsData state;
387     state.simId = 0;
388     auto ret = rdb.QueryAll();
389     EXPECT_TRUE(ret.empty());
390 
391     rdb.InsertData(state);
392     ret = rdb.QueryAll();
393     EXPECT_EQ(ret.size(), 1);
394     rdb.DeleteBySimId(state.simId);
395 }
396 
397 HWTEST_F(NetStatsNotificationTest, QueryBySimIdTest001, TestSize.Level1)
398 {
399     NetStatsRDB rdb;
400     NetStatsData state;
401     state.simId = 0;
402     auto ret = rdb.QueryBySimId(state.simId, state);
403     EXPECT_EQ(ret, NETMANAGER_ERROR);
404 
405     rdb.InsertData(state);
406     ret = rdb.QueryBySimId(1, state);
407     EXPECT_EQ(ret, NETMANAGER_ERROR);
408 
409     ret = rdb.QueryBySimId(state.simId, state);
410     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
411 }
412 
413 HWTEST_F(NetStatsNotificationTest, BackUpNetStatsFreqDBTest002, TestSize.Level1)
414 {
415     NetStatsRDB rdb;
416     auto ret = rdb.BackUpNetStatsFreqDB("", "");
417     EXPECT_EQ(ret, NETMANAGER_ERROR);
418 
419     ret = rdb.BackUpNetStatsFreqDB("./xxx.db", "");
420     EXPECT_EQ(ret, NETMANAGER_ERROR);
421 
422     ret = rdb.BackUpNetStatsFreqDB("", "./xxx.db");
423     EXPECT_EQ(ret, NETMANAGER_ERROR);
424 
425     ret = rdb.BackUpNetStatsFreqDB("./xxx.db", "./xxx.db");
426     EXPECT_EQ(ret, NETMANAGER_ERROR);
427 }
428 } // namespace NetManagerStandard
429 } // namespace OHOS