1 /*
2  * Copyright (c) 2022-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 "device_info_proxy.h"
21 #include "enterprise_device_mgr_stub_mock.h"
22 #include "edm_sys_manager_mock.h"
23 #include "utils.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 DeviceInfoProxyTest : public testing::Test {
33 protected:
34     void SetUp() override;
35 
36     void TearDown() override;
37 
38     static void TearDownTestSuite(void);
39     std::shared_ptr<DeviceInfoProxy> deviceInfoProxy = nullptr;
40     std::shared_ptr<EdmSysManager> edmSysManager_ = nullptr;
41     sptr<EnterpriseDeviceMgrStubMock> object_ = nullptr;
42 };
43 
SetUp()44 void DeviceInfoProxyTest::SetUp()
45 {
46     deviceInfoProxy = DeviceInfoProxy::GetDeviceInfoProxy();
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 DeviceInfoProxyTest::TearDown()
54 {
55     deviceInfoProxy.reset();
56     edmSysManager_->UnregisterSystemAbilityOfRemoteObject(ENTERPRISE_DEVICE_MANAGER_SA_ID);
57     object_ = nullptr;
58     Utils::SetEdmServiceDisable();
59 }
60 
TearDownTestSuite()61 void DeviceInfoProxyTest::TearDownTestSuite()
62 {
63     ASSERT_FALSE(Utils::GetEdmServiceState());
64     std::cout << "EdmServiceState : " << Utils::GetEdmServiceState() << std::endl;
65 }
66 
67 /**
68  * @tc.name: TestGetDeviceSerialSuc
69  * @tc.desc: Test GetDeviceSerial func.
70  * @tc.type: FUNC
71  */
72 HWTEST_F(DeviceInfoProxyTest, TestGetDeviceSerialSuc, 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::InvokeSendRequestGetPolicy));
79     std::string info;
80     int32_t ret = deviceInfoProxy->GetDeviceSerial(admin, info);
81     ASSERT_TRUE(ret == ERR_OK);
82     ASSERT_TRUE(info == RETURN_STRING);
83 }
84 
85 /**
86  * @tc.name: TestGetDeviceSerialFail
87  * @tc.desc: Test GetDeviceSerial func.
88  * @tc.type: FUNC
89  */
90 HWTEST_F(DeviceInfoProxyTest, TestGetDeviceSerialFail, TestSize.Level1)
91 {
92     Utils::SetEdmServiceDisable();
93     AppExecFwk::ElementName admin;
94     admin.SetBundleName(ADMIN_PACKAGENAME);
95     std::string info;
96     int32_t ret = deviceInfoProxy->GetDeviceSerial(admin, info);
97     ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
98 }
99 
100 /**
101  * @tc.name: TestGetDeviceNameSuc
102  * @tc.desc: Test GetDeviceName func.
103  * @tc.type: FUNC
104  */
105 HWTEST_F(DeviceInfoProxyTest, TestGetDeviceNameSuc, TestSize.Level1)
106 {
107     AppExecFwk::ElementName admin;
108     admin.SetBundleName(ADMIN_PACKAGENAME);
109     EXPECT_CALL(*object_, SendRequest(_, _, _, _))
110         .Times(1)
111         .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestGetPolicy));
112     std::string info;
113     int32_t ret = deviceInfoProxy->GetDeviceName(admin, info);
114     ASSERT_TRUE(ret == ERR_OK);
115     ASSERT_TRUE(info == RETURN_STRING);
116 }
117 
118 /**
119  * @tc.name: TestGetDeviceNameFail
120  * @tc.desc: Test GetDeviceName func.
121  * @tc.type: FUNC
122  */
123 HWTEST_F(DeviceInfoProxyTest, TestGetDeviceNameFail, TestSize.Level1)
124 {
125     Utils::SetEdmServiceDisable();
126     AppExecFwk::ElementName admin;
127     admin.SetBundleName(ADMIN_PACKAGENAME);
128     std::string info;
129     int32_t ret = deviceInfoProxy->GetDeviceName(admin, info);
130     ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
131 }
132 
133 /**
134  * @tc.name: TestGetDisplayVersionSuc
135  * @tc.desc: Test GetDisplayVersion func.
136  * @tc.type: FUNC
137  */
138 HWTEST_F(DeviceInfoProxyTest, TestGetDisplayVersionSuc, TestSize.Level1)
139 {
140     AppExecFwk::ElementName admin;
141     admin.SetBundleName(ADMIN_PACKAGENAME);
142     EXPECT_CALL(*object_, SendRequest(_, _, _, _))
143         .Times(1)
144         .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestGetPolicy));
145     std::string info;
146     int32_t ret = deviceInfoProxy->GetDisplayVersion(admin, info);
147     ASSERT_TRUE(ret == ERR_OK);
148     ASSERT_TRUE(info == RETURN_STRING);
149 }
150 
151 /**
152  * @tc.name: TestGetDisplayVersionFail
153  * @tc.desc: Test GetDisplayVersion func.
154  * @tc.type: FUNC
155  */
156 HWTEST_F(DeviceInfoProxyTest, TestGetDisplayVersionFail, TestSize.Level1)
157 {
158     Utils::SetEdmServiceDisable();
159     AppExecFwk::ElementName admin;
160     admin.SetBundleName(ADMIN_PACKAGENAME);
161     std::string info;
162     int32_t ret = deviceInfoProxy->GetDisplayVersion(admin, info);
163     ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
164 }
165 
166 /**
167  * @tc.name: TestGetDeviceInfoSyncSuc
168  * @tc.desc: Test GetDeviceInfoSync func.
169  * @tc.type: FUNC
170  */
171 HWTEST_F(DeviceInfoProxyTest, TestGetDeviceInfoSyncSuc, TestSize.Level1)
172 {
173     AppExecFwk::ElementName admin;
174     admin.SetBundleName(ADMIN_PACKAGENAME);
175     EXPECT_CALL(*object_, SendRequest(_, _, _, _))
176         .Times(1)
177         .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestGetPolicy));
178     std::string info;
179     int32_t ret = deviceInfoProxy->GetDeviceInfoSync(admin, "device_name", info);
180     ASSERT_TRUE(ret == ERR_OK);
181     ASSERT_TRUE(info == RETURN_STRING);
182 }
183 
184 /**
185  * @tc.name: TestGetDeviceInfoSyncFail
186  * @tc.desc: Test GetDeviceInfoSync func.
187  * @tc.type: FUNC
188  */
189 HWTEST_F(DeviceInfoProxyTest, TestGetDeviceInfoSyncFail, TestSize.Level1)
190 {
191     Utils::SetEdmServiceDisable();
192     AppExecFwk::ElementName admin;
193     admin.SetBundleName(ADMIN_PACKAGENAME);
194     std::string info;
195     int32_t ret = deviceInfoProxy->GetDeviceInfoSync(admin, "device_name", info);
196     ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
197 }
198 } // namespace TEST
199 } // namespace EDM
200 } // namespace OHOS
201