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