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 <gtest/gtest.h>
17
18 #include "i_net_monitor_callback.h"
19 #include "net_manager_constants.h"
20 #define private public
21 #include "net_monitor.h"
22 #undef private
23
24 namespace OHOS {
25 namespace NetManagerStandard {
26 namespace {
27 using namespace testing::ext;
28 constexpr uint32_t TEST_NETID = 999;
29 constexpr int32_t TEST_SOCKETFD = -1;
30 class TestMonitorCallback : public INetMonitorCallback {
31 public:
OnHandleNetMonitorResult(NetDetectionStatus netDetectionState,const std::string & urlRedirect)32 inline void OnHandleNetMonitorResult(NetDetectionStatus netDetectionState, const std::string &urlRedirect) override
33 {
34 (void)netDetectionState;
35 (void)urlRedirect;
36 }
37 };
38 } // namespace
39
40 class NetMonitorTest : public testing::Test {
41 public:
42 static void SetUpTestCase();
43 static void TearDownTestCase();
44 void SetUp();
45 void TearDown();
46 static inline std::shared_ptr<INetMonitorCallback> callback_ = std::make_shared<TestMonitorCallback>();
47 static inline std::shared_ptr<NetMonitor> instance_ = std::make_shared<NetMonitor>(TEST_NETID, callback_);
48 };
49
SetUpTestCase()50 void NetMonitorTest::SetUpTestCase()
51 {
52 instance_->Start();
53 }
54
TearDownTestCase()55 void NetMonitorTest::TearDownTestCase()
56 {
57 instance_->Stop();
58 }
59
SetUp()60 void NetMonitorTest::SetUp() {}
61
TearDown()62 void NetMonitorTest::TearDown() {}
63
64 HWTEST_F(NetMonitorTest, SetSocketParameterTest001, TestSize.Level1)
65 {
66 int32_t ret = instance_->SetSocketParameter(TEST_SOCKETFD);
67 EXPECT_EQ(ret, NETMANAGER_ERROR);
68 }
69
70 HWTEST_F(NetMonitorTest, IsDetectingTest001, TestSize.Level1)
71 {
72 bool ret = instance_->IsDetecting();
73 EXPECT_TRUE(ret);
74 instance_->Detection();
75 }
76
77 HWTEST_F(NetMonitorTest, GetStatusCodeFromResponse001, TestSize.Level1)
78 {
79 std::string str;
80 int32_t ret = instance_->GetStatusCodeFromResponse(str);
81 EXPECT_EQ(ret, -1);
82 str = "12 34";
83 ret = instance_->GetStatusCodeFromResponse(str);
84 EXPECT_EQ(ret, -1);
85 str = "12 \r\n";
86 ret = instance_->GetStatusCodeFromResponse(str);
87 EXPECT_EQ(ret, -1);
88 str = "12 34 \r\n";
89 ret = instance_->GetStatusCodeFromResponse(str);
90 EXPECT_EQ(ret, 34);
91 str = "12 34\r\n";
92 ret = instance_->GetStatusCodeFromResponse(str);
93 EXPECT_EQ(ret, -1);
94 str = "12 34 56 \r\n";
95 ret = instance_->GetStatusCodeFromResponse(str);
96 EXPECT_EQ(ret, 34);
97 }
98 } // namespace NetManagerStandard
99 } // namespace OHOS