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::ext;
23 
24 namespace OHOS {
25 namespace EDM {
26 namespace TEST {
SetUpTestSuite(void)27 void DeviceInfoPluginTest::SetUpTestSuite(void)
28 {
29     Utils::SetEdmInitialEnv();
30 }
31 
TearDownTestSuite(void)32 void DeviceInfoPluginTest::TearDownTestSuite(void)
33 {
34     Utils::ResetTokenTypeAndUid();
35     ASSERT_TRUE(Utils::IsOriginalUTEnv());
36     std::cout << "now ut process is orignal ut env : " << Utils::IsOriginalUTEnv() << std::endl;
37 }
38 
39 /**
40  * @tc.name: TestGetDisplayVersion
41  * @tc.desc: Test GetDisplayVersionPlugin::OnGetPolicy function.
42  * @tc.type: FUNC
43  */
44 HWTEST_F(DeviceInfoPluginTest, TestGetDisplayVersion, TestSize.Level1)
45 {
46     plugin_ = GetDisplayVersionPlugin::GetPlugin();
47     std::string policyData{"TestString"};
48     MessageParcel data;
49     MessageParcel reply;
50     ErrCode ret = plugin_->OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
51     ASSERT_TRUE(ret == ERR_OK);
52 }
53 
54 /**
55  * @tc.name: TestGetDeviceInfoSyncWithDeviceName
56  * @tc.desc: Test GetDeviceInfoPlugin::OnGetPolicy function.
57  * @tc.type: FUNC
58  */
59 HWTEST_F(DeviceInfoPluginTest, TestGetDeviceInfoSyncWithDeviceName, TestSize.Level1)
60 {
61     plugin_ = GetDeviceInfoPlugin::GetPlugin();
62     std::string policyData;
63     MessageParcel data;
64     MessageParcel reply;
65     data.WriteString(EdmConstants::DeviceInfo::DEVICE_NAME);
66     EdmDataAbilityUtils::SetResult("test Failed");
67     ErrCode ret = plugin_->OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
68     ASSERT_TRUE(ret == EdmReturnErrCode::SYSTEM_ABNORMALLY);
69 }
70 
71 /**
72  * @tc.name: TestGetDeviceInfoSyncWithDeviceNameEmpty
73  * @tc.desc: Test OnPolicy function.
74  * @tc.type: FUNC
75  */
76 HWTEST_F(DeviceInfoPluginTest, TestGetDeviceInfoSyncWithDeviceNameEmpty, TestSize.Level1)
77 {
78     std::shared_ptr<IPlugin> plugin = GetDeviceInfoPlugin::GetPlugin();
79     std::string policyValue;
80     MessageParcel data;
81     MessageParcel reply;
82     data.WriteString(EdmConstants::DeviceInfo::DEVICE_NAME);
83     EdmDataAbilityUtils::SetResult("test value nullptr");
84     ErrCode code = plugin->OnGetPolicy(policyValue, data, reply, DEFAULT_USER_ID);
85     EXPECT_TRUE(code == ERR_OK);
86 }
87 
88 /**
89  * @tc.name: TestGetDeviceInfoSyncWithDeviceNameSuc
90  * @tc.desc: Test OnPolicy function.
91  * @tc.type: FUNC
92  */
93 HWTEST_F(DeviceInfoPluginTest, TestGetDeviceInfoSyncWithDeviceNameSuc, TestSize.Level1)
94 {
95     std::shared_ptr<IPlugin> plugin = GetDeviceInfoPlugin::GetPlugin();
96     std::string policyValue;
97     MessageParcel data;
98     MessageParcel reply;
99     data.WriteString(EdmConstants::DeviceInfo::DEVICE_NAME);
100     EdmDataAbilityUtils::SetResult("test success");
101     ErrCode code = plugin->OnGetPolicy(policyValue, data, reply, DEFAULT_USER_ID);
102     EXPECT_TRUE(code == ERR_OK);
103 }
104 
105 /**
106  * @tc.name: TestGetDeviceInfoSyncWithDeviceSerial
107  * @tc.desc: Test GetDeviceInfoPlugin::OnGetPolicy function.
108  * @tc.type: FUNC
109  */
110 HWTEST_F(DeviceInfoPluginTest, TestGetDeviceInfoSyncWithDeviceSerial, TestSize.Level1)
111 {
112     plugin_ = GetDeviceInfoPlugin::GetPlugin();
113     std::string policyData;
114     MessageParcel data;
115     MessageParcel reply;
116     data.WriteString(EdmConstants::DeviceInfo::DEVICE_SERIAL);
117     ErrCode ret = plugin_->OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
118     ASSERT_TRUE(ret == ERR_OK);
119 }
120 
121 /**
122  * @tc.name: TestGetDeviceInfoSyncWithSimInfo
123  * @tc.desc: Test GetDeviceInfoPlugin::OnGetPolicy function.
124  * @tc.type: FUNC
125  */
126 HWTEST_F(DeviceInfoPluginTest, TestGetDeviceInfoSyncWithSimInfo, TestSize.Level1)
127 {
128     plugin_ = GetDeviceInfoPlugin::GetPlugin();
129     std::string policyData;
130     MessageParcel data;
131     MessageParcel reply;
132     data.WriteString(EdmConstants::DeviceInfo::SIM_INFO);
133     ErrCode ret = plugin_->OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
134 #ifdef TELEPHONY_CORE_EDM_ENABLE_TEST
135     ASSERT_TRUE(ret == ERR_OK);
136 #else
137     ASSERT_TRUE(ret == EdmReturnErrCode::INTERFACE_UNSUPPORTED);
138 #endif
139 }
140 
141 /**
142  * @tc.name: TestGetDeviceInfoSyncWithInvalidLabel
143  * @tc.desc: Test GetDeviceInfoPlugin::OnGetPolicy function.
144  * @tc.type: FUNC
145  */
146 HWTEST_F(DeviceInfoPluginTest, TestGetDeviceInfoSyncWithInvalidLabel, TestSize.Level1)
147 {
148     plugin_ = GetDeviceInfoPlugin::GetPlugin();
149     std::string policyData;
150     MessageParcel data;
151     MessageParcel reply;
152     data.WriteString("invalid");
153     ErrCode ret = plugin_->OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
154     ASSERT_TRUE(ret == EdmReturnErrCode::INTERFACE_UNSUPPORTED);
155 }
156 } // namespace TEST
157 } // namespace EDM
158 } // namespace OHOS