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