1/*
2 * Copyright (C) 2024 HiHope Open Source Organization.
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 <cstdio>
17#include <cstdlib>
18#include <fcntl.h>
19#include <string>
20#include <unistd.h>
21#include <vector>
22#include <gtest/gtest.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25#include "securec.h"
26
27using namespace testing::ext;
28using namespace std;
29
30static const char *OPEN_API_TEST_PATH = "/data/local/tmp";
31static const char *TEST_PATH = "/data/local/tmp/test_path";
32const int MAX_LEN = 128;
33
34class MkdiratApiTest : public testing::Test {
35public:
36    static void SetUpTestCase();
37    static void TearDownTestCase();
38    void SetUp();
39    void TearDown();
40private:
41};
42void MkdiratApiTest::SetUp()
43{
44}
45void MkdiratApiTest::TearDown()
46{
47}
48void MkdiratApiTest::SetUpTestCase()
49{
50}
51void MkdiratApiTest::TearDownTestCase()
52{
53}
54
55/*
56 * @tc.number : SUB_KERNEL_SYSCALL_MKDIRAT_0100
57 * @tc.name   : MkdiratCreateDirSuccess_0001
58 * @tc.desc   : mkdirat create a directory by fd success.
59 * @tc.size   : MediumTest
60 * @tc.type   : Function
61 * @tc.level  : Level 1
62 */
63HWTEST_F(MkdiratApiTest, MkdiratCreateDirSuccess_0001, Function | MediumTest | Level1)
64{
65    const char *testPath = "test_path";
66    mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
67    int dirFd = open(OPEN_API_TEST_PATH, O_RDONLY);
68    EXPECT_TRUE(dirFd > 0);
69
70    int ret = mkdirat(dirFd, testPath, mode);
71    EXPECT_TRUE(ret == 0);
72    close(dirFd);
73
74    char path[MAX_LEN] = { 0 };
75    int num = sprintf_s(path, MAX_LEN, "%s/%s", OPEN_API_TEST_PATH, testPath);
76    EXPECT_TRUE(num > 0);
77    ret = rmdir(path);
78    EXPECT_EQ(ret, 0);
79}
80
81/*
82 * @tc.number : SUB_KERNEL_SYSCALL_MKDIRAT_0200
83 * @tc.name   : MkdiratAT_FDCWDCreateDirSuccess_0002
84 * @tc.desc   : current the path to create a directory success.
85 * @tc.size   : MediumTest
86 * @tc.type   : Function
87 * @tc.level  : Level 1
88 */
89HWTEST_F(MkdiratApiTest, MkdiratAT_FDCWDCreateDirSuccess_0002, Function | MediumTest | Level1)
90{
91    mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
92
93    int ret = mkdirat(AT_FDCWD, TEST_PATH, mode);
94    EXPECT_EQ(ret, 0);
95
96    ret = rmdir(TEST_PATH);
97    EXPECT_EQ(ret, 0);
98}
99
100/*
101 * @tc.number : SUB_KERNEL_SYSCALL_MKDIRAT_0300
102 * @tc.name   : MkdiratCreateExistDirFailed_0003
103 * @tc.desc   : mkdirat create the exist directory failed, errno EEXIST.
104 * @tc.size   : MediumTest
105 * @tc.type   : Function
106 * @tc.level  : Level 2
107 */
108HWTEST_F(MkdiratApiTest, MkdiratCreateExistDirFailed_0003, Function | MediumTest | Level2)
109{
110    mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
111    errno = 0;
112    int ret = mkdirat(AT_FDCWD, OPEN_API_TEST_PATH, mode);
113    EXPECT_EQ(ret, -1);
114    EXPECT_EQ(errno, EEXIST);
115}
116
117/*
118 * @tc.number : SUB_KERNEL_SYSCALL_MKDIRAT_0400
119 * @tc.name   : MkdiratCreatePathInNotExistDirFailed_0004
120 * @tc.desc   : mkdirat path in not exist directory failed, errno ENOENT.
121 * @tc.size   : MediumTest
122 * @tc.type   : Function
123 * @tc.level  : Level 2
124 */
125HWTEST_F(MkdiratApiTest, MkdiratCreatePathInNotExistDirFailed_0004, Function | MediumTest | Level2)
126{
127    const char *path = "/data/local/tmp/abcd/abcd";
128    mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
129    errno = 0;
130    int ret = mkdirat(AT_FDCWD, path, mode);
131    EXPECT_EQ(ret, -1);
132    EXPECT_EQ(errno, ENOENT);
133}
134
135/*
136 * @tc.number : SUB_KERNEL_SYSCALL_MKDIRAT_0500
137 * @tc.name   : MkdiratUseInvalidFdFailed_0005
138 * @tc.desc   : mkdirat use invalid fd failed, errno EBADF.
139 * @tc.size   : MediumTest
140 * @tc.type   : Function
141 * @tc.level  : Level 2
142 */
143HWTEST_F(MkdiratApiTest, MkdiratUseInvalidFdFailed_0005, Function | MediumTest | Level2)
144{
145    const char *testPath = "test_path";
146    mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
147    errno = 0;
148    int ret = mkdirat(-1, testPath, mode);
149    EXPECT_EQ(ret, -1);
150    EXPECT_EQ(errno, EBADF);
151}
152