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