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 "erofs_overlay_common.h" 18 #include "param_stub.h" 19 20 using namespace std; 21 using namespace testing::ext; 22 23 namespace init_ut { 24 class ErofsCommonOverlayUnitTest : public testing::Test { 25 public: SetUpTestCase(void)26 static void SetUpTestCase(void) {}; TearDownTestCase(void)27 static void TearDownTestCase(void) {}; SetUp(void)28 void SetUp(void) {}; TearDown(void)29 void TearDown(void) {}; 30 }; 31 32 HWTEST_F(ErofsCommonOverlayUnitTest, Init_IsOverlayEnable_001, TestSize.Level0) 33 { 34 const char *cmdLine; 35 36 cmdLine = "test=test"; 37 CreateTestFile(BOOT_CMD_LINE, cmdLine); 38 bool ret = IsOverlayEnable(); 39 EXPECT_EQ(ret, false); 40 remove(BOOT_CMD_LINE); 41 42 cmdLine = "oemmode=test"; 43 CreateTestFile(BOOT_CMD_LINE, cmdLine); 44 ret = IsOverlayEnable(); 45 EXPECT_EQ(ret, false); 46 remove(BOOT_CMD_LINE); 47 48 cmdLine = "oemmode=test buildvariant=user"; 49 CreateTestFile(BOOT_CMD_LINE, cmdLine); 50 ret = IsOverlayEnable(); 51 EXPECT_EQ(ret, false); 52 remove(BOOT_CMD_LINE); 53 54 cmdLine = "oemmode=test buildvariant=eng"; 55 CreateTestFile(BOOT_CMD_LINE, cmdLine); 56 ret = IsOverlayEnable(); 57 EXPECT_EQ(ret, true); 58 remove(BOOT_CMD_LINE); 59 PrepareCmdLineData(); 60 } 61 62 HWTEST_F(ErofsCommonOverlayUnitTest, Init_IsOverlayCheckExt4_001, TestSize.Level0) 63 { 64 bool ret = CheckIsExt4(STARTUP_INIT_UT_PATH "/data/notexistfile", 0); 65 EXPECT_EQ(ret, false); 66 67 const char *fileName = STARTUP_INIT_UT_PATH "/data/ext4_super_block"; 68 CheckAndCreateDir(fileName); 69 ret = CheckIsExt4(fileName, 0); 70 EXPECT_EQ(ret, false); 71 72 int fd = open(fileName, O_WRONLY); 73 struct ext4_super_block super; 74 super.s_magic = EXT4_SUPER_MAGIC; 75 write(fd, &super, sizeof(ext4_super_block)); 76 ret = CheckIsExt4(fileName, 0); 77 EXPECT_EQ(ret, false); 78 write(fd, &super, sizeof(ext4_super_block)); 79 ret = CheckIsExt4(fileName, 0); 80 remove(fileName); 81 } 82 83 HWTEST_F(ErofsCommonOverlayUnitTest, Init_IsOverlayCheckErofs_001, TestSize.Level0) 84 { 85 bool ret = CheckIsErofs(STARTUP_INIT_UT_PATH"/data/notexistfile"); 86 EXPECT_EQ(ret, false); 87 88 const char *fileName = STARTUP_INIT_UT_PATH "/data/erofsfile"; 89 CheckAndCreateDir(fileName); 90 ret = CheckIsErofs(fileName); 91 EXPECT_EQ(ret, false); 92 93 int fd = open(fileName, O_WRONLY); 94 struct ext4_super_block super; 95 super.s_magic = EXT4_SUPER_MAGIC; 96 write(fd, &super, sizeof(ext4_super_block)); 97 ret = CheckIsErofs(fileName); 98 EXPECT_EQ(ret, false); 99 100 write(fd, &super, sizeof(ext4_super_block)); 101 ret = CheckIsErofs(fileName); 102 remove(fileName); 103 } 104 }