1 /*
2 * Copyright (c) 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 <unistd.h>
17 #include "iostream"
18 #include "hilog_wrapper.h"
19 #include "string_ex.h"
20 #include "iservice_registry.h"
21 #include "accesstoken_kit.h"
22 #include "token_setproc.h"
23 #include "nativetoken_kit.h"
24 #include "driver_extension_controller.h"
25 namespace OHOS {
26 namespace ExternalDeviceManager {
27 using namespace std;
28 using namespace OHOS::Security::AccessToken;
29 constexpr uint32_t PARAM_COUNT_NEED = 4;
30 constexpr uint32_t PARAM_INDEX_OF_ACTION = 1;
31 constexpr uint32_t PARAM_INDEX_OF_BUNDLENAME = 2;
32 constexpr uint32_t PARAM_INDEX_OF_ABILITYNAME = 3;
33 constexpr int32_t MAX_WAIT_TIME_SECOND = 10;
34 TokenInfoParams g_sysInfoInstance = {
35 .dcapsNum = 0,
36 .permsNum = 0,
37 .aclsNum = 0,
38 .dcaps = nullptr,
39 .perms = nullptr,
40 .acls = nullptr,
41 .processName = "usb_manager",
42 .aplStr = "system_basic",
43 };
SetTestCaseNative(TokenInfoParams * infoInstance)44 static void SetTestCaseNative(TokenInfoParams *infoInstance)
45 {
46 uint64_t tokenId = GetAccessTokenId(infoInstance);
47 int ret = SetSelfTokenID(tokenId);
48 if (ret == 0) {
49 cout << "SetSelfTokenID success" << endl;
50 } else {
51 cout << "SetSelfTokenID fail" << endl;
52 }
53 AccessTokenKit::ReloadNativeTokenInfo();
54 }
55
56 class DriverExtensionControllerMt {
57 public:
TestEntry(int argc,char ** argv)58 int TestEntry(int argc, char **argv)
59 {
60 int ret = 0;
61 cout << "[info]driver_extension_controller_mt" << endl;
62 cout << "[usage]: start/stop/connect bundleName ExtensionAbilityName" << endl;
63 if (argc != PARAM_COUNT_NEED) {
64 cout << "error! wrong param count" << endl;
65 return 0;
66 }
67 auto &drvExtCtrl = DriverExtensionController::GetInstance();
68 string action(argv[PARAM_INDEX_OF_ACTION]);
69 string bundleName(argv[PARAM_INDEX_OF_BUNDLENAME]);
70 string abilityName(argv[PARAM_INDEX_OF_ABILITYNAME]);
71 cout << "args: "<< action << ", " << bundleName << ", " << abilityName << endl;
72 SetTestCaseNative(&g_sysInfoInstance);
73 if (action == "start") {
74 cout << "begin to connect extension ability" << endl;
75 ret = drvExtCtrl.StartDriverExtension(bundleName, abilityName);
76 } else if (action == "stop") {
77 cout << "begin to stop extension ability" << endl;
78 ret = drvExtCtrl.StopDriverExtension(bundleName, abilityName);
79 } else if (action == "connect") {
80 ConnectAbilityTest(bundleName, abilityName);
81 } else {
82 cout << "wrong param! please check!" << endl;
83 return 0;
84 }
85 if (ret == 0) {
86 cout << "driver_extension_controller_mt sucess" << endl;
87 } else {
88 cout << "driver_extension_controller_mt fail , ret = " << ret << endl;
89 }
90 cout << "delay 10s to exit" << endl;
91 for (int i = 0; i < MAX_WAIT_TIME_SECOND; i++) {
92 sleep(1);
93 }
94 cout << "exit 0" << endl;
95 return 0;
96 }
97 private:
ConnectAbilityTest(const string bundleName,const string abilityName)98 void ConnectAbilityTest(const string bundleName, const string abilityName)
99 {
100 auto &drvExtCtrl = DriverExtensionController::GetInstance();
101 shared_ptr<ConCb> conCb = make_shared<ConCb>();
102 cout << "begin to Connect extension ability" << endl;
103 auto ret = drvExtCtrl.ConnectDriverExtension(bundleName, abilityName, conCb);
104 if (ret == 0) {
105 cout << "connect suncess, wait 5 second to disconnect" << endl;
106 for (int i = 0; i < MAX_WAIT_TIME_SECOND; i++) {
107 sleep(1);
108 if (conCb->IsConnectDone()) {
109 cout << "connectDone suncess, remoteObj = " << conCb->GetRemoteObj() << endl;
110 break;
111 }
112 }
113 ret = drvExtCtrl.DisconnectDriverExtension(bundleName, abilityName, conCb);
114 for (int i = 0; i < MAX_WAIT_TIME_SECOND; i++) {
115 sleep(1);
116 if (!conCb->IsConnectDone()) {
117 cout << "DisconnectDone sucess" << endl;
118 break;
119 }
120 }
121 }
122 };
123 class ConCb : public IDriverExtensionConnectCallback {
124 public:
ConCb()125 ConCb() { };
OnConnectDone(const sptr<IRemoteObject> & remote,int resultCode)126 int32_t OnConnectDone(const sptr<IRemoteObject> &remote, int resultCode) override
127 {
128 cout << "ConCb OnConnectDone, "<< remote.GetRefPtr() << ", " << resultCode << endl;
129 return 0;
130 };
OnDisconnectDone(int resultCode)131 int32_t OnDisconnectDone(int resultCode) override
132 {
133 cout << "ConCb OnDisconnectDone, " << resultCode << endl;
134 return 0;
135 }
136 };
137 };
138 }
139 }
140 using namespace OHOS::ExternalDeviceManager;
main(int argc,char ** argv)141 int main(int argc, char **argv)
142 {
143 return DriverExtensionControllerMt().TestEntry(argc, argv);
144 }
145