• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <unistd.h>
17 #include <stdlib.h>
18 #include "functionalext.h"
19 
20 char *my_env[] = {"THIS=environment will be", "PASSED=to new process by", "the EXEC=functions", NULL};
21 
22 /**
23  * @tc.name      : execvpe_0100
24  * @tc.desc      : Verify that the specified file can be executed
25  *                 (all parameters are valid, PATH is not empty by default)
26  * @tc.level     : Level 0
27  */
execvpe_0100(void)28 void execvpe_0100(void)
29 {
30     char *path = getenv("touch");
31     pid_t fpid;
32     fpid = fork();
33     if (fpid == 0) {
34         char *argv[] = {"touch", "touch", "/data/test.txt", 0};
35         int result = execvpe("touch", argv, &path);
36         EXPECT_NE("execvpe_0100", result, -1);
37     }
38 
39     sleep(1);
40     FILE *fptr = fopen("/data/test.txt", "r");
41     EXPECT_PTRNE("execvpe_0100", fptr, NULL);
42     fclose(fptr);
43     remove("/data/test.txt");
44 }
45 
46 /**
47  * @tc.name      : execvpe_0200
48  * @tc.desc      : Verify that the specified file can be executed (all parameters are valid, PATH is null)
49  * @tc.level     : Level 0
50  */
execvpe_0200(void)51 void execvpe_0200(void)
52 {
53     char *path = getenv("touch");
54     setenv("touch", "\0", 1);
55     pid_t fpid;
56     fpid = fork();
57     if (fpid == 0) {
58         char *argv[] = {"touch", "touch", "/data/test.txt", 0};
59         int result = execvpe("touch", argv, &path);
60         EXPECT_NE("execvpe_0100", result, -1);
61     }
62 
63     sleep(1);
64     FILE *fptr = fopen("/data/test.txt", "r");
65     EXPECT_PTRNE("execvpe_0100", fptr, NULL);
66 
67     fclose(fptr);
68     remove("/data/test.txt");
69     unsetenv("touch");
70 }
71 
72 /**
73  * @tc.name      : execvpe_0300
74  * @tc.desc      : Verify that the specified file cannot be executed (file argument is invalid, file
75  *                 argument points to NULL)
76  * @tc.level     : Level 2
77  */
execvpe_0300(void)78 void execvpe_0300(void)
79 {
80     char buff[] = "\0";
81     char *argv[] = {"ls", "-al", "/etc/passwd", 0};
82     int result = execvpe(buff, argv, my_env);
83     EXPECT_EQ("execvpe_0300", result, -1);
84 }
85 
86 /**
87  * @tc.name      : execvpe_0400
88  * @tc.desc      : Verify that the specified file cannot be executed (file argument is invalid, file
89  *                 length exceeds NAME_MAX)
90  * @tc.level     : Level 2
91  */
execvpe_0400(void)92 void execvpe_0400(void)
93 {
94     char *argv[] = {"ls", "-al", "/etc/passwd", 0};
95     char buff[300];
96     for (int i = 0; i < 300; i++) {
97         buff[i] = 'a';
98     }
99     int result = execvpe(buff, argv, my_env);
100     EXPECT_EQ("execvpe_0400", result, -1);
101 }
102 
main(int argc,char * argv[])103 int main(int argc, char *argv[])
104 {
105     execvpe_0100();
106     execvpe_0200();
107     execvpe_0300();
108     execvpe_0400();
109     return t_status;
110 }