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 #include "disabled_network_interface_plugin.h"
19 #include "edm_ipc_interface_code.h"
20 #include "get_all_network_interfaces_plugin.h"
21 #include "get_ip_address_plugin.h"
22 #include "get_mac_plugin.h"
23 #include "iplugin_manager.h"
24 #include "map_string_serializer.h"
25 #include "plugin_singleton.h"
26 #include "utils.h"
27
28 using namespace testing::ext;
29 using namespace testing;
30
31 namespace OHOS {
32 namespace EDM {
33 namespace TEST {
34 const std::string VALID_NETWORK_INTERFACE = "lo";
35 const std::string INVALID_NETWORK_INTERFACE = "fail";
36
37 class NetworkManagerPluginTest : public testing::Test {
38 protected:
39 static void SetUpTestSuite(void);
40
41 static void TearDownTestSuite(void);
42 };
43
SetUpTestSuite(void)44 void NetworkManagerPluginTest::SetUpTestSuite(void)
45 {
46 Utils::SetEdmInitialEnv();
47 }
48
TearDownTestSuite(void)49 void NetworkManagerPluginTest::TearDownTestSuite(void)
50 {
51 Utils::ResetTokenTypeAndUid();
52 ASSERT_TRUE(Utils::IsOriginalUTEnv());
53 std::cout << "now ut process is orignal ut env : " << Utils::IsOriginalUTEnv() << std::endl;
54 }
55
56 /**
57 * @tc.name: TestGetAllNetworkInterfaces
58 * @tc.desc: Test GetAllNetworkInterfacesPlugin.
59 * @tc.type: FUNC
60 */
61 HWTEST_F(NetworkManagerPluginTest, TestGetAllNetworkInterfaces, TestSize.Level1)
62 {
63 std::shared_ptr<IPlugin> plugin = GetAllNetworkInterfacesPlugin::GetPlugin();
64 std::string policyData{"TestGetAllNetworkInterfaces"};
65 MessageParcel data;
66 MessageParcel reply;
67 ErrCode ret = plugin->OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
68 ASSERT_TRUE(ret == ERR_OK);
69 }
70
71 /**
72 * @tc.name: TestGetIpAddress
73 * @tc.desc: Test GetIpAddressPlugin.
74 * @tc.type: FUNC
75 */
76 HWTEST_F(NetworkManagerPluginTest, TestGetIpAddress, TestSize.Level1)
77 {
78 std::shared_ptr<IPlugin> plugin = GetIpAddressPlugin::GetPlugin();
79 std::string policyData{"TestGetIpAddress"};
80 MessageParcel data;
81 MessageParcel reply;
82 ErrCode ret = plugin->OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
83 ASSERT_TRUE(ret == ERR_OK);
84 }
85
86 /**
87 * @tc.name: TestGetMac
88 * @tc.desc: Test GetMacPlugin.
89 * @tc.type: FUNC
90 */
91 HWTEST_F(NetworkManagerPluginTest, TestGetMac, TestSize.Level1)
92 {
93 std::shared_ptr<IPlugin> plugin = GetMacPlugin::GetPlugin();
94 std::string policyData{"TestGetMac"};
95 MessageParcel data;
96 MessageParcel reply;
97 ErrCode ret = plugin->OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
98 ASSERT_TRUE(ret == ERR_OK);
99 }
100
101 /**
102 * @tc.name: TestIsNetworkInterfaceDisabled
103 * @tc.desc: Test IsNetworkInterfaceDisabled fail.
104 * @tc.type: FUNC
105 */
106 HWTEST_F(NetworkManagerPluginTest, TestIsNetworkInterfaceDisabledFail, TestSize.Level1)
107 {
108 std::shared_ptr<IPlugin> plugin = DisabledNetworkInterfacePlugin::GetPlugin();
109 std::string policyData;
110 MessageParcel data;
111 MessageParcel reply;
112 // NetworkInterface is not exist.
113 data.WriteString(INVALID_NETWORK_INTERFACE);
114 ErrCode ret = plugin->OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
115 ASSERT_TRUE(ret == EdmReturnErrCode::PARAM_ERROR);
116 }
117
118 /**
119 * @tc.name: TestIsNetworkInterfaceDisabled
120 * @tc.desc: Test IsNetworkInterfaceDisabled success.
121 * @tc.type: FUNC
122 */
123 HWTEST_F(NetworkManagerPluginTest, TestIsNetworkInterfaceDisabledSuc, TestSize.Level1)
124 {
125 std::shared_ptr<IPlugin> plugin = DisabledNetworkInterfacePlugin::GetPlugin();
126 std::string policyData;
127 MessageParcel data;
128 MessageParcel reply;
129 // NetworkInterface is exist.
130 data.WriteString(VALID_NETWORK_INTERFACE);
131 ErrCode ret = plugin->OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
132 ASSERT_TRUE(ret == ERR_OK);
133 }
134
135 /**
136 * @tc.name: TestNetworkInterfaceNotExist
137 * @tc.desc: Test SetNetworkInterfaceDisabled when NetworkInterface is not exist.
138 * @tc.type: FUNC
139 */
140 HWTEST_F(NetworkManagerPluginTest, TestNetworkInterfaceNotExist, TestSize.Level1)
141 {
142 std::shared_ptr<IPlugin> plugin = DisabledNetworkInterfacePlugin::GetPlugin();
143 uint32_t code = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::DISABLED_NETWORK_INTERFACE);
144 // data is empty.
145 MessageParcel data;
146 MessageParcel reply;
147 HandlePolicyData handlePolicyData{"", "", false};
148 ErrCode ret = plugin->OnHandlePolicy(code, data, reply, handlePolicyData, DEFAULT_USER_ID);
149 ASSERT_TRUE(ret == EdmReturnErrCode::PARAM_ERROR);
150 // NetworkInterface is invalid.
151 std::vector<std::string> key { INVALID_NETWORK_INTERFACE };
152 std::vector<std::string> value { "true" };
153 data.WriteStringVector(key);
154 data.WriteStringVector(value);
155 ret = plugin->OnHandlePolicy(code, data, reply, handlePolicyData, DEFAULT_USER_ID);
156 ASSERT_TRUE(ret == EdmReturnErrCode::PARAM_ERROR);
157 }
158
159 /**
160 * @tc.name: TestNetworkInterfaceDisabled
161 * @tc.desc: Test SetNetworkInterfaceDisabled when set network interface disabled.
162 * @tc.type: FUNC
163 */
164 HWTEST_F(NetworkManagerPluginTest, TestNetworkInterfaceDisabled, TestSize.Level1)
165 {
166 std::shared_ptr<IPlugin> plugin = DisabledNetworkInterfacePlugin::GetPlugin();
167 uint32_t code = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::DISABLED_NETWORK_INTERFACE);
168 MessageParcel data;
169 MessageParcel reply;
170 std::string policyStr;
171 std::map<std::string, std::string> policyMap;
172 policyMap[VALID_NETWORK_INTERFACE] = "false";
173 MapStringSerializer::GetInstance()->Serialize(policyMap, policyStr);
174 HandlePolicyData handlePolicyData {policyStr, "", false};
175 // set network interface disabled.
176 std::vector<std::string> key { VALID_NETWORK_INTERFACE };
177 std::vector<std::string> value { "true" };
178 data.WriteStringVector(key);
179 data.WriteStringVector(value);
180 ErrCode ret = plugin->OnHandlePolicy(code, data, reply, handlePolicyData, DEFAULT_USER_ID);
181 ASSERT_TRUE(ret == ERR_OK);
182 std::map<std::string, std::string> policyMap1;
183 MapStringSerializer::GetInstance()->Deserialize(handlePolicyData.policyData, policyMap1);
184 ASSERT_EQ(policyMap1[VALID_NETWORK_INTERFACE], "true");
185 ASSERT_TRUE(handlePolicyData.isChanged);
186 }
187
188 /**
189 * @tc.name: TestNetworkInterfaceDisabledFalse
190 * @tc.desc: Test SetNetworkInterfaceDisabled when set network interface disabled.
191 * @tc.type: FUNC
192 */
193 HWTEST_F(NetworkManagerPluginTest, TestNetworkInterfaceDisabledFalse, TestSize.Level1)
194 {
195 std::shared_ptr<IPlugin> plugin = DisabledNetworkInterfacePlugin::GetPlugin();
196 uint32_t code = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::DISABLED_NETWORK_INTERFACE);
197 MessageParcel data;
198 MessageParcel reply;
199 std::string policyStr;
200 std::map<std::string, std::string> policyMap;
201 policyMap[VALID_NETWORK_INTERFACE] = "true";
202 MapStringSerializer::GetInstance()->Serialize(policyMap, policyStr);
203 HandlePolicyData handlePolicyData {policyStr, "", false};
204 // set network interface disabled.
205 std::vector<std::string> key { VALID_NETWORK_INTERFACE };
206 std::vector<std::string> value { "false" };
207 data.WriteStringVector(key);
208 data.WriteStringVector(value);
209 ErrCode ret = plugin->OnHandlePolicy(code, data, reply, handlePolicyData, DEFAULT_USER_ID);
210 ASSERT_TRUE(ret == ERR_OK);
211 std::map<std::string, std::string> policyMap1;
212 MapStringSerializer::GetInstance()->Deserialize(handlePolicyData.policyData, policyMap1);
213 ASSERT_TRUE(policyMap1.empty());
214 ASSERT_TRUE(handlePolicyData.isChanged);
215 }
216 } // namespace TEST
217 } // namespace EDM
218 } // namespace OHOS