1 /*
2 * Copyright (c) 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 #include <iostream>
18 #include <memory>
19 #include <string>
20 #include <vector>
21
22 #include "distributed_device_profile_constants.h"
23 #include "distributed_device_profile_errors.h"
24 #include "distributed_device_profile_log.h"
25 #include "profile_cache.h"
26 #include "static_profile_manager.h"
27 using namespace testing::ext;
28 namespace OHOS {
29 namespace DistributedDeviceProfile {
30 using namespace std;
31 namespace {
32 const std::string TAG = "StaticProfileManagerTest";
33 }
34 class StaticProfileManagerTest : public testing::Test {
35 public:
36 static void SetUpTestCase();
37 static void TearDownTestCase();
38 void SetUp();
39 void TearDown();
40 };
41
SetUpTestCase()42 void StaticProfileManagerTest::SetUpTestCase()
43 {
44 }
45
TearDownTestCase()46 void StaticProfileManagerTest::TearDownTestCase()
47 {
48 }
49
SetUp()50 void StaticProfileManagerTest::SetUp()
51 {
52 }
53
TearDown()54 void StaticProfileManagerTest::TearDown()
55 {
56 }
57
58 /*
59 * @tc.name: Init_001
60 * @tc.desc: Init
61 * @tc.type: FUNC
62 * @tc.require: I4NY1T
63 */
64 HWTEST_F(StaticProfileManagerTest, Init_001, TestSize.Level1)
65 {
66 int32_t errCode = StaticProfileManager::GetInstance().Init();
67 EXPECT_EQ(errCode, DP_SUCCESS);
68 }
69
70 /*
71 * @tc.name: UnInit_001
72 * @tc.desc: UnInit
73 * @tc.type: FUNC
74 * @tc.require: I4NY1T
75 */
76 HWTEST_F(StaticProfileManagerTest, UnInit_001, TestSize.Level1)
77 {
78 StaticProfileManager::GetInstance().Init();
79 int32_t errCode = StaticProfileManager::GetInstance().UnInit();
80 EXPECT_EQ(errCode, DP_SUCCESS);
81 }
82
83 /*
84 * @tc.name: ReInit_001
85 * @tc.desc: ReInit
86 * @tc.type: FUNC
87 * @tc.require: I4NY1T
88 */
89 HWTEST_F(StaticProfileManagerTest, ReInit_001, TestSize.Level1)
90 {
91 StaticProfileManager::GetInstance().Init();
92 int32_t errCode = StaticProfileManager::GetInstance().ReInit();
93 EXPECT_EQ(errCode, DP_SUCCESS);
94 }
95
96 /*
97 * @tc.name: PutCharacteristicProfile_001
98 * @tc.desc: PutCharacteristicProfile
99 * @tc.type: FUNC
100 * @tc.require: I4NY1T
101 */
102 HWTEST_F(StaticProfileManagerTest, PutCharacteristicProfile_001, TestSize.Level1)
103 {
104 CharacteristicProfile charProfile;
105 int32_t errCode = StaticProfileManager::GetInstance().PutCharacteristicProfile(charProfile);
106 EXPECT_EQ(errCode, DP_INVALID_PARAMS);
107 }
108
109 /*
110 * @tc.name: GetCharacteristicProfile_001
111 * @tc.desc: GetCharacteristicProfile
112 * @tc.type: FUNC
113 * @tc.require: I4NY1T
114 */
115 HWTEST_F(StaticProfileManagerTest, GetCharacteristicProfile_001, TestSize.Level1)
116 {
117 std::string deviceId;
118 std::string serviceName;
119 std::string characteristicKey;
120 CharacteristicProfile charProfile;
121 int32_t errCode = StaticProfileManager::GetInstance().GetCharacteristicProfile(deviceId, serviceName,
122 characteristicKey, charProfile);
123 EXPECT_EQ(errCode, DP_INVALID_PARAMS);
124 }
125
126 /*
127 * @tc.name: GetCharacteristicProfile_002
128 * @tc.desc: GetCharacteristicProfile
129 * @tc.type: FUNC
130 * @tc.require: I4NY1T
131 */
132 HWTEST_F(StaticProfileManagerTest, GetCharacteristicProfile_002, TestSize.Level1)
133 {
134 std::string deviceId = "deviceId";
135 std::string serviceName = "serviceName";
136 std::string characteristicKey = "";
137 CharacteristicProfile charProfile;
138 int32_t errCode = StaticProfileManager::GetInstance().GetCharacteristicProfile(deviceId, serviceName,
139 characteristicKey, charProfile);
140 EXPECT_EQ(errCode, DP_INVALID_PARAMS);
141 }
142
143 /*
144 * @tc.name: GetCharacteristicProfile_003
145 * @tc.desc: GetCharacteristicProfile
146 * @tc.type: FUNC
147 * @tc.require: I4NY1T
148 */
149 HWTEST_F(StaticProfileManagerTest, GetCharacteristicProfile_003, TestSize.Level1)
150 {
151 std::string deviceId = "";
152 std::string serviceName = "serviceName";
153 std::string characteristicKey = "characteristicKey";
154 CharacteristicProfile charProfile;
155 int32_t errCode = StaticProfileManager::GetInstance().GetCharacteristicProfile(deviceId, serviceName,
156 characteristicKey, charProfile);
157 EXPECT_EQ(errCode, DP_INVALID_PARAMS);
158 }
159
160 /*
161 * @tc.name: GetCharacteristicProfile_004
162 * @tc.desc: GetCharacteristicProfile
163 * @tc.type: FUNC
164 * @tc.require: I4NY1T
165 */
166 HWTEST_F(StaticProfileManagerTest, GetCharacteristicProfile_004, TestSize.Level1)
167 {
168 std::string deviceId = "deviceId";
169 std::string serviceName = "";
170 std::string characteristicKey = "characteristicKey";
171 CharacteristicProfile charProfile;
172 int32_t errCode = StaticProfileManager::GetInstance().GetCharacteristicProfile(deviceId, serviceName,
173 characteristicKey, charProfile);
174 EXPECT_EQ(errCode, DP_INVALID_PARAMS);
175 }
176
177 #ifndef DEVICE_PROFILE_STATIC_DISABLE
178 /*
179 * @tc.name: GetCharacteristicProfile_005
180 * @tc.desc: GetCharacteristicProfile
181 * @tc.type: FUNC
182 * @tc.require: I4NY1T
183 */
184 HWTEST_F(StaticProfileManagerTest, GetCharacteristicProfile_005, TestSize.Level1)
185 {
186 std::string deviceId = "deviceId";
187 std::string serviceName = "serviceName";
188 std::string characteristicKey = "characteristicKey";
189 CharacteristicProfile charProfile;
190 std::string charProfileKey =
191 CHAR_PREFIX + SEPARATOR + deviceId + SEPARATOR + serviceName + SEPARATOR + characteristicKey;
192 ProfileCache::GetInstance().staticCharProfileMap_.emplace(charProfileKey, charProfile);
193 int32_t errCode = StaticProfileManager::GetInstance().GetCharacteristicProfile(deviceId, serviceName,
194 characteristicKey, charProfile);
195 EXPECT_EQ(errCode, DP_SUCCESS);
196 }
197
198 /*
199 * @tc.name: GetCharacteristicProfile_006
200 * @tc.desc: GetCharacteristicProfile
201 * @tc.type: FUNC
202 * @tc.require: I4NY1T
203 */
204 HWTEST_F(StaticProfileManagerTest, GetCharacteristicProfile_006, TestSize.Level1)
205 {
206 std::string deviceId = "deviceId";
207 std::string serviceName = "serviceName";
208 std::string characteristicKey = "characteristicKey";
209 CharacteristicProfile charProfile;
210 int32_t errCode = StaticProfileManager::GetInstance().GetCharacteristicProfile(deviceId, serviceName,
211 characteristicKey, charProfile);
212 EXPECT_EQ(errCode, DP_SUCCESS);
213 }
214
215 /*
216 * @tc.name: GetAllCharacteristicProfile_001
217 * @tc.desc: GetAllCharacteristicProfile
218 * @tc.type: FUNC
219 * @tc.require: I4NY1T
220 */
221 HWTEST_F(StaticProfileManagerTest, GetAllCharacteristicProfile_001, TestSize.Level1)
222 {
223 std::vector<CharacteristicProfile> staticCapabilityProfiles;
224 int32_t errCode = StaticProfileManager::GetInstance().GetAllCharacteristicProfile(staticCapabilityProfiles);
225 EXPECT_EQ(errCode, DP_SUCCESS);
226 }
227 #endif // DEVICE_PROFILE_STATIC_DISABLE
228
229 /*
230 * @tc.name: GenerateStaticInfoProfile_001
231 * @tc.desc: GenerateStaticInfoProfile
232 * @tc.type: FUNC
233 * @tc.require: I4NY1T
234 */
235 HWTEST_F(StaticProfileManagerTest, GenerateStaticInfoProfile_001, TestSize.Level1)
236 {
237 CharacteristicProfile staticCapabilityProfile;
238 std::unordered_map<std::string, CharacteristicProfile> staticInfoProfiles;
239 int32_t errCode = StaticProfileManager::GetInstance().GenerateStaticInfoProfile(staticCapabilityProfile,
240 staticInfoProfiles);
241 EXPECT_EQ(errCode, DP_PARSE_STATIC_CAP_FAIL);
242 }
243
244 HWTEST_F(StaticProfileManagerTest, GenerateStaticInfoProfile_002, TestSize.Level1)
245 {
246 CharacteristicProfile staticCapabilityProfile;
247 staticCapabilityProfile.SetCharacteristicValue(R"({
248 "version": "1.0",
249 "value": "test_value"
250 })");
251 staticCapabilityProfile.SetDeviceId("device_123");
252
253 std::unordered_map<std::string, CharacteristicProfile> staticInfoProfiles;
254
255 int32_t result =
256 StaticProfileManager::GetInstance().GenerateStaticInfoProfile(staticCapabilityProfile, staticInfoProfiles);
257 EXPECT_EQ(result, DP_PARSE_STATIC_CAP_FAIL);
258 }
259
260 HWTEST_F(StaticProfileManagerTest, GenerateStaticInfoProfile_003, TestSize.Level1)
261 {
262 CharacteristicProfile staticCapabilityProfile;
263 staticCapabilityProfile.SetCharacteristicValue("invalid_json");
264
265 std::unordered_map<std::string, CharacteristicProfile> staticInfoProfiles;
266
267 int32_t result =
268 StaticProfileManager::GetInstance().GenerateStaticInfoProfile(staticCapabilityProfile, staticInfoProfiles);
269 EXPECT_EQ(result, DP_PARSE_STATIC_CAP_FAIL);
270 }
271
272 HWTEST_F(StaticProfileManagerTest, GenerateStaticInfoProfile_004, TestSize.Level1)
273 {
274 CharacteristicProfile staticCapabilityProfile;
275 staticCapabilityProfile.SetCharacteristicValue(R"({ "value": "test_value" })");
276
277 std::unordered_map<std::string, CharacteristicProfile> staticInfoProfiles;
278
279 int32_t result =
280 StaticProfileManager::GetInstance().GenerateStaticInfoProfile(staticCapabilityProfile, staticInfoProfiles);
281 EXPECT_EQ(result, DP_PARSE_STATIC_CAP_FAIL);
282 }
283
284 HWTEST_F(StaticProfileManagerTest, GenerateStaticInfoProfile_005, TestSize.Level1)
285 {
286 CharacteristicProfile staticCapabilityProfile;
287 staticCapabilityProfile.SetCharacteristicValue(R"({ "version": "1.0" })");
288
289 std::unordered_map<std::string, CharacteristicProfile> staticInfoProfiles;
290
291 int32_t result =
292 StaticProfileManager::GetInstance().GenerateStaticInfoProfile(staticCapabilityProfile, staticInfoProfiles);
293 EXPECT_EQ(result, DP_PARSE_STATIC_CAP_FAIL);
294 }
295 } // namespace DistributedDeviceProfile
296 } // namespace OHOS
297