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