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 <string.h>
17f6603c60Sopenharmony_ci#include <unistd.h>
18f6603c60Sopenharmony_ci#include "utils.h"
19f6603c60Sopenharmony_ci
20f6603c60Sopenharmony_ciusing namespace testing::ext;
21f6603c60Sopenharmony_ci
22f6603c60Sopenharmony_ci// delete test file and dir
23f6603c60Sopenharmony_civoid DeleteTestFiles()
24f6603c60Sopenharmony_ci{
25f6603c60Sopenharmony_ci    if (access(TOP_DIR "/DIR_NEW", F_OK) == 0) {
26f6603c60Sopenharmony_ci        RemoveDir(TOP_DIR "/DIR_NEW");
27f6603c60Sopenharmony_ci    }
28f6603c60Sopenharmony_ci    if (access(TOP_DIR "/" DIR0, F_OK) == 0) {
29f6603c60Sopenharmony_ci        RemoveDir(TOP_DIR "/" DIR0);
30f6603c60Sopenharmony_ci    }
31f6603c60Sopenharmony_ci    if (access(TOP_DIR "/" FILE0, F_OK) == 0) {
32f6603c60Sopenharmony_ci        remove(TOP_DIR "/" FILE0);
33f6603c60Sopenharmony_ci    }
34f6603c60Sopenharmony_ci}
35f6603c60Sopenharmony_ci
36f6603c60Sopenharmony_ci// before testCase
37f6603c60Sopenharmony_civoid FileSystemTest::SetUp()
38f6603c60Sopenharmony_ci{
39f6603c60Sopenharmony_ci    DeleteTestFiles();
40f6603c60Sopenharmony_ci    errno = 0;
41f6603c60Sopenharmony_ci    LOG("------- case start");
42f6603c60Sopenharmony_ci    mCurPath = GetCurrentPath();
43f6603c60Sopenharmony_ci    int rt = chdir(TOP_DIR);
44f6603c60Sopenharmony_ci    if (rt == -1) {
45f6603c60Sopenharmony_ci        LOG("== chdir to %s failed! rt = %d, errno = %d", TOP_DIR, rt, errno);
46f6603c60Sopenharmony_ci    } else {
47f6603c60Sopenharmony_ci        LOG("== chdir to %s OK!", TOP_DIR);
48f6603c60Sopenharmony_ci    }
49f6603c60Sopenharmony_ci}
50f6603c60Sopenharmony_ci
51f6603c60Sopenharmony_ci// after testCase
52f6603c60Sopenharmony_civoid FileSystemTest::TearDown()
53f6603c60Sopenharmony_ci{
54f6603c60Sopenharmony_ci    DeleteTestFiles();
55f6603c60Sopenharmony_ci    int rt = chdir(mCurPath);
56f6603c60Sopenharmony_ci    if (rt == -1) {
57f6603c60Sopenharmony_ci        LOG("== chdir to %s failed! rt = %d, errno = %d", mCurPath, rt, errno);
58f6603c60Sopenharmony_ci    } else {
59f6603c60Sopenharmony_ci        LOG("== chdir to %s OK!", mCurPath);
60f6603c60Sopenharmony_ci    }
61f6603c60Sopenharmony_ci    LOG("------- case end\n");
62f6603c60Sopenharmony_ci}
63f6603c60Sopenharmony_ci
64f6603c60Sopenharmony_ci// check TOP_DIR file system, 0 is exist, -1 is non-exist
65f6603c60Sopenharmony_ciint CheckFsMount()
66f6603c60Sopenharmony_ci{
67f6603c60Sopenharmony_ci    const int lenMax = 100;
68f6603c60Sopenharmony_ci    int len;
69f6603c60Sopenharmony_ci    char buf[lenMax];
70f6603c60Sopenharmony_ci    const char mountInfoFile[] = "/proc/mounts";
71f6603c60Sopenharmony_ci
72f6603c60Sopenharmony_ci    // check TOP_DIR exist
73f6603c60Sopenharmony_ci    if (access(TOP_DIR, F_OK) != 0) {
74f6603c60Sopenharmony_ci        LOG("'%s' not accessible, Test Stop!", TOP_DIR);
75f6603c60Sopenharmony_ci        return -1;
76f6603c60Sopenharmony_ci    }
77f6603c60Sopenharmony_ci
78f6603c60Sopenharmony_ci    FILE *fp = fopen(mountInfoFile, "r");
79f6603c60Sopenharmony_ci    if (fp != nullptr) {
80f6603c60Sopenharmony_ci        while (fgets(buf, lenMax, fp) != nullptr) {
81f6603c60Sopenharmony_ci            len = strlen(buf);
82f6603c60Sopenharmony_ci            if (strlen(buf) != 0) {
83f6603c60Sopenharmony_ci                buf[len - 1] = '\0';
84f6603c60Sopenharmony_ci            }
85f6603c60Sopenharmony_ci
86f6603c60Sopenharmony_ci            if (strstr(buf, TOP_DIR_MOUNT_INFO) != nullptr) {
87f6603c60Sopenharmony_ci                fclose(fp);
88f6603c60Sopenharmony_ci                return 0;
89f6603c60Sopenharmony_ci            }
90f6603c60Sopenharmony_ci        }
91f6603c60Sopenharmony_ci        fclose(fp);
92f6603c60Sopenharmony_ci    }
93f6603c60Sopenharmony_ci    LOG("'%s' not mount properly, Test Stop!", TOP_DIR);
94f6603c60Sopenharmony_ci    return -1;
95f6603c60Sopenharmony_ci}
96f6603c60Sopenharmony_ci
97f6603c60Sopenharmony_ciint main(int argc, char *argv[])
98f6603c60Sopenharmony_ci{
99f6603c60Sopenharmony_ci    testing::GTEST_FLAG(output) = "xml:";
100f6603c60Sopenharmony_ci    testing::InitGoogleTest(&argc, argv);
101f6603c60Sopenharmony_ci    if (CheckFsMount() != 0) {
102f6603c60Sopenharmony_ci        return 1;
103f6603c60Sopenharmony_ci    }
104f6603c60Sopenharmony_ci    DeleteTestFiles();
105f6603c60Sopenharmony_ci    return RUN_ALL_TESTS();
106f6603c60Sopenharmony_ci}