1 /* 2 * Copyright (c) 2024 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 16 #include <gtest/gtest.h> 17 #include "remount_overlay.h" 18 #include "param_stub.h" 19 #include "mntent.h" 20 21 using namespace std; 22 using namespace testing::ext; 23 24 namespace init_ut { 25 class RemountOverlayUnitTest : public testing::Test { 26 public: SetUpTestCase(void)27 static void SetUpTestCase(void) {}; TearDownTestCase(void)28 static void TearDownTestCase(void) {}; SetUp(void)29 void SetUp(void) {}; TearDown(void)30 void TearDown(void) {}; 31 }; 32 33 HWTEST_F(RemountOverlayUnitTest, Init_MntNeedRemountTest_001, TestSize.Level0) 34 { 35 const char *path = "/test"; 36 bool ret = MntNeedRemount(path); 37 EXPECT_EQ(ret, false); 38 39 const char *path2 = "/"; 40 ret = MntNeedRemount(path2); 41 EXPECT_EQ(ret, true); 42 } 43 44 HWTEST_F(RemountOverlayUnitTest, Init_IsSkipRemountTest_001, TestSize.Level0) 45 { 46 struct mntent mentry; 47 bool ret = IsSkipRemount(mentry); 48 EXPECT_EQ(ret, true); 49 50 char ufs[] = "ufs"; 51 char test[] = "test"; 52 mentry.mnt_type = ufs; 53 mentry.mnt_dir = test; 54 ret = IsSkipRemount(mentry); 55 EXPECT_EQ(ret, true); 56 57 char root[] = "/"; 58 mentry.mnt_dir = root; 59 ret = IsSkipRemount(mentry); 60 EXPECT_EQ(ret, true); 61 62 char erofs1[] = "er11ofs"; 63 mentry.mnt_type = erofs1; 64 ret = IsSkipRemount(mentry); 65 EXPECT_EQ(ret, true); 66 67 char erofs[] = "erofs"; 68 char ndm[] = "/dev/block/ndm-"; 69 mentry.mnt_type = erofs; 70 mentry.mnt_fsname = ndm; 71 ret = IsSkipRemount(mentry); 72 EXPECT_EQ(ret, true); 73 74 char dm1[] = "/dev/block/dm-1"; 75 mentry.mnt_fsname = dm1; 76 ret = IsSkipRemount(mentry); 77 EXPECT_EQ(ret, false); 78 } 79 80 HWTEST_F(RemountOverlayUnitTest, Init_ExecCommand_001, TestSize.Level0) 81 { 82 char *nullArgv[] = {NULL}; // A valid command 83 int result = ExecCommand(0, nullArgv); 84 EXPECT_NE(result, 0); // Expect success 85 86 const char *valid = "/bin/ls"; 87 char *validArgv[] = {const_cast<char*>(valid), NULL}; // A valid command 88 result = ExecCommand(1, validArgv); 89 EXPECT_NE(result, 0); // Expect success 90 91 const char *invalid = "/notexit/ls"; 92 char *invalidArgv[] = {const_cast<char*>(invalid), NULL}; // A valid command 93 result = ExecCommand(1, invalidArgv); 94 EXPECT_NE(result, 0); // Expect success 95 } 96 97 HWTEST_F(RemountOverlayUnitTest, Init_GetDevSizeTest_001, TestSize.Level0) 98 { 99 const char *fileName = ""; 100 int ret = GetDevSize(fileName, NULL); 101 EXPECT_EQ(ret, -1); 102 fileName = STARTUP_INIT_UT_PATH "/data/getDevSize"; 103 CheckAndCreateDir(fileName); 104 ret = GetDevSize(fileName, NULL); 105 EXPECT_EQ(ret, -1); 106 107 uint64_t devSize; 108 ret = GetDevSize(fileName, &devSize); 109 EXPECT_NE(ret, 0); 110 remove(fileName); 111 } 112 113 HWTEST_F(RemountOverlayUnitTest, Init_FormatExt4Test_001, TestSize.Level0) 114 { 115 const char *fileName = ""; 116 int ret = GetDevSize(fileName, NULL); 117 EXPECT_EQ(ret, -1); 118 fileName = STARTUP_INIT_UT_PATH "/data/getDevSize"; 119 CheckAndCreateDir(fileName); 120 ret = GetDevSize(fileName, NULL); 121 EXPECT_EQ(ret, -1); 122 123 uint64_t devSize; 124 ret = GetDevSize(fileName, &devSize); 125 EXPECT_NE(ret, 0); 126 remove(fileName); 127 } 128 }