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