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_remount_overlay.h"
17 #include "securec.h"
18 #include "param_stub.h"
19 #include "fs_manager.h"
20 using namespace std;
21 using namespace testing::ext;
22 namespace init_ut {
23 class ErofsRemountUnitTest : public testing::Test {
24 public:
SetUpTestCase(void)25     static void SetUpTestCase(void) {};
TearDownTestCase(void)26     static void TearDownTestCase(void) {};
SetUp(void)27     void SetUp(void) {};
TearDown(void)28     void TearDown(void) {};
29 };
30 
HWTEST_F(ErofsRemountUnitTest, Init_GetRemountResult_001, TestSize.Level0)31 HWTEST_F(ErofsRemountUnitTest, Init_GetRemountResult_001, TestSize.Level0)
32 {
33     const char *cmdLine;
34     rmdir(REMOUNT_RESULT_PATH);
35     SetRemountResultFlag(true);
36 
37     CheckAndCreateDir(REMOUNT_RESULT_FLAG);
38     SetRemountResultFlag(false);
39     SetRemountResultFlag(true);
40     rmdir(REMOUNT_RESULT_PATH);
41 }
42 
HWTEST_F(ErofsRemountUnitTest, Init_Modem2Exchange_001, TestSize.Level0)43 HWTEST_F(ErofsRemountUnitTest, Init_Modem2Exchange_001, TestSize.Level0)
44 {
45     const char *srcPath = STARTUP_INIT_UT_PATH"/data/modem2Exchange";
46     const char *targetPath = STARTUP_INIT_UT_PATH"/data/modem2Exchangetarget";
47 
48     CheckAndCreateDir(srcPath);
49     CheckAndCreateDir(targetPath);
50 
51     SetStubResult(STUB_MOUNT, -1);
52     int ret = Modem2Exchange(srcPath, targetPath);
53     EXPECT_EQ(ret, -1);
54 
55     SetStubResult(STUB_MOUNT, 0);
56     ret = Modem2Exchange(srcPath, targetPath);
57     EXPECT_EQ(ret, 0);
58 
59     rmdir(srcPath);
60     rmdir(targetPath);
61 }
62 
HWTEST_F(ErofsRemountUnitTest, Init_ExchangeToMode_001, TestSize.Level0)63 HWTEST_F(ErofsRemountUnitTest, Init_ExchangeToMode_001, TestSize.Level0)
64 {
65     const char *srcPath = STARTUP_INIT_UT_PATH"/data/Exchange2Mode";
66     const char *targetPath = STARTUP_INIT_UT_PATH"/data/Exchange2Modetarget";
67 
68     int ret = Exchange2Modem(srcPath, targetPath);
69     EXPECT_EQ(ret, 0);
70 
71     CheckAndCreateDir(srcPath);
72     CheckAndCreateDir(targetPath);
73 
74     SetStubResult(STUB_MOUNT, 0);
75     SetStubResult(STUB_UMOUNT, 0);
76 
77     ret = Exchange2Modem(srcPath, targetPath);
78     EXPECT_EQ(ret, 0);
79 
80     CheckAndCreateDir(targetPath);
81     SetStubResult(STUB_MOUNT, -1);
82     SetStubResult(STUB_UMOUNT, -1);
83 
84     Exchange2Modem(srcPath, targetPath);
85     EXPECT_EQ(ret, 0);
86 
87     rmdir(srcPath);
88     rmdir(targetPath);
89 }
90 
HWTEST_F(ErofsRemountUnitTest, Init_OverlayRemountVendorPre_001, TestSize.Level0)91 HWTEST_F(ErofsRemountUnitTest, Init_OverlayRemountVendorPre_001, TestSize.Level0)
92 {
93     CheckAndCreateDir(MODEM_DRIVER_MNT_PATH);
94     CheckAndCreateDir(MODEM_VENDOR_MNT_PATH);
95     CheckAndCreateDir(MODEM_FW_MNT_PATH);
96     OverlayRemountVendorPre();
97     rmdir(MODEM_DRIVER_MNT_PATH);
98     rmdir(MODEM_VENDOR_MNT_PATH);
99     rmdir(MODEM_FW_MNT_PATH);
100 
101     OverlayRemountVendorPost();
102 }
103 
HWTEST_F(ErofsRemountUnitTest, Init_RemountOverlayTest_001, TestSize.Level0)104 HWTEST_F(ErofsRemountUnitTest, Init_RemountOverlayTest_001, TestSize.Level0)
105 {
106     int ret = RemountOverlay();
107     EXPECT_EQ(ret, 0);
108 }
109 }