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 "net_activate.h"
19 #include "net_conn_callback_stub.h"
20 #include "net_manager_constants.h"
21
22 namespace OHOS {
23 namespace NetManagerStandard {
24 namespace {
25 using namespace testing::ext;
26 constexpr uint32_t INVALID_NET_CAP = 1000;
27 } // namespace
28
29 class NetCapsTest : public testing::Test {
30 public:
31 static void SetUpTestCase();
32 static void TearDownTestCase();
33 void SetUp();
34 void TearDown();
35
36 static inline std::unique_ptr<NetCaps> instance_ = nullptr;
37 };
38
SetUpTestCase()39 void NetCapsTest::SetUpTestCase()
40 {
41 std::set<NetCap> caps = {NetCap::NET_CAPABILITY_MMS,
42 NetCap::NET_CAPABILITY_NOT_METERED,
43 NetCap::NET_CAPABILITY_INTERNET,
44 NetCap::NET_CAPABILITY_VALIDATED,
45 NetCap::NET_CAPABILITY_CAPTIVE_PORTAL,
46 NetCap::NET_CAPABILITY_INTERNAL_DEFAULT};
47 instance_ = std::make_unique<NetCaps>(caps);
48 }
49
TearDownTestCase()50 void NetCapsTest::TearDownTestCase() {}
51
SetUp()52 void NetCapsTest::SetUp() {}
53
TearDown()54 void NetCapsTest::TearDown() {}
55
56 HWTEST_F(NetCapsTest, ConstructorTest001, TestSize.Level1)
57 {
58 std::unique_ptr<NetCaps> caps = std::make_unique<NetCaps>();
59 EXPECT_NE(caps, nullptr);
60 }
61
62 HWTEST_F(NetCapsTest, ConstructorTest002, TestSize.Level1)
63 {
64 std::set<NetCap> caps = {NetCap::NET_CAPABILITY_MMS,
65 NetCap::NET_CAPABILITY_NOT_METERED,
66 NetCap::NET_CAPABILITY_INTERNET,
67 NetCap::NET_CAPABILITY_NOT_VPN,
68 NetCap::NET_CAPABILITY_VALIDATED,
69 NetCap::NET_CAPABILITY_CAPTIVE_PORTAL,
70 NetCap::NET_CAPABILITY_INTERNAL_DEFAULT};
71 std::unique_ptr<NetCaps> netCaps = std::make_unique<NetCaps>(caps);
72 EXPECT_NE(netCaps, nullptr);
73 }
74
75 HWTEST_F(NetCapsTest, operatorTest001, TestSize.Level1)
76 {
77 NetCaps lCaps;
78 NetCaps rCaps;
79 EXPECT_TRUE(lCaps == rCaps);
80 }
81
82 HWTEST_F(NetCapsTest, IsValidNetCapTest001, TestSize.Level1)
83 {
84 auto result = instance_->IsValidNetCap(NetCap::NET_CAPABILITY_NOT_VPN);
85 EXPECT_TRUE(result);
86 }
87
88 HWTEST_F(NetCapsTest, IsValidNetCapTest002, TestSize.Level1)
89 {
90 auto result = instance_->IsValidNetCap(static_cast<NetCap>(INVALID_NET_CAP));
91 EXPECT_FALSE(result);
92 }
93
94 HWTEST_F(NetCapsTest, InsertNetCapTest001, TestSize.Level1)
95 {
96 instance_->InsertNetCap(NetCap::NET_CAPABILITY_VALIDATED);
97 auto result = instance_->HasNetCap(NetCap::NET_CAPABILITY_VALIDATED);
98 EXPECT_TRUE(result);
99 }
100
101 HWTEST_F(NetCapsTest, InsertNetCapTest002, TestSize.Level1)
102 {
103 instance_->RemoveNetCap(NetCap::NET_CAPABILITY_VALIDATED);
104 auto result = instance_->HasNetCap(NetCap::NET_CAPABILITY_VALIDATED);
105 EXPECT_TRUE(!result);
106 }
107
108 HWTEST_F(NetCapsTest, HasNetCapTest001, TestSize.Level1)
109 {
110 auto result = instance_->HasNetCap(NetCap::NET_CAPABILITY_VALIDATED);
111 EXPECT_TRUE(!result);
112 }
113
114 HWTEST_F(NetCapsTest, HasNetCapTest002, TestSize.Level1)
115 {
116 auto result = instance_->HasNetCap(NetCap::NET_CAPABILITY_NOT_VPN);
117 EXPECT_FALSE(result);
118 }
119
120 HWTEST_F(NetCapsTest, HasNetCapsTest001, TestSize.Level1)
121 {
122 std::set<NetCap> caps;
123 auto result = instance_->HasNetCaps(caps);
124 EXPECT_TRUE(result);
125 }
126
127 HWTEST_F(NetCapsTest, HasNetCapsTest002, TestSize.Level1)
128 {
129 std::set<NetCap> caps = {NetCap::NET_CAPABILITY_NOT_VPN};
130 auto result = instance_->HasNetCaps(caps);
131 EXPECT_FALSE(result);
132 }
133
134 HWTEST_F(NetCapsTest, ToSetTest001, TestSize.Level1)
135 {
136 auto result = instance_->ToSet();
137 EXPECT_FALSE(result.empty());
138 }
139
140 } // namespace NetManagerStandard
141 } // namespace OHOS