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