• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "dm_auth_manager.h"
17 
18 #include "nlohmann/json.hpp"
19 
20 #include "dm_log.h"
21 #include "dm_constants.h"
22 #include "auth_message_processor.h"
23 #include "UTTest_dm_auth_manager.h"
24 
25 namespace OHOS {
26 namespace DistributedHardware {
SetUp()27 void DmAuthManagerTest::SetUp()
28 {
29 }
TearDown()30 void DmAuthManagerTest::TearDown()
31 {
32 }
SetUpTestCase()33 void DmAuthManagerTest::SetUpTestCase()
34 {
35 }
TearDownTestCase()36 void DmAuthManagerTest::TearDownTestCase()
37 {
38 }
39 
40 namespace {
41 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
42 std::shared_ptr<DeviceManagerServiceListener> listener = std::make_shared<DeviceManagerServiceListener>();
43 std::shared_ptr<HiChainConnector> hiChainConnector_ = std::make_shared<HiChainConnector>();
44 
45 /**
46  * @tc.name: DmAuthManager::UnAuthenticateDevice_001
47  * @tc.desc: Call unauthenticateddevice to check whether the return value is DM_ FAILED
48  * @tc.type: FUNC
49  * @tc.require: AR000GHSJK
50  */
51 HWTEST_F(DmAuthManagerTest, UnAuthenticateDevice_001, testing::ext::TestSize.Level0)
52 {
53     std::shared_ptr<DmAuthManager> authManager =
54         std::make_shared<DmAuthManager>(softbusConnector, listener, hiChainConnector_);
55     std::shared_ptr<SoftbusSession> sessionSession = std::shared_ptr<SoftbusSession>();
56     std::shared_ptr<DmAuthRequestContext> authRequestContext = std::make_shared<DmAuthRequestContext>();
57     std::string pkgName = "";
58     std::string deviceId = "222";
59     int32_t ret = authManager->UnAuthenticateDevice(pkgName, deviceId);
60     ASSERT_EQ(ret, DM_FAILED);
61 }
62 
63 /**
64  * @tc.name: DmAuthManager::HandleAuthenticateTimeout_001
65  * @tc.desc: authResponseContext_= nullptr; Call handleauthenticatemeout to check whether the return value is DM_FAILED
66  * @tc.type: FUNC
67  * @tc.require: AR000GHSJK
68  */
69 HWTEST_F(DmAuthManagerTest, HandleAuthenticateTimeout_001, testing::ext::TestSize.Level0)
70 {
71     std::string name = "test";
72     std::shared_ptr<DmAuthManager> authManager =
73         std::make_shared<DmAuthManager>(softbusConnector, listener, hiChainConnector_);
74     std::shared_ptr<AuthRequestState> authRequestState = std::make_shared<AuthRequestNetworkState>();
75     authManager->authRequestContext_ = std::make_shared<DmAuthRequestContext>();
76     authManager->authRequestState_ = std::make_shared<AuthRequestNetworkState>();
77     authManager->authResponseContext_ = nullptr;
78     authManager->SetAuthRequestState(authRequestState);
79     authManager->HandleAuthenticateTimeout(name);
80     ASSERT_TRUE(authManager->authResponseContext_ != nullptr);
81 }
82 
83 /**
84  * @tc.name: DmAuthManager::HandleAuthenticateTimeout_002
85  * @tc.desc: authResponseContext_= nullptr; Call handleauthenticatemeout to check whether the return value is DM_OK
86  * @tc.type: FUNC
87  * @tc.require: AR000GHSJK
88  */
89 HWTEST_F(DmAuthManagerTest, HandleAuthenticateTimeout_002, testing::ext::TestSize.Level0)
90 {
91     std::string name = "test";
92     std::shared_ptr<DmAuthManager> authManager =
93         std::make_shared<DmAuthManager>(softbusConnector, listener, hiChainConnector_);
94     std::shared_ptr<AuthRequestState> authRequestState = std::make_shared<AuthRequestFinishState>();
95     authManager->authResponseContext_ = std::make_shared<DmAuthResponseContext>();
96     authManager->authRequestContext_ = std::make_shared<DmAuthRequestContext>();
97     authManager->authRequestState_ = std::make_shared<AuthRequestFinishState>();
98     authManager->SetAuthRequestState(authRequestState);
99     authManager->HandleAuthenticateTimeout(name);
100     ASSERT_TRUE(authManager->authRequestState_ != nullptr);
101 }
102 
103 /**
104  * @tc.name: DmAuthManager::EstablishAuthChannel_001
105  * @tc.desc: Call establishauthchannel to check whether the return value is DM_ FAILED
106  * @tc.type: FUNC
107  * @tc.require: AR000GHSJK
108  */
109 HWTEST_F(DmAuthManagerTest, EstablishAuthChannel_001, testing::ext::TestSize.Level0)
110 {
111     std::shared_ptr<DmAuthManager> authManager =
112         std::make_shared<DmAuthManager>(softbusConnector, listener, hiChainConnector_);
113     std::shared_ptr<SoftbusSession> sessionSession = std::shared_ptr<SoftbusSession>();
114     std::shared_ptr<DmAuthResponseContext> authRequestContext = std::make_shared<DmAuthResponseContext>();
115     authManager->authResponseContext_ = std::make_shared<DmAuthResponseContext>();
116     authManager->authRequestContext_ = std::make_shared<DmAuthRequestContext>();
117     authManager->authRequestState_ = std::make_shared<AuthRequestFinishState>();
118     std::string deviceId1;
119     int32_t ret = authManager->EstablishAuthChannel(deviceId1);
120     ASSERT_EQ(ret, DM_OK);
121 }
122 
123 /**
124  * @tc.name: DmAuthManager::StartAuthProcess_001
125  * @tc.desc: Whether the return value of calling startauthprocess is DM_FAILED
126  * @tc.type: FUNC
127  * @tc.require: AR000GHSJK
128  */
129 HWTEST_F(DmAuthManagerTest, StartAuthProcess_001, testing::ext::TestSize.Level0)
130 {
131     std::shared_ptr<DmAuthManager> authManager =
132         std::make_shared<DmAuthManager>(softbusConnector, listener, hiChainConnector_);
133     std::shared_ptr<AuthResponseState> authResponseState = std::make_shared<AuthResponseConfirmState>();
134     authManager->authResponseContext_ = std::make_shared<DmAuthResponseContext>();
135     authManager->authRequestContext_ = std::make_shared<DmAuthRequestContext>();
136     authManager->authResponseState_ = std::make_shared<AuthResponseConfirmState>();
137     authManager->SetAuthResponseState(authResponseState);
138     int32_t action = 0;
139     authManager->StartAuthProcess(action);
140     bool ret = authManager->authRequestContext_->deviceName.empty();
141     ASSERT_EQ(ret, true);
142 }
143 
144 /**
145  * @tc.name: DmAuthManager::StartAuthProcess_002
146  * @tc.desc: Whether the return value of calling startauthprocess is DM_ OK
147  * @tc.type: FUNC
148  * @tc.require: AR000GHSJK
149  */
150 HWTEST_F(DmAuthManagerTest, StartAuthProcess_002, testing::ext::TestSize.Level0)
151 {
152     std::shared_ptr<DmAuthManager> authManager =
153         std::make_shared<DmAuthManager>(softbusConnector, listener, hiChainConnector_);
154     std::shared_ptr<AuthResponseState> authResponseState = std::make_shared<AuthResponseInitState>();
155     authManager->authResponseContext_ = std::make_shared<DmAuthResponseContext>();
156     authManager->authRequestContext_ = std::make_shared<DmAuthRequestContext>();
157     authManager->authResponseState_ = std::make_shared<AuthResponseInitState>();
158     authManager->authMessageProcessor_ = std::make_shared<AuthMessageProcessor>(authManager);
159     authManager->SetAuthResponseState(authResponseState);
160     authManager->authResponseContext_->sessionId = 111;
161     authManager->softbusConnector_->GetSoftbusSession()->RegisterSessionCallback(authManager);
162     int32_t action = 1;
163     int32_t ret = authManager->StartAuthProcess(action);
164     ASSERT_EQ(ret, DM_OK);
165 }
166 
167 /**
168  * @tc.name: DmAuthManager::CreateGroup_001
169  * @tc.desc: Whether the return value of calling creategroup is DM_ OK
170  * @tc.type: FUNC
171  * @tc.require: AR000GHSJK
172  */
173 HWTEST_F(DmAuthManagerTest, CreateGroup_001, testing::ext::TestSize.Level0)
174 {
175     std::shared_ptr<DmAuthManager> authManager =
176         std::make_shared<DmAuthManager>(softbusConnector, listener, hiChainConnector_);
177     std::shared_ptr<AuthResponseState> authResponseState = std::make_shared<AuthResponseConfirmState>();
178     std::shared_ptr<HiChainConnector> hiChainConnector = std::make_shared<HiChainConnector>();
179     authManager->authResponseContext_ = std::make_shared<DmAuthResponseContext>();
180     authManager->authRequestContext_ = std::make_shared<DmAuthRequestContext>();
181     authManager->authResponseState_ = std::make_shared<AuthResponseConfirmState>();
182     authManager->authMessageProcessor_ = std::make_shared<AuthMessageProcessor>(authManager);
183     authManager->SetAuthResponseState(authResponseState);
184     authManager->authResponseContext_->requestId = 111;
185     authManager->authResponseContext_->groupName = "111";
186     int32_t ret = authManager->CreateGroup();
187     ASSERT_EQ(ret, DM_OK);
188 }
189 
190 /**
191  * @tc.name: DmAuthManager::AddMember_001
192  * @tc.desc: Whether the return value of calling addmember is DM_ OK
193  * @tc.type: FUNC
194  * @tc.require: AR000GHSJK
195  */
196 HWTEST_F(DmAuthManagerTest, AddMember_001, testing::ext::TestSize.Level0)
197 {
198     std::shared_ptr<DmAuthManager> authManager =
199         std::make_shared<DmAuthManager>(softbusConnector, listener, hiChainConnector_);
200     std::shared_ptr<AuthResponseState> authResponseState = std::make_shared<AuthResponseInitState>();
201     authManager->authRequestContext_ = std::make_shared<DmAuthRequestContext>();
202     std::shared_ptr<HiChainConnector> hiChainConnector = std::make_shared<HiChainConnector>();
203     authManager->authMessageProcessor_ = std::make_shared<AuthMessageProcessor>(authManager);
204     authManager->authResponseContext_ = std::make_shared<DmAuthResponseContext>();
205     nlohmann::json jsonObject;
206     authManager->authResponseContext_->groupId = "111";
207     authManager->authResponseContext_->groupName = "222";
208     authManager->authResponseContext_->code = 123;
209     authManager->authResponseContext_->requestId = 234;
210     authManager->authResponseContext_->deviceId = "234";
211     int32_t pinCode = 444444;
212     authManager->timer_ = std::make_shared<DmTimer>();
213     authManager->hiChainConnector_->RegisterHiChainCallback(authManager);
214     authManager->SetAuthResponseState(authResponseState);
215     int32_t ret = authManager->AddMember(pinCode);
216     ASSERT_NE(ret, -1);
217 }
218 
219 /**
220  * @tc.name: DmAuthManager::JoinNetwork_001
221  * @tc.desc: Whether the return value of calling joinnetwork is DM_ OK
222  * @tc.type: FUNC
223  * @tc.require: AR000GHSJK
224  */
225 HWTEST_F(DmAuthManagerTest, JoinNetwork_001, testing::ext::TestSize.Level0)
226 {
227     std::shared_ptr<DmAuthManager> authManager =
228         std::make_shared<DmAuthManager>(softbusConnector, listener, hiChainConnector_);
229     std::shared_ptr<AuthRequestState> authRequestState = std::make_shared<AuthRequestFinishState>();
230     authManager->timer_ = std::make_shared<DmTimer>();
231     authManager->authMessageProcessor_ = std::make_shared<AuthMessageProcessor>(authManager);
232     authManager->authResponseContext_ = std::make_shared<DmAuthResponseContext>();
233     authManager->authRequestContext_ = std::make_shared<DmAuthRequestContext>();
234     authManager->authRequestState_ = std::make_shared<AuthRequestFinishState>();
235     authManager->SetAuthRequestState(authRequestState);
236     int32_t ret = authManager->JoinNetwork();
237     ASSERT_EQ(ret, DM_OK);
238 }
239 
240 /**
241  * @tc.name: DmAuthManager::SetAuthResponseState_001
242  * @tc.desc: Is the authresponsestate assignment successful
243  * @tc.type: FUNC
244  * @tc.require: AR000GHSJK
245  */
246 HWTEST_F(DmAuthManagerTest, SetAuthResponseState_001, testing::ext::TestSize.Level0)
247 {
248     std::shared_ptr<DmAuthManager> authManager =
249         std::make_shared<DmAuthManager>(softbusConnector, listener, hiChainConnector_);
250     std::shared_ptr<AuthResponseState> authResponseState = std::make_shared<AuthResponseFinishState>();
251     authManager->authResponseState_ = std::make_shared<AuthResponseFinishState>();
252     authManager->SetAuthResponseState(authResponseState);
253     int32_t ret = authManager->SetAuthResponseState(authResponseState);
254     ASSERT_EQ(ret, DM_OK);
255 }
256 
257 /**
258  * @tc.name: DmAuthManager::GetPinCode_001
259  * @tc.desc: Return authresponsecontext - > code
260  * @tc.type: FUNC
261  * @tc.require: AR000GHSJK
262  */
263 HWTEST_F(DmAuthManagerTest, GetPinCode_001, testing::ext::TestSize.Level0)
264 {
265     std::shared_ptr<DmAuthManager> authManager =
266         std::make_shared<DmAuthManager>(softbusConnector, listener, hiChainConnector_);
267     authManager->authResponseContext_ = std::make_shared<DmAuthResponseContext>();
268     authManager->authResponseContext_->code = 123456;
269     int32_t ret = authManager->GetPinCode();
270     ASSERT_EQ(ret, 123456);
271 }
272 } // namespace
273 } // namespace DistributedHardware
274 } // namespace OHOS
275