1 /*
2 * Copyright (c) 2023 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 #ifdef GTEST_API_
19 #define private public
20 #endif
21 #include "net_http_probe.h"
22 #include "net_http_probe_result.h"
23 #include "net_link_info.h"
24 #include "net_manager_constants.h"
25
26 namespace OHOS {
27 namespace NetManagerStandard {
28 namespace {
29 using namespace testing::ext;
30 constexpr int32_t TEST_NETID = 999;
31 constexpr const char *TEST_PROXY_HOST = "testHttpProxy";
32 constexpr const char *TEST_STRING = "testString";
33 constexpr const char *TEST_HTTP_URL = "http://connectivitycheck.platform.hicloud.com/generate_204";
34 constexpr const char *TEST_HTTPS_URL = "https://connectivitycheck.platform.hicloud.com/generate_204";
35
36 class NetHttpProbeTest : public testing::Test {
37 public:
38 static void SetUpTestCase();
39 static void TearDownTestCase();
40 void SetUp();
41 void TearDown();
42 static inline std::shared_ptr<NetHttpProbe> instance_ = nullptr;
43 };
44
SetUpTestCase()45 void NetHttpProbeTest::SetUpTestCase()
46 {
47 instance_ = std::make_shared<NetHttpProbe>(TEST_NETID, NetBearType::BEARER_DEFAULT, NetLinkInfo(), PROBE_HTTP);
48 }
49
TearDownTestCase()50 void NetHttpProbeTest::TearDownTestCase() {}
51
SetUp()52 void NetHttpProbeTest::SetUp() {}
53
TearDown()54 void NetHttpProbeTest::TearDown() {}
55
56 HWTEST_F(NetHttpProbeTest, SendProbeTest001, TestSize.Level1)
57 {
58 instance_->GetHttpProbeResult();
59 instance_->GetHttpsProbeResult();
60 HttpProxy httpProxy = {TEST_PROXY_HOST, 0, {}};
61 NetLinkInfo info;
62 instance_->UpdateNetLinkInfo(info);
63 instance_->UpdateGlobalHttpProxy(httpProxy);
64 int32_t ret = instance_->SendProbe(PROBE_HTTP_HTTPS, TEST_HTTP_URL, TEST_HTTPS_URL);
65 EXPECT_EQ(ret, NETMANAGER_ERR_INTERNAL);
66 }
67
68 HWTEST_F(NetHttpProbeTest, NetHttpProbeBranchTest001, TestSize.Level1)
69 {
70 instance_->SendHttpProbeRequest();
71 instance_->RecvHttpProbeResponse();
72
73 std::string domain = "";
74 std::string result = instance_->GetAddrInfo(domain);
75 ASSERT_TRUE(result.empty());
76
77 int32_t port = 0;
78 bool ret = instance_->SetResolveOption(ProbeType::PROBE_HTTP, domain, TEST_STRING, port);
79 ASSERT_FALSE(ret);
80
81 ret = instance_->SetResolveOption(ProbeType::PROBE_HTTP, TEST_STRING, TEST_STRING, port);
82 ASSERT_FALSE(ret);
83
84 ret = instance_->SetResolveOption(ProbeType::PROBE_HTTPS, TEST_STRING, TEST_STRING, port);
85 ASSERT_FALSE(ret);
86
87 bool useProxy = true;
88 ret = instance_->SendDnsProbe(ProbeType::PROBE_HTTPS, TEST_STRING, TEST_STRING, useProxy);
89 ASSERT_TRUE(ret);
90
91 ret = instance_->SendDnsProbe(ProbeType::PROBE_HTTPS, TEST_STRING, TEST_STRING, useProxy);
92 ASSERT_TRUE(ret);
93 }
94 } // namespace
95 } // namespace NetManagerStandard
96 } // namespace OHOS
97