• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <errno.h>
2 #include <gtest/gtest.h>
3 #include <limits.h>
4 
5 using namespace testing::ext;
6 
7 class MiscRealpathTest : public testing::Test {
SetUp()8     void SetUp() override {}
TearDown()9     void TearDown() override {}
10 };
11 
12 /**
13  * @tc.name: realpath_001
14  * @tc.desc: This test case aims to test whether the realpath () function correctly sets errno and returns nullptr when
15  *           passing invalid parameters.
16  * @tc.type: FUNC
17  */
18 HWTEST_F(MiscRealpathTest, realpath_001, TestSize.Level1)
19 {
20     errno = 0;
21     const char* testPath = nullptr;
22     char* ptr = realpath(testPath, nullptr);
23     ASSERT_TRUE(ptr == nullptr);
24     EXPECT_EQ(errno, EINVAL);
25 }
26 
27 /**
28  * @tc.name: realpath_002
29  * @tc.desc: This test case aims to test whether the realpath () function correctly sets errno and returns nullptr when
30  *           passing an empty path.
31  * @tc.type: FUNC
32  */
33 HWTEST_F(MiscRealpathTest, realpath_002, TestSize.Level1)
34 {
35     errno = 0;
36     char* ptr = realpath("", nullptr);
37     ASSERT_TRUE(ptr == nullptr);
38     EXPECT_EQ(errno, ENOENT);
39 }
40 
41 /**
42  * @tc.name: realpath_003
43  * @tc.desc: This test case aims to test whether the realpath () function correctly sets errno and returns nullptr when
44  *           passing non-existent paths.
45  * @tc.type: FUNC
46  */
47 HWTEST_F(MiscRealpathTest, realpath_003, TestSize.Level1)
48 {
49     errno = 0;
50     char* ptr = realpath("/musl/test", nullptr);
51     ASSERT_TRUE(ptr == nullptr);
52     EXPECT_EQ(errno, ENOENT);
53 }
54 
55 /**
56  * @tc.name: realpath_005
57  * @tc.desc: This test case aims to test whether the realpath () function correctly sets errno and returns nullptr when
58  *           passing non directory paths.
59  * @tc.type: FUNC
60  */
61 HWTEST_F(MiscRealpathTest, realpath_005, TestSize.Level1)
62 {
63     char existingPath[] = "/tmp";
64     errno = 0;
65     char* ptr = realpath(existingPath, nullptr);
66     ASSERT_NE(ptr, nullptr);
67     free(ptr);
68 }
69 
70 /**
71  * @tc.name: realpath_006
72  * @tc.desc: The purpose of this test case is to test that the realpath () function can correctly ignore the dot symbols
73  *           in the path when parsing it.
74  * @tc.type: FUNC
75  */
76 HWTEST_F(MiscRealpathTest, realpath_006, TestSize.Level1)
77 {
78     char* resolvedPath = realpath("/proc/./version", nullptr);
79     EXPECT_EQ(strcmp("/proc/version", resolvedPath), 0);
80     free(resolvedPath);
81 }
82 
83 /**
84  * @tc.name: realpath_007
85  * @tc.desc: The purpose of this test case is to test that the realpath () function can correctly handle double dot
86  *           symbols in the path when parsing it.
87  * @tc.type: FUNC
88  */
89 HWTEST_F(MiscRealpathTest, realpath_007, TestSize.Level1)
90 {
91     char* resolvedPath = realpath("/dev/../proc/version", nullptr);
92     EXPECT_EQ(strcmp("/proc/version", resolvedPath), 0);
93     free(resolvedPath);
94 }