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
19 #include "lnn_auth_mock.h"
20 #include "lnn_connection_fsm.h"
21 #include "lnn_net_builder.h"
22 #include "lnn_net_builder_deps_mock.h"
23 #include "lnn_service_mock.h"
24 #include "message_handler.h"
25 #include "softbus_adapter_mem.h"
26 #include "softbus_common.h"
27 #include "softbus_errcode.h"
28 #include "softbus_log.h"
29
30 constexpr char IP[IP_STR_MAX_LEN] = "127.0.0.1";
31 constexpr uint16_t PORT = 1000;
32 constexpr char PEERUID[MAX_ACCOUNT_HASH_LEN] = "021315ASD";
33
34 namespace OHOS {
35 using namespace testing;
36 using namespace testing::ext;
37
38 static LnnConnectionFsm *connFsm = nullptr;
39 static ConnectionAddr target = {
40 .type = CONNECTION_ADDR_WLAN,
41 .info.ip.port = PORT,
42 };
43 static LnnConnectionFsm *connFsm2 = nullptr;
44 class LnnConnectionFsmTest : public testing::Test {
45 public:
46 static void SetUpTestCase();
47 static void TearDownTestCase();
48 void SetUp();
49 void TearDown();
50 };
51
SetUpTestCase()52 void LnnConnectionFsmTest::SetUpTestCase()
53 {
54 LooperInit();
55 memcpy_s(target.peerUid, MAX_ACCOUNT_HASH_LEN, PEERUID, strlen(PEERUID));
56 memcpy_s(target.info.ip.ip, IP_STR_MAX_LEN, IP, strlen(IP));
57 connFsm2 = LnnCreateConnectionFsm(&target);
58 EXPECT_TRUE(connFsm2 != nullptr);
59 }
60
TearDownTestCase()61 void LnnConnectionFsmTest::TearDownTestCase()
62 {
63 LooperDeinit();
64 LnnDestroyConnectionFsm(connFsm2);
65 }
66
SetUp()67 void LnnConnectionFsmTest::SetUp()
68 {
69 }
70
TearDown()71 void LnnConnectionFsmTest::TearDown()
72 {
73 }
74
FsmStopCallback(struct tagLnnConnectionFsm * connFsm)75 void FsmStopCallback(struct tagLnnConnectionFsm *connFsm)
76 {
77 }
78
79 /*
80 * @tc.name: LNN_CREATE_CONNECTION_FSM_TEST_001
81 * @tc.desc: para is null
82 * @tc.type: FUNC
83 * @tc.require:I5PRUD
84 */
85 HWTEST_F(LnnConnectionFsmTest, LNN_CREATE_CONNECTION_FSM_TEST_001, TestSize.Level1)
86 {
87 ConnectionAddr *target1 = nullptr;
88 LnnConnectionFsm *fsm = LnnCreateConnectionFsm(target1);
89 EXPECT_TRUE(fsm == nullptr);
90 }
91
92 /*
93 * @tc.name: LNN_DESTROY_CONNECTION_FSM_TEST_001
94 * @tc.desc: test LnnDestroyConnectionFsm
95 * @tc.type: FUNC
96 * @tc.require: I5PRUD
97 */
98 HWTEST_F(LnnConnectionFsmTest, LNN_DESTROY_CONNECTION_FSM_TEST_001, TestSize.Level1)
99 {
100 LnnConnectionFsm *fsm = nullptr;
101 LnnDestroyConnectionFsm(fsm);
102 }
103
104 /*
105 * @tc.name: LNN_START_CONNECTION_FSM_TEST_001
106 * @tc.desc: test LnnStartConnectionFsm
107 * @tc.type: FUNC
108 * @tc.require: I5PRUD
109 */
110 HWTEST_F(LnnConnectionFsmTest, LNN_START_CONNECTION_FSM_TEST_001, TestSize.Level1)
111 {
112 int32_t ret = LnnStartConnectionFsm(connFsm);
113 EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM);
114
115 ret = LnnStartConnectionFsm(connFsm2);
116 EXPECT_TRUE(ret == SOFTBUS_OK);
117 SoftBusSleepMs(200);
118 }
119
120 /*
121 * @tc.name: LNN_SEND_JOIN_REQUEST_TO_CONNFSM_TEST_001
122 * @tc.desc: test LnnSendJoinRequestToConnFsm
123 * @tc.type: FUNC
124 * @tc.require: I5PRUD
125 */
126 HWTEST_F(LnnConnectionFsmTest, LNN_SEND_JOIN_REQUEST_TO_CONNFSM_TEST_001, TestSize.Level1)
127 {
128 int32_t ret = LnnStartConnectionFsm(connFsm2);
129 EXPECT_TRUE(ret == SOFTBUS_OK);
130 NiceMock<LnnAuthtInterfaceMock> authMock;
131 NiceMock<NetBuilderDepsInterfaceMock> netBuilderMock;
132 NiceMock<LnnServicetInterfaceMock> serviceMock;
133 ON_CALL(netBuilderMock, AuthGenRequestId).WillByDefault(Return(1));
134 EXPECT_CALL(authMock, AuthStartVerify).WillOnce(Return(SOFTBUS_OK)).WillRepeatedly(Return(SOFTBUS_ERR));
135 ON_CALL(serviceMock, LnnNotifyJoinResult).WillByDefault(Return());
136 ret = LnnSendJoinRequestToConnFsm(connFsm2);
137 EXPECT_TRUE(ret == SOFTBUS_OK);
138 SoftBusSleepMs(1000);
139 }
140 } // namespace OHOS
141