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.READ_IMAGEVIDEO",
70             "ohos.permission.FILE_CROSS_APP",
71             "ohos.permission.ACTIVATE_THEME_PACKAGE",
72             "ohos.permission.GET_WALLPAPER",
73         };
74         size_t count = sizeof(permissions) / sizeof(permissions[0]);
75         for (size_t i = 0; i < count; i++) {
76             ret = AppSpawnReqMsgAddPermission(reqHandle, permissions[i]);
77             APPSPAWN_CHECK(ret == 0, break, "Failed to create req %{public}s", bundleName);
78         }
79         return reqHandle;
80     } while (0);
81     AppSpawnReqMsgFree(reqHandle);
82     return INVALID_REQ_HANDLE;
83 }
84 
CreateClient(const char * serviceName)85 static AppSpawnClientHandle CreateClient(const char *serviceName)
86 {
87     AppSpawnClientHandle clientHandle = NULL;
88     int ret = AppSpawnClientInit(serviceName, &clientHandle);
89     APPSPAWN_CHECK(ret == 0, return nullptr, "Failed to create client %{public}s", serviceName);
90     return clientHandle;
91 }
92 
93 HWTEST_F(AppSpawnClientTest, AppSpawn_Client_test001, TestSize.Level0)
94 {
95     AppSpawnClientHandle clientHandle = CreateClient(APPSPAWN_SERVER_NAME);
96     ASSERT_EQ(clientHandle != NULL, 1);
97     AppSpawnReqMsgHandle reqHandle = CreateMsg(clientHandle, "ohos.samples.clock", MODE_FOR_APP_SPAWN);
98     ASSERT_EQ(reqHandle != INVALID_REQ_HANDLE, 1);
99 
100     AppSpawnResult result = {};
101     int ret = AppSpawnClientSendMsg(clientHandle, reqHandle, &result);
102     if (ret == 0 && result.pid > 0) {
103         kill(result.pid, SIGKILL);
104     }
105     AppSpawnClientDestroy(clientHandle);
106 }
107 
108 HWTEST_F(AppSpawnClientTest, AppSpawn_Client_test002, TestSize.Level0)
109 {
110     AppSpawnClientHandle clientHandle = CreateClient(NATIVESPAWN_SERVER_NAME);
111     ASSERT_EQ(clientHandle != NULL, 1);
112     AppSpawnReqMsgHandle reqHandle = CreateMsg(clientHandle, "ohos.samples.clock", MODE_FOR_NATIVE_SPAWN);
113     ASSERT_EQ(reqHandle != INVALID_REQ_HANDLE, 1);
114 
115     AppSpawnResult result = {};
116     int ret = AppSpawnClientSendMsg(clientHandle, reqHandle, &result);
117     if (ret == 0 && result.pid > 0) {
118         kill(result.pid, SIGKILL);
119     }
120     AppSpawnClientDestroy(clientHandle);
121 }
122 
123 }  // namespace AppSpawn
124 }  // namespace OHOS
125