19762338dSopenharmony_ci/*
29762338dSopenharmony_ci * Copyright (C) 2024 HiHope Open Source Organization.
39762338dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
49762338dSopenharmony_ci * you may not use this file except in compliance with the License.
59762338dSopenharmony_ci * You may obtain a copy of the License at
69762338dSopenharmony_ci *
79762338dSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
89762338dSopenharmony_ci *
99762338dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
109762338dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
119762338dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
129762338dSopenharmony_ci * See the License for the specific language governing permissions and
139762338dSopenharmony_ci * limitations under the License.
149762338dSopenharmony_ci */
159762338dSopenharmony_ci
169762338dSopenharmony_ci#include <cstdio>
179762338dSopenharmony_ci#include <cstdlib>
189762338dSopenharmony_ci#include <fcntl.h>
199762338dSopenharmony_ci#include <string>
209762338dSopenharmony_ci#include <unistd.h>
219762338dSopenharmony_ci#include <vector>
229762338dSopenharmony_ci#include <gtest/gtest.h>
239762338dSopenharmony_ci#include <sys/stat.h>
249762338dSopenharmony_ci#include <sys/types.h>
259762338dSopenharmony_ci#include "securec.h"
269762338dSopenharmony_ci
279762338dSopenharmony_ciusing namespace testing::ext;
289762338dSopenharmony_ciusing namespace std;
299762338dSopenharmony_ci
309762338dSopenharmony_cistatic const char *OPEN_API_TEST_PATH = "/data/local/tmp";
319762338dSopenharmony_cistatic const char *TEST_PATH = "/data/local/tmp/test_path";
329762338dSopenharmony_ciconst int MAX_LEN = 128;
339762338dSopenharmony_ci
349762338dSopenharmony_ciclass MkdiratApiTest : public testing::Test {
359762338dSopenharmony_cipublic:
369762338dSopenharmony_ci    static void SetUpTestCase();
379762338dSopenharmony_ci    static void TearDownTestCase();
389762338dSopenharmony_ci    void SetUp();
399762338dSopenharmony_ci    void TearDown();
409762338dSopenharmony_ciprivate:
419762338dSopenharmony_ci};
429762338dSopenharmony_civoid MkdiratApiTest::SetUp()
439762338dSopenharmony_ci{
449762338dSopenharmony_ci}
459762338dSopenharmony_civoid MkdiratApiTest::TearDown()
469762338dSopenharmony_ci{
479762338dSopenharmony_ci}
489762338dSopenharmony_civoid MkdiratApiTest::SetUpTestCase()
499762338dSopenharmony_ci{
509762338dSopenharmony_ci}
519762338dSopenharmony_civoid MkdiratApiTest::TearDownTestCase()
529762338dSopenharmony_ci{
539762338dSopenharmony_ci}
549762338dSopenharmony_ci
559762338dSopenharmony_ci/*
569762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_MKDIRAT_0100
579762338dSopenharmony_ci * @tc.name   : MkdiratCreateDirSuccess_0001
589762338dSopenharmony_ci * @tc.desc   : mkdirat create a directory by fd success.
599762338dSopenharmony_ci * @tc.size   : MediumTest
609762338dSopenharmony_ci * @tc.type   : Function
619762338dSopenharmony_ci * @tc.level  : Level 1
629762338dSopenharmony_ci */
639762338dSopenharmony_ciHWTEST_F(MkdiratApiTest, MkdiratCreateDirSuccess_0001, Function | MediumTest | Level1)
649762338dSopenharmony_ci{
659762338dSopenharmony_ci    const char *testPath = "test_path";
669762338dSopenharmony_ci    mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
679762338dSopenharmony_ci    int dirFd = open(OPEN_API_TEST_PATH, O_RDONLY);
689762338dSopenharmony_ci    EXPECT_TRUE(dirFd > 0);
699762338dSopenharmony_ci
709762338dSopenharmony_ci    int ret = mkdirat(dirFd, testPath, mode);
719762338dSopenharmony_ci    EXPECT_TRUE(ret == 0);
729762338dSopenharmony_ci    close(dirFd);
739762338dSopenharmony_ci
749762338dSopenharmony_ci    char path[MAX_LEN] = { 0 };
759762338dSopenharmony_ci    int num = sprintf_s(path, MAX_LEN, "%s/%s", OPEN_API_TEST_PATH, testPath);
769762338dSopenharmony_ci    EXPECT_TRUE(num > 0);
779762338dSopenharmony_ci    ret = rmdir(path);
789762338dSopenharmony_ci    EXPECT_EQ(ret, 0);
799762338dSopenharmony_ci}
809762338dSopenharmony_ci
819762338dSopenharmony_ci/*
829762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_MKDIRAT_0200
839762338dSopenharmony_ci * @tc.name   : MkdiratAT_FDCWDCreateDirSuccess_0002
849762338dSopenharmony_ci * @tc.desc   : current the path to create a directory success.
859762338dSopenharmony_ci * @tc.size   : MediumTest
869762338dSopenharmony_ci * @tc.type   : Function
879762338dSopenharmony_ci * @tc.level  : Level 1
889762338dSopenharmony_ci */
899762338dSopenharmony_ciHWTEST_F(MkdiratApiTest, MkdiratAT_FDCWDCreateDirSuccess_0002, Function | MediumTest | Level1)
909762338dSopenharmony_ci{
919762338dSopenharmony_ci    mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
929762338dSopenharmony_ci
939762338dSopenharmony_ci    int ret = mkdirat(AT_FDCWD, TEST_PATH, mode);
949762338dSopenharmony_ci    EXPECT_EQ(ret, 0);
959762338dSopenharmony_ci
969762338dSopenharmony_ci    ret = rmdir(TEST_PATH);
979762338dSopenharmony_ci    EXPECT_EQ(ret, 0);
989762338dSopenharmony_ci}
999762338dSopenharmony_ci
1009762338dSopenharmony_ci/*
1019762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_MKDIRAT_0300
1029762338dSopenharmony_ci * @tc.name   : MkdiratCreateExistDirFailed_0003
1039762338dSopenharmony_ci * @tc.desc   : mkdirat create the exist directory failed, errno EEXIST.
1049762338dSopenharmony_ci * @tc.size   : MediumTest
1059762338dSopenharmony_ci * @tc.type   : Function
1069762338dSopenharmony_ci * @tc.level  : Level 2
1079762338dSopenharmony_ci */
1089762338dSopenharmony_ciHWTEST_F(MkdiratApiTest, MkdiratCreateExistDirFailed_0003, Function | MediumTest | Level2)
1099762338dSopenharmony_ci{
1109762338dSopenharmony_ci    mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
1119762338dSopenharmony_ci    errno = 0;
1129762338dSopenharmony_ci    int ret = mkdirat(AT_FDCWD, OPEN_API_TEST_PATH, mode);
1139762338dSopenharmony_ci    EXPECT_EQ(ret, -1);
1149762338dSopenharmony_ci    EXPECT_EQ(errno, EEXIST);
1159762338dSopenharmony_ci}
1169762338dSopenharmony_ci
1179762338dSopenharmony_ci/*
1189762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_MKDIRAT_0400
1199762338dSopenharmony_ci * @tc.name   : MkdiratCreatePathInNotExistDirFailed_0004
1209762338dSopenharmony_ci * @tc.desc   : mkdirat path in not exist directory failed, errno ENOENT.
1219762338dSopenharmony_ci * @tc.size   : MediumTest
1229762338dSopenharmony_ci * @tc.type   : Function
1239762338dSopenharmony_ci * @tc.level  : Level 2
1249762338dSopenharmony_ci */
1259762338dSopenharmony_ciHWTEST_F(MkdiratApiTest, MkdiratCreatePathInNotExistDirFailed_0004, Function | MediumTest | Level2)
1269762338dSopenharmony_ci{
1279762338dSopenharmony_ci    const char *path = "/data/local/tmp/abcd/abcd";
1289762338dSopenharmony_ci    mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
1299762338dSopenharmony_ci    errno = 0;
1309762338dSopenharmony_ci    int ret = mkdirat(AT_FDCWD, path, mode);
1319762338dSopenharmony_ci    EXPECT_EQ(ret, -1);
1329762338dSopenharmony_ci    EXPECT_EQ(errno, ENOENT);
1339762338dSopenharmony_ci}
1349762338dSopenharmony_ci
1359762338dSopenharmony_ci/*
1369762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_MKDIRAT_0500
1379762338dSopenharmony_ci * @tc.name   : MkdiratUseInvalidFdFailed_0005
1389762338dSopenharmony_ci * @tc.desc   : mkdirat use invalid fd failed, errno EBADF.
1399762338dSopenharmony_ci * @tc.size   : MediumTest
1409762338dSopenharmony_ci * @tc.type   : Function
1419762338dSopenharmony_ci * @tc.level  : Level 2
1429762338dSopenharmony_ci */
1439762338dSopenharmony_ciHWTEST_F(MkdiratApiTest, MkdiratUseInvalidFdFailed_0005, Function | MediumTest | Level2)
1449762338dSopenharmony_ci{
1459762338dSopenharmony_ci    const char *testPath = "test_path";
1469762338dSopenharmony_ci    mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
1479762338dSopenharmony_ci    errno = 0;
1489762338dSopenharmony_ci    int ret = mkdirat(-1, testPath, mode);
1499762338dSopenharmony_ci    EXPECT_EQ(ret, -1);
1509762338dSopenharmony_ci    EXPECT_EQ(errno, EBADF);
1519762338dSopenharmony_ci}
152