• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef APPSPAWN_CLIENT_TEST_H
16 #define APPSPAWN_CLIENT_TEST_H
17 
18 #include <sstream>
19 #include <fstream>
20 
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <cstdio>
24 #include <vector>
25 
26 #include "client_socket.h"
27 #include "appspawn_server.h"
28 #include "parameter.h"
29 
30 namespace OHOS {
31 namespace AppSpawn {
32 
33 class AppSpawnTestClient {
34 public:
35     AppSpawnTestClient() = default;
36     ~AppSpawnTestClient() = default;
37 
38     int ClientCreateSocket(std::string path = "/dev/unix/socket/NWebSpawn")
39     {
40         // close old socket and create new
41         if (clientSocket_ != nullptr) {
42             clientSocket_->CloseClient();
43             clientSocket_ = nullptr;
44         }
45         clientSocket_ = std::make_shared<ClientSocket>(path);
46         APPSPAWN_CHECK(clientSocket_ != nullptr, return -1, "Failed to create client for path %s", path.c_str());
47         int ret = clientSocket_->CreateClient();
48         APPSPAWN_CHECK(ret == 0, return -1, "Failed to create socket for path %ss", path.c_str());
49         ret = clientSocket_->ConnectSocket();
50         APPSPAWN_CHECK(ret == 0, return -1, "Failed to connect to server %ss", path.c_str());
51         return 0;
52     }
53 
ClientClose()54     void ClientClose()
55     {
56         if (clientSocket_ != nullptr) {
57             clientSocket_->CloseClient();
58             clientSocket_ = nullptr;
59         }
60     }
ClientSendMsg(const uint8_t * buf,int len)61     int ClientSendMsg(const uint8_t *buf, int len)
62     {
63         int curr = 0;
64         int real = 0;
65         uint8_t *buffer = const_cast<uint8_t *>(buf);
66         const int maxLen = 5 * 1024;
67         while (curr < len) {
68             if ((len - curr) > maxLen) {
69                 real = maxLen;
70             } else {
71                 real = len - curr;
72             }
73             if (clientSocket_->WriteSocketMessage(static_cast<const void *>(buffer + curr), real) != real) {
74                 return -1;
75             }
76             curr += real;
77         }
78         return 0;
79     }
80 
ClientRecvMsg(pid_t & pid)81     int ClientRecvMsg(pid_t &pid)
82     {
83         pid = -1;
84         std::vector<uint8_t> data(sizeof(pid_t)); // 4 pid size
85         if (clientSocket_->ReadSocketMessage(data.data(), data.size()) == sizeof(pid_t)) {
86             int ret = *(reinterpret_cast<int *>(data.data()));
87             if (ret < 0) {
88                 return ret;
89             }
90             pid = static_cast<pid_t>(ret);
91             return 0;
92         }
93         return -1;
94     }
95 
ClientFillMsg(AppParameter * request,const std::string & processName,const std::string & cmd)96     void ClientFillMsg(AppParameter *request, const std::string &processName, const std::string &cmd)
97     {
98         request->code = DEFAULT;
99         request->flags = 0;
100         request->uid = 20010033; // 20010033 test uid
101         request->gid = 20010033; // 20010033 test gid
102         request->gidCount = 0;
103         request->accessTokenId = 0x200a509d; // 0x200a509d test token id
104         request->accessTokenIdEx = 0x4832514205; // 0x4832514205 test token id
105         request->allowInternet = 1;
106         request->extraInfo.totalLength = 0;
107         request->extraInfo.savedLength = 0;
108         request->extraInfo.data = nullptr;
109         int ret = strcpy_s(request->apl, sizeof(request->processName), processName.c_str());
110         ret += strcpy_s(request->processName, sizeof(request->processName), processName.c_str());
111         ret += strcpy_s(request->bundleName, sizeof(request->bundleName), processName.c_str());
112         ret += strcpy_s(request->renderCmd, sizeof(request->renderCmd), cmd.c_str());
113         ret += strcpy_s(request->ownerId, sizeof(request->ownerId), cmd.c_str());
114         if (ret != 0) {
115             printf("Failed to copy bundle name \n");
116         }
117     }
118 private:
119     std::shared_ptr<ClientSocket> clientSocket_ {};
120 };
121 
122 }  // namespace AppSpawn
123 }  // namespace OHOS
124 #endif // APPSPAWN_CLIENT_TEST_H