FileSystemTest.cpp 2.7 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 25 26 27 28 29 30 31 32
// delete test file and dir
void DeleteTestFiles()
{
    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 已提交
33 34 35
// before testCase
void FileSystemTest::SetUp()
{
M
mamingshuai 已提交
36 37
    DeleteTestFiles();
    errno = 0;
W
wenjun 已提交
38 39 40 41
    LOG("------- case start");
    mCurPath = GetCurrentPath();
    int rt = chdir(TOP_DIR);
    if (rt == -1) {
M
mamingshuai 已提交
42
        LOG("== chdir to %s failed! rt = %d, errno = %d", TOP_DIR, rt, errno);
W
wenjun 已提交
43 44 45 46 47 48 49 50
    } else {
        LOG("== chdir to %s OK!", TOP_DIR);
    }
}

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

M
mamingshuai 已提交
61 62 63 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
// 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 已提交
94 95 96 97
int main(int argc, char *argv[])
{
    testing::GTEST_FLAG(output) = "xml:";
    testing::InitGoogleTest(&argc, argv);
M
mamingshuai 已提交
98 99
    if (CheckFsMount() != 0) {
        return 1;
W
wenjun 已提交
100
    }
M
mamingshuai 已提交
101 102
    DeleteTestFiles();
    return RUN_ALL_TESTS();
W
wenjun 已提交
103
}