diff --git a/test/BUILD.gn b/test/BUILD.gn index b18aa1153269895e1a9e57b3e0a6662b649f30c5..df808f4c107e41718af7df6ab02f1d3043b4a576 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -23,6 +23,7 @@ group("testgroup") { "moduletest:paramtestmodule", "unittest:init_dmverify_unittest", "unittest:init_fshvb_unittest", + "unittest:init_switch_root_unittest", "unittest:init_unittest", "unittest/single_test/init_firststage:init_firststage_unittest", "unittest/single_test/fstab_mount:fstab_mount_unittest", diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 00e7d3bad317868a19446dce6a5ddc71fa983f42..b9af4fee6da421e00a43305d35f57c62cdfe1fac 100755 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -556,3 +556,49 @@ ohos_unittest("init_fshvb_unittest") { } cflags_cc = [ "-fexceptions" ] } + +ohos_unittest("init_switch_root_unittest") { + module_out_path = "init/init" + sources = [ + "//base/startup/init/interfaces/innerkits/fs_manager/switch_root/switch_root.c", + "//base/startup/init/services/utils/init_utils.c", + "//base/startup/init/test/unittest/fs_manager/switch_root/switch_root_unittest.cpp", + "//base/startup/init/test/unittest/fs_manager/switch_root/switch_root_mock.c", + ] + + include_dirs = [ + "//base/startup/init/interfaces/innerkits/include/fs_manager", + "//base/startup/init/interfaces/innerkits/include", + "//base/startup/init/interfaces/innerkits/fs_manager/switch_root/include", + "//base/startup/init/services/log", + "//base/startup/init/test/unittest/fs_manager/switch_root", + ] + + deps = [ + "//base/startup/init/interfaces/innerkits:libbegetutil", + "//base/startup/init/interfaces/innerkits/fs_manager:libfsmanager_static", + ] + + defines = [ "STARTUP_INIT_UT_PATH =\"/data/init_ut\"" ] + + configs = [] + + external_deps = [ + "cJSON:cjson", + "c_utils:utils", + "googletest:gtest", + "hilog:libhilog", + ] + + cflags = [ + "-Wno-implicit-fallthrough", + "-Wno-unused-function", + "-Dprivate=public", + "-Dprotected=public", + "-DReadFstabFromFile=LocalReadFstabFromFile", + "-Dchdir=LocalChdir", + "-Dmount=LocalMount", + "-Dchroot=LocalChroot", + ] + cflags_cc = [ "-fexceptions" ] +} diff --git a/test/unittest/fs_manager/switch_root/switch_root_mock.c b/test/unittest/fs_manager/switch_root/switch_root_mock.c new file mode 100644 index 0000000000000000000000000000000000000000..67ac21fbe8493009e63ea66377b0c22c6890e614 --- /dev/null +++ b/test/unittest/fs_manager/switch_root/switch_root_mock.c @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "switch_root_mock.h" +#include +int g_openDebug = 0; + +int LocalChdir(const char *file) +{ + (void)(file); + if (OpenDebug() == CHDIR_DEBUG) { + return -1; + } + return 0; +} + +int LocalMount(const char *source, const char *target, + const char *filesystemtype, unsigned long mountflags, + const void *data) +{ + (void)(source); + (void)(target); + (void)(filesystemtype); + (void)(mountflags); + (void)(data); + if (OpenDebug() == MOUNT_DEBUG) { + return -1; + } + return 0; +} + +int LocalChroot(const char *file) +{ + (void)(file); + if (OpenDebug() == CHROOT_DEBUG) { + return -1; + } + return 0; +} + +Fstab *LocalReadFstabFromFile(const char *file, bool procMounts) +{ + if (OpenDebug() == READ_FSTAB_FROM_FILE_DEBUG) { + return NULL; + } + Fstab *fstab = NULL; + fstab = (Fstab *)calloc(1, sizeof(Fstab)); + + FstabItem *item = NULL; + item = (FstabItem *)malloc(sizeof(FstabItem)); + + size_t length = strlen("/dev/block/dm-0") + 1; + item->deviceName = malloc(length * sizeof(char)); + strcpy(item->deviceName, "/dev/block/dm-0"); + + length = strlen("/") + 1; + item->mountPoint = malloc(length * sizeof(char)); + strcpy(item->mountPoint, "/"); + + length = strlen("erofs") + 1; + item->fsType = malloc(length * sizeof(char)); + strcpy(item->fsType, "erofs"); + + length = strlen("ro,relatime,hmmac=inherit_parent,seclabel") + 1; + item->mountOptions = malloc(length * sizeof(char)); + strcpy(item->mountOptions, "ro,relatime,hmmac=inherit_parent,seclabel"); + + item->fsManagerFlags = 0; + item->next = NULL; + + if (fstab->tail == NULL) { + fstab->head = fstab->tail = item; + } else { + fstab->tail->next = item; + fstab->tail = item; + } + + return fstab; +} + +int OpenDebug() +{ + return g_openDebug; +} + +void SetOpenDebug(int openDebug) +{ + g_openDebug = openDebug; +} \ No newline at end of file diff --git a/test/unittest/fs_manager/switch_root/switch_root_mock.h b/test/unittest/fs_manager/switch_root/switch_root_mock.h new file mode 100644 index 0000000000000000000000000000000000000000..612f7f2ad6c3224a8e05dc648fe8bbc53654d09a --- /dev/null +++ b/test/unittest/fs_manager/switch_root/switch_root_mock.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "fs_manager.h" + +#define READ_FSTAB_FROM_FILE_DEBUG 1 +#define CHDIR_DEBUG 2 +#define MOUNT_DEBUG 3 +#define CHROOT_DEBUG 4 +int OpenDebug(); +void SetOpenDebug(int openDebug); +Fstab *LocalReadFstabFromFile(const char *file, bool procMounts); +int LocalChroot(const char *file); +int LocalMount(const char *source, const char *target, + const char *filesystemtype, unsigned long mountflags, + const void *data); +int LocalChdir(const char *file); \ No newline at end of file diff --git a/test/unittest/fs_manager/switch_root/switch_root_unittest.cpp b/test/unittest/fs_manager/switch_root/switch_root_unittest.cpp new file mode 100644 index 0000000000000000000000000000000000000000..deb80f6b9ac2b7e8b2c3f43bc4c25bd4c095cffa --- /dev/null +++ b/test/unittest/fs_manager/switch_root/switch_root_unittest.cpp @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +extern "C" { +#include "switch_root_mock.h" +#include "switch_root.h" +#include "init_log.h" +#include "beget_ext.h" +} + +using namespace testing::ext; +using namespace std; + +namespace init_ut { +class SwitchRootUnitTest : public testing::Test { +public: + static void SetUpTestCase(void) {}; + static void TearDownTestCase(void) {}; +}; + +HWTEST_F(SwitchRootUnitTest, SwitchRoot_001, TestSize.Level0) +{ + int ret = 0; + ret = SwitchRoot(NULL); + EXPECT_EQ(ret, -1); +} + +HWTEST_F(SwitchRootUnitTest, SwitchRoot_002, TestSize.Level0) +{ + int ret = SwitchRoot("/test"); + EXPECT_EQ(ret, -1); +} + + +HWTEST_F(SwitchRootUnitTest, SwitchRoot_003, TestSize.Level0) +{ + int ret = SwitchRoot("/usr"); + EXPECT_EQ(ret, 0); +} + +HWTEST_F(SwitchRootUnitTest, SwitchRoot_004, TestSize.Level0) +{ + SetOpenDebug(READ_FSTAB_FROM_FILE_DEBUG); + int ret = SwitchRoot("/dev"); + EXPECT_EQ(ret, -1); +} + +HWTEST_F(SwitchRootUnitTest, SwitchRoot_005, TestSize.Level0) +{ + SetOpenDebug(CHDIR_DEBUG); + int ret = SwitchRoot("/dev"); + EXPECT_EQ(ret, -1); +} + +HWTEST_F(SwitchRootUnitTest, SwitchRoot_006, TestSize.Level0) +{ + SetOpenDebug(MOUNT_DEBUG); + int ret = SwitchRoot("/dev"); + EXPECT_EQ(ret, -1); +} + +HWTEST_F(SwitchRootUnitTest, SwitchRoot_007, TestSize.Level0) +{ + SetOpenDebug(CHROOT_DEBUG); + int ret = SwitchRoot("/dev"); + EXPECT_EQ(ret, -1); +} + +}