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