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 <gtest/gtest.h>
17 #include <securec.h>
18 #include <sys/time.h>
19 #include <cinttypes>
20
21 #include "auth_common.h"
22 #include "auth_connection_mock.h"
23 #include "auth_hichain.h"
24 #include "auth_hichain_mock.h"
25 #include "auth_interface.h"
26 #include "auth_net_ledger_mock.h"
27 #include "message_handler.h"
28 #include "softbus_adapter_mem.h"
29 #include "softbus_access_token_test.h"
30 #include "softbus_errcode.h"
31 #include "softbus_log.h"
32
33 namespace OHOS {
34 using namespace testing;
35 using namespace testing::ext;
36 class AuthTestEnhance : public testing::Test {
37 public:
38 static void SetUpTestCase();
39 static void TearDownTestCase();
40 void SetUp();
41 void TearDown();
42 };
43
SetUpTestCase()44 void AuthTestEnhance::SetUpTestCase()
45 {
46 SetAceessTokenPermission("AuthTestEnhance");
47 int32_t ret = LooperInit();
48 EXPECT_TRUE(ret == SOFTBUS_OK);
49 }
50
TearDownTestCase()51 void AuthTestEnhance::TearDownTestCase()
52 {
53 LooperDeinit();
54 }
55
SetUp()56 void AuthTestEnhance::SetUp()
57 {
58 LOG_INFO("AuthTest start.");
59 }
60
TearDown()61 void AuthTestEnhance::TearDown()
62 {
63 }
64
65 /*
66 * @tc.name: AUTH_START_LISTENING_Test_001
67 * @tc.desc: auth common test
68 * @tc.type: FUNC
69 * @tc.require:
70 */
71 HWTEST_F(AuthTestEnhance, AUTH_START_LISTENING_Test_001, TestSize.Level0)
72 {
73 AuthConnectInterfaceMock connMock;
74 {
75 EXPECT_CALL(connMock, ConnStartLocalListening(_)).WillRepeatedly(Return(SOFTBUS_OK));
76 int32_t port = 5566;
77 int32_t ret = AuthStartListening(AUTH_LINK_TYPE_P2P, "192.168.78.1", port);
78 printf("ret %d\n", ret);
79 EXPECT_TRUE(ret == SOFTBUS_OK);
80 }
81 {
82 EXPECT_CALL(connMock, ConnStartLocalListening(_)).WillRepeatedly(Return(SOFTBUS_ERR));
83 int32_t port = 5566;
84 int32_t ret = AuthStartListening(AUTH_LINK_TYPE_P2P, "192.168.78.1", port);
85 printf("ret %d\n", ret);
86 EXPECT_TRUE(ret == SOFTBUS_ERR);
87 }
88 }
89
90 /*
91 * @tc.name: AUTH_HICHAIN_START_AUTH_Test_001
92 * @tc.desc: auth common test
93 * @tc.type: FUNC
94 * @tc.require:
95 */
96 HWTEST_F(AuthTestEnhance, AUTH_HICHAIN_START_AUTH_Test_001, TestSize.Level0)
97 {
98 const char *udid = "1111222233334444";
99 const char *uid = "8888";
100 int64_t authSeq = 5678;
101 AuthHichainInterfaceMock hichainMock;
102 GroupAuthManager authManager;
103 authManager.authDevice = AuthHichainInterfaceMock::InvokeAuthDevice;
104 EXPECT_CALL(hichainMock, InitDeviceAuthService()).WillRepeatedly(Return(0));
105 EXPECT_CALL(hichainMock, GetGaInstance()).WillRepeatedly(Return(&authManager));
106 int32_t ret = HichainStartAuth(authSeq, udid, uid);
107 EXPECT_TRUE(ret == SOFTBUS_OK);
108 }
109
110 /*
111 * @tc.name: AUTH_INIT_Test_001
112 * @tc.desc: auth common test
113 * @tc.type: FUNC
114 * @tc.require:
115 */
116 HWTEST_F(AuthTestEnhance, AUTH_INIT_Test_001, TestSize.Level0)
117 {
118 AuthConnectInterfaceMock connMock;
119 AuthHichainInterfaceMock hichainMock;
120 GroupAuthManager authManager;
121 DeviceGroupManager groupManager;
122 groupManager.regDataChangeListener = AuthHichainInterfaceMock::InvokeDataChangeListener;
123 EXPECT_CALL(connMock, ConnSetConnectCallback(_, _)).WillRepeatedly(Return(SOFTBUS_OK));
124 EXPECT_CALL(hichainMock, InitDeviceAuthService()).WillRepeatedly(Return(0));
125 EXPECT_CALL(hichainMock, GetGaInstance()).WillRepeatedly(Return(&authManager));
126 EXPECT_CALL(hichainMock, GetGmInstance()).WillRepeatedly(Return(&groupManager));
127 int32_t ret = AuthInit();
128 EXPECT_TRUE(ret == SOFTBUS_OK);
129 }
130 } // namespace OHOS
131