• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 #include <chrono>
16 #include <thread>
17 #include <string>
18 #include <iostream>
19 #include <gtest/gtest.h>
20 
21 #include "accesstoken_kit.h"
22 #include "parameter.h"
23 #include "system_ability_definition.h"
24 #include "if_system_ability_manager.h"
25 #include "iservice_registry.h"
26 #include "parcel.h"
27 #include "string_ex.h"
28 #include "device_info_kits.h"
29 #include "device_info_load.h"
30 #include "device_info_proxy.h"
31 #include "idevice_info.h"
32 #include "device_info_stub.h"
33 #include "sysparam_errno.h"
34 #include "deviceinfoservice_ipc_interface_code.h"
35 
36 using namespace testing::ext;
37 using namespace std;
38 using namespace OHOS;
39 
40 int g_tokenType = OHOS::Security::AccessToken::TOKEN_HAP;
41 int g_tokenVerifyResult = 0;
42 namespace OHOS {
43 namespace Security {
44 namespace AccessToken {
GetTokenTypeFlag(AccessTokenID tokenID)45 ATokenTypeEnum AccessTokenKit::GetTokenTypeFlag(AccessTokenID tokenID)
46 {
47     return static_cast<ATokenTypeEnum>(g_tokenType);
48 }
VerifyAccessToken(AccessTokenID tokenID,const std::string & permissionName)49 int AccessTokenKit::VerifyAccessToken(AccessTokenID tokenID, const std::string& permissionName)
50 {
51     return g_tokenVerifyResult;
52 }
53 } // namespace AccessToken
54 } // namespace Security
55 } // namespace OHOS
56 
57 const int UDID_LEN = 65;
58 namespace init_ut {
59 using DeviceInfoServicePtr = OHOS::device_info::DeviceInfoService *;
60 class DeviceInfoUnittest : public testing::Test {
61 public:
DeviceInfoUnittest()62     DeviceInfoUnittest() {};
~DeviceInfoUnittest()63     virtual ~DeviceInfoUnittest() {};
SetUpTestCase(void)64     static void SetUpTestCase(void) {};
TearDownTestCase(void)65     static void TearDownTestCase(void) {};
SetUp()66     void SetUp() {};
TearDown()67     void TearDown() {};
TestBody(void)68     void TestBody(void) {};
GetDeviceInfoService()69     DeviceInfoServicePtr GetDeviceInfoService()
70     {
71         static DeviceInfoServicePtr deviceInfoServicePtr = nullptr;
72         if (deviceInfoServicePtr == nullptr) {
73             deviceInfoServicePtr = new OHOS::device_info::DeviceInfoService(0, true);
74             if (deviceInfoServicePtr == nullptr) {
75                 return nullptr;
76             }
77             deviceInfoServicePtr->OnStart();
78         }
79         return deviceInfoServicePtr;
80     }
81 };
82 
83 HWTEST_F(DeviceInfoUnittest, DevInfoAgentTest, TestSize.Level1)
84 {
85     OHOS::device_info::DeviceInfoKits &kits = OHOS::device_info::DeviceInfoKits::GetInstance();
86     std::string serial = {};
87     int ret = kits.GetSerialID(serial);
88     EXPECT_EQ(ret, SYSPARAM_PERMISSION_DENIED);
89     ret = kits.GetUdid(serial);
90     EXPECT_EQ(ret, SYSPARAM_PERMISSION_DENIED);
91 }
92 
93 HWTEST_F(DeviceInfoUnittest, DevInfoDiedTest, TestSize.Level1)
94 {
95     sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
96     DINFO_CHECK(samgr != nullptr, return, "Get samgr failed");
97     sptr<IRemoteObject> object = samgr->GetSystemAbility(SYSPARAM_DEVICE_SERVICE_ID);
98     DINFO_CHECK(object != nullptr, return, "Get deviceinfo manager object from samgr failed");
99     OHOS::device_info::DeviceInfoKits &kits = OHOS::device_info::DeviceInfoKits::GetInstance();
100     if (kits.GetDeathRecipient() != nullptr) {
101         kits.GetDeathRecipient()->OnRemoteDied(object);
102     }
103     std::string serial = {};
104     int ret = kits.GetSerialID(serial);
105     EXPECT_EQ(ret, SYSPARAM_PERMISSION_DENIED);
106 }
107 
108 HWTEST_F(DeviceInfoUnittest, DevInfoAgentFail, TestSize.Level1)
109 {
110     sptr<OHOS::device_info::DeviceInfoLoad> deviceInfoLoad = new (std::nothrow) OHOS::device_info::DeviceInfoLoad();
111     ASSERT_NE(deviceInfoLoad, nullptr);
112     deviceInfoLoad->OnLoadSystemAbilityFail(SYSPARAM_DEVICE_SERVICE_ID);
113     deviceInfoLoad->OnLoadSystemAbilityFail(SYSPARAM_DEVICE_SERVICE_ID + 1);
114 
115     OHOS::device_info::DeviceInfoKits &kits = OHOS::device_info::DeviceInfoKits::GetInstance();
116     kits.FinishStartSAFailed();
117 }
118 
119 HWTEST_F(DeviceInfoUnittest, DeviceInfoServiceInvalidTokenTest, TestSize.Level1)
120 {
121     string result;
122     DeviceInfoServicePtr deviceInfoService = GetDeviceInfoService();
123     ASSERT_NE(deviceInfoService, nullptr);
124     MessageParcel data;
125     MessageParcel reply;
126     MessageOption option;
127     g_tokenType = OHOS::Security::AccessToken::TOKEN_INVALID;
128     data.WriteInterfaceToken(OHOS::device_info::DeviceInfoStub::GetDescriptor());
129     deviceInfoService->OnRemoteRequest
130         (static_cast<uint32_t> (OHOS::device_info::DeviceInfoInterfaceCode::COMMAND_GET_UDID), data, reply, option);
131 }
132 
133 HWTEST_F(DeviceInfoUnittest, DeviceInfoServiceFailTest, TestSize.Level1)
134 {
135     string result;
136     DeviceInfoServicePtr deviceInfoService = GetDeviceInfoService();
137     ASSERT_NE(deviceInfoService, nullptr);
138     MessageParcel data;
139     MessageParcel reply;
140     MessageOption option;
141     g_tokenType = OHOS::Security::AccessToken::TOKEN_HAP;
142     g_tokenVerifyResult = OHOS::Security::AccessToken::TypePermissionState::PERMISSION_DENIED;
143 
144     // udid
145     data.WriteInterfaceToken(OHOS::device_info::DeviceInfoStub::GetDescriptor());
146     deviceInfoService->OnRemoteRequest
147         (static_cast<uint32_t> (OHOS::device_info::DeviceInfoInterfaceCode::COMMAND_GET_UDID),
148         data, reply, option);
149     // serial
150     data.WriteInterfaceToken(OHOS::device_info::DeviceInfoStub::GetDescriptor());
151     deviceInfoService->OnRemoteRequest
152         (static_cast<uint32_t> (OHOS::device_info::DeviceInfoInterfaceCode::COMMAND_GET_SERIAL_ID),
153         data, reply, option);
154 }
155 
156 HWTEST_F(DeviceInfoUnittest, DeviceInfoServiceTest, TestSize.Level1)
157 {
158     string result;
159     DeviceInfoServicePtr deviceInfoService = GetDeviceInfoService();
160     ASSERT_NE(deviceInfoService, nullptr);
161     MessageParcel data;
162     MessageParcel reply;
163     MessageOption option;
164     g_tokenType = OHOS::Security::AccessToken::TOKEN_HAP;
165     g_tokenVerifyResult = OHOS::Security::AccessToken::TypePermissionState::PERMISSION_GRANTED;
166     data.WriteInterfaceToken(OHOS::device_info::DeviceInfoStub::GetDescriptor());
167     deviceInfoService->OnRemoteRequest
168         (static_cast<uint32_t> (OHOS::device_info::DeviceInfoInterfaceCode::COMMAND_GET_UDID),
169         data, reply, option);
170     data.WriteInterfaceToken(OHOS::device_info::DeviceInfoStub::GetDescriptor());
171     deviceInfoService->OnRemoteRequest
172         (static_cast<uint32_t> (OHOS::device_info::DeviceInfoInterfaceCode::COMMAND_GET_SERIAL_ID),
173         data, reply, option);
174     data.WriteInterfaceToken(OHOS::device_info::DeviceInfoStub::GetDescriptor());
175     deviceInfoService->OnRemoteRequest
176         (static_cast<uint32_t> (OHOS::device_info::DeviceInfoInterfaceCode::COMMAND_GET_SERIAL_ID) + 1,
177         data, reply, option);
178 
179     deviceInfoService->OnRemoteRequest
180         (static_cast<uint32_t> (OHOS::device_info::DeviceInfoInterfaceCode::COMMAND_GET_SERIAL_ID) + 1,
181         data, reply, option);
182     std::this_thread::sleep_for(std::chrono::seconds(3)); // wait sa unload 3s
183     deviceInfoService->GetUdid(result);
184     deviceInfoService->GetSerialID(result);
185     deviceInfoService->OnStop();
186     std::vector<std::u16string> args = {};
187     deviceInfoService->Dump(STDOUT_FILENO, args);
188     deviceInfoService->Dump(-1, args);
189 }
190 
191 HWTEST_F(DeviceInfoUnittest, TestInterface, TestSize.Level1)
192 {
193     char localDeviceId[UDID_LEN] = {0};
194     int ret = AclGetDevUdid(nullptr, UDID_LEN);
195     ASSERT_NE(ret, 0);
196     ret = AclGetDevUdid(localDeviceId, 2); // 2 test
197     ASSERT_NE(ret, 0);
198 
199     ret = AclGetDevUdid(localDeviceId, UDID_LEN);
200     const char *serialNumber = AclGetSerial();
201     EXPECT_NE(nullptr, serialNumber);
202 }
203 
204 HWTEST_F(DeviceInfoUnittest, TestDeviceInfoProxy1, TestSize.Level1)
205 {
206     auto remotePtr = device_info::DeviceInfoKits::GetInstance().GetService();
207     ASSERT_NE(remotePtr, nullptr);
208     auto remote = remotePtr->AsObject();
209     sptr<device_info::DeviceInfoProxy> proxy = new(std::nothrow) device_info::DeviceInfoProxy(remote);
210     ASSERT_NE(proxy, nullptr);
211 
212     device_info::DeviceInfoKits::GetInstance().FinishStartSASuccess(proxy->AsObject());
213     std::string udid;
214     std::string serialId;
215     proxy->GetUdid(udid);
216     proxy->GetSerialID(serialId);
217 
218     char localDeviceId[UDID_LEN] = {0};
219     (void)AclGetDevUdid(localDeviceId, UDID_LEN);
220     const char *serialNumber = AclGetSerial();
221     EXPECT_NE(nullptr, serialNumber);
222 }
223 
224 HWTEST_F(DeviceInfoUnittest, TestDeviceInfoProxy2, TestSize.Level1)
225 {
226     sptr<device_info::DeviceInfoProxy> proxy = new(std::nothrow) device_info::DeviceInfoProxy(nullptr);
227     ASSERT_NE(proxy, nullptr);
228 
229     std::string udid;
230     std::string serialId;
231     proxy->GetUdid(udid);
232     proxy->GetSerialID(serialId);
233 }
234 }  // namespace init_ut
235