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 "usb_common_test.h"
17 #include "usb_errors.h"
18 #include "hilog_wrapper.h"
19
20 using namespace OHOS::Security::AccessToken;
21
22 namespace OHOS {
23 namespace USB {
24 namespace Common {
25
SetTestCaseNative(TokenInfoParams * infoInstance)26 void UsbCommonTest::SetTestCaseNative(TokenInfoParams *infoInstance)
27 {
28 uint64_t tokenId = GetAccessTokenId(infoInstance);
29 int ret = SetSelfTokenID(tokenId);
30 if (ret == 0) {
31 USB_HILOGI(MODULE_USB_SERVICE, "SetSelfTokenID success %{public}d", __LINE__);
32 } else {
33 USB_HILOGE(MODULE_USB_SERVICE, "SetSelfTokenID fail %{public}d", ret);
34 }
35 ret = AccessTokenKit::ReloadNativeTokenInfo();
36 if (ret == 0) {
37 USB_HILOGI(MODULE_USB_SERVICE, "ReloadNativeTokenInfo success %{public}d", __LINE__);
38 } else {
39 USB_HILOGE(MODULE_USB_SERVICE, "ReloadNativeTokenInfo fail %{public}d", ret);
40 }
41 }
42
GrantPermissionSysNative()43 void UsbCommonTest::GrantPermissionSysNative()
44 {
45 const char **permsInfo = new(std::nothrow)const char* [1];
46 permsInfo[0] = "ohos.permission.MANAGE_USB_CONFIG";
47 TokenInfoParams g_sysInfoInstance = {
48 .dcapsNum = 0,
49 .permsNum = 1,
50 .aclsNum = 0,
51 .dcaps = nullptr,
52 .perms = permsInfo,
53 .acls = nullptr,
54 .processName = "usb_manager_test_sys_with_perms",
55 .aplStr = "system_basic",
56 };
57 SetTestCaseNative(&g_sysInfoInstance);
58 }
59
GrantPermissionNormalNative()60 void UsbCommonTest::GrantPermissionNormalNative()
61 {
62 TokenInfoParams g_normalInfoInstance = {
63 .dcapsNum = 0,
64 .permsNum = 0,
65 .aclsNum = 0,
66 .dcaps = nullptr,
67 .perms = nullptr,
68 .acls = nullptr,
69 .processName = "usb_manager_test_normal",
70 .aplStr = "normal",
71 };
72 SetTestCaseNative(&g_normalInfoInstance);
73 }
74
GrantNormalPermissionNative()75 void UsbCommonTest::GrantNormalPermissionNative()
76 {
77 const char **permsInfo = new(std::nothrow)const char* [1];
78 permsInfo[0] = "ohos.permission.MANAGE_USB_CONFIG";
79 TokenInfoParams g_normalInfoInstance = {
80 .dcapsNum = 0,
81 .permsNum = 1,
82 .aclsNum = 0,
83 .dcaps = nullptr,
84 .perms = permsInfo,
85 .acls = nullptr,
86 .processName = "usb_manager_test_normal_with_perms",
87 .aplStr = "normal",
88 };
89 SetTestCaseNative(&g_normalInfoInstance);
90 }
91
GrantSysNoPermissionNative()92 void UsbCommonTest::GrantSysNoPermissionNative()
93 {
94 TokenInfoParams g_sysInfoInstance = {
95 .dcapsNum = 0,
96 .permsNum = 0,
97 .aclsNum = 0,
98 .dcaps = nullptr,
99 .perms = nullptr,
100 .acls = nullptr,
101 .processName = "usb_manager_test_sys",
102 .aplStr = "system_basic",
103 };
104 SetTestCaseNative(&g_sysInfoInstance);
105 }
106
SwitchErrCode(int32_t ret)107 int32_t UsbCommonTest::SwitchErrCode(int32_t ret)
108 {
109 return ret == UEC_SERVICE_NOT_SUPPORT_SWITCH_PORT ? UEC_OK : ret;
110 }
111
112 } // namespace Common
113 } // namespace USB
114 } // namespace OHOS
115