• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "device_info_plugin_test.h"
17 
18 #include "edm_data_ability_utils_mock.h"
19 #include "plugin_singleton.h"
20 #include "utils.h"
21 
22 using namespace testing;
23 using namespace testing::ext;
24 
25 namespace OHOS {
26 namespace EDM {
27 namespace TEST {
SetUpTestSuite(void)28 void DeviceInfoPluginTest::SetUpTestSuite(void)
29 {
30     Utils::SetEdmInitialEnv();
31 }
32 
TearDownTestSuite(void)33 void DeviceInfoPluginTest::TearDownTestSuite(void)
34 {
35     Utils::ResetTokenTypeAndUid();
36     ASSERT_TRUE(Utils::IsOriginalUTEnv());
37     std::cout << "now ut process is orignal ut env : " << Utils::IsOriginalUTEnv() << std::endl;
38 }
39 
40 /**
41  * @tc.name: TestGetDeviceInfoSyncWithDeviceName
42  * @tc.desc: Test GetDeviceInfoPlugin::OnGetPolicy function.
43  * @tc.type: FUNC
44  */
45 HWTEST_F(DeviceInfoPluginTest, TestGetDeviceInfoSyncWithDeviceName, TestSize.Level1)
46 {
47     std::string policyData;
48     MessageParcel data;
49     MessageParcel reply;
50     data.WriteString(EdmConstants::DeviceInfo::DEVICE_NAME);
51 
52     EXPECT_CALL(*pluginMock_, GetExternalManagerFactory).WillRepeatedly(DoAll(Return(factoryMock_)));
53     EXPECT_CALL(*factoryMock_, CreateOsAccountManager).WillRepeatedly(DoAll(Return(osAccountMgrMock_)));
54     std::vector<int32_t> ids = {DEFAULT_USER_ID};
55     EXPECT_CALL(*osAccountMgrMock_, QueryActiveOsAccountIds).WillOnce(DoAll(SetArgReferee<0>(ids), Return(ERR_OK)));
56 
57     EdmDataAbilityUtils::SetResult("test Failed");
58     ErrCode ret = pluginMock_->OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
59     ASSERT_TRUE(ret == EdmReturnErrCode::SYSTEM_ABNORMALLY);
60 }
61 
62 /**
63  * @tc.name: TestGetDeviceInfoSyncWithDeviceNameEmpty
64  * @tc.desc: Test OnPolicy function.
65  * @tc.type: FUNC
66  */
67 HWTEST_F(DeviceInfoPluginTest, TestGetDeviceInfoSyncWithDeviceNameEmpty, TestSize.Level1)
68 {
69     std::string policyValue;
70     MessageParcel data;
71     MessageParcel reply;
72     data.WriteString(EdmConstants::DeviceInfo::DEVICE_NAME);
73 
74     EXPECT_CALL(*pluginMock_, GetExternalManagerFactory).WillRepeatedly(DoAll(Return(factoryMock_)));
75     EXPECT_CALL(*factoryMock_, CreateOsAccountManager).WillRepeatedly(DoAll(Return(osAccountMgrMock_)));
76     std::vector<int32_t> ids = {DEFAULT_USER_ID};
77     EXPECT_CALL(*osAccountMgrMock_, QueryActiveOsAccountIds).WillOnce(DoAll(SetArgReferee<0>(ids), Return(ERR_OK)));
78 
79     EdmDataAbilityUtils::SetResult("test value nullptr");
80     ErrCode code = pluginMock_->OnGetPolicy(policyValue, data, reply, DEFAULT_USER_ID);
81     EXPECT_TRUE(code == ERR_OK);
82 }
83 
84 /**
85  * @tc.name: TestGetDeviceInfoSyncWithDeviceNameSuc
86  * @tc.desc: Test OnPolicy function.
87  * @tc.type: FUNC
88  */
89 HWTEST_F(DeviceInfoPluginTest, TestGetDeviceInfoSyncWithDeviceNameSuc, TestSize.Level1)
90 {
91     std::string policyValue;
92     MessageParcel data;
93     MessageParcel reply;
94     data.WriteString(EdmConstants::DeviceInfo::DEVICE_NAME);
95 
96     EXPECT_CALL(*pluginMock_, GetExternalManagerFactory).WillRepeatedly(DoAll(Return(factoryMock_)));
97     EXPECT_CALL(*factoryMock_, CreateOsAccountManager).WillRepeatedly(DoAll(Return(osAccountMgrMock_)));
98     std::vector<int32_t> ids = {DEFAULT_USER_ID};
99     EXPECT_CALL(*osAccountMgrMock_, QueryActiveOsAccountIds).WillOnce(DoAll(SetArgReferee<0>(ids), Return(ERR_OK)));
100 
101     EdmDataAbilityUtils::SetResult("test success");
102     ErrCode code = pluginMock_->OnGetPolicy(policyValue, data, reply, DEFAULT_USER_ID);
103     EXPECT_TRUE(code == ERR_OK);
104 }
105 
106 /**
107  * @tc.name: TestGetDeviceInfoSyncWithQueryCurrentIdFailed
108  * @tc.desc: Test OnPolicy function.
109  * @tc.type: FUNC
110  */
111 HWTEST_F(DeviceInfoPluginTest, TestGetDeviceInfoSyncWithQueryCurrentIdFailed, TestSize.Level1)
112 {
113     std::string policyValue;
114     MessageParcel data;
115     MessageParcel reply;
116     data.WriteString(EdmConstants::DeviceInfo::DEVICE_NAME);
117 
118     EXPECT_CALL(*pluginMock_, GetExternalManagerFactory).WillRepeatedly(DoAll(Return(factoryMock_)));
119     EXPECT_CALL(*factoryMock_, CreateOsAccountManager).WillRepeatedly(DoAll(Return(osAccountMgrMock_)));
120     EXPECT_CALL(*osAccountMgrMock_, QueryActiveOsAccountIds).WillOnce(DoAll(Return(-1)));
121 
122     ErrCode code = pluginMock_->OnGetPolicy(policyValue, data, reply, DEFAULT_USER_ID);
123     EXPECT_TRUE(code == EdmReturnErrCode::SYSTEM_ABNORMALLY);
124 }
125 
126 /**
127  * @tc.name: TestGetDeviceInfoSyncWithAccountIdsEmpty
128  * @tc.desc: Test OnPolicy function.
129  * @tc.type: FUNC
130  */
131 HWTEST_F(DeviceInfoPluginTest, TestGetDeviceInfoSyncWithAccountIdsEmpty, TestSize.Level1)
132 {
133     std::string policyValue;
134     MessageParcel data;
135     MessageParcel reply;
136     data.WriteString(EdmConstants::DeviceInfo::DEVICE_NAME);
137 
138     EXPECT_CALL(*pluginMock_, GetExternalManagerFactory).WillRepeatedly(DoAll(Return(factoryMock_)));
139     EXPECT_CALL(*factoryMock_, CreateOsAccountManager).WillRepeatedly(DoAll(Return(osAccountMgrMock_)));
140     std::vector<int32_t> ids = {};
141     EXPECT_CALL(*osAccountMgrMock_, QueryActiveOsAccountIds).WillOnce(DoAll(SetArgReferee<0>(ids), Return(ERR_OK)));
142 
143     ErrCode code = pluginMock_->OnGetPolicy(policyValue, data, reply, DEFAULT_USER_ID);
144     EXPECT_TRUE(code == EdmReturnErrCode::SYSTEM_ABNORMALLY);
145 }
146 
147 /**
148  * @tc.name: TestGetDeviceInfoSyncWithDeviceSerial
149  * @tc.desc: Test GetDeviceInfoPlugin::OnGetPolicy function.
150  * @tc.type: FUNC
151  */
152 HWTEST_F(DeviceInfoPluginTest, TestGetDeviceInfoSyncWithDeviceSerial, TestSize.Level1)
153 {
154     plugin_ = GetDeviceInfoPlugin::GetPlugin();
155     std::string policyData;
156     MessageParcel data;
157     MessageParcel reply;
158     data.WriteString(EdmConstants::DeviceInfo::DEVICE_SERIAL);
159     ErrCode ret = plugin_->OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
160     ASSERT_TRUE(ret == ERR_OK);
161 }
162 
163 /**
164  * @tc.name: TestGetDeviceInfoSyncWithSimInfo
165  * @tc.desc: Test GetDeviceInfoPlugin::OnGetPolicy function.
166  * @tc.type: FUNC
167  */
168 HWTEST_F(DeviceInfoPluginTest, TestGetDeviceInfoSyncWithSimInfo, TestSize.Level1)
169 {
170     plugin_ = GetDeviceInfoPlugin::GetPlugin();
171     std::string policyData;
172     MessageParcel data;
173     MessageParcel reply;
174     data.WriteString(EdmConstants::DeviceInfo::SIM_INFO);
175     ErrCode ret = plugin_->OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
176 #ifdef TELEPHONY_CORE_EDM_ENABLE_TEST
177     ASSERT_TRUE(ret == ERR_OK);
178 #else
179     ASSERT_TRUE(ret == EdmReturnErrCode::INTERFACE_UNSUPPORTED);
180 #endif
181 }
182 
183 /**
184  * @tc.name: TestGetDeviceInfoSyncWithInvalidLabel
185  * @tc.desc: Test GetDeviceInfoPlugin::OnGetPolicy function.
186  * @tc.type: FUNC
187  */
188 HWTEST_F(DeviceInfoPluginTest, TestGetDeviceInfoSyncWithInvalidLabel, TestSize.Level1)
189 {
190     plugin_ = GetDeviceInfoPlugin::GetPlugin();
191     std::string policyData;
192     MessageParcel data;
193     MessageParcel reply;
194     data.WriteString("invalid");
195     ErrCode ret = plugin_->OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
196     ASSERT_TRUE(ret == EdmReturnErrCode::INTERFACE_UNSUPPORTED);
197 }
198 } // namespace TEST
199 } // namespace EDM
200 } // namespace OHOS