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 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
113 .Times(1)
114 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
115 int32_t ret = wifiManagerProxy->SetWifiProfile(admin, config);
116 ASSERT_TRUE(ret == ERR_OK);
117 }
118
119 /**
120 * @tc.name: TestSetWifiProfileFail
121 * @tc.desc: Test SetWifiProfile func without enable edm service.
122 * @tc.type: FUNC
123 */
124 HWTEST_F(WifiManagerProxyTest, TestSetWifiProfileFail, TestSize.Level1)
125 {
126 Utils::SetEdmServiceDisable();
127 AppExecFwk::ElementName admin;
128 admin.SetBundleName(ADMIN_PACKAGENAME);
129 Wifi::WifiDeviceConfig config;
130 config.wifiIpConfig.staticIpAddress.ipAddress.address.addressIpv6 = { 0x01 };
131 int32_t ret = wifiManagerProxy->SetWifiProfile(admin, config);
132 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
133 }
134 } // namespace TEST
135 } // namespace EDM
136 } // namespace OHOS
137