1 /*
2  * Copyright (c) 2023 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 "conn_event.h"
17 #include "conn_hisysevent_matcher.h"
18 #include "hisysevent_mock.h"
19 #include "softbus_hisysevent_matcher.h"
20 #include "gtest/gtest.h"
21 
22 using namespace std;
23 using namespace testing;
24 using namespace testing::ext;
25 
26 namespace OHOS {
27 class ConnEventTest : public testing::Test { };
28 
29 /**
30  * @tc.name: ConnEventTest001
31  * @tc.desc: Test conn event form size
32  * @tc.type: FUNC
33  * @tc.require: I8HA59
34  */
35 HWTEST_F(ConnEventTest, ConnEventTest001, TestSize.Level0)
36 {
37     ConnEventExtra extra = {
38         .result = 1,
39         .errcode = 2233,
40         .requestId = 0, // invalid
41         .peerPort = "9000",
42         .bootLinkType = 2,
43         .isRenegotiate = 3,
44         .isReuse = 4,
45         .negotiateTime = 5,
46         .linkTime = 6,
47     };
48     constexpr int32_t VALID_EXTRA_SIZE = 9;
49 
50     HiSysEventMock mock;
51     EXPECT_CALL(mock,
52         HiSysEvent_Write(_, _, StrEq(SOFTBUS_EVENT_DOMAIN), StrEq(CONN_EVENT_NAME), Eq(SOFTBUS_EVENT_TYPE_BEHAVIOR), _,
53             ParamArraySizeMatcher(VALID_EXTRA_SIZE)))
54         .Times(1);
55     CONN_EVENT(EVENT_SCENE_OPEN_CHANNEL, EVENT_STAGE_START_CONNECT, extra);
56 }
57 
58 static ConnEventExtra g_validExtra = {
59     .result = 1,
60     .errcode = 2,
61     .connectionId = 3,
62     .requestId = 4,
63     .linkType = 5,
64     .authType = 6,
65     .authId = 7,
66     .lnnType = "testLnnType",
67     .expectRole = 8,
68     .costTime = 9,
69     .rssi = 10,
70     .load = 11,
71     .frequency = 12,
72     .connProtocol = 13,
73     .connRole = 14,
74     .connRcDelta = 15,
75     .connRc = 16,
76     .supportFeature = 17,
77     .moduleId = 18,
78     .proType = 19,
79     .fd = 20,
80     .cfd = 21,
81     .challengeCode = "2024",
82     .peerIp = "10.11.12.1",
83     .peerBrMac = "dd-15-bc-b9-f2-04",
84     .peerBleMac = "dd-15-bc-b9-f2-04",
85     .peerWifiMac = "dd-15-bc-b9-f2-04",
86     .peerPort = "testPeerPort",
87     .peerNetworkId = "a8ynvpdaihw1f6nknjd2hkfhxljxypkr6kvjsbhnhpp16974uo4fvsrpfa6t50fm",
88     .peerUdid = "testPeerUdid",
89     .peerDeviceType = "testPeerDeviceType",
90     .localNetworkId = "a8ynvpdaihw1f6nknjd2hkfhxljxypkr6kvjsbhnhpp16974uo4fvsrpfa6t50fm",
91     .callerPkg = "testCallerPkg",
92     .calleePkg = "testCalleePkg",
93     .bootLinkType = 13,
94     .isRenegotiate = 14,
95     .isReuse = 15,
96     .negotiateTime = 16,
97     .linkTime = 17,
98 };
99 
100 /**
101  * @tc.name: ConnEventTest002
102  * @tc.desc: Test all valid conn event form items
103  * @tc.type: FUNC
104  * @tc.require: I8HA59
105  */
106 HWTEST_F(ConnEventTest, ConnEventTest002, TestSize.Level0)
107 {
108     constexpr int32_t VALID_EXTRA_SIZE = CONN_ASSIGNER_SIZE;
109 
110     HiSysEventMock mock;
111     EXPECT_CALL(mock,
112         HiSysEvent_Write(_, _, StrEq(SOFTBUS_EVENT_DOMAIN), StrEq(CONN_EVENT_NAME), Eq(SOFTBUS_EVENT_TYPE_BEHAVIOR),
113             ConnValidParamArrayMatcher(g_validExtra, VALID_EXTRA_SIZE), ParamArraySizeMatcher(VALID_EXTRA_SIZE)))
114         .Times(1);
115     CONN_EVENT(EVENT_SCENE_OPEN_CHANNEL, EVENT_STAGE_CONNECT_INVOKE_PROTOCOL, g_validExtra);
116 }
117 
118 static ConnEventExtra g_invalidExtra = {
119     .result = -1,  // valid
120     .errcode = -2, // valid
121     .connectionId = -3,
122     .requestId = -4,
123     .linkType = -5,
124     .authType = -6,
125     .authId = -7,
126     .lnnType = "",
127     .expectRole = -8,
128     .costTime = -9,
129     .rssi = -10,
130     .load = -11,
131     .frequency = -12,
132     .connProtocol = -13,
133     .connRole = -14,
134     .connRcDelta = -15,
135     .connRc = -16,
136     .supportFeature = -17,
137     .moduleId = -18,
138     .proType = 19,
139     .fd = -20,
140     .cfd = -21,
141     .challengeCode = "",
142     .peerIp = "",
143     .peerBrMac = "",
144     .peerBleMac = "",
145     .peerWifiMac = "\0",
146     .peerPort = nullptr,
147     .peerNetworkId = "",
148     .peerUdid = "\0",
149     .peerDeviceType = "\0",
150     .localNetworkId = "",
151     .callerPkg = "\0",
152     .calleePkg = nullptr,
153     .bootLinkType = -13,
154     .isRenegotiate = -14,
155     .isReuse = -15,
156     .negotiateTime = 0,
157     .linkTime = 0,
158 };
159 
160 /**
161  * @tc.name: ConnEventTest003
162  * @tc.desc: Test all invalid conn event form items
163  * @tc.type: FUNC
164  * @tc.require: I8HA59
165  */
166 HWTEST_F(ConnEventTest, ConnEventTest003, TestSize.Level0)
167 {
168     constexpr int32_t TWO_VALID_EXTRA_SIZE = 2; // result, errcode is valid
169     constexpr int32_t VALID_EXTRA_SIZE = 8;
170 
171     HiSysEventMock mock;
172     EXPECT_CALL(mock,
173         HiSysEvent_Write(_, _, StrEq(SOFTBUS_EVENT_DOMAIN), StrEq(CONN_EVENT_NAME), Eq(SOFTBUS_EVENT_TYPE_BEHAVIOR),
174         ConnInvalidParamArrayMatcher(g_invalidExtra, TWO_VALID_EXTRA_SIZE), ParamArraySizeMatcher(VALID_EXTRA_SIZE)))
175         .Times(1);
176     CONN_EVENT(EVENT_SCENE_CONNECT, EVENT_STAGE_CONNECT_END, g_invalidExtra);
177 }
178 
179 /**
180  * @tc.name: ConnEventTest004
181  * @tc.desc: Test empty conn event form
182  * @tc.type: FUNC
183  * @tc.require: I8HA59
184  */
185 HWTEST_F(ConnEventTest, ConnEventTest004, TestSize.Level0)
186 {
187     ConnEventExtra emptyExtra = { 0 };
188     constexpr int32_t TWO_VALID_EXTRA_SIZE = 2; // result, errcode is valid
189     constexpr int32_t VALID_EXTRA_SIZE = 8;
190 
191     HiSysEventMock mock;
192     EXPECT_CALL(mock,
193         HiSysEvent_Write(_, _, StrEq(SOFTBUS_EVENT_DOMAIN), StrEq(CONN_EVENT_NAME), Eq(SOFTBUS_EVENT_TYPE_BEHAVIOR),
194             ConnInvalidParamArrayMatcher(emptyExtra, TWO_VALID_EXTRA_SIZE), ParamArraySizeMatcher(VALID_EXTRA_SIZE)))
195         .Times(1);
196     CONN_EVENT(EVENT_SCENE_CONNECT, EVENT_STAGE_CONNECT_START, emptyExtra);
197 }
198 
199 static ConnAuditExtra g_extra = {
200     .errcode = 1000,
201     .auditType = AUDIT_EVENT_MSG_ERROR,
202     .connectionId = 222,
203     .requestId = 101,
204     .linkType = 1,
205     .expectRole = 9,
206     .costTime = 999,
207     .connectTimes = 3,
208     .frequency = "3999",
209     .challengeCode = "1",
210     .peerBrMac = "11:22:33:44:55:66",
211     .localBrMac = "12:22:23:33:33:91",
212     .peerBleMac = "22:66:55:44:33:22",
213     .localBleMac = "91:33:33:23:22:12",
214     .peerDeviceType = "phone",
215     .peerUdid = "aassddffggh565",
216     .localUdid = "sqqqddffggh565",
217     .connPayload = "100/3/14/588",
218     .localDeviceName = "test_connection",
219     .peerIp = "127.1.1.1",
220     .localIp = "127.0.0.0",
221     .callerPkg = "nearby",
222     .calleePkg = "test_name",
223     .peerPort = "3512",
224     .localPort = "2484",
225 };
226 
227 /**
228  * @tc.name: ConnEventTest001
229  * @tc.desc: Test conn event form size
230  * @tc.type: FUNC
231  * @tc.require: I8HA59
232  */
233 HWTEST_F(ConnEventTest, ConnEventTest005, TestSize.Level0)
234 {
235     constexpr int32_t VALID_EXTRA_SIZE = 25;
236 
237     HiSysEventMock mock;
238     EXPECT_CALL(mock,
239         HiSysEvent_Write(_, _, StrEq(SOFTBUS_EVENT_DOMAIN), StrEq(CONN_AUDIT_NAME), Eq(SOFTBUS_EVENT_TYPE_SECURITY),
240             ConnAuditValidParamArrayMatcher(g_extra, VALID_EXTRA_SIZE), ParamArraySizeMatcher(VALID_EXTRA_SIZE)))
241     .Times(1);
242     CONN_AUDIT(AUDIT_SCENE_CONN_HML_GROUP_TIMEOUT, g_extra);
243 }
244 
245 } // namespace OHOS
246