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