1d9f0492fSopenharmony_ci/*
2d9f0492fSopenharmony_ci* Copyright (c) 2024 Huawei Device Co., Ltd.
3d9f0492fSopenharmony_ci* Licensed under the Apache License, Version 2.0 (the "License");
4d9f0492fSopenharmony_ci* you may not use this file except in compliance with the License.
5d9f0492fSopenharmony_ci* You may obtain a copy of the License at
6d9f0492fSopenharmony_ci*
7d9f0492fSopenharmony_ci*     http://www.apache.org/licenses/LICENSE-2.0
8d9f0492fSopenharmony_ci*
9d9f0492fSopenharmony_ci* Unless required by applicable law or agreed to in writing, software
10d9f0492fSopenharmony_ci* distributed under the License is distributed on an "AS IS" BASIS,
11d9f0492fSopenharmony_ci* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12d9f0492fSopenharmony_ci* See the License for the specific language governing permissions and
13d9f0492fSopenharmony_ci* limitations under the License.
14d9f0492fSopenharmony_ci*/
15d9f0492fSopenharmony_ci
16d9f0492fSopenharmony_ci#include "remount_overlay.h"
17d9f0492fSopenharmony_ci#include "securec.h"
18d9f0492fSopenharmony_ci#include "param_stub.h"
19d9f0492fSopenharmony_ci#include "mntent.h"
20d9f0492fSopenharmony_ciusing namespace std;
21d9f0492fSopenharmony_ciusing namespace testing::ext;
22d9f0492fSopenharmony_cinamespace init_ut {
23d9f0492fSopenharmony_ciclass RemountOverlayUnitTest : public testing::Test {
24d9f0492fSopenharmony_cipublic:
25d9f0492fSopenharmony_ci    static void SetUpTestCase(void) {};
26d9f0492fSopenharmony_ci    static void TearDownTestCase(void) {};
27d9f0492fSopenharmony_ci    void SetUp(void) {};
28d9f0492fSopenharmony_ci    void TearDown(void) {};
29d9f0492fSopenharmony_ci};
30d9f0492fSopenharmony_ci
31d9f0492fSopenharmony_ciHWTEST_F(RemountOverlayUnitTest, Init_MntNeedRemountTest_001, TestSize.Level0)
32d9f0492fSopenharmony_ci{
33d9f0492fSopenharmony_ci    char *path = "/test";
34d9f0492fSopenharmony_ci    bool ret = MntNeedRemount(path);
35d9f0492fSopenharmony_ci    EXPECT_EQ(ret, false);
36d9f0492fSopenharmony_ci
37d9f0492fSopenharmony_ci    char *path2 = "/";
38d9f0492fSopenharmony_ci    ret = MntNeedRemount(path2);
39d9f0492fSopenharmony_ci    EXPECT_EQ(ret, true);
40d9f0492fSopenharmony_ci}
41d9f0492fSopenharmony_ci
42d9f0492fSopenharmony_ciHWTEST_F(RemountOverlayUnitTest, Init_IsSkipRemountTest_001, TestSize.Level0)
43d9f0492fSopenharmony_ci{
44d9f0492fSopenharmony_ci    struct mntent mentry;
45d9f0492fSopenharmony_ci    bool ret = IsSkipRemount(mentry);
46d9f0492fSopenharmony_ci    EXPECT_EQ(ret, true);
47d9f0492fSopenharmony_ci
48d9f0492fSopenharmony_ci    mentry.mnt_type = "ufs";
49d9f0492fSopenharmony_ci    mentry.mnt_dir = "test";
50d9f0492fSopenharmony_ci    ret = IsSkipRemount(mentry);
51d9f0492fSopenharmony_ci    EXPECT_EQ(ret, true);
52d9f0492fSopenharmony_ci
53d9f0492fSopenharmony_ci    mentry.mnt_dir = "/";
54d9f0492fSopenharmony_ci    ret = IsSkipRemount(mentry);
55d9f0492fSopenharmony_ci    EXPECT_EQ(ret, true);
56d9f0492fSopenharmony_ci
57d9f0492fSopenharmony_ci    mentry.mnt_type = "er11ofs";
58d9f0492fSopenharmony_ci    ret = IsSkipRemount(mentry);
59d9f0492fSopenharmony_ci    EXPECT_EQ(ret, true);
60d9f0492fSopenharmony_ci
61d9f0492fSopenharmony_ci    mentry.mnt_type = "erofs";
62d9f0492fSopenharmony_ci    mentry.mnt_fsname = "/dev/block/ndm-";
63d9f0492fSopenharmony_ci    ret = IsSkipRemount(mentry);
64d9f0492fSopenharmony_ci    EXPECT_EQ(ret, true);
65d9f0492fSopenharmony_ci
66d9f0492fSopenharmony_ci    mentry.mnt_fsname = "/dev/block/dm-1";
67d9f0492fSopenharmony_ci    ret = IsSkipRemount(mentry);
68d9f0492fSopenharmony_ci    EXPECT_EQ(ret, false);
69d9f0492fSopenharmony_ci}
70d9f0492fSopenharmony_ci
71d9f0492fSopenharmony_ciHWTEST_F(RemountOverlayUnitTest, Init_ExecCommand_001, TestSize.Level0)
72d9f0492fSopenharmony_ci{
73d9f0492fSopenharmony_ci    char *nullArgv[] = {NULL};  // A valid command
74d9f0492fSopenharmony_ci    int result = ExecCommand(0, nullArgv);
75d9f0492fSopenharmony_ci    EXPECT_NE(result, 0);  // Expect success
76d9f0492fSopenharmony_ci
77d9f0492fSopenharmony_ci    char *validArgv[] = {"/bin/ls", NULL};  // A valid command
78d9f0492fSopenharmony_ci    result = ExecCommand(1, validArgv);
79d9f0492fSopenharmony_ci    EXPECT_NE(result, 0);  // Expect success
80d9f0492fSopenharmony_ci
81d9f0492fSopenharmony_ci    char *invalidArgv[] = {"/notexit/ls", NULL};  // A valid command
82d9f0492fSopenharmony_ci    result = ExecCommand(1, invalidArgv);
83d9f0492fSopenharmony_ci    EXPECT_NE(result, 0);  // Expect success
84d9f0492fSopenharmony_ci}
85d9f0492fSopenharmony_ci
86d9f0492fSopenharmony_ciHWTEST_F(RemountOverlayUnitTest, Init_GetDevSizeTest_001, TestSize.Level0)
87d9f0492fSopenharmony_ci{
88d9f0492fSopenharmony_ci    const char *fileName = "";
89d9f0492fSopenharmony_ci    int ret = GetDevSize(fileName, NULL);
90d9f0492fSopenharmony_ci    EXPECT_EQ(ret, -1);
91d9f0492fSopenharmony_ci    fileName = STARTUP_INIT_UT_PATH "/data/getDevSize";
92d9f0492fSopenharmony_ci    CheckAndCreateDir(fileName);
93d9f0492fSopenharmony_ci    ret = GetDevSize(fileName, NULL);
94d9f0492fSopenharmony_ci    EXPECT_EQ(ret, -1);
95d9f0492fSopenharmony_ci
96d9f0492fSopenharmony_ci    uint64_t devSize;
97d9f0492fSopenharmony_ci    ret = GetDevSize(fileName, &devSize);
98d9f0492fSopenharmony_ci    EXPECT_NE(ret, 0);
99d9f0492fSopenharmony_ci    remove(fileName);
100d9f0492fSopenharmony_ci}
101d9f0492fSopenharmony_ciHWTEST_F(RemountOverlayUnitTest, Init_FormatExt4Test_001, TestSize.Level0)
102d9f0492fSopenharmony_ci{
103d9f0492fSopenharmony_ci    const char *fileName = "";
104d9f0492fSopenharmony_ci    int ret = GetDevSize(fileName, NULL);
105d9f0492fSopenharmony_ci    EXPECT_EQ(ret, -1);
106d9f0492fSopenharmony_ci    fileName = STARTUP_INIT_UT_PATH "/data/getDevSize";
107d9f0492fSopenharmony_ci    CheckAndCreateDir(fileName);
108d9f0492fSopenharmony_ci    ret = GetDevSize(fileName, NULL);
109d9f0492fSopenharmony_ci    EXPECT_EQ(ret, -1);
110d9f0492fSopenharmony_ci
111d9f0492fSopenharmony_ci    uint64_t devSize;
112d9f0492fSopenharmony_ci    ret = GetDevSize(fileName, &devSize);
113d9f0492fSopenharmony_ci    EXPECT_NE(ret, 0);
114d9f0492fSopenharmony_ci    remove(fileName);
115d9f0492fSopenharmony_ci}
116d9f0492fSopenharmony_ci
117d9f0492fSopenharmony_ci}