1 /*
2  * Copyright (c) 2022 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 "distributed_sched_utils_test.h"
17 
18 #include <string>
19 
20 #include "config_policy_utils.h"
21 
22 #include "distributed_sched_utils.h"
23 #include "dtbschedmgr_log.h"
24 
25 using namespace testing;
26 using namespace testing::ext;
27 
28 namespace OHOS {
29 namespace DistributedSchedule {
30 const std::string TAG = "DistributedSchedUtilsTest";
31 constexpr int32_t MAX_TEST_PATH_LEN = 1024;
32 const std::string TEST_CONFIG_RELATIVE_PATH = "etc/distributedhardware/distributed_hardware_components_cfg.json";
33 
SetUpTestCase()34 void DistributedSchedUtilsTest::SetUpTestCase()
35 {
36     HILOGI("DistributedSchedUtilsTest::SetUpTestCase");
37 }
38 
TearDownTestCase()39 void DistributedSchedUtilsTest::TearDownTestCase()
40 {
41     HILOGI("DistributedSchedUtilsTest::TearDownTestCase");
42 }
43 
TearDown()44 void DistributedSchedUtilsTest::TearDown()
45 {
46     HILOGI("DistributedSchedUtilsTest::TearDown");
47 }
48 
SetUp()49 void DistributedSchedUtilsTest::SetUp()
50 {
51     HILOGI("DistributedSchedUtilsTest::SetUp");
52 }
53 
54 /**
55  * @tc.name: IsValidPath_001
56  * @tc.desc: File path is invalid
57  * @tc.type: FUNC
58  * @tc.require: I5WKCK
59  */
60 HWTEST_F(DistributedSchedUtilsTest, IsValidPath_001, TestSize.Level1)
61 {
62     std::string inFilePath = "invalid_path";
63     std::string realFilePath = "";
64     EXPECT_FALSE(IsValidPath(inFilePath, realFilePath));
65     EXPECT_EQ("", realFilePath);
66 
67     inFilePath = "/data/123_test.json";
68     EXPECT_FALSE(IsValidPath(inFilePath, realFilePath));
69     EXPECT_EQ("", realFilePath);
70 }
71 
72 /**
73  * @tc.name: IsValidPath_002
74  * @tc.desc: File path is valid
75  * @tc.type: FUNC
76  * @tc.require: I5WKCK
77  */
78 HWTEST_F(DistributedSchedUtilsTest, IsValidPath_002, TestSize.Level1)
79 {
80     char cfgPathBuf[MAX_TEST_PATH_LEN] = { 0 };
81     char *filePath  = GetOneCfgFile(TEST_CONFIG_RELATIVE_PATH.c_str(), cfgPathBuf, MAX_TEST_PATH_LEN);
82     EXPECT_NE(nullptr, filePath);
83     EXPECT_EQ(cfgPathBuf, filePath);
84 
85     std::string inFilePath = std::string(cfgPathBuf);
86     std::string realFilePath = "";
87     EXPECT_TRUE(IsValidPath(inFilePath, realFilePath));
88     EXPECT_FALSE(realFilePath.empty());
89 }
90 
91 /**
92  * @tc.name: CheckBundleContinueConfig_001
93  * @tc.desc: Check bundle continue config when existing config file
94  * @tc.type: FUNC
95  * @tc.require: I5WKCK
96  */
97 HWTEST_F(DistributedSchedUtilsTest, CheckBundleContinueConfig_001, TestSize.Level1)
98 {
99     std::string cfgJsonStr = R"({
100         "allow_applist":["test_bundle_0"]
101     })";
102     EXPECT_TRUE(UpdateAllowAppList(cfgJsonStr));
103 
104     std::string bundleName = "test";
105     EXPECT_FALSE(CheckBundleContinueConfig(bundleName));
106 
107     bundleName = "test_bundle_0";
108     EXPECT_TRUE(CheckBundleContinueConfig(bundleName));
109 }
110 
111 /**
112  * @tc.name: UpdateAllowAppList_001
113  * @tc.desc: Update allow app list with invalid cfgJsonStr
114  * @tc.type: FUNC
115  * @tc.require: I5WKCK
116  */
117 HWTEST_F(DistributedSchedUtilsTest, UpdateAllowAppList_001, TestSize.Level1)
118 {
119     std::string cfgJsonStr = "";
120     EXPECT_FALSE(UpdateAllowAppList(cfgJsonStr));
121 
122     cfgJsonStr = "12345";
123     EXPECT_FALSE(UpdateAllowAppList(cfgJsonStr));
124 
125     cfgJsonStr = R"({
126         "Name":["test_one"],
127         "ID":"12345"
128     })";
129     EXPECT_FALSE(UpdateAllowAppList(cfgJsonStr));
130 
131     cfgJsonStr = R"({
132         "allow_applist":"12345"
133     })";
134     EXPECT_FALSE(UpdateAllowAppList(cfgJsonStr));
135 }
136 
137 /**
138  * @tc.name: UpdateAllowAppList_002
139  * @tc.desc: Update allow app list with valid cfgJsonStr
140  * @tc.type: FUNC
141  * @tc.require: I5WKCK
142  */
143 HWTEST_F(DistributedSchedUtilsTest, UpdateAllowAppList_002, TestSize.Level1)
144 {
145     std::string cfgJsonStr = R"({
146         "allow_applist":["test_bundle_1"]
147     })";
148     EXPECT_TRUE(UpdateAllowAppList(cfgJsonStr));
149 }
150 
151 /**
152  * @tc.name: LoadContinueConfig_001
153  * @tc.desc: Load continue config success
154  * @tc.type: FUNC
155  * @tc.require: I5WKCK
156  */
157 HWTEST_F(DistributedSchedUtilsTest, LoadContinueConfig_001, TestSize.Level1)
158 {
159     EXPECT_EQ(ERR_OK, LoadContinueConfig());
160     EXPECT_EQ(ERR_OK, LoadContinueConfig());
161 }
162 
163 /**
164  * @tc.name: CheckBundleContinueConfig_002
165  * @tc.desc: Check bundle continue config when missing config file
166  * @tc.type: FUNC
167  * @tc.require: I5WKCK
168  */
169 HWTEST_F(DistributedSchedUtilsTest, CheckBundleContinueConfig_002, TestSize.Level1)
170 {
171     EXPECT_EQ(ERR_OK, LoadContinueConfig());
172 
173     std::string bundleName = "test_bundle_1";
174     EXPECT_TRUE(CheckBundleContinueConfig(bundleName));
175 }
176 } // namespace DistributedSchedule
177 } // namespace OHOS
178