1 /*
2 * Copyright (c) 2024 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 <gmock/gmock.h>
18 #include "netsys_net_dns_result_data.h"
19
20 namespace OHOS {
21 namespace NetsysNative {
22 namespace {
23 using namespace testing::ext;
24 using ::testing::_;
25 using ::testing::Return;
26 using ::testing::DoAll;
27 using ::testing::SetArgReferee;
28 using ::testing::InSequence;
29 } // namespace
30
31 class ParcelMock : public Parcel {
32 public:
ParcelMock()33 ParcelMock() {}
~ParcelMock()34 virtual ~ParcelMock() {}
35 MOCK_METHOD1(WriteUint32, bool(uint32_t));
36 MOCK_METHOD1(WriteString, bool(const std::string &));
37 MOCK_METHOD1(WriteUint16, bool(uint16_t));
38 MOCK_METHOD1(WriteBool, bool(bool));
39 MOCK_METHOD1(WriteUint8, bool(uint8_t));
40 MOCK_METHOD1(WriteUint64, bool(uint64_t));
41 MOCK_METHOD1(WriteInt32, bool(int32_t));
42 MOCK_METHOD1(ReadUint32, bool(uint32_t &));
43 MOCK_METHOD1(ReadString, bool(const std::string &));
44 MOCK_METHOD1(ReadUint16, bool(uint16_t));
45 MOCK_METHOD1(ReadBool, bool(bool));
46 MOCK_METHOD1(ReadUint8, bool(uint8_t));
47 MOCK_METHOD1(ReadUint64, bool(uint64_t));
48 MOCK_METHOD1(ReadInt32, bool(int32_t));
49 };
50
51 class NetDnsResultReportTest : public testing::Test {
52 public:
53 static void SetUpTestCase();
54 static void TearDownTestCase();
55 void SetUp();
56 void TearDown();
57 NetDnsResultReport report;
58 ParcelMock parcel;
59 };
60
SetUpTestCase()61 void NetDnsResultReportTest::SetUpTestCase() {}
62
TearDownTestCase()63 void NetDnsResultReportTest::TearDownTestCase() {}
64
SetUp()65 void NetDnsResultReportTest::SetUp()
66 {
67 report.netid_ = 1;
68 report.uid_ = 1;
69 report.pid_ = 1;
70 report.timeused_ = 1;
71 report.queryresult_ = 1;
72 report.host_ = "test.host";
73
74 NetDnsResultAddrInfo addrInfo1;
75 addrInfo1.type_ = NetDnsResultAddrType::ADDR_TYPE_IPV4;
76 addrInfo1.addr_ = "192.168.1.1";
77 report.addrlist_.push_back(addrInfo1);
78
79 NetDnsResultAddrInfo addrInfo2;
80 addrInfo2.type_ = NetDnsResultAddrType::ADDR_TYPE_IPV6;
81 addrInfo2.addr_ = "2001:db8::1";
82 report.addrlist_.push_back(addrInfo2);
83 }
84
TearDown()85 void NetDnsResultReportTest::TearDown() {}
86
87 HWTEST_F(NetDnsResultReportTest, Marshalling_ShouldReturnFalse_001, TestSize.Level0)
88 {
89 EXPECT_CALL(parcel, WriteUint32(_)).WillOnce(Return(false));
90 EXPECT_FALSE(report.Marshalling(parcel));
91 }
92
93 HWTEST_F(NetDnsResultReportTest, Marshalling_ShouldReturnFalse_002, TestSize.Level0)
94 {
95 EXPECT_CALL(parcel, WriteUint32(_)).Times(2).WillOnce(Return(true)).WillOnce(Return(false));
96 EXPECT_FALSE(report.Marshalling(parcel));
97 }
98
99 HWTEST_F(NetDnsResultReportTest, Marshalling_ShouldReturnFalse_003, TestSize.Level0)
100 {
101 EXPECT_CALL(parcel, WriteUint32(_)).Times(3).WillOnce(Return(true)).WillOnce(Return(true)).WillOnce(Return(false));
102 EXPECT_FALSE(report.Marshalling(parcel));
103 }
104
105 HWTEST_F(NetDnsResultReportTest, Marshalling_ShouldReturnFalse_004, TestSize.Level0)
106 {
107 InSequence s;
108 EXPECT_CALL(parcel, WriteUint32(_)).Times(3).WillRepeatedly(Return(true));
109 EXPECT_CALL(parcel, WriteUint32(_)).WillOnce(Return(false));
110 EXPECT_FALSE(report.Marshalling(parcel));
111 }
112
113 HWTEST_F(NetDnsResultReportTest, Marshalling_ShouldReturnFalse_005, TestSize.Level0)
114 {
115 InSequence s;
116 EXPECT_CALL(parcel, WriteUint32(_)).Times(4).WillRepeatedly(Return(true));
117 EXPECT_CALL(parcel, WriteUint32(_)).Times(1).WillOnce(Return(false));
118 EXPECT_FALSE(report.Marshalling(parcel));
119 }
120
121 HWTEST_F(NetDnsResultReportTest, Marshalling_ShouldReturnFalse_006, TestSize.Level0)
122 {
123 InSequence s;
124 EXPECT_CALL(parcel, WriteUint32(_)).Times(5).WillRepeatedly(Return(true));
125 EXPECT_CALL(parcel, WriteString(_)).WillOnce(Return(false));
126 EXPECT_FALSE(report.Marshalling(parcel));
127 }
128
129 HWTEST_F(NetDnsResultReportTest, Marshalling_ShouldReturnFalse_007, TestSize.Level0)
130 {
131 InSequence s;
132 EXPECT_CALL(parcel, WriteUint32(_)).Times(5).WillRepeatedly(Return(true));
133 EXPECT_CALL(parcel, WriteString(_)).WillOnce(Return(true));
134 EXPECT_CALL(parcel, WriteUint32(_)).WillOnce(Return(false));
135 EXPECT_FALSE(report.Marshalling(parcel));
136 }
137
138 HWTEST_F(NetDnsResultReportTest, Marshalling_ShouldReturnFalse_008, TestSize.Level0)
139 {
140 InSequence s;
141 EXPECT_CALL(parcel, WriteUint32(_)).Times(5).WillRepeatedly(Return(true));
142 EXPECT_CALL(parcel, WriteString(_)).WillOnce(Return(true));
143 EXPECT_CALL(parcel, WriteUint32(_)).WillOnce(Return(true));
144 EXPECT_CALL(parcel, WriteUint32(_)).WillOnce(Return(false));
145 EXPECT_FALSE(report.Marshalling(parcel));
146 }
147
148 HWTEST_F(NetDnsResultReportTest, Marshalling_ShouldReturnFalse_009, TestSize.Level0)
149 {
150 InSequence s;
151 EXPECT_CALL(parcel, WriteUint32(_)).Times(5).WillRepeatedly(Return(true));
152 EXPECT_CALL(parcel, WriteString(_)).WillOnce(Return(true));
153 EXPECT_CALL(parcel, WriteUint32(_)).WillOnce(Return(true));
154 EXPECT_CALL(parcel, WriteUint32(_)).WillOnce(Return(true));
155 EXPECT_CALL(parcel, WriteString(_)).WillOnce(Return(false));
156 EXPECT_FALSE(report.Marshalling(parcel));
157 }
158
159 HWTEST_F(NetDnsResultReportTest, Marshalling_ShouldReturnFalse_010, TestSize.Level0)
160 {
161 InSequence s;
162 EXPECT_CALL(parcel, WriteUint32(_)).Times(5).WillRepeatedly(Return(true));
163 EXPECT_CALL(parcel, WriteString(_)).WillOnce(Return(true));
164 EXPECT_CALL(parcel, WriteUint32(_)).WillOnce(Return(true));
165 EXPECT_CALL(parcel, WriteUint32(_)).WillOnce(Return(true));
166 EXPECT_CALL(parcel, WriteString(_)).WillOnce(Return(true));
167 EXPECT_CALL(parcel, WriteUint32(_)).WillOnce(Return(false));
168 EXPECT_FALSE(report.Marshalling(parcel));
169 }
170
171 HWTEST_F(NetDnsResultReportTest, Marshalling_ShouldReturnTrue_001, TestSize.Level0)
172 {
173 EXPECT_CALL(parcel, WriteUint32(_)).WillRepeatedly(Return(true));
174 EXPECT_CALL(parcel, WriteString(_)).WillRepeatedly(Return(true));
175 EXPECT_TRUE(report.Marshalling(parcel));
176 }
177
178 HWTEST_F(NetDnsResultReportTest, Unmarshalling_ShouldReturnFalse_001, TestSize.Level0)
179 {
180 InSequence s;
181 EXPECT_CALL(parcel, ReadUint32(_)).WillOnce(Return(false));
182 EXPECT_FALSE(NetDnsResultReport::Unmarshalling(parcel, report));
183 }
184
185 HWTEST_F(NetDnsResultReportTest, Unmarshalling_ShouldReturnFalse_002, TestSize.Level0)
186 {
187 InSequence s;
188 EXPECT_CALL(parcel, ReadUint32(_)).WillOnce(Return(true));
189 EXPECT_CALL(parcel, ReadUint32(_)).WillOnce(Return(false));
190 EXPECT_FALSE(NetDnsResultReport::Unmarshalling(parcel, report));
191 }
192
193 HWTEST_F(NetDnsResultReportTest, Unmarshalling_ShouldReturnFalse_003, TestSize.Level0)
194 {
195 InSequence s;
196 EXPECT_CALL(parcel, ReadUint32(_)).Times(2).WillRepeatedly(Return(true));
197 EXPECT_CALL(parcel, ReadUint32(_)).WillOnce(Return(false));
198 EXPECT_FALSE(NetDnsResultReport::Unmarshalling(parcel, report));
199 }
200
201 HWTEST_F(NetDnsResultReportTest, Unmarshalling_ShouldReturnFalse_004, TestSize.Level0)
202 {
203 InSequence s;
204 EXPECT_CALL(parcel, ReadUint32(_)).Times(3).WillRepeatedly(Return(true));
205 EXPECT_CALL(parcel, ReadUint32(_)).WillOnce(Return(false));
206 EXPECT_FALSE(NetDnsResultReport::Unmarshalling(parcel, report));
207 }
208
209 HWTEST_F(NetDnsResultReportTest, Unmarshalling_ShouldReturnFalse_005, TestSize.Level0)
210 {
211 InSequence s;
212 EXPECT_CALL(parcel, ReadUint32(_)).Times(4).WillRepeatedly(Return(true));
213 EXPECT_CALL(parcel, ReadUint32(_)).WillOnce(Return(false));
214 EXPECT_FALSE(NetDnsResultReport::Unmarshalling(parcel, report));
215 }
216
217 HWTEST_F(NetDnsResultReportTest, Unmarshalling_ShouldReturnFalse_006, TestSize.Level0)
218 {
219 InSequence s;
220 EXPECT_CALL(parcel, ReadUint32(_)).Times(5).WillRepeatedly(Return(true));
221 EXPECT_CALL(parcel, ReadString(_)).WillOnce(Return(false));
222 EXPECT_FALSE(NetDnsResultReport::Unmarshalling(parcel, report));
223 }
224
225 HWTEST_F(NetDnsResultReportTest, Unmarshalling_ShouldReturnFalse_007, TestSize.Level0)
226 {
227 InSequence s;
228 EXPECT_CALL(parcel, ReadUint32(_)).Times(5).WillRepeatedly(Return(true));
229 EXPECT_CALL(parcel, ReadString(_)).WillOnce(Return(true));
230 EXPECT_CALL(parcel, ReadUint32(_)).WillOnce(Return(false));
231 EXPECT_FALSE(NetDnsResultReport::Unmarshalling(parcel, report));
232 }
233
234 HWTEST_F(NetDnsResultReportTest, Unmarshalling_ShouldReturnFalse_008, TestSize.Level0)
235 {
236 InSequence s;
237 uint32_t size = 2;
238 EXPECT_CALL(parcel, ReadUint32(_)).Times(5).WillRepeatedly(Return(true));
239 EXPECT_CALL(parcel, ReadString(_)).WillOnce(Return(true));
240 EXPECT_CALL(parcel, ReadUint32(_)).WillOnce(DoAll(SetArgReferee<0>(size), Return(true)));
241 EXPECT_CALL(parcel, ReadUint32(_)).WillOnce(Return(false));
242 EXPECT_FALSE(NetDnsResultReport::Unmarshalling(parcel, report));
243 }
244
245 HWTEST_F(NetDnsResultReportTest, Unmarshalling_ShouldReturnFalse_009, TestSize.Level0)
246 {
247 InSequence s;
248 uint32_t size = 2;
249 EXPECT_CALL(parcel, ReadUint32(_)).Times(5).WillRepeatedly(Return(true));
250 EXPECT_CALL(parcel, ReadString(_)).WillOnce(Return(true));
251 EXPECT_CALL(parcel, ReadUint32(_)).WillOnce(DoAll(SetArgReferee<0>(size), Return(true)));
252 EXPECT_CALL(parcel, ReadUint32(_)).WillOnce(Return(true));
253 EXPECT_CALL(parcel, ReadString(_)).WillOnce(Return(false));
254 EXPECT_FALSE(NetDnsResultReport::Unmarshalling(parcel, report));
255 }
256
257 HWTEST_F(NetDnsResultReportTest, Unmarshalling_ShouldReturnFalse_010, TestSize.Level0)
258 {
259 InSequence s;
260 uint32_t size = 2;
261 EXPECT_CALL(parcel, ReadUint32(_)).Times(5).WillRepeatedly(Return(true));
262 EXPECT_CALL(parcel, ReadString(_)).WillOnce(Return(true));
263 EXPECT_CALL(parcel, ReadUint32(_)).WillOnce(DoAll(SetArgReferee<0>(size), Return(true)));
264 EXPECT_CALL(parcel, ReadUint32(_)).WillOnce(Return(true));
265 EXPECT_CALL(parcel, ReadString(_)).WillOnce(Return(true));
266 EXPECT_CALL(parcel, ReadUint32(_)).WillOnce(Return(false));
267 EXPECT_FALSE(NetDnsResultReport::Unmarshalling(parcel, report));
268 }
269
270 HWTEST_F(NetDnsResultReportTest, Unmarshalling_ShouldReturnTrue_001, TestSize.Level0)
271 {
272 EXPECT_CALL(parcel, ReadUint32(_)).WillRepeatedly(Return(true));
273 EXPECT_CALL(parcel, ReadString(_)).WillRepeatedly(Return(true));
274 EXPECT_TRUE(NetDnsResultReport::Unmarshalling(parcel, report));
275 }
276
277 } // namespace NetsysNative
278 } // namespace OHOS