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 "UTTest_pin_auth.h"
17
18 #include <unistd.h>
19 #include <memory>
20
21 #include "dm_constants.h"
22 #include "dm_log.h"
23 #include "nlohmann/json.hpp"
24 #include "device_manager_service_listener.h"
25
26 namespace OHOS {
27 namespace DistributedHardware {
SetUp()28 void PinAuthTest::SetUp()
29 {
30 }
31
TearDown()32 void PinAuthTest::TearDown()
33 {
34 }
35
SetUpTestCase()36 void PinAuthTest::SetUpTestCase()
37 {
38 }
39
TearDownTestCase()40 void PinAuthTest::TearDownTestCase()
41 {
42 }
43
44 namespace {
45 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
46 std::shared_ptr<DeviceManagerServiceListener> listener = std::make_shared<DeviceManagerServiceListener>();
47 std::shared_ptr<HiChainConnector> hiChainConnector = std::make_shared<HiChainConnector>();
48 std::shared_ptr<DmAuthManager> authManager =
49 std::make_shared<DmAuthManager>(softbusConnector, listener, hiChainConnector);
50 /**
51 * @tc.name: DmAuthManager::ShowAuthInfo_001
52 * @tc.desc: Call unauthenticateddevice to check whether the return value is ERR_DM_FAILED
53 * @tc.type: FUNC
54 * @tc.require: AR000GHSJK
55 */
56 HWTEST_F(PinAuthTest, ShowAuthInfo_001, testing::ext::TestSize.Level0)
57 {
58 std::shared_ptr<PinAuth> pinAuth = std::make_shared<PinAuth>();
59 std::string authToken = "123456";
60 int32_t ret = pinAuth->ShowAuthInfo(authToken, authManager);
61 ASSERT_EQ(ret, ERR_DM_FAILED);
62 }
63
64 /**
65 * @tc.name: DmAuthManager::ShowAuthInfo_002
66 * @tc.desc: Call unauthenticateddevice to check whether the return value is ERR_DM_FAILED
67 * @tc.type: FUNC
68 * @tc.require: AR000GHSJK
69 */
70 HWTEST_F(PinAuthTest, ShowAuthInfo_002, testing::ext::TestSize.Level0)
71 {
72 std::shared_ptr<PinAuth> pinAuth = std::make_shared<PinAuth>();
73 nlohmann::json jsonObject;
74 jsonObject[PIN_TOKEN] = 123456;
75 std::string authToken = jsonObject.dump();
76 int32_t ret = pinAuth->ShowAuthInfo(authToken, authManager);
77 ASSERT_EQ(ret, ERR_DM_FAILED);
78 }
79
80 /**
81 * @tc.name: DmAuthManager::ShowAuthInfo_003
82 * @tc.desc: Call unauthenticateddevice to check whether the return value is ERR_DM_FAILED
83 * @tc.type: FUNC
84 * @tc.require: AR000GHSJK
85 */
86 HWTEST_F(PinAuthTest, ShowAuthInfo_003, testing::ext::TestSize.Level0)
87 {
88 std::shared_ptr<PinAuth> pinAuth = std::make_shared<PinAuth>();
89 nlohmann::json jsonObject;
90 jsonObject[PIN_CODE_KEY] = 123456;
91 std::string authToken = jsonObject.dump();
92 int32_t ret = pinAuth->ShowAuthInfo(authToken, nullptr);
93 ASSERT_EQ(ret, ERR_DM_FAILED);
94 }
95
96 /**
97 * @tc.name: DmAuthManager::ShowAuthInfo_004
98 * @tc.desc: Call unauthenticateddevice to check whether the return value is DM_OK
99 * @tc.type: FUNC
100 * @tc.require: AR000GHSJK
101 */
102 HWTEST_F(PinAuthTest, ShowAuthInfo_004, testing::ext::TestSize.Level0)
103 {
104 std::shared_ptr<PinAuth> pinAuth = std::make_shared<PinAuth>();
105 nlohmann::json jsonObject;
106 jsonObject[PIN_CODE_KEY] = 123456;
107 std::string authToken = jsonObject.dump();
108 int32_t ret = pinAuth->ShowAuthInfo(authToken, authManager);
109 ASSERT_EQ(ret, DM_OK);
110 }
111 /**
112 * @tc.name: DmAuthManager::StartAuth_001
113 * @tc.desc: Call unauthenticateddevice to check whether the return value is ERR_DM_FAILED
114 * @tc.type: FUNC
115 * @tc.require: AR000GHSJK
116 */
117 HWTEST_F(PinAuthTest, StartAuth_001, testing::ext::TestSize.Level0)
118 {
119 std::shared_ptr<PinAuth> pinAuth = std::make_shared<PinAuth>();
120 std::string authToken = "";
121 int32_t ret = pinAuth->StartAuth(authToken, nullptr);
122 ASSERT_EQ(ret, ERR_DM_FAILED);
123 }
124
125 /**
126 * @tc.name: DmAuthManager::StartAuth_002
127 * @tc.desc: Call unauthenticateddevice to check whether the return value is DM_OK
128 * @tc.type: FUNC
129 * @tc.require: AR000GHSJK
130 */
131 HWTEST_F(PinAuthTest, StartAuth_002, testing::ext::TestSize.Level0)
132 {
133 std::shared_ptr<PinAuth> pinAuth = std::make_shared<PinAuth>();
134 std::string authToken = "";
135 int32_t ret = pinAuth->StartAuth(authToken, authManager);
136 ASSERT_EQ(ret, DM_OK);
137 }
138
139 /**
140 * @tc.name: DmAuthManager::StartAuth_003
141 * @tc.desc: Call unauthenticateddevice to check whether the return value is DM_OK
142 * @tc.type: FUNC
143 * @tc.require: AR000GHSJK
144 */
145 HWTEST_F(PinAuthTest, StartAuth_003, testing::ext::TestSize.Level0)
146 {
147 std::shared_ptr<PinAuth> pinAuth = std::make_shared<PinAuth>();
148 nlohmann::json jsonObject;
149 jsonObject[PIN_TOKEN] = "123456";
150 std::string authToken = jsonObject.dump();
151 int32_t ret = pinAuth->StartAuth(authToken, authManager);
152 ASSERT_EQ(ret, DM_OK);
153 }
154
155 /**
156 * @tc.name: DmAuthManager::StartAuth_004
157 * @tc.desc: Call unauthenticateddevice to check whether the return value is DM_OK
158 * @tc.type: FUNC
159 * @tc.require: AR000GHSJK
160 */
161 HWTEST_F(PinAuthTest, StartAuth_004, testing::ext::TestSize.Level0)
162 {
163 std::shared_ptr<PinAuth> pinAuth = std::make_shared<PinAuth>();
164 nlohmann::json jsonObject;
165 jsonObject[PIN_CODE_KEY] = "authTokenTest";
166 std::string authToken = jsonObject.dump();
167 int32_t ret = pinAuth->StartAuth(authToken, authManager);
168 ASSERT_EQ(ret, DM_OK);
169 }
170
171 /**
172 * @tc.name: DmAuthManager::VerifyAuthentication_001
173 * @tc.desc: Call unauthenticateddevice to check whether the return value is ERR_DM_FAILED
174 * @tc.type: FUNC
175 * @tc.require: AR000GHSJK
176 */
177 HWTEST_F(PinAuthTest, VerifyAuthentication_001, testing::ext::TestSize.Level0)
178 {
179 std::shared_ptr<PinAuth> pinAuth = std::make_shared<PinAuth>();
180 std::string authToken;
181 std::string authParam = "1";
182 int32_t ret = pinAuth->VerifyAuthentication(authToken, authParam);
183 ASSERT_EQ(ret, ERR_DM_FAILED);
184 }
185
186 /**
187 * @tc.name: DmAuthManager::VerifyAuthentication_001
188 * @tc.desc: Call unauthenticateddevice to check whether the return value is DM_OK
189 * @tc.type: FUNC
190 * @tc.require: AR000GHSJK
191 */
192 HWTEST_F(PinAuthTest, VerifyAuthentication_002, testing::ext::TestSize.Level0)
193 {
194 std::shared_ptr<PinAuth> pinAuth = std::make_shared<PinAuth>();
195 std::string authToken;
196 std::string authParam = "0";
197 int32_t ret = pinAuth->VerifyAuthentication(authToken, authParam);
198 ASSERT_EQ(ret, DM_OK);
199 }
200
201 /**
202 * @tc.name: DmAuthManager::VerifyAuthentication_003
203 * @tc.desc: Call unauthenticateddevice to check whether the return value is DM_OK
204 * @tc.type: FUNC
205 * @tc.require: AR000GHSJK
206 */
207 HWTEST_F(PinAuthTest, VerifyAuthentication_003, testing::ext::TestSize.Level0)
208 {
209 std::shared_ptr<PinAuth> pinAuth = std::make_shared<PinAuth>();
210 nlohmann::json authParamJson;
211 authParamJson[PIN_CODE_KEY] = 123;
212 authParamJson[PIN_TOKEN] = "456";
213 nlohmann::json authTokenJson;
214 authTokenJson[PIN_CODE_KEY] = 123;
215 authTokenJson[PIN_TOKEN] = "456";
216 std::string authToken = authTokenJson.dump();
217 std::string authParam = authParamJson.dump();
218 int32_t ret = pinAuth->VerifyAuthentication(authToken, authParam);
219 ASSERT_EQ(ret, DM_OK);
220 }
221
222 /**
223 * @tc.name: DmAuthManager::VerifyAuthentication_004
224 * @tc.desc: Call unauthenticateddevice to check whether the return value is ERR_DM_INPUT_PARA_INVALID
225 * @tc.type: FUNC
226 * @tc.require: AR000GHSJK
227 */
228 HWTEST_F(PinAuthTest, VerifyAuthentication_004, testing::ext::TestSize.Level0)
229 {
230 std::shared_ptr<PinAuth> pinAuth = std::make_shared<PinAuth>();
231 nlohmann::json authParamJson;
232 authParamJson[PIN_CODE_KEY] = 12345;
233 authParamJson[PIN_TOKEN] = "123456";
234 nlohmann::json authTokenJson;
235 authTokenJson[PIN_CODE_KEY] = 123456;
236 authTokenJson[PIN_TOKEN] = "123456";
237 std::string authToken = authTokenJson.dump();
238 std::string authParam = authParamJson.dump();
239 int32_t ret = pinAuth->VerifyAuthentication(authToken, authParam);
240 ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID);
241 }
242
243 /**
244 * @tc.name: DmAuthManager::VerifyAuthentication_005
245 * @tc.desc: Call unauthenticateddevice to check whether the return value is ERR_DM_FAILED
246 * @tc.type: FUNC
247 * @tc.require: AR000GHSJK
248 */
249 HWTEST_F(PinAuthTest, VerifyAuthentication_005, testing::ext::TestSize.Level0)
250 {
251 std::shared_ptr<PinAuth> pinAuth = std::make_shared<PinAuth>();
252 nlohmann::json authParamJson;
253 authParamJson[PIN_CODE_KEY] = 12345;
254 authParamJson[PIN_TOKEN] = "123456";
255 nlohmann::json authTokenJson;
256 authTokenJson[PIN_CODE_KEY] = 123456;
257 authTokenJson[PIN_TOKEN] = "123456";
258 std::string authToken = authTokenJson.dump();
259 std::string authParam = authParamJson.dump();
260 pinAuth->VerifyAuthentication(authToken, authParam);
261 pinAuth->VerifyAuthentication(authToken, authParam);
262 int32_t ret = pinAuth->VerifyAuthentication(authToken, authParam);
263 ASSERT_EQ(ret, ERR_DM_FAILED);
264 }
265 }
266 } // namespace DistributedHardware
267 } // namespace OHOS
268