FileSystemTest.cpp 2.8 KB
Newer Older
W
wenjun 已提交
1
/*
M
mamingshuai 已提交
2
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
W
wenjun 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "FileSystemTest.h"
M
mamingshuai 已提交
16
#include <string.h>
W
wenjun 已提交
17 18 19 20 21
#include <unistd.h>
#include "utils.h"

using namespace testing::ext;

M
mamingshuai 已提交
22 23 24
// delete test file and dir
void DeleteTestFiles()
{
C
chuaizhzh 已提交
25 26 27
    if (access(TOP_DIR "/DIR_NEW", F_OK) == 0) {
        RemoveDir(TOP_DIR "/DIR_NEW");
    }
M
mamingshuai 已提交
28 29 30 31 32 33 34 35
    if (access(TOP_DIR "/" DIR0, F_OK) == 0) {
        RemoveDir(TOP_DIR "/" DIR0);
    }
    if (access(TOP_DIR "/" FILE0, F_OK) == 0) {
        remove(TOP_DIR "/" FILE0);
    }
}

W
wenjun 已提交
36 37 38
// before testCase
void FileSystemTest::SetUp()
{
M
mamingshuai 已提交
39 40
    DeleteTestFiles();
    errno = 0;
W
wenjun 已提交
41 42 43 44
    LOG("------- case start");
    mCurPath = GetCurrentPath();
    int rt = chdir(TOP_DIR);
    if (rt == -1) {
M
mamingshuai 已提交
45
        LOG("== chdir to %s failed! rt = %d, errno = %d", TOP_DIR, rt, errno);
W
wenjun 已提交
46 47 48 49 50 51 52 53
    } else {
        LOG("== chdir to %s OK!", TOP_DIR);
    }
}

// after testCase
void FileSystemTest::TearDown()
{
M
mamingshuai 已提交
54
    DeleteTestFiles();
W
wenjun 已提交
55 56
    int rt = chdir(mCurPath);
    if (rt == -1) {
M
mamingshuai 已提交
57
        LOG("== chdir to %s failed! rt = %d, errno = %d", mCurPath, rt, errno);
W
wenjun 已提交
58 59 60 61 62 63
    } else {
        LOG("== chdir to %s OK!", mCurPath);
    }
    LOG("------- case end\n");
}

M
mamingshuai 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
// check TOP_DIR file system, 0 is exist, -1 is non-exist
int CheckFsMount()
{
    const int lenMax = 100;
    int len;
    char buf[lenMax];
    const char mountInfoFile[] = "/proc/mounts";

    // check TOP_DIR exist
    if (access(TOP_DIR, F_OK) != 0) {
        LOG("'%s' not accessable, Test Stop!", TOP_DIR);
        return -1;
    }

    FILE *fp = fopen(mountInfoFile, "r");
    if (fp != nullptr) {
        while (fgets(buf, lenMax, fp) != nullptr) {
            len = strlen(buf);
            if (strlen(buf) != 0) {
                buf[len - 1] = '\0';
            }

            if (strstr(buf, TOP_DIR_MOUNT_INFO) != nullptr) {
                fclose(fp);
                return 0;
            }
        }
        fclose(fp);
    }
    LOG("'%s' not mount properly, Test Stop!", TOP_DIR);
    return -1;
}

W
wenjun 已提交
97 98 99 100
int main(int argc, char *argv[])
{
    testing::GTEST_FLAG(output) = "xml:";
    testing::InitGoogleTest(&argc, argv);
M
mamingshuai 已提交
101 102
    if (CheckFsMount() != 0) {
        return 1;
W
wenjun 已提交
103
    }
M
mamingshuai 已提交
104 105
    DeleteTestFiles();
    return RUN_ALL_TESTS();
W
wenjun 已提交
106
}