1 /* 2 * Copyright (c) 2021-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 18 #include "net_packet.h" 19 20 namespace OHOS { 21 namespace MMI { 22 namespace { 23 using namespace testing::ext; 24 using namespace OHOS::MMI; 25 } // namespace 26 class NetPacketTest : public testing::Test { 27 public: SetUpTestCase(void)28 static void SetUpTestCase(void) {} TearDownTestCase(void)29 static void TearDownTestCase(void) {} 30 }; 31 32 /** 33 * @tc.name:construct_001 34 * @tc.desc:Verify net packet 35 * @tc.type: FUNC 36 * @tc.require: 37 */ 38 HWTEST_F(NetPacketTest, construct_001, TestSize.Level1) 39 { 40 MmiMessageId idMsg = MmiMessageId::INVALID; 41 NetPacket pkt(idMsg); 42 } 43 44 /** 45 * @tc.name:construct_002 46 * @tc.desc:Verify net packet 47 * @tc.type: FUNC 48 * @tc.require: 49 */ 50 HWTEST_F(NetPacketTest, construct_002, TestSize.Level1) 51 { 52 MmiMessageId idMsg = MmiMessageId::INVALID; 53 NetPacket pkt(idMsg); 54 NetPacket packTmp(pkt); 55 } 56 57 /** 58 * @tc.name:construct_002 59 * @tc.desc:Verify net packet 60 * @tc.type: FUNC 61 * @tc.require: 62 */ 63 HWTEST_F(NetPacketTest, construct_003, TestSize.Level1) 64 { 65 MmiMessageId idMsg = static_cast<MmiMessageId>(-2002); 66 NetPacket pkt(idMsg); 67 NetPacket packTmp(pkt); 68 } 69 70 /** 71 * @tc.name:construct_002 72 * @tc.desc:Verify net packet 73 * @tc.type: FUNC 74 * @tc.require: 75 */ 76 HWTEST_F(NetPacketTest, GetSize_001, TestSize.Level1) 77 { 78 MmiMessageId idMsg = MmiMessageId::INVALID; 79 NetPacket pkt(idMsg); 80 size_t retResult = pkt.GetSize(); 81 EXPECT_TRUE(retResult == 0); 82 } 83 84 /** 85 * @tc.name:construct_002 86 * @tc.desc:Verify net packet 87 * @tc.type: FUNC 88 * @tc.require: 89 */ 90 HWTEST_F(NetPacketTest, GetSize_002, TestSize.Level1) 91 { 92 MmiMessageId idMsg = static_cast<MmiMessageId>(-1001); 93 NetPacket pkt(idMsg); 94 size_t retResult = pkt.GetSize(); 95 EXPECT_TRUE(retResult == 0); 96 } 97 98 /** 99 * @tc.name:construct_002 100 * @tc.desc:Verify net packet 101 * @tc.type: FUNC 102 * @tc.require: 103 */ 104 HWTEST_F(NetPacketTest, GetSize_003, TestSize.Level1) 105 { 106 MmiMessageId idMsg = static_cast<MmiMessageId>(65535); 107 NetPacket pkt(idMsg); 108 size_t retResult = pkt.GetSize(); 109 EXPECT_TRUE(retResult == 0); 110 } 111 112 /** 113 * @tc.name:construct_002 114 * @tc.desc:Verify net packet 115 * @tc.type: FUNC 116 * @tc.require: 117 */ 118 HWTEST_F(NetPacketTest, GetData_001, TestSize.Level1) 119 { 120 MmiMessageId idMsg = MmiMessageId::INVALID; 121 NetPacket pkt(idMsg); 122 const char *retResult = pkt.GetData(); 123 EXPECT_TRUE(retResult != nullptr); 124 } 125 126 /** 127 * @tc.name:construct_002 128 * @tc.desc:Verify net packet 129 * @tc.type: FUNC 130 * @tc.require: 131 */ 132 HWTEST_F(NetPacketTest, GetData_002, TestSize.Level1) 133 { 134 MmiMessageId idMsg = static_cast<MmiMessageId>(-3003); 135 136 NetPacket pkt(idMsg); 137 const char *retResult = pkt.GetData(); 138 EXPECT_TRUE(retResult != nullptr); 139 } 140 141 /** 142 * @tc.name:construct_002 143 * @tc.desc:Verify net packet 144 * @tc.type: FUNC 145 * @tc.require: 146 */ 147 HWTEST_F(NetPacketTest, GetData_003, TestSize.Level1) 148 { 149 MmiMessageId idMsg = static_cast<MmiMessageId>(65535); 150 151 NetPacket pkt(idMsg); 152 const char *retResult = pkt.GetData(); 153 EXPECT_TRUE(retResult != nullptr); 154 } 155 156 /** 157 * @tc.name:construct_002 158 * @tc.desc:Verify net packet 159 * @tc.type: FUNC 160 * @tc.require: 161 */ 162 HWTEST_F(NetPacketTest, GetMsgId_001, TestSize.Level1) 163 { 164 MmiMessageId idMsg = static_cast<MmiMessageId>(22); 165 166 NetPacket pkt(idMsg); 167 const MmiMessageId retResult = pkt.GetMsgId(); 168 EXPECT_TRUE(retResult == idMsg); 169 } 170 171 /** 172 * @tc.name:construct_002 173 * @tc.desc:Verify net packet 174 * @tc.type: FUNC 175 * @tc.require: 176 */ 177 HWTEST_F(NetPacketTest, GetMsgId_002, TestSize.Level1) 178 { 179 MmiMessageId idMsg = static_cast<MmiMessageId>(-33); 180 181 NetPacket pkt(idMsg); 182 const MmiMessageId retResult = pkt.GetMsgId(); 183 EXPECT_TRUE(retResult == idMsg); 184 } 185 186 /** 187 * @tc.name:construct_002 188 * @tc.desc:Verify net packet 189 * @tc.type: FUNC 190 * @tc.require: 191 */ 192 HWTEST_F(NetPacketTest, GetMsgId_003, TestSize.Level1) 193 { 194 MmiMessageId idMsg = static_cast<MmiMessageId>(65535); 195 196 NetPacket pkt(idMsg); 197 const MmiMessageId retResult = pkt.GetMsgId(); 198 EXPECT_TRUE(retResult == idMsg); 199 } 200 201 /** 202 * @tc.name:construct_002 203 * @tc.desc:Verify net packet 204 * @tc.type: FUNC 205 * @tc.require: 206 */ 207 HWTEST_F(NetPacketTest, ReadAndWrite, TestSize.Level1) 208 { 209 int32_t p1 = 112; 210 std::string p2 = "test111"; 211 NetPacket pkt(MmiMessageId::INVALID); 212 pkt << p1 << p2; 213 214 int32_t r1 = 0; 215 std::string r2; 216 pkt >> r1 >> r2; 217 EXPECT_EQ(p1, r1); 218 EXPECT_EQ(p2, r2); 219 } 220 221 /** 222 * @tc.name:construct_002 223 * @tc.desc:Verify net packet 224 * @tc.type: FUNC 225 * @tc.require: 226 */ 227 HWTEST_F(NetPacketTest, WriteError, TestSize.Level1) 228 { 229 int32_t p1 = 112; 230 std::string p2 = "test111"; 231 NetPacket pkt(MmiMessageId::INVALID); 232 pkt << p1 << p2; 233 EXPECT_FALSE(pkt.ChkRWError()); 234 struct TestData { 235 int32_t xx; 236 char szTest[MAX_STREAM_BUF_SIZE]; 237 }; 238 TestData data = {333, "test111"}; 239 pkt << data; 240 EXPECT_TRUE(pkt.ChkRWError()); 241 } 242 243 /** 244 * @tc.name:construct_002 245 * @tc.desc:Verify net packet 246 * @tc.type: FUNC 247 * @tc.require: 248 */ 249 HWTEST_F(NetPacketTest, ReadError, TestSize.Level1) 250 { 251 int32_t p1 = 112; 252 std::string p2 = "test111"; 253 NetPacket pkt(MmiMessageId::INVALID); 254 pkt << p1 << p2; 255 EXPECT_FALSE(pkt.ChkRWError()); 256 257 int32_t r1 = 0; 258 std::string r2; 259 pkt >> r1 >> r2; 260 EXPECT_FALSE(pkt.ChkRWError()); 261 EXPECT_EQ(p1, r1); 262 EXPECT_EQ(p2, r2); 263 int32_t r3; 264 pkt >> r3; 265 EXPECT_TRUE(pkt.ChkRWError()); 266 } 267 } // namespace MMI 268 } // namespace OHOS 269