1 /*
2 * Copyright (c) 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 <netdb.h>
17 #include <gtest/gtest.h>
18 #include <sys/socket.h>
19
20 #include "i_net_monitor_callback.h"
21 #include "net_manager_constants.h"
22 #define private public
23 #include "net_monitor.h"
24 #undef private
25
26 namespace OHOS {
27 namespace NetManagerStandard {
28 namespace {
29 using namespace testing::ext;
30 constexpr uint32_t TEST_NETID = 999;
31 constexpr int32_t TEST_SOCKETFD = -1;
32 constexpr int32_t TEST_NORMAL_FD = 9000;
33 class TestMonitorCallback : public INetMonitorCallback {
34 public:
OnHandleNetMonitorResult(NetDetectionStatus netDetectionState,const std::string & urlRedirect)35 inline void OnHandleNetMonitorResult(NetDetectionStatus netDetectionState, const std::string &urlRedirect) override
36 {
37 (void)netDetectionState;
38 (void)urlRedirect;
39 }
40 };
41 } // namespace
42
43 class NetMonitorTest : public testing::Test {
44 public:
45 static void SetUpTestCase();
46 static void TearDownTestCase();
47 void SetUp();
48 void TearDown();
49 static inline std::shared_ptr<INetMonitorCallback> callback_ = std::make_shared<TestMonitorCallback>();
50 static inline std::shared_ptr<NetMonitor> instance_ = std::make_shared<NetMonitor>(TEST_NETID, callback_);
51 };
52
SetUpTestCase()53 void NetMonitorTest::SetUpTestCase()
54 {
55 instance_->Start();
56 }
57
TearDownTestCase()58 void NetMonitorTest::TearDownTestCase()
59 {
60 instance_->Stop();
61 }
62
SetUp()63 void NetMonitorTest::SetUp() {}
64
TearDown()65 void NetMonitorTest::TearDown() {}
66
67 HWTEST_F(NetMonitorTest, SetSocketParameterTest001, TestSize.Level1)
68 {
69 int32_t ret = instance_->SetSocketParameter(TEST_SOCKETFD);
70 EXPECT_EQ(ret, NETMANAGER_ERROR);
71 ret = instance_->SetSocketParameter(TEST_NORMAL_FD);
72 EXPECT_EQ(ret, NETMANAGER_ERROR);
73 }
74
75 HWTEST_F(NetMonitorTest, IsDetectingTest001, TestSize.Level1)
76 {
77 bool ret = instance_->IsDetecting();
78 EXPECT_TRUE(ret);
79 instance_->Detection();
80 instance_->Stop();
81 }
82
83 HWTEST_F(NetMonitorTest, GetStatusCodeFromResponse001, TestSize.Level1)
84 {
85 std::string str;
86 int32_t ret = instance_->GetStatusCodeFromResponse(str);
87 EXPECT_EQ(ret, -1);
88 str = "12 34";
89 ret = instance_->GetStatusCodeFromResponse(str);
90 EXPECT_EQ(ret, -1);
91 str = "12 \r\n";
92 ret = instance_->GetStatusCodeFromResponse(str);
93 EXPECT_EQ(ret, -1);
94 str = "12 34 \r\n";
95 ret = instance_->GetStatusCodeFromResponse(str);
96 EXPECT_EQ(ret, 34);
97 str = "12 34\r\n";
98 ret = instance_->GetStatusCodeFromResponse(str);
99 EXPECT_EQ(ret, -1);
100 str = "12 34 56 \r\n";
101 ret = instance_->GetStatusCodeFromResponse(str);
102 EXPECT_EQ(ret, 34);
103 }
104
105 HWTEST_F(NetMonitorTest, SendParallelHttpProbes001, TestSize.Level1)
106 {
107 int32_t ret = instance_->SendParallelHttpProbes();
108 EXPECT_EQ(ret, INVALID_DETECTION_STATE);
109 }
110
111 HWTEST_F(NetMonitorTest, SendHttpProbe001, TestSize.Level1)
112 {
113 std::string domain;
114 std::string urlPath;
115 NetDetectionStatus ret = instance_->SendHttpProbe(domain, urlPath, 80);
116 EXPECT_EQ(ret, INVALID_DETECTION_STATE);
117 domain = "114.114.114.114";
118 urlPath = "www.baidu.com";
119 ret = instance_->SendHttpProbe(domain, urlPath, 80);
120 EXPECT_EQ(ret, INVALID_DETECTION_STATE);
121 }
122
123 HWTEST_F(NetMonitorTest, ConnectIpv4001, TestSize.Level1)
124 {
125 int32_t sockFd = 9000;
126 uint16_t port = 80;
127 std::string ipAddr = "192.168.111.11";
128 int32_t ret = instance_->ConnectIpv4(sockFd, port, ipAddr);
129 EXPECT_EQ(ret, -1);
130 }
131
132 HWTEST_F(NetMonitorTest, ConnectIpv6001, TestSize.Level1)
133 {
134 int32_t sockFd = 9000;
135 uint16_t port = 80;
136 std::string ipAddr = "192.168.111.11";
137 int32_t ret = instance_->ConnectIpv6(sockFd, port, ipAddr);
138 EXPECT_EQ(ret, -1);
139 }
140
141 HWTEST_F(NetMonitorTest, Send001, TestSize.Level1)
142 {
143 int32_t sockFd = 9000;
144 std::string domain = "114.114.114.114";
145 std::string url = "www.baidu.com";
146 int32_t ret = instance_->Send(sockFd, domain, url);
147 EXPECT_EQ(ret, -1);
148 }
149
150 HWTEST_F(NetMonitorTest, Receive001, TestSize.Level1)
151 {
152 int32_t sockFd = 9000;
153 std::string probResult;
154 int32_t ret = instance_->Receive(sockFd, probResult);
155 EXPECT_EQ(ret, -1);
156 }
157
158 HWTEST_F(NetMonitorTest, dealRecvResult001, TestSize.Level1)
159 {
160 std::string strResponse = "ok";
161 NetDetectionStatus ret = instance_->dealRecvResult(strResponse);
162 EXPECT_EQ(ret, INVALID_DETECTION_STATE);
163 }
164
165 HWTEST_F(NetMonitorTest, GetIpAddr001, TestSize.Level1)
166 {
167 std::string domain = "www.baidu.com";
168 std::string ip_addr = "192.168.111.111";
169 int socketType = 1;
170 int32_t ret = instance_->GetIpAddr(domain, ip_addr, socketType);
171 EXPECT_EQ(ret, -1);
172 }
173
174 HWTEST_F(NetMonitorTest, GetDefaultNetDetectionUrlFromCfg001, TestSize.Level1)
175 {
176 std::string strUrl;
177 int32_t ret = instance_->GetDefaultNetDetectionUrlFromCfg(strUrl);
178 EXPECT_EQ(ret, 0);
179 }
180
181 HWTEST_F(NetMonitorTest, ParseUrl001, TestSize.Level1)
182 {
183 std::string url = "www.baidu.com";
184 std::string domain;
185 std::string urlPath;
186 int32_t ret = instance_->ParseUrl(url, domain, urlPath);
187 EXPECT_EQ(ret, -1);
188 }
189
190 HWTEST_F(NetMonitorTest, GetUrlRedirectFromResponse001, TestSize.Level1)
191 {
192 std::string tmp;
193 std::string empty;
194 std::string redirFirst = "Location: baidu.com";
195 std::string redirSecond = "http baidu.com";
196 int ret = instance_->GetUrlRedirectFromResponse(empty, tmp);
197 EXPECT_EQ(ret, -1);
198 ret = instance_->GetUrlRedirectFromResponse(redirFirst, tmp);
199 EXPECT_NE(ret, -1);
200 ret = instance_->GetUrlRedirectFromResponse(redirSecond, tmp);
201 EXPECT_NE(ret, -1);
202 }
203 } // namespace NetManagerStandard
204 } // namespace OHOS