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 DM_OK
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 /**
113 * @tc.name: DmAuthManager::StartAuth_001
114 * @tc.desc: Call unauthenticateddevice to check whether the return value is ERR_DM_FAILED
115 * @tc.type: FUNC
116 * @tc.require: AR000GHSJK
117 */
118 HWTEST_F(PinAuthTest, StartAuth_001, testing::ext::TestSize.Level0)
119 {
120 std::shared_ptr<PinAuth> pinAuth = std::make_shared<PinAuth>();
121 std::string authToken = "";
122 int32_t ret = pinAuth->StartAuth(authToken, nullptr);
123 ASSERT_EQ(ret, ERR_DM_FAILED);
124 }
125
126 /**
127 * @tc.name: DmAuthManager::StartAuth_002
128 * @tc.desc: Call unauthenticateddevice to check whether the return value is ERR_DM_FAILED
129 * @tc.type: FUNC
130 * @tc.require: AR000GHSJK
131 */
132 HWTEST_F(PinAuthTest, StartAuth_002, testing::ext::TestSize.Level0)
133 {
134 std::shared_ptr<PinAuth> pinAuth = std::make_shared<PinAuth>();
135 std::string authToken = "";
136 int32_t ret = pinAuth->StartAuth(authToken, authManager);
137 ASSERT_EQ(ret, DM_OK);
138 }
139
140 /**
141 * @tc.name: DmAuthManager::VerifyAuthentication_001
142 * @tc.desc: Call unauthenticateddevice to check whether the return value is ERR_DM_FAILED
143 * @tc.type: FUNC
144 * @tc.require: AR000GHSJK
145 */
146 HWTEST_F(PinAuthTest, VerifyAuthentication_001, testing::ext::TestSize.Level0)
147 {
148 std::shared_ptr<PinAuth> pinAuth = std::make_shared<PinAuth>();
149 std::string authToken;
150 std::string authParam = "1";
151 int32_t ret = pinAuth->VerifyAuthentication(authToken, authParam);
152 ASSERT_EQ(ret, ERR_DM_FAILED);
153 }
154
155 /**
156 * @tc.name: DmAuthManager::VerifyAuthentication_001
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, VerifyAuthentication_002, testing::ext::TestSize.Level0)
162 {
163 std::shared_ptr<PinAuth> pinAuth = std::make_shared<PinAuth>();
164 std::string authToken;
165 std::string authParam = "0";
166 int32_t ret = pinAuth->VerifyAuthentication(authToken, authParam);
167 ASSERT_EQ(ret, DM_OK);
168 }
169
170 /**
171 * @tc.name: DmAuthManager::VerifyAuthentication_003
172 * @tc.desc: Call unauthenticateddevice to check whether the return value is DM_OK
173 * @tc.type: FUNC
174 * @tc.require: AR000GHSJK
175 */
176 HWTEST_F(PinAuthTest, VerifyAuthentication_003, testing::ext::TestSize.Level0)
177 {
178 std::shared_ptr<PinAuth> pinAuth = std::make_shared<PinAuth>();
179 nlohmann::json authParamJson;
180 authParamJson[PIN_CODE_KEY] = 123;
181 authParamJson[PIN_TOKEN] = "456";
182 nlohmann::json authTokenJson;
183 authTokenJson[PIN_CODE_KEY] = 123;
184 authTokenJson[PIN_TOKEN] = "456";
185 std::string authToken = authTokenJson.dump();
186 std::string authParam = authParamJson.dump();
187 int32_t ret = pinAuth->VerifyAuthentication(authToken, authParam);
188 ASSERT_EQ(ret, DM_OK);
189 }
190
191 /**
192 * @tc.name: DmAuthManager::VerifyAuthentication_004
193 * @tc.desc: Call unauthenticateddevice to check whether the return value is ERR_DM_INPUT_PARA_INVALID
194 * @tc.type: FUNC
195 * @tc.require: AR000GHSJK
196 */
197 HWTEST_F(PinAuthTest, VerifyAuthentication_004, testing::ext::TestSize.Level0)
198 {
199 std::shared_ptr<PinAuth> pinAuth = std::make_shared<PinAuth>();
200 nlohmann::json authParamJson;
201 authParamJson[PIN_CODE_KEY] = 12345;
202 authParamJson[PIN_TOKEN] = "123456";
203 nlohmann::json authTokenJson;
204 authTokenJson[PIN_CODE_KEY] = 123456;
205 authTokenJson[PIN_TOKEN] = "123456";
206 std::string authToken = authTokenJson.dump();
207 std::string authParam = authParamJson.dump();
208 int32_t ret = pinAuth->VerifyAuthentication(authToken, authParam);
209 ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID);
210 }
211
212 /**
213 * @tc.name: DmAuthManager::VerifyAuthentication_005
214 * @tc.desc: Call unauthenticateddevice to check whether the return value is ERR_DM_FAILED
215 * @tc.type: FUNC
216 * @tc.require: AR000GHSJK
217 */
218 HWTEST_F(PinAuthTest, VerifyAuthentication_005, testing::ext::TestSize.Level0)
219 {
220 std::shared_ptr<PinAuth> pinAuth = std::make_shared<PinAuth>();
221 nlohmann::json authParamJson;
222 authParamJson[PIN_CODE_KEY] = 12345;
223 authParamJson[PIN_TOKEN] = "123456";
224 nlohmann::json authTokenJson;
225 authTokenJson[PIN_CODE_KEY] = 123456;
226 authTokenJson[PIN_TOKEN] = "123456";
227 std::string authToken = authTokenJson.dump();
228 std::string authParam = authParamJson.dump();
229 pinAuth->VerifyAuthentication(authToken, authParam);
230 pinAuth->VerifyAuthentication(authToken, authParam);
231 int32_t ret = pinAuth->VerifyAuthentication(authToken, authParam);
232 ASSERT_EQ(ret, ERR_DM_FAILED);
233 }
234 }
235 } // namespace DistributedHardware
236 } // namespace OHOS
237