1/*
2 * Copyright (c) 2020-2021 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#include "FileSystemTest.h"
16#include <stdio.h>
17#include <string.h>
18#include <stdlib.h>
19
20#include <sys/stat.h>
21#include <sys/types.h>
22#include <fcntl.h>
23#include <unistd.h>
24#include <dirent.h>
25#include <ftw.h>
26#include <libgen.h>
27
28#include <gtest/gtest.h>
29
30#include "utils.h"
31#include "log.h"
32#include "KernelConstants.h"
33#include "libfs.h"
34
35using namespace testing::ext;
36/**
37 * @tc.number   SUB_KERNEL_FS_STDLIB_0400
38 * @tc.name     basic function test : mktemp create a temporary file name, mkdtemp create a directory
39 * @tc.desc     [C- SOFTWARE -0200]
40 */
41HWTEST_F(FileSystemTest, testMktempMkdtemp, Function | MediumTest | Level2)
42{
43    char tmpFileNamePre[] = "tmpFile_XXXXXX";
44    char tmpFileName[] = "tmpFile_XXXXXX";
45    EXPECT_NE(mktemp(tmpFileName), nullptr) << "> mktemp errno = " << errno;
46    EXPECT_STRNE(tmpFileName, tmpFileNamePre) << "> tmpFileName no change";
47
48    char tmpDirName[] = "tmpDir_XXXXXXX";
49    EXPECT_NE(mkdtemp(tmpDirName), nullptr) << "> mktemp errno = " << errno;
50    EXPECT_NE(remove(tmpDirName), -1) << "> remove errno = " << errno;
51}
52
53/**
54 * @tc.number   SUB_KERNEL_FS_STDLIB_0500
55 * @tc.name     basic function test : Use the realpath function to obtain the path.
56 * @tc.desc     [C- SOFTWARE -0200]
57 */
58HWTEST_F(FileSystemTest, testRealpath, Function | MediumTest | Level3)
59{
60    int fd = 0;
61    fd = creat(FILE0, 0777);
62    EXPECT_NE(fd, -1) << "> creat faild errno = " << errno;
63    EXPECT_NE(close(fd), -1) << "> close errno = " << errno;
64
65    // get Absolute Path
66    const char *realPathStandard = TOP_DIR "/" FILE0;
67    char *realPath = (char *)malloc(256);
68    if (realpath(FILE0, realPath) == nullptr)
69    {
70        LOG("> realpath errno == %d", errno);
71        free(realPath);
72    }
73    else
74    {
75        EXPECT_STREQ(realPath, realPathStandard);
76        LOG("> realPath = %s", realPath);
77        free(realPath);
78    }
79}
80