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
19 #include "netnative_log_wrapper.h"
20 #include "netsys_net_dns_health_data.h"
21
22 namespace OHOS {
23 namespace NetsysNative {
24 namespace {
25 using namespace testing::ext;
26 using ::testing::_;
27 using ::testing::Return;
28 } // namespace
29
30 class ParcelMock : public Parcel {
31 public:
ParcelMock()32 ParcelMock() {}
~ParcelMock()33 virtual ~ParcelMock() {}
34 MOCK_METHOD1(WriteUint32, bool(uint32_t));
35 MOCK_METHOD1(WriteString, bool(const std::string &));
36 MOCK_METHOD1(WriteUint16, bool(uint16_t));
37 MOCK_METHOD1(WriteBool, bool(bool));
38 MOCK_METHOD1(ReadUint32, bool(uint32_t));
39 MOCK_METHOD1(ReadString, bool(const std::string &));
40 MOCK_METHOD1(ReadUint16, bool(uint16_t));
41 MOCK_METHOD1(ReadBool, bool(bool));
42 };
43
44 class NetDnsHealthReportTest : public testing::Test {
45 public:
46 static void SetUpTestCase();
47 static void TearDownTestCase();
48 void SetUp();
49 void TearDown();
50 NetDnsHealthReport report;
51 ParcelMock parcel;
52 };
53
SetUpTestCase()54 void NetDnsHealthReportTest::SetUpTestCase() {}
55
TearDownTestCase()56 void NetDnsHealthReportTest::TearDownTestCase() {}
57
SetUp()58 void NetDnsHealthReportTest::SetUp()
59 {
60 report.netid_ = 1;
61 report.uid_ = 1;
62 report.appid_ = 1;
63 report.host_ = "test.host";
64 report.type_ = 1;
65 report.result_ = true;
66 }
67
TearDown()68 void NetDnsHealthReportTest::TearDown() {}
69
70 HWTEST_F(NetDnsHealthReportTest, Marshalling_WhenWriteUint32Fails_netid, TestSize.Level0)
71 {
72 EXPECT_CALL(parcel, WriteUint32(_)).WillOnce(Return(false));
73 EXPECT_EQ(report.Marshalling(parcel), false);
74 }
75
76 HWTEST_F(NetDnsHealthReportTest, Marshalling_WhenWriteUint32Fails_uid, TestSize.Level0)
77 {
78 EXPECT_CALL(parcel, WriteUint32(_)).Times(2).WillOnce(Return(true)).WillOnce(Return(false));
79 EXPECT_EQ(report.Marshalling(parcel), false);
80 }
81
82 HWTEST_F(NetDnsHealthReportTest, Marshalling_WhenWriteUint32Fails_appid, TestSize.Level0)
83 {
84 EXPECT_CALL(parcel, WriteUint32(_)).Times(3).WillOnce(Return(true)).WillOnce(Return(true)).WillOnce(Return(false));
85 EXPECT_EQ(report.Marshalling(parcel), false);
86 }
87
88 HWTEST_F(NetDnsHealthReportTest, Marshalling_ShouldReturnFalse_WhenWriteStringFails, TestSize.Level0)
89 {
90 EXPECT_CALL(parcel, WriteUint32(_)).WillRepeatedly(Return(true));
91 EXPECT_CALL(parcel, WriteString(_)).WillOnce(Return(false));
92 EXPECT_EQ(report.Marshalling(parcel), false);
93 }
94
95 HWTEST_F(NetDnsHealthReportTest, Marshalling_ShouldReturnFalse_WhenWriteUint16Fails, TestSize.Level0)
96 {
97 EXPECT_CALL(parcel, WriteUint32(_)).WillRepeatedly(Return(true));
98 EXPECT_CALL(parcel, WriteString(_)).WillOnce(Return(true));
99 EXPECT_CALL(parcel, WriteUint16(_)).WillOnce(Return(false));
100 EXPECT_EQ(report.Marshalling(parcel), false);
101 }
102
103 HWTEST_F(NetDnsHealthReportTest, Marshalling_ShouldReturnFalse_WhenWriteBoolFails, TestSize.Level0)
104 {
105 EXPECT_CALL(parcel, WriteUint32(_)).WillRepeatedly(Return(true));
106 EXPECT_CALL(parcel, WriteString(_)).WillOnce(Return(true));
107 EXPECT_CALL(parcel, WriteUint16(_)).WillOnce(Return(true));
108 EXPECT_CALL(parcel, WriteBool(_)).WillOnce(Return(false));
109 EXPECT_EQ(report.Marshalling(parcel), false);
110 }
111
112 HWTEST_F(NetDnsHealthReportTest, Marshalling_ShouldReturnTrue_WhenAllWriteFunctionsSucceed, TestSize.Level0)
113 {
114 EXPECT_CALL(parcel, WriteUint32(_)).WillRepeatedly(Return(true));
115 EXPECT_CALL(parcel, WriteString(_)).WillOnce(Return(true));
116 EXPECT_CALL(parcel, WriteUint16(_)).WillOnce(Return(true));
117 EXPECT_CALL(parcel, WriteBool(_)).WillOnce(Return(true));
118 EXPECT_EQ(report.Marshalling(parcel), true);
119 }
120
121 HWTEST_F(NetDnsHealthReportTest, Unmarshalling_ShouldReturnTrue_WhenAllReadOperationsAreSuccessful, TestSize.Level0)
122 {
123 EXPECT_CALL(parcel, ReadUint32(_)).WillRepeatedly(Return(true));
124 EXPECT_CALL(parcel, ReadUint16(_)).WillOnce(Return(true));
125 EXPECT_CALL(parcel, ReadString(_)).WillOnce(Return(true));
126 EXPECT_CALL(parcel, ReadBool(_)).WillOnce(Return(true));
127 EXPECT_EQ(report.Unmarshalling(parcel, report), true);
128 }
129
130 HWTEST_F(NetDnsHealthReportTest, Unmarshalling_ShouldReturnFalse_WhenAllReadUint32Fails, TestSize.Level0)
131 {
132 EXPECT_CALL(parcel, ReadUint32(_)).WillRepeatedly(Return(false));
133 EXPECT_EQ(report.Unmarshalling(parcel, report), false);
134 }
135
136 HWTEST_F(NetDnsHealthReportTest, Unmarshalling_ShouldReturnFalse_WhenAllReadStringFails, TestSize.Level0)
137 {
138 EXPECT_CALL(parcel, ReadUint32(_)).WillRepeatedly(Return(true));
139 EXPECT_CALL(parcel, ReadString(_)).WillOnce(Return(false));
140 EXPECT_EQ(report.Unmarshalling(parcel, report), false);
141 }
142
143 HWTEST_F(NetDnsHealthReportTest, Unmarshalling_ShouldReturnFalse_WhenAllReadUint16Fails, TestSize.Level0)
144 {
145 EXPECT_CALL(parcel, ReadUint32(_)).WillRepeatedly(Return(true));
146 EXPECT_CALL(parcel, ReadString(_)).WillOnce(Return(true));
147 EXPECT_CALL(parcel, ReadUint16(_)).WillOnce(Return(false));
148 EXPECT_EQ(report.Unmarshalling(parcel, report), false);
149 }
150
151 HWTEST_F(NetDnsHealthReportTest, Unmarshalling_ShouldReturnFalse_WhenAllReadBoolFails, TestSize.Level0)
152 {
153 EXPECT_CALL(parcel, ReadUint32(_)).WillRepeatedly(Return(true));
154 EXPECT_CALL(parcel, ReadString(_)).WillOnce(Return(true));
155 EXPECT_CALL(parcel, ReadUint16(_)).WillOnce(Return(true));
156 EXPECT_CALL(parcel, ReadBool(_)).WillOnce(Return(false));
157 EXPECT_EQ(report.Unmarshalling(parcel, report), false);
158 }
159 } // namespace NetsysNative
160 } // namespace OHOS