• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <errno.h>
2 #include <gtest/gtest.h>
3 #include <unistd.h>
4 
5 using namespace testing::ext;
6 
7 class ProcessUnsetenvTest : public testing::Test {
SetUp()8     void SetUp() override {}
TearDown()9     void TearDown() override {}
10 };
11 
12 /**
13  * @tc.name: unsetenv_001
14  * @tc.desc: If the variable has been successfully removed by retrieving its value using getenv,
15  *           and expects the value to be nullptr.
16  * @tc.type: FUNC
17  **/
18 HWTEST_F(ProcessUnsetenvTest, unsetenv_001, TestSize.Level1)
19 {
20     setenv("MY_VAR", "Hello", 1);
21     unsetenv("MY_VAR");
22     char* value = getenv("MY_VAR");
23     EXPECT_EQ(nullptr, value);
24 }
25 
26 /**
27  * @tc.name: unsetenv_002
28  * @tc.desc: Check how the unsetenv function handles invalid input parameters and whether it sets the errno
29  *           appropriately in such cases.
30  * @tc.type: FUNC
31  **/
32 HWTEST_F(ProcessUnsetenvTest, unsetenv_002, TestSize.Level1)
33 {
34     errno = 0;
35     int result = unsetenv("");
36     EXPECT_EQ(-1, result);
37     EXPECT_EQ(EINVAL, errno);
38     result = unsetenv("a = b");
39     EXPECT_EQ(-1, result);
40     EXPECT_EQ(EINVAL, errno);
41 }