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
18 #include "device_profile_errors.h"
19 #include "distributed_device_profile_client.h"
20 #include "distributed_device_profile_service.h"
21 #include "subscribe_info.h"
22 #include "utils.h"
23
24 namespace OHOS {
25 namespace DeviceProfile {
26 using namespace testing;
27 using namespace testing::ext;
28
29 namespace {
30 constexpr uint32_t MAX_EVENT_LEN = 1000001;
31 const std::u16string DP_INTERFACE_TOKEN = u"OHOS.DeviceProfile.IDistributedDeviceProfile";
32 }
33
34 class DistributedDeviceProfileServiceTest : public testing::Test {
35 public:
36 static void SetUpTestCase();
37 static void TearDownTestCase();
38 void SetUp();
39 void TearDown();
40 };
41
42 class StorageProfileEventCallback : public IProfileEventCallback {
43 };
44
45
SetUpTestCase()46 void DistributedDeviceProfileServiceTest::SetUpTestCase()
47 {
48 DTEST_LOG << "SetUpTestCase" << std::endl;
49 }
50
TearDownTestCase()51 void DistributedDeviceProfileServiceTest::TearDownTestCase()
52 {
53 DTEST_LOG << "TearDownTestCase" << std::endl;
54 }
55
SetUp()56 void DistributedDeviceProfileServiceTest::SetUp()
57 {
58 DTEST_LOG << "SetUp" << std::endl;
59 }
60
TearDown()61 void DistributedDeviceProfileServiceTest::TearDown()
62 {
63 DTEST_LOG << "TearDown" << std::endl;
64 }
65
66 /**
67 * @tc.name: OnRemoteRequest_001
68 * @tc.desc: WriteProfileEvents
69 * @tc.type: FUNC
70 * @tc.require: I4NY1T
71 */
72 HWTEST_F(DistributedDeviceProfileServiceTest, OnRemoteRequest_001, TestSize.Level3)
73 {
74 MessageParcel data;
75 MessageParcel reply;
76 MessageOption option;
77 int32_t ret = DistributedDeviceProfileService::GetInstance().OnRemoteRequest(0, data, reply, option);
78 EXPECT_NE(ret, ERR_NONE);
79 }
80
81 /**
82 * @tc.name: OnRemoteRequest_002
83 * @tc.desc: WriteProfileEvents
84 * @tc.type: FUNC
85 * @tc.require: I4NY1T
86 */
87 HWTEST_F(DistributedDeviceProfileServiceTest, OnRemoteRequest_002, TestSize.Level3)
88 {
89 MessageParcel data;
90 MessageParcel reply;
91 MessageOption option;
92 int32_t ret = DistributedDeviceProfileService::GetInstance().OnRemoteRequest(1, data, reply, option);
93 EXPECT_NE(ret, ERR_NONE);
94 }
95
96 /**
97 * @tc.name: PutDeviceProfileInner_001
98 * @tc.desc: PutDeviceProfileInner
99 * @tc.type: FUNC
100 * @tc.require: I4NY1T
101 */
102 HWTEST_F(DistributedDeviceProfileServiceTest, PutDeviceProfileInner_001, TestSize.Level3)
103 {
104 MessageParcel data;
105 MessageParcel reply;
106 int32_t ret = DistributedDeviceProfileService::GetInstance().PutDeviceProfileInner(data, reply);
107 EXPECT_NE(ret, ERR_NONE);
108 }
109
110 /**
111 * @tc.name: SubscribeProfileEventInner_001
112 * @tc.desc: SubscribeProfileEventInner
113 * @tc.type: FUNC
114 * @tc.require: I4NY1T
115 */
116 HWTEST_F(DistributedDeviceProfileServiceTest, SubscribeProfileEventInner_001, TestSize.Level3)
117 {
118 MessageParcel data;
119 data.WriteUint32(0);
120 MessageParcel reply;
121 int32_t ret = DistributedDeviceProfileService::GetInstance().GetDeviceProfileInner(data, reply);
122 EXPECT_NE(ret, ERR_NONE);
123 }
124
125 /**
126 * @tc.name: SubscribeProfileEventInner_001
127 * @tc.desc: SubscribeProfileEventInner
128 * @tc.type: FUNC
129 * @tc.require: I4NY1T
130 */
131 HWTEST_F(DistributedDeviceProfileServiceTest, SubscribeProfileEventInner_002, TestSize.Level3)
132 {
133 MessageParcel data;
134 data.WriteUint32(MAX_EVENT_LEN);
135 MessageParcel reply;
136 int32_t ret = DistributedDeviceProfileService::GetInstance().GetDeviceProfileInner(data, reply);
137 EXPECT_NE(ret, ERR_NONE);
138 }
139
140 /**
141 * @tc.name: Dump_001
142 * @tc.desc: Dump
143 * @tc.type: FUNC
144 * @tc.require: I4NY1T
145 */
146 HWTEST_F(DistributedDeviceProfileServiceTest, Dump_001, TestSize.Level3)
147 {
148 std::vector<std::u16string> args;
149 int32_t fd = 0;
150 int32_t ret = DistributedDeviceProfileService::GetInstance().Dump(fd, args);
151 EXPECT_EQ(ERR_DP_FILE_FAILED_ERR, ret);
152 }
153
154 /**
155 * @tc.name: Dump_002
156 * @tc.desc: Dump
157 * @tc.type: FUNC
158 * @tc.require: I4NY1T
159 */
160 HWTEST_F(DistributedDeviceProfileServiceTest, Dump_002, TestSize.Level3)
161 {
162 std::vector<std::u16string> args;
163 std::u16string help(u"-h");
164 args.emplace_back(help);
165 int32_t fd = 0;
166 int32_t ret = DistributedDeviceProfileService::GetInstance().Dump(fd, args);
167 EXPECT_EQ(ERR_DP_FILE_FAILED_ERR, ret);
168 }
169
170 /**
171 * @tc.name: PutDeviceProfile_001
172 * @tc.desc: put device service without permission
173 * @tc.type: FUNC
174 * @tc.require: I4NY1T
175 */
176 HWTEST_F(DistributedDeviceProfileServiceTest, PutDeviceProfile_001, TestSize.Level3)
177 {
178 ServiceCharacteristicProfile profile;
179 profile.SetServiceId("");
180 profile.SetServiceType("");
181 int32_t ret = DistributedDeviceProfileService::GetInstance().PutDeviceProfile(profile);
182 EXPECT_EQ(ERR_DP_PERMISSION_DENIED, ret);
183 }
184
185 /**
186 * @tc.name: DeleteDeviceProfile_001
187 * @tc.desc: put device service without permission
188 * @tc.type: FUNC
189 * @tc.require: I4NY1T
190 */
191 HWTEST_F(DistributedDeviceProfileServiceTest, DeleteDeviceProfile_001, TestSize.Level3)
192 {
193 int32_t ret = DistributedDeviceProfileService::GetInstance().DeleteDeviceProfile("");
194 EXPECT_EQ(ERR_DP_PERMISSION_DENIED, ret);
195 }
196
197 /**
198 * @tc.name: GetDeviceProfile_001
199 * @tc.desc: put device service without permission
200 * @tc.type: FUNC
201 * @tc.require: I4NY1T
202 */
203 HWTEST_F(DistributedDeviceProfileServiceTest, GetDeviceProfile_001, TestSize.Level3)
204 {
205 ServiceCharacteristicProfile profile;
206 profile.SetServiceId("");
207 profile.SetServiceType("");
208 int32_t ret = DistributedDeviceProfileService::GetInstance().GetDeviceProfile("", "", profile);
209 EXPECT_EQ(ERR_DP_PERMISSION_DENIED, ret);
210 }
211
212 /**
213 * @tc.name: SyncDeviceProfile_001
214 * @tc.desc: put device service without permission
215 * @tc.type: FUNC
216 * @tc.require: I4NY1T
217 */
218 HWTEST_F(DistributedDeviceProfileServiceTest, SyncDeviceProfile_001, TestSize.Level3)
219 {
220 auto syncCb = std::make_shared<StorageProfileEventCallback>();
221 sptr<IRemoteObject> notifier =
222 sptr<ProfileEventNotifierStub>(new ProfileEventNotifierStub(syncCb));
223 SyncOptions syncOptions;
224 int32_t ret = DistributedDeviceProfileService::GetInstance().SyncDeviceProfile(syncOptions, notifier);
225 EXPECT_EQ(ERR_DP_PERMISSION_DENIED, ret);
226 }
227 }
228 }