1 /*
2 * Copyright (c) 2025 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_state.h"
17 #include "UTTest_auth_acl.h"
18 #include "dm_auth_message_processor_mock.h"
19 #include "device_manager_service_listener.h"
20
21 using namespace testing;
22
23 namespace OHOS {
24 namespace DistributedHardware {
25 constexpr const char *TEST_NONE_EMPTY_STRING = "test";
SetUpTestCase()26 void AuthAclTest::SetUpTestCase()
27 {
28 LOGI("AuthAclTest::SetUpTestCase start.");
29 DmSoftbusConnector::dmSoftbusConnector = dmSoftbusConnectorMock;
30 DmSoftbusSession::dmSoftbusSession = dmSoftbusSessionMock;
31 DmAuthMessageProcessorMock::dmAuthMessageProcessorMock = std::make_shared<DmAuthMessageProcessorMock>();
32 }
33
TearDownTestCase()34 void AuthAclTest::TearDownTestCase()
35 {
36 LOGI("AuthAclTest::TearDownTestCase start.");
37 DmSoftbusConnector::dmSoftbusConnector = nullptr;
38 dmSoftbusConnectorMock = nullptr;
39 DmSoftbusSession::dmSoftbusSession = nullptr;
40 dmSoftbusSessionMock = nullptr;
41 DmAuthMessageProcessorMock::dmAuthMessageProcessorMock = nullptr;
42 }
43
SetUp()44 void AuthAclTest::SetUp()
45 {
46 LOGI("AuthAclTest::SetUp start.");
47 softbusConnector = std::make_shared<SoftbusConnector>();
48 listener = std::make_shared<DeviceManagerServiceListener>();
49 hiChainAuthConnector = std::make_shared<HiChainAuthConnector>();
50 hiChainConnector = std::make_shared<HiChainConnector>();
51 }
52
TearDown()53 void AuthAclTest::TearDown()
54 {
55 LOGI("AuthAclTest::TearDown start.");
56 authManager = nullptr;
57 context = nullptr;
58
59 Mock::VerifyAndClearExpectations(&*DmAuthMessageProcessorMock::dmAuthMessageProcessorMock);
60 Mock::VerifyAndClearExpectations(&*DmSoftbusConnector::dmSoftbusConnector);
61 Mock::VerifyAndClearExpectations(&*DmSoftbusSession::dmSoftbusSession);
62 }
63
64 HWTEST_F(AuthAclTest, AuthSinkAcl_002, testing::ext::TestSize.Level1)
65 {
66 authManager = std::make_shared<AuthSinkManager>(softbusConnector, hiChainConnector, listener,
67 hiChainAuthConnector);
68 context = authManager->GetAuthContext();
69 std::shared_ptr<DmAuthState> authState = std::make_shared<AuthSinkDataSyncState>();
70
71 EXPECT_EQ(authState->GetStateType(), DmAuthStateType::AUTH_SINK_DATA_SYNC_STATE);
72 }
73
74 HWTEST_F(AuthAclTest, AuthSrcAcl_002, testing::ext::TestSize.Level1)
75 {
76 authManager = std::make_shared<AuthSrcManager>(softbusConnector, hiChainConnector, listener,
77 hiChainAuthConnector);
78 context = authManager->GetAuthContext();
79 std::shared_ptr<DmAuthState> authState = std::make_shared<AuthSrcDataSyncState>();
80
81 EXPECT_EQ(authState->GetStateType(), DmAuthStateType::AUTH_SRC_DATA_SYNC_STATE);
82 }
83
84 HWTEST_F(AuthAclTest, AuthSrcFinish_001, testing::ext::TestSize.Level1)
85 {
86 authManager = std::make_shared<AuthSrcManager>(softbusConnector, hiChainConnector, listener,
87 hiChainAuthConnector);
88 context = authManager->GetAuthContext();
89 std::shared_ptr<DmAuthState> authState = std::make_shared<AuthSrcFinishState>();
90 context->reason = DM_OK;
91 context->connDelayCloseTime = 10;
92 EXPECT_EQ(authState->Action(context), DM_OK);
93 }
94
95 HWTEST_F(AuthAclTest, AuthSrcFinish_002, testing::ext::TestSize.Level1)
96 {
97 authManager = std::make_shared<AuthSrcManager>(softbusConnector, hiChainConnector, listener,
98 hiChainAuthConnector);
99 context = authManager->GetAuthContext();
100 std::shared_ptr<DmAuthState> authState = std::make_shared<AuthSrcFinishState>();
101
102 EXPECT_EQ(authState->GetStateType(), DmAuthStateType::AUTH_SRC_FINISH_STATE);
103 }
104 }
105 }