1 /*
2 * Copyright (c) 2021 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
22 #include "net_conn_types.h"
23 #include "net_mgr_log_wrapper.h"
24 #include "net_supplier.h"
25
26 namespace OHOS {
27 namespace NetManagerStandard {
28 using namespace testing::ext;
29 class NetScoreTest : public testing::Test {
30 public:
31 static void SetUpTestCase();
32 static void TearDownTestCase();
33 void SetUp();
34 void TearDown();
35 };
36
SetUpTestCase()37 void NetScoreTest::SetUpTestCase() {}
38
TearDownTestCase()39 void NetScoreTest::TearDownTestCase() {}
40
SetUp()41 void NetScoreTest::SetUp() {}
42
TearDown()43 void NetScoreTest::TearDown() {}
44
45 HWTEST_F(NetScoreTest, GetServiceScore, TestSize.Level1)
46 {
47 std::set<NetCap> netCaps {NET_CAPABILITY_MMS, NET_CAPABILITY_INTERNET};
48 std::string ident = "ident";
49 NetBearType bearerType = BEARER_CELLULAR;
50 sptr<NetSupplier> supplier = (std::make_unique<NetSupplier>(bearerType, ident, netCaps)).release();
51 supplier->SetDetectionDone();
52
53 // mock Failed to detect network
54 supplier->SetNetValid(INVALID_DETECTION_STATE);
55
56 ASSERT_TRUE(supplier->GetNetScore() == static_cast<int32_t>(NetTypeScoreValue::CELLULAR_VALUE));
57 ASSERT_TRUE(supplier->GetRealScore() ==
58 (static_cast<int32_t>(NetTypeScoreValue::CELLULAR_VALUE) - NET_VALID_SCORE));
59
60 supplier->SetNetValid(CAPTIVE_PORTAL_STATE);
61
62 ASSERT_TRUE(supplier->GetNetScore() == static_cast<int32_t>(NetTypeScoreValue::CELLULAR_VALUE));
63 ASSERT_TRUE(supplier->GetRealScore() ==
64 (static_cast<int32_t>(NetTypeScoreValue::CELLULAR_VALUE) - NET_VALID_SCORE));
65
66 // mock successed to detect network
67 supplier->SetNetValid(VERIFICATION_STATE);
68
69 ASSERT_TRUE(supplier->GetNetScore() == static_cast<int32_t>(NetTypeScoreValue::CELLULAR_VALUE));
70 ASSERT_TRUE(supplier->GetRealScore() == static_cast<int32_t>(NetTypeScoreValue::CELLULAR_VALUE));
71
72 // quality_poor
73 supplier->SetNetValid(QUALITY_POOR_STATE);
74 ASSERT_TRUE(supplier->GetNetScore() == static_cast<int32_t>(NetTypeScoreValue::CELLULAR_VALUE));
75 ASSERT_TRUE(supplier->GetRealScore() ==
76 static_cast<int32_t>(NetTypeScoreValue::CELLULAR_VALUE) - DIFF_SCORE_BETWEEN_GOOD_POOR);
77 // quality_good
78 supplier->SetNetValid(QUALITY_GOOD_STATE);
79 ASSERT_TRUE(supplier->GetNetScore() == static_cast<int32_t>(NetTypeScoreValue::CELLULAR_VALUE));
80 ASSERT_TRUE(supplier->GetRealScore() ==
81 static_cast<int32_t>(NetTypeScoreValue::CELLULAR_VALUE));
82 }
83
84 HWTEST_F(NetScoreTest, NetSupplierBranchTest, TestSize.Level1)
85 {
86 std::set<NetCap> netCaps{NET_CAPABILITY_MMS, NET_CAPABILITY_INTERNET};
87 std::string ident = "ident";
88 NetBearType bearerType = BEARER_CELLULAR;
89 sptr<NetSupplier> supplier = (std::make_unique<NetSupplier>(bearerType, ident, netCaps)).release();
90
91 HttpProxy httpProxy;
92 supplier->ClearDefault();
93 supplier->UpdateGlobalHttpProxy(httpProxy);
94 uint32_t reqId = 0;
95 supplier->RemoveBestRequest(reqId);
96 supplier->IsConnecting();
97
98 NetLinkInfo netLinkInfo = {};
99 supplier->UpdateNetLinkInfo(netLinkInfo);
100 supplier->GetHttpProxy(httpProxy);
101 supplier->ClearDefault();
102 supplier->UpdateGlobalHttpProxy(httpProxy);
103
104 std::set<NetCap> caps;
105 uint32_t uid = 0;
106 bool ret = supplier->CompareNetCaps(caps);
107 ASSERT_TRUE(ret);
108
109 ret = supplier->HasNetCaps(caps);
110 ASSERT_TRUE(ret);
111
112 ret = supplier->IsConnecting();
113 ASSERT_FALSE(ret);
114
115 supplier->netController_ = nullptr;
116 supplier->netSupplierInfo_.isAvailable_ = true;
117 ret = supplier->SupplierDisconnection(caps, uid);
118 ASSERT_FALSE(ret);
119
120 int32_t result = supplier->GetNetId();
121 EXPECT_EQ(result, INVALID_NET_ID);
122 }
123 } // namespace NetManagerStandard
124 } // namespace OHOS