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 #include "erofs_mount_overlay.h"
16 #include "securec.h"
17 #include "param_stub.h"
18 #include "init_utils.h"
19 using namespace std;
20 using namespace testing::ext;
21 namespace init_ut {
22 class ErofsMountUnitTest : 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 };
HWTEST_F(ErofsMountUnitTest, Init_AllocDmName_001, TestSize.Level0)29 HWTEST_F(ErofsMountUnitTest, Init_AllocDmName_001, TestSize.Level0)
30 {
31     char nameExt4[MAX_BUFFER_LEN] = {0};
32     char nameRofs[MAX_BUFFER_LEN] = {0};
33     const char *devName = STARTUP_INIT_UT_PATH"/data/erofs/mount/rofs";
34     AllocDmName(devName, nameRofs, MAX_BUFFER_LEN, nameExt4, MAX_BUFFER_LEN);
35 }
HWTEST_F(ErofsMountUnitTest, Init_LookupErofsEnd_001, TestSize.Level0)36 HWTEST_F(ErofsMountUnitTest, Init_LookupErofsEnd_001, TestSize.Level0)
37 {
38     const char *devMount = STARTUP_INIT_UT_PATH"/data/erofs/mount/lookup";
39     int ret = LookupErofsEnd(devMount);
40     EXPECT_EQ(ret, 0);
41     mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
42     CheckAndCreatFile(devMount, mode);
43     int fd = open(devMount, O_WRONLY | O_TRUNC);
44     void *data = calloc(1, EROFS_SUPER_BLOCK_START_POSITION);
45     ret = write(fd, data, EROFS_SUPER_BLOCK_START_POSITION);
46     EXPECT_EQ(ret, EROFS_SUPER_BLOCK_START_POSITION);
47 
48     close(fd);
49     free(data);
50     ret = LookupErofsEnd(devMount);
51     EXPECT_EQ(ret, 0);
52     struct erofs_super_block sb;
53     sb.magic = EROFS_SUPER_MAGIC;
54     sb.blocks = 1;
55     data = calloc(1, sizeof(sb));
56     memcpy_s(data, EROFS_SUPER_BLOCK_START_POSITION, &sb, sizeof(sb));
57     fd = open(devMount, O_WRONLY | O_APPEND);
58     ret = write(fd, data, sizeof(sb));
59     EXPECT_EQ(ret, sizeof(sb));
60 
61     close(fd);
62     free(data);
63     ret = LookupErofsEnd(devMount);
64     EXPECT_NE(ret, 0);
65     remove(devMount);
66 }
67 
68 }