• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 
16 #include <fcntl.h>
17 #include <fstream>
18 #include <sstream>
19 #include <sys/mount.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 
23 #include "gtest/gtest.h"
24 #include "storage_service_errno.h"
25 #include "storage_service_log.h"
26 #include "utils/string_utils.h"
27 #include "utils/storage_statistics_radar.h"
28 
29 namespace OHOS {
30 namespace StorageDaemon {
31 using namespace testing::ext;
32 
33 class StorageStatisticRadarTest : public testing::Test {
34 public:
SetUpTestCase(void)35     static void SetUpTestCase(void) {};
TearDownTestCase(void)36     static void TearDownTestCase(void) {};
37     static RadarStatisticInfo getRadarInfo();
SetUp()38     void SetUp() {};
TearDown()39     void TearDown() {};
40 };
41 
getRadarInfo()42 RadarStatisticInfo StorageStatisticRadarTest::getRadarInfo()
43 {
44     RadarStatisticInfo info;
45     info.keyLoadSuccCount = 1;
46     info.keyLoadFailCount = 2;
47     info.keyUnloadSuccCount = 3;
48     info.keyUnloadFailCount = 4;
49     info.userAddSuccCount = 5;
50     info.userAddFailCount = 6;
51     info.userRemoveSuccCount = 7;
52     info.userRemoveFailCount = 8;
53     info.userStartSuccCount = 9;
54     info.userStartFailCount = 10;
55     info.userStopSuccCount = 11;
56     info.userStopFailCount = 12;
57     return info;
58 }
59 
60 /**
61  * @tc.name: StorageStatisticRadarTest_CreateStatisticFile_001
62  * @tc.desc: Verify the CreateStatisticFile function.
63  * @tc.type: FUNC
64  * @tc.require: AR000GK4HB
65  */
66 HWTEST_F(StorageStatisticRadarTest, StorageStatisticRadarTest_CreateStatisticFile_001, TestSize.Level1)
67 {
68     GTEST_LOG_(INFO) << "StorageStatisticRadarTest_CreateStatisticFile_001 start";
69     std::string PATH_STORAGE_RADAR = "/data/service/el1/public/storage_daemon/radar/StorageStatisticFile.json";
70     StorageStatisticRadar radar;
71 
72     std::map<uint32_t, RadarStatisticInfo> statistics;
73     RadarStatisticInfo info = StorageStatisticRadarTest::getRadarInfo();
74     statistics.insert(std::make_pair(1, info));
75     std::string jsonStr = radar.CreateJsonString(statistics);
76     std::string errMsg = "";
77 
78     SaveStringToFileSync(PATH_STORAGE_RADAR.c_str(), jsonStr, errMsg);
79     EXPECT_TRUE(radar.CreateStatisticFile());
80 
81     std::remove(PATH_STORAGE_RADAR.c_str());
82     EXPECT_TRUE(radar.CreateStatisticFile());
83     GTEST_LOG_(INFO) << "StorageStatisticRadarTest_CreateStatisticFile_001 end";
84 }
85 
86 /**
87  * @tc.name: StorageStatisticRadarTest_CleanStatisticFile_001
88  * @tc.desc: Verify the CleanStatisticFile function.
89  * @tc.type: FUNC
90  * @tc.require: AR000GK4HB
91  */
92 HWTEST_F(StorageStatisticRadarTest, StorageStatisticRadarTest_CleanStatisticFile_001, TestSize.Level1)
93 {
94     GTEST_LOG_(INFO) << "StorageStatisticRadarTest_CleanStatisticFile_001 start";
95     StorageStatisticRadar radar;
96     std::string countInfo = "1,2,3,4,5,6,7,8,9,10,11";
97     std::string PATH_STORAGE_RADAR = "/data/service/el1/public/storage_daemon/radar/StorageStatisticFile.json";
98 
99     std::map<uint32_t, RadarStatisticInfo> statistics;
100     RadarStatisticInfo info = StorageStatisticRadarTest::getRadarInfo();
101     statistics.insert(std::make_pair(1, info));
102     std::string jsonStr = radar.CreateJsonString(statistics);
103     std::string errMsg = "";
104     SaveStringToFileSync(PATH_STORAGE_RADAR.c_str(), jsonStr, errMsg);
105 
106     radar.CleanStatisticFile();
107     std::ifstream inFile(PATH_STORAGE_RADAR.c_str());
108     std::string content((std::istreambuf_iterator<char>(inFile)), std::istreambuf_iterator<char>());
109     EXPECT_TRUE(content.empty());
110 
111     std::remove(PATH_STORAGE_RADAR.c_str());
112     std::ifstream inFile1(PATH_STORAGE_RADAR.c_str());
113     std::string content1((std::istreambuf_iterator<char>(inFile1)), std::istreambuf_iterator<char>());
114     EXPECT_TRUE(content1.empty());
115     GTEST_LOG_(INFO) << "StorageStatisticRadarTest_CleanStatisticFile_001 end";
116 }
117 
118 /**
119  * @tc.name: StorageStatisticRadarTest_GetCountInfoString_001
120  * @tc.desc: Verify the GetCountInfoString function.
121  * @tc.type: FUNC
122  * @tc.require: AR000GK4HB
123  */
124 HWTEST_F(StorageStatisticRadarTest, StorageStatisticRadarTest_GetCountInfoString_001, TestSize.Level1)
125 {
126     GTEST_LOG_(INFO) << "StorageStatisticRadarTest_GetCountInfoString_001 start";
127     StorageStatisticRadar radar;
128     RadarStatisticInfo info = StorageStatisticRadarTest::getRadarInfo();
129     std::string expected = "1,2,3,4,5,6,7,8,9,10,11,12";
130     EXPECT_EQ(expected, radar.GetCountInfoString(info));
131     GTEST_LOG_(INFO) << "StorageStatisticRadarTest_GetCountInfoString_001 end";
132 }
133 
134 /**
135  * @tc.name: StorageStatisticRadarTest_CreateJsonString_001
136  * @tc.desc: Verify the CreateJsonString function.
137  * @tc.type: FUNC
138  * @tc.require: AR000GK4HB
139  */
140 HWTEST_F(StorageStatisticRadarTest, StorageStatisticRadarTest_CreateJsonString_001, TestSize.Level1)
141 {
142     GTEST_LOG_(INFO) << "StorageStatisticRadarTest_CreateJsonString_001 start";
143     StorageStatisticRadar radar;
144     std::map<uint32_t, RadarStatisticInfo> statistics;
145     RadarStatisticInfo info = StorageStatisticRadarTest::getRadarInfo();
146     statistics.insert(std::make_pair(1, info));
147     std::string expected = "{\"storageStatisticFile\":[{\"userId\":1,\"oprateCount\":\"1,2,3,4,5,6,7,8,9,10,11,12\"}]}";
148     std::string jsonStr = radar.CreateJsonString(statistics);
__anone5e4140f0102(char c) 149     jsonStr.erase(std::remove_if(jsonStr.begin(), jsonStr.end(), [](char c) {
150         return c == '\n' || c == '\t';
151     }), jsonStr.end());
152     EXPECT_EQ(expected, jsonStr);
153     GTEST_LOG_(INFO) << "StorageStatisticRadarTest_CreateJsonString_001 end";
154 }
155 
156 /**
157  * @tc.name: StorageStatisticRadarTest_ReadStatisticFile_001
158  * @tc.desc: Verify the ReadStatisticFile function.
159  * @tc.type: FUNC
160  * @tc.require: AR000GK4HB
161  */
162 HWTEST_F(StorageStatisticRadarTest, StorageStatisticRadarTest_ReadStatisticFile_001, TestSize.Level1)
163 {
164     GTEST_LOG_(INFO) << "StorageStatisticRadarTest_ReadStatisticFile_001 start";
165 
166     StorageStatisticRadar radar;
167     std::string PATH_STORAGE_RADAR = "/data/service/el1/public/storage_daemon/radar/StorageStatisticFile.json";
168     std::remove(PATH_STORAGE_RADAR.c_str());
169 
170     std::map<uint32_t, RadarStatisticInfo> statistics1;
171     EXPECT_FALSE(radar.ReadStatisticFile(statistics1));
172     EXPECT_TRUE(statistics1.empty());
173 
174     std::string radarInfo = "";
175     std::string errMsg = "";
176     SaveStringToFileSync(PATH_STORAGE_RADAR.c_str(), radarInfo, errMsg);
177     std::map<uint32_t, RadarStatisticInfo> statistics2;
178     EXPECT_FALSE(radar.ReadStatisticFile(statistics2));
179     EXPECT_TRUE(statistics2.empty());
180     std::remove(PATH_STORAGE_RADAR.c_str());
181 
182     std::string radarInfo1 = "test json";
183     SaveStringToFileSync(PATH_STORAGE_RADAR.c_str(), radarInfo1, errMsg);
184     std::map<uint32_t, RadarStatisticInfo> statistics3;
185     EXPECT_FALSE(radar.ReadStatisticFile(statistics3));
186     EXPECT_TRUE(statistics3.empty());
187     std::remove(PATH_STORAGE_RADAR.c_str());
188 
189     std::string expected = "{\"storageStatisticFile\":\"userId\":1,\"oprateCount\":\"1,2,3,4,5,6,7,8,9,10,11,12\"}]}";
190     SaveStringToFileSync(PATH_STORAGE_RADAR.c_str(), expected, errMsg);
191     std::remove(PATH_STORAGE_RADAR.c_str());
192     EXPECT_FALSE(radar.ReadStatisticFile(statistics3));
193     EXPECT_TRUE(statistics3.empty());
194     std::remove(PATH_STORAGE_RADAR.c_str());
195 
196     std::map<uint32_t, RadarStatisticInfo> statistics4;
197     RadarStatisticInfo info = StorageStatisticRadarTest::getRadarInfo();
198     statistics4.insert(std::make_pair(1, info));
199     std::string jsonStr = radar.CreateJsonString(statistics4);
200     SaveStringToFileSync(PATH_STORAGE_RADAR.c_str(), jsonStr, errMsg);
201     std::map<uint32_t, RadarStatisticInfo> statistics;
202     EXPECT_TRUE(radar.ReadStatisticFile(statistics));
203     EXPECT_FALSE(statistics.empty());
204     GTEST_LOG_(INFO) << "StorageStatisticRadarTest_ReadStatisticFile_001 end";
205 }
206 
207 /**
208  * @tc.name: StorageStatisticRadarTest_ParseJsonInfo_001
209  * @tc.desc: Verify the ParseJsonInfo function.
210  * @tc.type: FUNC
211  * @tc.require: AR000GK4HB
212  */
213 HWTEST_F(StorageStatisticRadarTest, StorageStatisticRadarTest_ParseJsonInfo_001, TestSize.Level1)
214 {
215     GTEST_LOG_(INFO) << "StorageStatisticRadarTest_ParseJsonInfo_001 start";
216     StorageStatisticRadar radar;
217     std::map<uint32_t, RadarStatisticInfo> statistics;
218     std::string countInfo = "1,2,3,4,5,6,7,8,9,10,11,12";
219     EXPECT_TRUE(radar.ParseJsonInfo(1, countInfo, statistics));
220     EXPECT_FALSE(statistics.empty());
221 
222     std::map<uint32_t, RadarStatisticInfo> statistics2;
223     std::string countInfo11 = "1,2,3,4,5,6,7,8,9,10,11";
224     EXPECT_FALSE(radar.ParseJsonInfo(1, countInfo11, statistics2));
225     EXPECT_TRUE(statistics2.empty());
226     GTEST_LOG_(INFO) << "StorageStatisticRadarTest_ParseJsonInfo_001 end";
227 }
228 } // STORAGE_DAEMON
229 } // OHOS
230