1 /*
2 * Copyright (c) 2022-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 <memory>
17 #include <cstdio>
18 #include <unistd.h>
19
20 #include "dfs_error.h"
21 #include "gtest/gtest.h"
22 #include "network/devsl_dispatcher.h"
23 #include "network/kernel_talker.h"
24 #include "softbus_bus_center.h"
25 #include "utils_log.h"
26
27 namespace {
28 int32_t g_getHighestSecLevel;
29 }
30
DATASL_GetHighestSecLevel(DEVSLQueryParams * queryParams,uint32_t * levelInfo)31 int32_t DATASL_GetHighestSecLevel(DEVSLQueryParams *queryParams, uint32_t *levelInfo)
32 {
33 return g_getHighestSecLevel;
34 }
35
36 namespace OHOS {
37 namespace Storage {
38 namespace DistributedFile {
39 namespace Test {
40 using namespace testing::ext;
41 using namespace std;
42
43 class DevslDispatcherTest : public testing::Test {
44 public:
SetUpTestCase(void)45 static void SetUpTestCase(void) {};
TearDownTestCase(void)46 static void TearDownTestCase(void) {};
SetUp()47 void SetUp() {};
TearDown()48 void TearDown() {};
49 };
50
51 /**
52 * @tc.name: DevslDispatcherTest_Start_Stop_0100
53 * @tc.desc: Verify the Start/Stop function.
54 * @tc.type: FUNC
55 * @tc.require: SR000H0387
56 */
57 HWTEST_F(DevslDispatcherTest, DevslDispatcherTest_Start_Stop_0100, TestSize.Level1)
58 {
59 GTEST_LOG_(INFO) << "DevslDispatcherTest_Start_Stop_0100 start";
60 bool res = true;
61 try {
62 DevslDispatcher::Start();
63 DevslDispatcher::Stop();
64 } catch (const exception &e) {
65 res = false;
66 LOGE("%{public}s", e.what());
67 }
68 EXPECT_TRUE(res == true);
69 GTEST_LOG_(INFO) << "DevslDispatcherTest_Start_Stop_0100 end";
70 }
71
72 /**
73 * @tc.name: DevslDispatcherTest_MakeDevslQueryParams_0100
74 * @tc.desc: Verify the MakeDevslQueryParams function.
75 * @tc.type: FUNC
76 * @tc.require: SR000H0387
77 */
78 HWTEST_F(DevslDispatcherTest, DevslDispatcherTest_MakeDevslQueryParams_0100, TestSize.Level1)
79 {
80 GTEST_LOG_(INFO) << "DevslDispatcherTest_MakeDevslQueryParams_0100 start";
81 bool res = true;
82 string udid = "0123";
83 try {
84 DEVSLQueryParams queryParam = DevslDispatcher::MakeDevslQueryParams(udid);
85 EXPECT_TRUE(queryParam.udidLen == udid.size());
86 } catch (const exception &e) {
87 res = false;
88 LOGE("%{public}s", e.what());
89 }
90 EXPECT_TRUE(res == true);
91 GTEST_LOG_(INFO) << "DevslDispatcherTest_MakeDevslQueryParams_0100 end";
92 }
93
94 /**
95 * @tc.name: DevslDispatcherTest_DevslGottonCallbackAsync_0100
96 * @tc.desc: Verify the DevslGottonCallbackAsync function.
97 * @tc.type: FUNC
98 * @tc.require: SR000H0387
99 */
100 HWTEST_F(DevslDispatcherTest, DevslDispatcherTest_DevslGottonCallbackAsync_0100, TestSize.Level1)
101 {
102 GTEST_LOG_(INFO) << "DevslDispatcherTest_DevslGottonCallbackAsync_0100 start";
103 bool res = true;
104 string udid = "0";
105 uint32_t devsl = 1;
106 try {
107 DevslDispatcher::DevslGottonCallbackAsync(udid, devsl);
108 } catch (const exception &e) {
109 res = false;
110 LOGE("%{public}s", e.what());
111 }
112 EXPECT_TRUE(res == true);
113 GTEST_LOG_(INFO) << "DevslDispatcherTest_DevslGottonCallbackAsync_0100 end";
114 }
115
116 /**
117 * @tc.name: DevslDispatcherTest_DevslGottonCallback_0100
118 * @tc.desc: Verify the DevslGottonCallback function.
119 * @tc.type: FUNC
120 * @tc.require: SR000H0387
121 */
122 HWTEST_F(DevslDispatcherTest, DevslDispatcherTest_DevslGottonCallback_0100, TestSize.Level1)
123 {
124 GTEST_LOG_(INFO) << "DevslDispatcherTest_DevslGottonCallback_0100 start";
125 bool res = true;
126 uint32_t level = 0;
127 DEVSLQueryParams queryParam = {
128 .udid = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0},
129 .udidLen = 10
130 };
131
132 try {
133 DevslDispatcher::DevslGottonCallback(&queryParam, 0, level);
134 DevslDispatcher::DevslGottonCallback(&queryParam, 1, level);
135 } catch (const exception &e) {
136 res = false;
137 LOGE("%{public}s", e.what());
138 }
139
140 EXPECT_TRUE(res == true);
141 GTEST_LOG_(INFO) << "DevslDispatcherTest_DevslGottonCallback_0100 end";
142 }
143
144 /**
145 * @tc.name: DevslDispatcherTest_DevslGottonCallback_0101
146 * @tc.desc: Verify the DevslGottonCallback function.
147 * @tc.type: FUNC
148 * @tc.require: SR000H0387
149 */
150 HWTEST_F(DevslDispatcherTest, DevslDispatcherTest_DevslGottonCallback_0101, TestSize.Level1)
151 {
152 GTEST_LOG_(INFO) << "DevslDispatcherTest_DevslGottonCallback_0101 start";
153 bool res = true;
154 uint32_t level = 0;
155 DEVSLQueryParams queryParam;
156 try {
157 DevslDispatcher::DevslGottonCallback(&queryParam, 0, level);
158 DevslDispatcher::DevslGottonCallback(&queryParam, 1, level);
159 } catch (const exception &e) {
160 res = false;
161 LOGE("%{public}s", e.what());
162 }
163
164 EXPECT_TRUE(res == true);
165 GTEST_LOG_(INFO) << "DevslDispatcherTest_DevslGottonCallback_0101 end";
166 }
167
168 /**
169 * @tc.name: DevslDispatcherTest_DevslGetRegister_0100
170 * @tc.desc: Verify the DevslGetRegister function.
171 * @tc.type: FUNC
172 * @tc.require: SR000H0387
173 */
174 HWTEST_F(DevslDispatcherTest, DevslDispatcherTest_DevslGetRegister_0100, TestSize.Level1)
175 {
176 GTEST_LOG_(INFO) << "DevslDispatcherTest_DevslGetRegister_0100 start";
177 bool res = true;
178 const string cid = "scid";
179 constexpr int userId = 100;
180 auto smp = make_shared<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(userId, "account"));
181 weak_ptr<MountPoint> wmp = smp;
__anon97dddebd0202(NotifyParam ¶m) 182 auto kernelTalker = std::make_shared<KernelTalker>(wmp, [](NotifyParam ¶m) {}, [](const std::string &) {});
183
184 try {
185 DevslDispatcher::DevslGetRegister(cid, kernelTalker);
186 } catch (const exception &e) {
187 res = false;
188 LOGE("%{public}s", e.what());
189 }
190
191 EXPECT_TRUE(res == true);
192 GTEST_LOG_(INFO) << "DevslDispatcherTest_DevslGetRegister_0100 end";
193 }
194
195 /**
196 * @tc.name: DevslDispatcherTest_CompareDevslWithLocal_0100
197 * @tc.desc: Verify the CompareDevslWithLocal function.
198 * @tc.type: FUNC
199 * @tc.require: SR000H0387
200 */
201 HWTEST_F(DevslDispatcherTest, DevslDispatcherTest_CompareDevslWithLocal_0100, TestSize.Level1)
202 {
203 GTEST_LOG_(INFO) << "DevslDispatcherTest_CompareDevslWithLocal_0100 start";
204 EXPECT_FALSE(DevslDispatcher::CompareDevslWithLocal("test", {}));
205
206 DevslDispatcher::devslMap_["test"] = FileManagement::ERR_BAD_VALUE;
207 EXPECT_FALSE(DevslDispatcher::CompareDevslWithLocal("test", {"path"}));
208
209 DevslDispatcher::devslMap_["test"] = 1;
210 EXPECT_FALSE(DevslDispatcher::CompareDevslWithLocal("test", {"path"}));
211
212 DevslDispatcher::devslMap_["test"] = static_cast<int32_t>(SecurityLabel::S4);
213 EXPECT_TRUE(DevslDispatcher::CompareDevslWithLocal("test", {"path"}));
214
215 GTEST_LOG_(INFO) << "DevslDsispatcherTest_CompareDevslWithLocal_0100 end";
216 }
217
218 /**
219 * @tc.name: DevslDispatcherTest_GetDeviceDevsl_0100
220 * @tc.desc: Verify the GetDeviceDevsl function.
221 * @tc.type: FUNC
222 * @tc.require: SR000H0387
223 */
224 HWTEST_F(DevslDispatcherTest, DevslDispatcherTest_GetDeviceDevsl_0100, TestSize.Level1)
225 {
226 GTEST_LOG_(INFO) << "DevslDispatcherTest_GetDeviceDevsl_0100 start";
227 EXPECT_EQ(DevslDispatcher::GetDeviceDevsl(""), -1);
228
229 DevslDispatcher::devslMap_.clear();
230 DevslDispatcher::devslMap_.insert(make_pair("test", 1));
231 EXPECT_EQ(DevslDispatcher::GetDeviceDevsl("test"), 1);
232
233 DevslDispatcher::devslMap_.clear();
234 g_getHighestSecLevel = FileManagement::ERR_BAD_VALUE;
235 EXPECT_EQ(DevslDispatcher::GetDeviceDevsl("test"), FileManagement::ERR_BAD_VALUE);
236
237 DevslDispatcher::devslMap_.clear();
238 g_getHighestSecLevel = FileManagement::E_OK;
239 EXPECT_EQ(DevslDispatcher::GetDeviceDevsl("test"), FileManagement::E_OK);
240 GTEST_LOG_(INFO) << "DevslDsispatcherTest_GetDeviceDevsl_0100 end";
241 }
242 } // namespace Test
243 } // namespace DistributedFile
244 } // namespace Storage
245 } // namespace OHOS
246