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 16 #ifndef KERNEL_LITE_FILESYSTEMTEST 17 #define KERNEL_LITE_FILESYSTEMTEST 18 19 #include <stdio.h> 20 #include <string.h> 21 #include <stdlib.h> 22 #include <sys/stat.h> 23 #include <sys/types.h> 24 #include <fcntl.h> 25 #include <unistd.h> 26 #include <dirent.h> 27 #include <gtest/gtest.h> 28 #include "utils.h" 29 #include "log.h" 30 #include "KernelConstants.h" 31 #include "libfs.h" 32 33 class FileSystemTest : public::testing::Test { 34 protected: 35 char *mCurPath; 36 void SetUp(); 37 void TearDown(); 38 }; 39 40 // Creating Folders and Files for the Test 41 #define FILE0 "FILE0" // FILE0 42 #define DIR0 "DIR0" // DIR0/ 43 #define DIR0_FILE0 "DIR0_FILE0" // ├── DIR0_FILE0 44 #define DIR0_DIR0 "DIR0_DIR0" // ├── DIR0_DIR0/ 45 #define DIR0_DIR1 "DIR0_DIR1" // └── DIR0_DIR1/ 46 #define DIR0_DIR1_FILE0 "DIR0_DIR1_FILE0" // ├── DIR0_DIR1_FILE0 47 #define DIR0_DIR1_DIR0 "DIR0_DIR1_DIR0" // └── DIR0_DIR1_DIR0/ 48 #define FOURTY_MS 40 49 50 // Creating Folders and Files for the Test 51 #define CreateTestFolder() do { \ 52 int thisFd; \ 53 ASSERT_NE(mkdir(DIR0, 0777), -1) << "> mkdir errno = " << errno; \ 54 ASSERT_NE((thisFd = creat((DIR0 "/" DIR0_FILE0), 0777)), -1) << "> creat errno = " << errno; \ 55 ASSERT_NE(close(thisFd), -1) << "> close errno = " << errno; \ 56 ASSERT_NE(mkdir((DIR0 "/" DIR0_DIR0), 0777), -1) << "> mkdir errno = " << errno; \ 57 ASSERT_NE(mkdir((DIR0 "/" DIR0_DIR1), 0777), -1) << "> mkdir errno = " << errno; \ 58 ASSERT_NE((thisFd = creat((DIR0 "/" DIR0_DIR1"/" DIR0_DIR1_FILE0), 0777)), -1) << "> creat errno = " << errno; \ 59 ASSERT_NE(close(thisFd), -1) << "> close errno = " << errno; \ 60 ASSERT_NE(mkdir((DIR0 "/" DIR0_DIR1"/" DIR0_DIR1_DIR0), 0777), -1) << "> mkdir errno = " << errno; \ 61 } while (0) 62 63 // write and close 64 #define WriteCloseTest(fd) do { \ 65 char writeBuf[] = "this is a file"; \ 66 EXPECT_NE(write(fd, writeBuf, sizeof(writeBuf)), -1) << "> write errno = " << errno; \ 67 EXPECT_NE(close(fd), -1) << "> close errno = " << errno; \ 68 } while (0) \ 69 70 // read and close 71 #define ReadCloseTest(fd) do { \ 72 char writeBuf[] = "this is a file"; \ 73 char readBuf[100]; \ 74 EXPECT_NE(read(fd, readBuf, 20), -1) << "> read errno = " << errno; \ 75 EXPECT_STREQ(writeBuf, readBuf) << "> writeBuf = " << writeBuf \ 76 << "\n> readBuf = " << readBuf; \ 77 EXPECT_NE(close(fd), -1) << "> close errno = " << errno; \ 78 } while (0) \ 79 80 #endif 81