• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <gtest/gtest.h>
17 #include <memory>
18 
19 #include "device_profile_errors.h"
20 #include "iprofile_event_callback.h"
21 #include "iprofile_event_notifier.h"
22 #include "profile_change_notification.h"
23 #include "profile_event.h"
24 #include "profile_event_notifier_proxy.h"
25 #include "profile_event_notifier_stub.h"
26 
27 #include "utils.h"
28 
29 namespace OHOS {
30 namespace DeviceProfile {
31 using namespace testing;
32 using namespace testing::ext;
33 
34 namespace {
35 constexpr int32_t MAX_ENTRY_LEN = 1000001;
36 }
37 
38 class ProfileChangeNotificationTest : public testing::Test {
39 public:
40     static void SetUpTestCase();
41     static void TearDownTestCase();
42     void SetUp();
43     void TearDown();
44 };
45 
46 class StorageProfileEventCallback : public IProfileEventCallback {
47 };
48 
SetUpTestCase()49 void ProfileChangeNotificationTest::SetUpTestCase()
50 {
51     DTEST_LOG << "SetUpTestCase" << std::endl;
52 }
53 
TearDownTestCase()54 void ProfileChangeNotificationTest::TearDownTestCase()
55 {
56     DTEST_LOG << "TearDownTestCase" << std::endl;
57 }
58 
SetUp()59 void ProfileChangeNotificationTest::SetUp()
60 {
61     DTEST_LOG << "SetUp" << std::endl;
62 }
63 
TearDown()64 void ProfileChangeNotificationTest::TearDown()
65 {
66     DTEST_LOG << "TearDown" << std::endl;
67 }
68 
69 /**
70  * @tc.name: GetProfileEntries_001
71  * @tc.desc: get profile entries
72  * @tc.type: FUNC
73  * @tc.require: I4NY1T
74  */
75 HWTEST_F(ProfileChangeNotificationTest, GetProfileEntries_001, TestSize.Level3)
76 {
77     ProfileChangeNotification notification;
78     auto result = notification.GetProfileEntries();
79     EXPECT_EQ(true, result.empty());
80 }
81 
82 /**
83  * @tc.name: GetProfileEntries_002
84  * @tc.desc: get profile entries
85  * @tc.type: FUNC
86  * @tc.require: I4NY1T
87  */
88 HWTEST_F(ProfileChangeNotificationTest, GetProfileEntries_002, TestSize.Level3)
89 {
90     std::vector<ProfileEntry> profileEntries;
91     std::string networkId;
92     bool isLocal = true;
93     ProfileChangeNotification notification(profileEntries, networkId, isLocal);
94     auto result = notification.GetProfileEntries();
95     EXPECT_EQ(true, result.empty());
96 }
97 
98 /**
99  * @tc.name: GetDeviceId_001
100  * @tc.desc: get device id
101  * @tc.type: FUNC
102  * @tc.require: I4NY1T
103  */
104 HWTEST_F(ProfileChangeNotificationTest, GetDeviceId_001, TestSize.Level3)
105 {
106     std::vector<ProfileEntry> profileEntries;
107     std::string networkId;
108     bool isLocal = true;
109     std::shared_ptr<ProfileChangeNotification> temp =
110         std::make_shared<ProfileChangeNotification>(profileEntries, networkId, isLocal);
111     std::string result = temp->GetDeviceId();
112     EXPECT_EQ("", result);
113 }
114 
115 /**
116  * @tc.name: IsLocal_001
117  * @tc.desc: judge is local
118  * @tc.type: FUNC
119  * @tc.require: I4NY1T
120  */
121 HWTEST_F(ProfileChangeNotificationTest, IsLocal_001, TestSize.Level3)
122 {
123     std::shared_ptr<ProfileChangeNotification> temp = std::make_shared<ProfileChangeNotification>();
124     bool result = temp->IsLocal();
125     EXPECT_EQ(false, result);
126 }
127 
128 /**
129  * @tc.name: Marshalling_001
130  * @tc.desc: marshalling of profile change notification
131  * @tc.type: FUNC
132  * @tc.require: I4NY1T
133  */
134 HWTEST_F(ProfileChangeNotificationTest, Marshalling_001, TestSize.Level3)
135 {
136     Parcel parcel;
137     std::shared_ptr<ProfileChangeNotification> temp = std::make_shared<ProfileChangeNotification>();
138     bool result = temp->Marshalling(parcel);
139     EXPECT_EQ(true, result);
140 }
141 
142 /**
143  * @tc.name: Marshalling_001
144  * @tc.desc: marshalling of profile change notification
145  * @tc.type: FUNC
146  * @tc.require: I4NY1T
147  */
148 HWTEST_F(ProfileChangeNotificationTest, Marshalling_002, TestSize.Level3)
149 {
150     ProfileEntry entry;
151     std::vector<ProfileEntry> profileEntries(1, entry);
152     std::string networkId = "112223";
153     bool isLocal = true;
154     ProfileChangeNotification notification(profileEntries, networkId, isLocal);
155     Parcel parcel;
156     bool ret = notification.Marshalling(parcel);
157     EXPECT_TRUE(ret);
158 }
159 
160 /**
161  * @tc.name: Unmarshalling_001
162  * @tc.desc: unmarshalling of profile change notification
163  * @tc.type: FUNC
164  * @tc.require: I4NY1T
165  */
166 HWTEST_F(ProfileChangeNotificationTest, Unmarshalling_001, TestSize.Level3)
167 {
168     Parcel parcel;
169     std::shared_ptr<ProfileChangeNotification> temp = std::make_shared<ProfileChangeNotification>();
170     bool result = temp->Unmarshalling(parcel);
171     EXPECT_EQ(false, result);
172 }
173 
174 /**
175  * @tc.name: Unmarshalling_002
176  * @tc.desc: unmarshalling of profile change notification
177  * @tc.type: FUNC
178  * @tc.require: I4NY1T
179  */
180 HWTEST_F(ProfileChangeNotificationTest, Unmarshalling_002, TestSize.Level3)
181 {
182     Parcel parcel;
183     parcel.WriteInt32(-1);
184     std::shared_ptr<ProfileChangeNotification> temp = std::make_shared<ProfileChangeNotification>();
185     bool result = temp->Unmarshalling(parcel);
186     EXPECT_FALSE(result);
187 }
188 
189 /**
190  * @tc.name: Unmarshalling_002
191  * @tc.desc: unmarshalling of profile change notification
192  * @tc.type: FUNC
193  * @tc.require: I4NY1T
194  */
195 HWTEST_F(ProfileChangeNotificationTest, Unmarshalling_003, TestSize.Level3)
196 {
197     Parcel parcel;
198     parcel.WriteInt32(MAX_ENTRY_LEN);
199     std::shared_ptr<ProfileChangeNotification> temp = std::make_shared<ProfileChangeNotification>();
200     bool result = temp->Unmarshalling(parcel);
201     EXPECT_FALSE(result);
202 }
203 
204 /**
205  * @tc.name: Unmarshalling_002
206  * @tc.desc: unmarshalling of profile change notification
207  * @tc.type: FUNC
208  * @tc.require: I4NY1T
209  */
210 HWTEST_F(ProfileChangeNotificationTest, Unmarshalling_004, TestSize.Level3)
211 {
212     Parcel parcel;
213     parcel.WriteInt32(1);
214     parcel.WriteString("111");
215     parcel.WriteString("222");
216     uint8_t type = 1;
217     parcel.WriteUint8(type);
218     parcel.WriteString("222");
219     std::shared_ptr<ProfileChangeNotification> temp = std::make_shared<ProfileChangeNotification>();
220     bool result = temp->Unmarshalling(parcel);
221     EXPECT_TRUE(result);
222 }
223 
224 /**
225  * @tc.name: Unmarshalling_002
226  * @tc.desc: unmarshalling of profile change notification
227  * @tc.type: FUNC
228  * @tc.require: I4NY1T
229  */
230 HWTEST_F(ProfileChangeNotificationTest, Unmarshalling_005, TestSize.Level3)
231 {
232     Parcel parcel;
233     parcel.WriteInt32(1);
234     std::shared_ptr<ProfileChangeNotification> temp = std::make_shared<ProfileChangeNotification>();
235     bool result = temp->Unmarshalling(parcel);
236     EXPECT_FALSE(result);
237 }
238 
239 /**
240  * @tc.name: OnRemoteRequest_001
241  * @tc.desc: OnRemoteRequest of profile event notification
242  * @tc.type: FUNC
243  * @tc.require: I4NY1T
244  */
245 HWTEST_F(ProfileChangeNotificationTest, OnRemoteRequest_001, TestSize.Level3)
246 {
247     MessageParcel data;
248     MessageParcel reply;
249     MessageOption option { MessageOption::TF_ASYNC };
250     SyncResult syncResults;
251     syncResults.emplace("testdeviceid", SUCCEEDED);
252     if (!data.WriteInterfaceToken(ProfileEventNotifierProxy::GetDescriptor())) {
253         return;
254     }
255     if (!data.WriteInt32(static_cast<int32_t>(syncResults.size()))) {
256         return;
257     }
258 
259     for (const auto& [deviceId, syncResult] : syncResults) {
260         if (!data.WriteString(deviceId) ||
261             !data.WriteInt32(static_cast<int32_t>(syncResult))) {
262             return;
263         }
264     }
265 
266     auto syncCb = std::make_shared<StorageProfileEventCallback>();
267     std::shared_ptr<ProfileEventNotifierStub> temp = std::make_shared<ProfileEventNotifierStub>(syncCb);
268     int32_t result = temp->OnRemoteRequest(EVENT_SYNC_COMPLETED, data, reply, option);
269     DTEST_LOG << "result: " << result << std::endl;
270     EXPECT_EQ(0, result);
271 }
272 /**
273  * @tc.name: OnRemoteRequest_002
274  * @tc.desc: OnRemoteRequest of profile event notification
275  * @tc.type: FUNC
276  * @tc.require: I4NY1T
277  */
278 HWTEST_F(ProfileChangeNotificationTest, OnRemoteRequest_002, TestSize.Level3)
279 {
280     MessageParcel data;
281     MessageParcel reply;
282     MessageOption option { MessageOption::TF_ASYNC };
283     if (!data.WriteInterfaceToken(ProfileEventNotifierProxy::GetDescriptor())) {
284         return;
285     }
286 
287     auto syncCb = std::make_shared<StorageProfileEventCallback>();
288     std::shared_ptr<ProfileEventNotifierStub> temp = std::make_shared<ProfileEventNotifierStub>(syncCb);
289     int32_t result = temp->OnRemoteRequest(EVENT_PROFILE_CHANGED, data, reply, option);
290     DTEST_LOG << "result: " << result << std::endl;
291     EXPECT_EQ(ERR_DP_INVALID_PARAMS, result);
292 }
293 
294 /**
295  * @tc.name: OnRemoteRequest_003
296  * @tc.desc: OnRemoteRequest of profile event notification
297  * @tc.type: FUNC
298  * @tc.require: I4NY1T
299  */
300 HWTEST_F(ProfileChangeNotificationTest, OnRemoteRequest_003, TestSize.Level3)
301 {
302     MessageParcel data;
303     MessageParcel reply;
304     MessageOption option { MessageOption::TF_ASYNC };
305     SyncResult syncResults;
306     if (!data.WriteInterfaceToken(ProfileEventNotifierProxy::GetDescriptor())) {
307         return;
308     }
309     if (!data.WriteInt32(static_cast<int32_t>(syncResults.size()))) {
310         return;
311     }
312 
313     auto syncCb = std::make_shared<StorageProfileEventCallback>();
314     std::shared_ptr<ProfileEventNotifierStub> temp = std::make_shared<ProfileEventNotifierStub>(syncCb);
315     int32_t result = temp->OnRemoteRequest(EVENT_SYNC_COMPLETED, data, reply, option);
316     DTEST_LOG << "result: " << result << std::endl;
317     EXPECT_EQ(ERR_DP_INVALID_PARAMS, result);
318 }
319 }
320 }