1f6603c60Sopenharmony_ci/* 2f6603c60Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd. 3f6603c60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4f6603c60Sopenharmony_ci * you may not use this file except in compliance with the License. 5f6603c60Sopenharmony_ci * You may obtain a copy of the License at 6f6603c60Sopenharmony_ci * 7f6603c60Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8f6603c60Sopenharmony_ci * 9f6603c60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10f6603c60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11f6603c60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12f6603c60Sopenharmony_ci * See the License for the specific language governing permissions and 13f6603c60Sopenharmony_ci * limitations under the License. 14f6603c60Sopenharmony_ci */ 15f6603c60Sopenharmony_ci#include "FileSystemTest.h" 16f6603c60Sopenharmony_ci#include <stdio.h> 17f6603c60Sopenharmony_ci#include <string.h> 18f6603c60Sopenharmony_ci#include <stdlib.h> 19f6603c60Sopenharmony_ci 20f6603c60Sopenharmony_ci#include <sys/stat.h> 21f6603c60Sopenharmony_ci#include <sys/types.h> 22f6603c60Sopenharmony_ci#include <fcntl.h> 23f6603c60Sopenharmony_ci#include <unistd.h> 24f6603c60Sopenharmony_ci#include <dirent.h> 25f6603c60Sopenharmony_ci#include <ftw.h> 26f6603c60Sopenharmony_ci#include <libgen.h> 27f6603c60Sopenharmony_ci 28f6603c60Sopenharmony_ci#include <gtest/gtest.h> 29f6603c60Sopenharmony_ci 30f6603c60Sopenharmony_ci#include "utils.h" 31f6603c60Sopenharmony_ci#include "log.h" 32f6603c60Sopenharmony_ci#include "KernelConstants.h" 33f6603c60Sopenharmony_ci#include "libfs.h" 34f6603c60Sopenharmony_ci 35f6603c60Sopenharmony_ciusing namespace testing::ext; 36f6603c60Sopenharmony_ci/** 37f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_FS_STDLIB_0400 38f6603c60Sopenharmony_ci * @tc.name basic function test : mktemp create a temporary file name, mkdtemp create a directory 39f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 40f6603c60Sopenharmony_ci */ 41f6603c60Sopenharmony_ciHWTEST_F(FileSystemTest, testMktempMkdtemp, Function | MediumTest | Level2) 42f6603c60Sopenharmony_ci{ 43f6603c60Sopenharmony_ci char tmpFileNamePre[] = "tmpFile_XXXXXX"; 44f6603c60Sopenharmony_ci char tmpFileName[] = "tmpFile_XXXXXX"; 45f6603c60Sopenharmony_ci EXPECT_NE(mktemp(tmpFileName), nullptr) << "> mktemp errno = " << errno; 46f6603c60Sopenharmony_ci EXPECT_STRNE(tmpFileName, tmpFileNamePre) << "> tmpFileName no change"; 47f6603c60Sopenharmony_ci 48f6603c60Sopenharmony_ci char tmpDirName[] = "tmpDir_XXXXXXX"; 49f6603c60Sopenharmony_ci EXPECT_NE(mkdtemp(tmpDirName), nullptr) << "> mktemp errno = " << errno; 50f6603c60Sopenharmony_ci EXPECT_NE(remove(tmpDirName), -1) << "> remove errno = " << errno; 51f6603c60Sopenharmony_ci} 52f6603c60Sopenharmony_ci 53f6603c60Sopenharmony_ci/** 54f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_FS_STDLIB_0500 55f6603c60Sopenharmony_ci * @tc.name basic function test : Use the realpath function to obtain the path. 56f6603c60Sopenharmony_ci * @tc.desc [C- SOFTWARE -0200] 57f6603c60Sopenharmony_ci */ 58f6603c60Sopenharmony_ciHWTEST_F(FileSystemTest, testRealpath, Function | MediumTest | Level3) 59f6603c60Sopenharmony_ci{ 60f6603c60Sopenharmony_ci int fd = 0; 61f6603c60Sopenharmony_ci fd = creat(FILE0, 0777); 62f6603c60Sopenharmony_ci EXPECT_NE(fd, -1) << "> creat faild errno = " << errno; 63f6603c60Sopenharmony_ci EXPECT_NE(close(fd), -1) << "> close errno = " << errno; 64f6603c60Sopenharmony_ci 65f6603c60Sopenharmony_ci // get Absolute Path 66f6603c60Sopenharmony_ci const char *realPathStandard = TOP_DIR "/" FILE0; 67f6603c60Sopenharmony_ci char *realPath = (char *)malloc(256); 68f6603c60Sopenharmony_ci if (realpath(FILE0, realPath) == nullptr) 69f6603c60Sopenharmony_ci { 70f6603c60Sopenharmony_ci LOG("> realpath errno == %d", errno); 71f6603c60Sopenharmony_ci free(realPath); 72f6603c60Sopenharmony_ci } 73f6603c60Sopenharmony_ci else 74f6603c60Sopenharmony_ci { 75f6603c60Sopenharmony_ci EXPECT_STREQ(realPath, realPathStandard); 76f6603c60Sopenharmony_ci LOG("> realPath = %s", realPath); 77f6603c60Sopenharmony_ci free(realPath); 78f6603c60Sopenharmony_ci } 79f6603c60Sopenharmony_ci} 80