1 /*
2 * Copyright (c) 2022 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 <stdlib.h>
17 #include "functionalext.h"
18
19 /**
20 * @tc.name : execvp_0100
21 * @tc.desc : Each parameter is valid, and the specified file can be executed.
22 * @tc.level : Level 0
23 */
execvp_0100(void)24 void execvp_0100(void)
25 {
26 pid_t fpid;
27 fpid = fork();
28 if (fpid == 0) {
29 char *argv[] = {"touch", "execvptest.txt", NULL};
30 execvp("touch", argv);
31 }
32 sleep(1);
33 int isExist = access("execvptest.txt", F_OK);
34 EXPECT_EQ("execvp_0100", isExist, 0);
35 if (isExist == 0) {
36 remove("execvptest.txt");
37 }
38 }
39
40 /**
41 * @tc.name : execvp_0200
42 * @tc.desc : Each parameter is valid, the PATH is empty, and the specified file can be executed.
43 * @tc.level : Level 0
44 */
execvp_0200(void)45 void execvp_0200(void)
46 {
47 pid_t fpid;
48 fpid = fork();
49 if (fpid == 0) {
50 char *argv[] = {"touch", "execvptest.txt", NULL};
51 char *buff = getenv("ls");
52 setenv("ls", " ", 1);
53 execvp("touch", argv);
54 setenv("ls", buff, 1);
55 }
56 sleep(1);
57 int isExist = access("execvptest.txt", F_OK);
58 EXPECT_EQ("execvp_0200", isExist, 0);
59 if (isExist == 0) {
60 remove("execvptest.txt");
61 }
62 }
63
64 /**
65 * @tc.name : execvp_0300
66 * @tc.desc : The content pointed to by the path parameter is empty, and the specified file cannot be executed.
67 * @tc.level : Level 2
68 */
execvp_0300(void)69 void execvp_0300(void)
70 {
71 char *argv[] = {"touch", "execvptest.txt", NULL};
72 int ret = execvp(" ", argv);
73 EXPECT_EQ("execvp_0300", ret, -1);
74 }
75
76 /**
77 * @tc.name : execvp_0400
78 * @tc.desc : The length of path exceeds NAME_MAX, and the specified file cannot be executed.
79 * @tc.level : Level 2
80 */
execvp_0400(void)81 void execvp_0400(void)
82 {
83 char buff[300];
84 for (int i = 0; i < 300; i++) {
85 buff[i] = 'a';
86 }
87 char *argv[] = {"touch", "execvptest.txt", NULL};
88 int ret = execvp(buff, argv);
89 EXPECT_EQ("execvp_0400", ret, -1);
90 }
91
main(int argc,char * argv[])92 int main(int argc, char *argv[])
93 {
94 execvp_0100();
95 execvp_0200();
96 execvp_0300();
97 execvp_0400();
98 return t_status;
99 }