• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "appspawn.h"
17 #include "appspawn_utils.h"
18 #include "securec.h"
19 #include "appspawn_server.h"
20 
21 #include <gtest/gtest.h>
22 
23 using namespace testing;
24 using namespace testing::ext;
25 
26 namespace OHOS {
27 namespace AppSpawn {
28 class AppSpawnClientTest : public testing::Test {
29 public:
SetUpTestCase()30     static void SetUpTestCase() {}
TearDownTestCase()31     static void TearDownTestCase() {}
SetUp()32     void SetUp() {}
TearDown()33     void TearDown() {}
34 };
35 
CreateMsg(AppSpawnClientHandle handle,const char * bundleName,RunMode mode)36 static AppSpawnReqMsgHandle CreateMsg(AppSpawnClientHandle handle, const char *bundleName, RunMode mode)
37 {
38     AppSpawnReqMsgHandle reqHandle = 0;
39     int ret = AppSpawnReqMsgCreate(MSG_APP_SPAWN, bundleName, &reqHandle);
40     APPSPAWN_CHECK(ret == 0, return 0, "Failed to create req %{public}s", bundleName);
41     do {
42         ret = AppSpawnReqMsgSetBundleInfo(reqHandle, 0, bundleName);
43         APPSPAWN_CHECK(ret == 0, break, "Failed to create req %{public}s", bundleName);
44 
45         AppDacInfo dacInfo = {};
46         dacInfo.uid = 20010029;              // 20010029 test data
47         dacInfo.gid = 20010029;              // 20010029 test data
48         dacInfo.gidCount = 2;                // 2 count
49         dacInfo.gidTable[0] = 20010029;      // 20010029 test data
50         dacInfo.gidTable[1] = 20010029 + 1;  // 20010029 test data
51         (void)strcpy_s(dacInfo.userName, sizeof(dacInfo.userName), "test-app-name");
52         ret = AppSpawnReqMsgSetAppDacInfo(reqHandle, &dacInfo);
53         APPSPAWN_CHECK(ret == 0, break, "Failed to add dac %{public}s", APPSPAWN_SERVER_NAME);
54 
55         AppSpawnReqMsgSetAppFlag(reqHandle, static_cast<AppFlagsIndex>(10));  // 10 test
56         if (mode == MODE_FOR_NATIVE_SPAWN) {
57             AppSpawnReqMsgSetAppFlag(reqHandle, static_cast<AppFlagsIndex>(23)); // 23 APP_FLAGS_ISOLATED_SANDBOX_TYPE
58             AppSpawnReqMsgSetAppFlag(reqHandle, static_cast<AppFlagsIndex>(26)); // 26 APP_FLAGS_ISOLATED_NETWORK
59         }
60 
61         const char *apl = "normal";
62         ret = AppSpawnReqMsgSetAppDomainInfo(reqHandle, 1, apl);
63         APPSPAWN_CHECK(ret == 0, break, "Failed to add domain %{public}s", APPSPAWN_SERVER_NAME);
64 
65         ret = AppSpawnReqMsgSetAppAccessToken(reqHandle, 12345678);  // 12345678
66         APPSPAWN_CHECK(ret == 0, break, "Failed to add access token %{public}s", APPSPAWN_SERVER_NAME);
67 
68         static const char *permissions[] = {
69             "ohos.permission.MANAGE_PRIVATE_PHOTOS",
70             "ohos.permission.ACTIVATE_THEME_PACKAGE",
71             "ohos.permission.GET_WALLPAPER",
72         };
73         size_t count = sizeof(permissions) / sizeof(permissions[0]);
74         for (size_t i = 0; i < count; i++) {
75             ret = AppSpawnReqMsgAddPermission(reqHandle, permissions[i]);
76             APPSPAWN_CHECK(ret == 0, break, "Failed to create req %{public}s", bundleName);
77         }
78         return reqHandle;
79     } while (0);
80     AppSpawnReqMsgFree(reqHandle);
81     return INVALID_REQ_HANDLE;
82 }
83 
CreateClient(const char * serviceName)84 static AppSpawnClientHandle CreateClient(const char *serviceName)
85 {
86     AppSpawnClientHandle clientHandle = NULL;
87     int ret = AppSpawnClientInit(serviceName, &clientHandle);
88     APPSPAWN_CHECK(ret == 0, return nullptr, "Failed to create client %{public}s", serviceName);
89     return clientHandle;
90 }
91 
92 HWTEST_F(AppSpawnClientTest, AppSpawn_Client_test001, TestSize.Level0)
93 {
94     AppSpawnClientHandle clientHandle = CreateClient(APPSPAWN_SERVER_NAME);
95     ASSERT_EQ(clientHandle != NULL, 1);
96     AppSpawnReqMsgHandle reqHandle = CreateMsg(clientHandle, "ohos.samples.clock", MODE_FOR_APP_SPAWN);
97     ASSERT_EQ(reqHandle != INVALID_REQ_HANDLE, 1);
98 
99     AppSpawnResult result = {};
100     int ret = AppSpawnClientSendMsg(clientHandle, reqHandle, &result);
101     if (ret == 0 && result.pid > 0) {
102         kill(result.pid, SIGKILL);
103     }
104     AppSpawnClientDestroy(clientHandle);
105 }
106 
107 HWTEST_F(AppSpawnClientTest, AppSpawn_Client_test002, TestSize.Level0)
108 {
109     AppSpawnClientHandle clientHandle = CreateClient(NATIVESPAWN_SERVER_NAME);
110     ASSERT_EQ(clientHandle != NULL, 1);
111     AppSpawnReqMsgHandle reqHandle = CreateMsg(clientHandle, "ohos.samples.clock", MODE_FOR_NATIVE_SPAWN);
112     ASSERT_EQ(reqHandle != INVALID_REQ_HANDLE, 1);
113 
114     AppSpawnResult result = {};
115     int ret = AppSpawnClientSendMsg(clientHandle, reqHandle, &result);
116     if (ret == 0 && result.pid > 0) {
117         kill(result.pid, SIGKILL);
118     }
119     AppSpawnClientDestroy(clientHandle);
120 }
121 
122 HWTEST_F(AppSpawnClientTest, AppSpawn_Client_test003, TestSize.Level0)
123 {
124     AppSpawnClientHandle clientHandle = CreateClient(APPSPAWN_SERVER_NAME);
125     ASSERT_EQ(clientHandle != NULL, 1);
126 
127     AppSpawnReqMsgHandle reqHandle = 0;
128     int ret = AppSpawnReqMsgCreate(MSG_UNINSTALL_DEBUG_HAP, "test.uninstall", &reqHandle);
129     ASSERT_EQ(ret, 0);
130 
131     ret = AppSpawnReqMsgSetBundleInfo(reqHandle, 100, "test.uninstall");
132     ASSERT_EQ(ret, 0);
133 
134     AppSpawnResult result = {};
135     ret = AppSpawnClientSendMsg(clientHandle, reqHandle, &result);
136     if (ret == 0 && result.pid > 0) {
137         kill(result.pid, SIGKILL);
138     }
139     AppSpawnClientDestroy(clientHandle);
140 }
141 
142 HWTEST_F(AppSpawnClientTest, AppSpawn_Client_test004, TestSize.Level0)
143 {
144     AppSpawnClientHandle clientHandle = CreateClient(APPSPAWN_SERVER_NAME);
145     ASSERT_EQ(clientHandle != NULL, 1);
146 
147     AppSpawnReqMsgHandle reqHandle = 0;
148     int ret = AppSpawnReqMsgCreate(MSG_UNINSTALL_DEBUG_HAP, "test.uninstall", &reqHandle);
149     ASSERT_EQ(ret, 0);
150 
151     ret = AppSpawnReqMsgAddStringInfo(reqHandle, MSG_EXT_NAME_USERID, "100");
152     ASSERT_EQ(ret, 0);
153 
154     AppSpawnResult result = {};
155     ret = AppSpawnClientSendMsg(clientHandle, reqHandle, &result);
156     if (ret == 0 && result.pid > 0) {
157         kill(result.pid, SIGKILL);
158     }
159     AppSpawnClientDestroy(clientHandle);
160 }
161 
162 /**
163  * @tc.name: AppSpawn_Client_test005
164  * @tc.desc: 模拟hybridspawn的客户端向hybridspawn服务端发送应用孵化请求
165  * @tc.type: FUNC
166  */
167 HWTEST_F(AppSpawnClientTest, AppSpawn_Client_test005, TestSize.Level0)
168 {
169     AppSpawnClientHandle clientHandle = CreateClient(HYBRIDSPAWN_SERVER_NAME);
170     ASSERT_EQ(clientHandle != NULL, 1);
171     AppSpawnReqMsgHandle reqHandle = CreateMsg(clientHandle, "ohos.samples.clock", MODE_FOR_HYBRID_SPAWN);
172     ASSERT_EQ(reqHandle != INVALID_REQ_HANDLE, 1);
173 
174     AppSpawnResult result = {};
175     int ret = AppSpawnClientSendMsg(clientHandle, reqHandle, &result);
176     if (ret == 0 && result.pid > 0) {
177         kill(result.pid, SIGKILL);
178     }
179     AppSpawnClientDestroy(clientHandle);
180 }
181 
182 }  // namespace AppSpawn
183 }  // namespace OHOS
184