1f6603c60Sopenharmony_ci/*
2f6603c60Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3f6603c60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4f6603c60Sopenharmony_ci * you may not use this file except in compliance with the License.
5f6603c60Sopenharmony_ci * You may obtain a copy of the License at
6f6603c60Sopenharmony_ci *
7f6603c60Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8f6603c60Sopenharmony_ci *
9f6603c60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10f6603c60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11f6603c60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f6603c60Sopenharmony_ci * See the License for the specific language governing permissions and
13f6603c60Sopenharmony_ci * limitations under the License.
14f6603c60Sopenharmony_ci */
15f6603c60Sopenharmony_ci
16f6603c60Sopenharmony_ci#include <errno.h>
17f6603c60Sopenharmony_ci#include <gtest/gtest.h>
18f6603c60Sopenharmony_ci#include "log.h"
19f6603c60Sopenharmony_ci#include "utils.h"
20f6603c60Sopenharmony_ci#include "libfs.h"
21f6603c60Sopenharmony_ci#include "KernelConstants.h"
22f6603c60Sopenharmony_ci
23f6603c60Sopenharmony_ciusing namespace testing::ext;
24f6603c60Sopenharmony_ci
25f6603c60Sopenharmony_ci#define RES_DIR_DYLOAD RES_DIR_KERNEL "dyload/"
26f6603c60Sopenharmony_ci
27f6603c60Sopenharmony_ciclass ExecApiTest : public testing::Test {
28f6603c60Sopenharmony_ci};
29f6603c60Sopenharmony_ci
30f6603c60Sopenharmony_ci/**
31f6603c60Sopenharmony_ci * @tc.number   SUB_KERNEL_DL_API_EXECL_0100
32f6603c60Sopenharmony_ci * @tc.name     execl api test
33f6603c60Sopenharmony_ci * @tc.desc     [C- SOFTWARE -0200]
34f6603c60Sopenharmony_ci */
35f6603c60Sopenharmony_ciHWTEST_F(ExecApiTest, testExecl, Function | MediumTest | Level1)
36f6603c60Sopenharmony_ci{
37f6603c60Sopenharmony_ci    char* resELF = RES_DIR_DYLOAD "executor2";
38f6603c60Sopenharmony_ci
39f6603c60Sopenharmony_ci    pid_t pid = fork();
40f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
41f6603c60Sopenharmony_ci    if (pid == 0) { // child
42f6603c60Sopenharmony_ci        int rt = execl(resELF, "1", "!@#$%^&*()_+", NULL);
43f6603c60Sopenharmony_ci        if (rt == -1) {
44f6603c60Sopenharmony_ci            PANIC("execl failed, errno=%d\n", errno);
45f6603c60Sopenharmony_ci        }
46f6603c60Sopenharmony_ci        exit(0);
47f6603c60Sopenharmony_ci    }
48f6603c60Sopenharmony_ci    // parent
49f6603c60Sopenharmony_ci    WaitProcExitedOK(pid);
50f6603c60Sopenharmony_ci}
51f6603c60Sopenharmony_ci
52f6603c60Sopenharmony_ci/**
53f6603c60Sopenharmony_ci * @tc.number   SUB_KERNEL_DL_API_EXECLP_0100
54f6603c60Sopenharmony_ci * @tc.name     execlp api test
55f6603c60Sopenharmony_ci * @tc.desc     [C- SOFTWARE -0200]
56f6603c60Sopenharmony_ci */
57f6603c60Sopenharmony_ciHWTEST_F(ExecApiTest, testExeclp, Function | MediumTest | Level1)
58f6603c60Sopenharmony_ci{
59f6603c60Sopenharmony_ci    pid_t pid = fork();
60f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
61f6603c60Sopenharmony_ci    if (pid == 0) { // child
62f6603c60Sopenharmony_ci        putenv("PATH=" RES_DIR_DYLOAD);
63f6603c60Sopenharmony_ci        int rt = execlp("executor2", "1", "!@#$%^&*()_+", NULL);
64f6603c60Sopenharmony_ci        if (rt == -1) {
65f6603c60Sopenharmony_ci            PANIC("execlp failed, errno=%d\n", errno);
66f6603c60Sopenharmony_ci        }
67f6603c60Sopenharmony_ci        LOG("Error, should never get here.");
68f6603c60Sopenharmony_ci        exit(1);
69f6603c60Sopenharmony_ci    }
70f6603c60Sopenharmony_ci    // parent
71f6603c60Sopenharmony_ci    WaitProcExitedOK(pid);
72f6603c60Sopenharmony_ci}
73f6603c60Sopenharmony_ci
74f6603c60Sopenharmony_ci/**
75f6603c60Sopenharmony_ci * @tc.number   SUB_KERNEL_DL_API_EXECLE_0100
76f6603c60Sopenharmony_ci * @tc.name     execle api test
77f6603c60Sopenharmony_ci * @tc.desc     [C- SOFTWARE -0200]
78f6603c60Sopenharmony_ci */
79f6603c60Sopenharmony_ciHWTEST_F(ExecApiTest, testExecle, Function | MediumTest | Level1)
80f6603c60Sopenharmony_ci{
81f6603c60Sopenharmony_ci    char* resELF = RES_DIR_DYLOAD "executor1";
82f6603c60Sopenharmony_ci    putenv("NAME=Alice");
83f6603c60Sopenharmony_ci
84f6603c60Sopenharmony_ci    pid_t pid = fork();
85f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
86f6603c60Sopenharmony_ci    if (pid == 0) { // child
87f6603c60Sopenharmony_ci        char *env[] = {"NAME=Bob", NULL};
88f6603c60Sopenharmony_ci        int rt = execle(resELF, "executor1", "-n", "NAME", "-v", "Bob", NULL, env);
89f6603c60Sopenharmony_ci        if (rt == -1) {
90f6603c60Sopenharmony_ci            PANIC("execle failed, errno=%d\n", errno);
91f6603c60Sopenharmony_ci        }
92f6603c60Sopenharmony_ci        LOG("Error, should never get here.");
93f6603c60Sopenharmony_ci        exit(1);
94f6603c60Sopenharmony_ci    }
95f6603c60Sopenharmony_ci    // parent
96f6603c60Sopenharmony_ci    WaitProcExitedOK(pid);
97f6603c60Sopenharmony_ci}
98f6603c60Sopenharmony_ci
99f6603c60Sopenharmony_ci/**
100f6603c60Sopenharmony_ci * @tc.number   SUB_KERNEL_DL_API_EXECV_0100
101f6603c60Sopenharmony_ci * @tc.name     execv api test
102f6603c60Sopenharmony_ci * @tc.desc     [C- SOFTWARE -0200]
103f6603c60Sopenharmony_ci */
104f6603c60Sopenharmony_ciHWTEST_F(ExecApiTest, testExecv, Function | MediumTest | Level1)
105f6603c60Sopenharmony_ci{
106f6603c60Sopenharmony_ci    char* resELF = RES_DIR_DYLOAD "executor1";
107f6603c60Sopenharmony_ci
108f6603c60Sopenharmony_ci    pid_t pid = fork();
109f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
110f6603c60Sopenharmony_ci    if (pid == 0) { // child
111f6603c60Sopenharmony_ci        putenv("NAME=Alice");
112f6603c60Sopenharmony_ci        char *arg[] = {"executor1", "-n", "NAME", "-v", "Alice", NULL};
113f6603c60Sopenharmony_ci        int rt = execv(resELF, arg);
114f6603c60Sopenharmony_ci        if (rt == -1) {
115f6603c60Sopenharmony_ci            PANIC("execv failed, errno=%d\n", errno);
116f6603c60Sopenharmony_ci        }
117f6603c60Sopenharmony_ci        LOG("Error, should never get here.");
118f6603c60Sopenharmony_ci        exit(1);
119f6603c60Sopenharmony_ci    }
120f6603c60Sopenharmony_ci    // parent
121f6603c60Sopenharmony_ci    WaitProcExitedOK(pid);
122f6603c60Sopenharmony_ci}
123f6603c60Sopenharmony_ci
124f6603c60Sopenharmony_ci/**
125f6603c60Sopenharmony_ci * @tc.number   SUB_KERNEL_DL_API_EXECVP_0100
126f6603c60Sopenharmony_ci * @tc.name     execvp api test
127f6603c60Sopenharmony_ci * @tc.desc     [C- SOFTWARE -0200]
128f6603c60Sopenharmony_ci */
129f6603c60Sopenharmony_ciHWTEST_F(ExecApiTest, testExecvp, Function | MediumTest | Level1)
130f6603c60Sopenharmony_ci{
131f6603c60Sopenharmony_ci    putenv("NAME=Alice");
132f6603c60Sopenharmony_ci
133f6603c60Sopenharmony_ci    pid_t pid = fork();
134f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
135f6603c60Sopenharmony_ci    if (pid == 0) { // child
136f6603c60Sopenharmony_ci        putenv("PATH=" RES_DIR_DYLOAD);
137f6603c60Sopenharmony_ci
138f6603c60Sopenharmony_ci        char *arg[] = {"executor1", "-n", "NAME", "-v", "Alice", NULL};
139f6603c60Sopenharmony_ci        int rt = execvp("executor1", arg);
140f6603c60Sopenharmony_ci        if (rt == -1) {
141f6603c60Sopenharmony_ci            PANIC("execvp failed, errno=%d\n", errno);
142f6603c60Sopenharmony_ci        }
143f6603c60Sopenharmony_ci        LOG("Error, should never get here.");
144f6603c60Sopenharmony_ci        exit(1);
145f6603c60Sopenharmony_ci    }
146f6603c60Sopenharmony_ci    // parent
147f6603c60Sopenharmony_ci    WaitProcExitedOK(pid);
148f6603c60Sopenharmony_ci}