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 <sys/wait.h>
18 #include "functionalext.h"
19
20 /**
21 * @tc.name : putenv_0100
22 * @tc.desc : Add or change the environment variable test
23 * @tc.level : Level 0
24 */
putenv_0100(void)25 void putenv_0100(void)
26 {
27 int ret = unsetenv("test");
28 EXPECT_EQ("putenv_0100", ret, CMPFLAG);
29
30 ret = putenv("test=putenv_0100");
31 EXPECT_EQ("putenv_0100", ret, CMPFLAG);
32 char *test = getenv("test");
33 if (test) {
34 EXPECT_STREQ("putenv_0100", test, "putenv_0100");
35 } else {
36 EXPECT_PTRNE("putenv_0100", test, NULL);
37 }
38
39 ret = putenv("test=putenv_new");
40 EXPECT_EQ("putenv_0100", ret, CMPFLAG);
41 test = getenv("test");
42 EXPECT_PTRNE("putenv_0100", test, NULL);
43 if (test) {
44 EXPECT_STREQ("putenv_0100", test, "putenv_new");
45 }
46 unsetenv("test");
47 }
48
49 /**
50 * @tc.name : putenv_0200
51 * @tc.desc : Provide exception parameters, add or modify the environment variable test
52 * @tc.level : Level 2
53 */
putenv_0200(void)54 void putenv_0200(void)
55 {
56 int ret = unsetenv("test");
57 EXPECT_EQ("putenv_0200", ret, CMPFLAG);
58
59 ret = putenv("test");
60 EXPECT_EQ("putenv_0200", ret, CMPFLAG);
61 char *test = getenv("test");
62 if (!test) {
63 EXPECT_PTREQ("putenv_0200", test, NULL);
64 }
65 pid_t pid = fork();
66 if (pid == -1) {
67 t_error("unsetenv_0100: Error forking process");
68 } else if (pid == 0) {
69 putenv("");
70 } else {
71 int status;
72 waitpid(pid, &status, 0);
73 if (WIFSIGNALED(status)) {
74 int sig = WTERMSIG(status);
75 EXPECT_EQ("putenv_0200", SIGABRT, sig);
76 }
77 }
78 unsetenv("test");
79 }
80
main(void)81 int main(void)
82 {
83 putenv_0100();
84 putenv_0200();
85 return t_status;
86 }