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 #include <string>
18 #include <system_ability_definition.h>
19
20 #include "edm_sys_manager_mock.h"
21 #include "enterprise_device_mgr_stub_mock.h"
22 #include "utils.h"
23 #include "wifi_manager_proxy.h"
24
25 using namespace testing::ext;
26 using namespace testing;
27
28 namespace OHOS {
29 namespace EDM {
30 namespace TEST {
31 const std::string ADMIN_PACKAGENAME = "com.edm.test.demo";
32 class WifiManagerProxyTest : public testing::Test {
33 protected:
34 void SetUp() override;
35
36 void TearDown() override;
37
38 static void TearDownTestSuite(void);
39 std::shared_ptr<WifiManagerProxy> wifiManagerProxy = nullptr;
40 std::shared_ptr<EdmSysManager> edmSysManager_ = nullptr;
41 sptr<EnterpriseDeviceMgrStubMock> object_ = nullptr;
42 };
43
SetUp()44 void WifiManagerProxyTest::SetUp()
45 {
46 wifiManagerProxy = WifiManagerProxy::GetWifiManagerProxy();
47 edmSysManager_ = std::make_shared<EdmSysManager>();
48 object_ = new (std::nothrow) EnterpriseDeviceMgrStubMock();
49 edmSysManager_->RegisterSystemAbilityOfRemoteObject(ENTERPRISE_DEVICE_MANAGER_SA_ID, object_);
50 Utils::SetEdmServiceEnable();
51 }
52
TearDown()53 void WifiManagerProxyTest::TearDown()
54 {
55 wifiManagerProxy.reset();
56 edmSysManager_->UnregisterSystemAbilityOfRemoteObject(ENTERPRISE_DEVICE_MANAGER_SA_ID);
57 object_ = nullptr;
58 Utils::SetEdmServiceDisable();
59 }
60
TearDownTestSuite()61 void WifiManagerProxyTest::TearDownTestSuite()
62 {
63 ASSERT_FALSE(Utils::GetEdmServiceState());
64 std::cout << "EdmServiceState : " << Utils::GetEdmServiceState() << std::endl;
65 }
66
67 /**
68 * @tc.name: TestIsWifiActiveSuc
69 * @tc.desc: Test IsWifiActive func.
70 * @tc.type: FUNC
71 */
72 HWTEST_F(WifiManagerProxyTest, TestIsWifiActiveSuc, TestSize.Level1)
73 {
74 AppExecFwk::ElementName admin;
75 admin.SetBundleName(ADMIN_PACKAGENAME);
76 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
77 .Times(1)
78 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeBoolSendRequestGetPolicy));
79 bool isActive = false;
80 int32_t ret = wifiManagerProxy->IsWifiActive(admin, isActive);
81 ASSERT_TRUE(ret == ERR_OK);
82 ASSERT_TRUE(isActive);
83 }
84
85 /**
86 * @tc.name: TestIsWifiActiveFail
87 * @tc.desc: Test IsWifiActive func without enable edm service.
88 * @tc.type: FUNC
89 */
90 HWTEST_F(WifiManagerProxyTest, TestIsWifiActiveFail, TestSize.Level1)
91 {
92 Utils::SetEdmServiceDisable();
93 AppExecFwk::ElementName admin;
94 admin.SetBundleName(ADMIN_PACKAGENAME);
95 bool isActive = false;
96 int32_t ret = wifiManagerProxy->IsWifiActive(admin, isActive);
97 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
98 ASSERT_FALSE(isActive);
99 }
100
101 /**
102 * @tc.name: TestSetWifiProfileSuc
103 * @tc.desc: Test SetWifiProfile success func.
104 * @tc.type: FUNC
105 */
106 HWTEST_F(WifiManagerProxyTest, TestSetWifiProfileSuc, TestSize.Level1)
107 {
108 AppExecFwk::ElementName admin;
109 admin.SetBundleName(ADMIN_PACKAGENAME);
110 Wifi::WifiDeviceConfig config;
111 config.wifiIpConfig.staticIpAddress.ipAddress.address.addressIpv6 = { 0x01 };
112 WifiPassword pwd;
113 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
114 .Times(1)
115 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
116 int32_t ret = wifiManagerProxy->SetWifiProfile(admin, config, pwd);
117 ASSERT_TRUE(ret == ERR_OK);
118 }
119
120 /**
121 * @tc.name: TestSetWifiProfileFail
122 * @tc.desc: Test SetWifiProfile func without enable edm service.
123 * @tc.type: FUNC
124 */
125 HWTEST_F(WifiManagerProxyTest, TestSetWifiProfileFail, TestSize.Level1)
126 {
127 Utils::SetEdmServiceDisable();
128 AppExecFwk::ElementName admin;
129 admin.SetBundleName(ADMIN_PACKAGENAME);
130 Wifi::WifiDeviceConfig config;
131 config.wifiIpConfig.staticIpAddress.ipAddress.address.addressIpv6 = { 0x01 };
132 WifiPassword pwd;
133 int32_t ret = wifiManagerProxy->SetWifiProfile(admin, config, pwd);
134 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
135 }
136
137 /**
138 * @tc.name: TestSetWifiDisabledSuc
139 * @tc.desc: Test SetWifiDisabled func.
140 * @tc.type: FUNC
141 */
142 HWTEST_F(WifiManagerProxyTest, TestSetWifiDisabledSuc, TestSize.Level1)
143 {
144 AppExecFwk::ElementName admin;
145 admin.SetBundleName(ADMIN_PACKAGENAME);
146 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
147 .Times(1)
148 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
149 bool isDisable = true;
150 int32_t ret = wifiManagerProxy->SetWifiDisabled(admin, isDisable);
151 ASSERT_TRUE(ret == ERR_OK);
152 }
153
154 /**
155 * @tc.name: TestSetWifiDisabledFail
156 * @tc.desc: Test SetWifiDisabled func without enable edm service.
157 * @tc.type: FUNC
158 */
159 HWTEST_F(WifiManagerProxyTest, TestSetWifiDisabledFail, TestSize.Level1)
160 {
161 Utils::SetEdmServiceDisable();
162 AppExecFwk::ElementName admin;
163 admin.SetBundleName(ADMIN_PACKAGENAME);
164 bool isDisable = true;
165 int32_t ret = wifiManagerProxy->SetWifiDisabled(admin, isDisable);
166 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
167 }
168
169 /**
170 * @tc.name: TestIsWifiDisabledSuc
171 * @tc.desc: Test IsWifiDisabled func.
172 * @tc.type: FUNC
173 */
174 HWTEST_F(WifiManagerProxyTest, TestIsWifiDisabledSuc, TestSize.Level1)
175 {
176 AppExecFwk::ElementName admin;
177 admin.SetBundleName(ADMIN_PACKAGENAME);
178 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
179 .Times(1)
180 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeBoolSendRequestGetPolicy));
181 bool isDisable = false;
182 int32_t ret = wifiManagerProxy->IsWifiDisabled(&admin, isDisable);
183 ASSERT_TRUE(ret == ERR_OK);
184 ASSERT_TRUE(isDisable);
185 }
186
187 /**
188 * @tc.name: TestIsWifiDisabledFail
189 * @tc.desc: Test IsWifiDisabled func without enable edm service.
190 * @tc.type: FUNC
191 */
192 HWTEST_F(WifiManagerProxyTest, TestIsWifiDisabledFail, TestSize.Level1)
193 {
194 Utils::SetEdmServiceDisable();
195 AppExecFwk::ElementName admin;
196 admin.SetBundleName(ADMIN_PACKAGENAME);
197 bool isDisable = false;
198 int32_t ret = wifiManagerProxy->IsWifiDisabled(&admin, isDisable);
199 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
200 ASSERT_FALSE(isDisable);
201 }
202 } // namespace TEST
203 } // namespace EDM
204 } // namespace OHOS
205