1/*
2 * Copyright (c) 2021 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 <errno.h>
17#include <gtest/gtest.h>
18#include "log.h"
19#include "utils.h"
20#include "libfs.h"
21#include "KernelConstants.h"
22
23using namespace testing::ext;
24
25#define RES_DIR_DYLOAD RES_DIR_KERNEL "dyload/"
26
27class ExecApiTest : public testing::Test {
28};
29
30/**
31 * @tc.number   SUB_KERNEL_DL_API_EXECL_0100
32 * @tc.name     execl api test
33 * @tc.desc     [C- SOFTWARE -0200]
34 */
35HWTEST_F(ExecApiTest, testExecl, Function | MediumTest | Level1)
36{
37    char* resELF = RES_DIR_DYLOAD "executor2";
38
39    pid_t pid = fork();
40    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
41    if (pid == 0) { // child
42        int rt = execl(resELF, "1", "!@#$%^&*()_+", NULL);
43        if (rt == -1) {
44            PANIC("execl failed, errno=%d\n", errno);
45        }
46        exit(0);
47    }
48    // parent
49    WaitProcExitedOK(pid);
50}
51
52/**
53 * @tc.number   SUB_KERNEL_DL_API_EXECLP_0100
54 * @tc.name     execlp api test
55 * @tc.desc     [C- SOFTWARE -0200]
56 */
57HWTEST_F(ExecApiTest, testExeclp, Function | MediumTest | Level1)
58{
59    pid_t pid = fork();
60    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
61    if (pid == 0) { // child
62        putenv("PATH=" RES_DIR_DYLOAD);
63        int rt = execlp("executor2", "1", "!@#$%^&*()_+", NULL);
64        if (rt == -1) {
65            PANIC("execlp failed, errno=%d\n", errno);
66        }
67        LOG("Error, should never get here.");
68        exit(1);
69    }
70    // parent
71    WaitProcExitedOK(pid);
72}
73
74/**
75 * @tc.number   SUB_KERNEL_DL_API_EXECLE_0100
76 * @tc.name     execle api test
77 * @tc.desc     [C- SOFTWARE -0200]
78 */
79HWTEST_F(ExecApiTest, testExecle, Function | MediumTest | Level1)
80{
81    char* resELF = RES_DIR_DYLOAD "executor1";
82    putenv("NAME=Alice");
83
84    pid_t pid = fork();
85    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
86    if (pid == 0) { // child
87        char *env[] = {"NAME=Bob", NULL};
88        int rt = execle(resELF, "executor1", "-n", "NAME", "-v", "Bob", NULL, env);
89        if (rt == -1) {
90            PANIC("execle failed, errno=%d\n", errno);
91        }
92        LOG("Error, should never get here.");
93        exit(1);
94    }
95    // parent
96    WaitProcExitedOK(pid);
97}
98
99/**
100 * @tc.number   SUB_KERNEL_DL_API_EXECV_0100
101 * @tc.name     execv api test
102 * @tc.desc     [C- SOFTWARE -0200]
103 */
104HWTEST_F(ExecApiTest, testExecv, Function | MediumTest | Level1)
105{
106    char* resELF = RES_DIR_DYLOAD "executor1";
107
108    pid_t pid = fork();
109    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
110    if (pid == 0) { // child
111        putenv("NAME=Alice");
112        char *arg[] = {"executor1", "-n", "NAME", "-v", "Alice", NULL};
113        int rt = execv(resELF, arg);
114        if (rt == -1) {
115            PANIC("execv failed, errno=%d\n", errno);
116        }
117        LOG("Error, should never get here.");
118        exit(1);
119    }
120    // parent
121    WaitProcExitedOK(pid);
122}
123
124/**
125 * @tc.number   SUB_KERNEL_DL_API_EXECVP_0100
126 * @tc.name     execvp api test
127 * @tc.desc     [C- SOFTWARE -0200]
128 */
129HWTEST_F(ExecApiTest, testExecvp, Function | MediumTest | Level1)
130{
131    putenv("NAME=Alice");
132
133    pid_t pid = fork();
134    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
135    if (pid == 0) { // child
136        putenv("PATH=" RES_DIR_DYLOAD);
137
138        char *arg[] = {"executor1", "-n", "NAME", "-v", "Alice", NULL};
139        int rt = execvp("executor1", arg);
140        if (rt == -1) {
141            PANIC("execvp failed, errno=%d\n", errno);
142        }
143        LOG("Error, should never get here.");
144        exit(1);
145    }
146    // parent
147    WaitProcExitedOK(pid);
148}