• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <iostream>
16 
17 #include <gtest/gtest.h>
18 #include <json/json.h>
19 
20 #include "event_json_util.h"
21 #include "file_util.h"
22 #include "hiappevent_config.h"
23 
24 using namespace testing::ext;
25 using namespace OHOS::HiviewDFX;
26 
27 namespace {
28 const std::string TEST_DIR = "/data/test/hiappevent/";
29 
30 class HiAppEventUtilityTest : public testing::Test {
31 public:
32     void SetUp();
TearDown()33     void TearDown() {}
34 };
35 
SetUp()36 void HiAppEventUtilityTest::SetUp()
37 {
38     HiAppEventConfig::GetInstance().SetStorageDir(TEST_DIR);
39 }
40 }
41 
42 /**
43  * @tc.name: HiAppEventJsonUtil001
44  * @tc.desc: test the event json util ParseUInt32.
45  * @tc.type: FUNC
46  * @tc.require: issueI5NTOS
47  */
48 HWTEST_F(HiAppEventUtilityTest, HiAppEventJsonUtil001, TestSize.Level1)
49 {
50     std::cout << "HiAppEventJsonUtil001 start" << std::endl;
51     Json::Value root;
52     uint32_t result = EventJsonUtil::ParseUInt32(root, "testKey");
53     EXPECT_EQ(result, 0u);
54 
55     root["testKey"] = Json::Value::null;
56     result = EventJsonUtil::ParseUInt32(root, "testKey");
57     EXPECT_EQ(result, 0u);
58 
59     root["testKey"] = 123.45;
60     result = EventJsonUtil::ParseUInt32(root, "testKey");
61     EXPECT_EQ(result, 0u);
62 
63     root["testKey"] = 123u;
64     result = EventJsonUtil::ParseUInt32(root, "testKey");
65     EXPECT_EQ(result, 123u);
66     std::cout << "HiAppEventJsonUtil001 end" << std::endl;
67 }
68 
69 /**
70  * @tc.name: HiAppEventJsonUtil002
71  * @tc.desc: test the event json util ParseInt.
72  * @tc.type: FUNC
73  * @tc.require: issueI5NTOS
74  */
75 HWTEST_F(HiAppEventUtilityTest, HiAppEventJsonUtil002, TestSize.Level1)
76 {
77     std::cout << "HiAppEventJsonUtil002 start" << std::endl;
78     Json::Value root;
79     int result = EventJsonUtil::ParseInt(root, "testKey");
80     EXPECT_EQ(result, 0);
81 
82     root["testKey"] = Json::Value::null;
83     result = EventJsonUtil::ParseInt(root, "testKey");
84     EXPECT_EQ(result, 0);
85 
86     root["testKey"] = 123.45;
87     result = EventJsonUtil::ParseInt(root, "testKey");
88     EXPECT_EQ(result, 0);
89 
90     root["testKey"] = 123;
91     result = EventJsonUtil::ParseInt(root, "testKey");
92     EXPECT_EQ(result, 123);
93     std::cout << "HiAppEventJsonUtil002 end" << std::endl;
94 }
95 
96 /**
97  * @tc.name: HiAppEventJsonUtil003
98  * @tc.desc: test the event json util ParseString.
99  * @tc.type: FUNC
100  * @tc.require: issueI5NTOS
101  */
102 HWTEST_F(HiAppEventUtilityTest, HiAppEventJsonUtil003, TestSize.Level1)
103 {
104     std::cout << "HiAppEventJsonUtil003 start" << std::endl;
105     Json::Value root;
106     std::string result = EventJsonUtil::ParseString(root, "testKey");
107     EXPECT_EQ(result, "");
108 
109     root["testKey"] = Json::Value::null;
110     result = EventJsonUtil::ParseString(root, "testKey");
111     EXPECT_EQ(result, "");
112 
113     root["testKey"] = 123.45;
114     result = EventJsonUtil::ParseString(root, "testKey");
115     EXPECT_EQ(result, "");
116 
117     root["testKey"] = "testStr";
118     result = EventJsonUtil::ParseString(root, "testKey");
119     EXPECT_EQ(result, "testStr");
120     std::cout << "HiAppEventJsonUtil003 end" << std::endl;
121 }
122 
123 /**
124  * @tc.name: HiAppEventJsonUtil004
125  * @tc.desc: test the event json util ParseStrings.
126  * @tc.type: FUNC
127  * @tc.require: issueI5NTOS
128  */
129 HWTEST_F(HiAppEventUtilityTest, HiAppEventJsonUtil004, TestSize.Level1)
130 {
131     std::cout << "HiAppEventJsonUtil004 start" << std::endl;
132     Json::Value root;
133     std::unordered_set<std::string> strs;
134 
135     EventJsonUtil::ParseStrings(root, "testKey", strs);
136     EXPECT_TRUE(strs.empty());
137 
138     root["testKey"] = Json::arrayValue;
139     root["testKey"].append("string1");
140     root["testKey"].append("string2");
141     EventJsonUtil::ParseStrings(root, "testKey", strs);
142     EXPECT_TRUE(strs.find("string1") != strs.end());
143     EXPECT_TRUE(strs.find("string2") != strs.end());
144     std::cout << "HiAppEventJsonUtil004 end" << std::endl;
145 }
146 
147 /**
148  * @tc.name: HiAppEventFileUtil001
149  * @tc.desc: test the FileUtil.
150  * @tc.type: FUNC
151  * @tc.require: issueI5NTOS
152  */
153 HWTEST_F(HiAppEventUtilityTest, HiAppEventFileUtil001, TestSize.Level1)
154 {
155     std::cout << "HiAppEventFileUtil001 start" << std::endl;
156     bool isDir = FileUtil::IsDirectory("");
157     EXPECT_FALSE(isDir);
158     std::string testDir = TEST_DIR + "test";
159     bool makeDirRes = FileUtil::ForceCreateDirectory(testDir);
160     EXPECT_TRUE(makeDirRes);
161     isDir = FileUtil::IsDirectory(testDir);
162     EXPECT_TRUE(isDir);
163 
164     bool setRes = FileUtil::SetDirXattr(testDir, "user.appevent", "testValue");
165     EXPECT_TRUE(setRes);
166     std::string testValue;
167     bool getRes = FileUtil::GetDirXattr(testDir, "user.appevent", testValue);
168     EXPECT_TRUE(getRes);
169     EXPECT_EQ(testValue, "testValue");
170 
171     std::string filePath = FileUtil::GetFilePathByDir("", "test.txt");
172     EXPECT_EQ(filePath, "test.txt");
173     filePath = FileUtil::GetFilePathByDir(TEST_DIR, "test.txt");
174     bool makeFileRes = FileUtil::CreateFile(filePath, S_IRUSR | S_IWUSR);
175     EXPECT_TRUE(makeFileRes);
176     bool writeFileRes = FileUtil::SaveStringToFile(filePath, "", true);
177     EXPECT_TRUE(writeFileRes);
178     writeFileRes = FileUtil::SaveStringToFile(filePath, filePath, true);
179     EXPECT_TRUE(writeFileRes);
180     std::vector<std::string> lines;
181     bool loadRes = FileUtil::LoadLinesFromFile(filePath, lines);
182     EXPECT_TRUE(loadRes);
183     EXPECT_EQ(lines[0], filePath);
184 
185     bool removeDirRes = FileUtil::ForceRemoveDirectory("", true);
186     EXPECT_FALSE(removeDirRes);
187     removeDirRes = FileUtil::ForceRemoveDirectory(testDir, true);
188     EXPECT_TRUE(removeDirRes);
189     isDir = FileUtil::IsDirectory(testDir);
190     EXPECT_FALSE(isDir);
191     std::cout << "HiAppEventFileUtil001 end" << std::endl;
192 }