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