From 4b5331fba0cbee1b32c289a955fb9b9110a5aa87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=B5=A9?= Date: Tue, 12 Aug 2025 11:50:13 +0800 Subject: [PATCH] add ut MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 杨浩 --- .../fs_manager/libfs_dm/fs_dm_snapshot.c | 2 +- .../innerkits/include/fs_manager/fs_manager.h | 10 + test/BUILD.gn | 1 + test/mock/init/fs_manager/fs_dm_mock.c | 187 +++++ test/mock/init/include/fs_dm_mock.h | 84 +++ .../libs}/include/func_wrapper.h | 34 +- .../libs}/src/func_wrapper.cpp | 112 ++- .../unittest/single_test/fstab_mount/BUILD.gn | 4 +- .../fstab_mount/src/fstab_mount_test.cpp | 2 +- .../single_test/libfs_dm_snapshot/BUILD.gn | 88 +++ .../include/libfs_dm_snapshot_test.h | 29 + .../libfs_dm_snapshot/include/libfs_dm_test.h | 27 + .../src/libfs_dm_snapshot_test.cpp | 637 ++++++++++++++++++ .../libfs_dm_snapshot/src/libfs_dm_test.cpp | 614 +++++++++++++++++ 14 files changed, 1823 insertions(+), 8 deletions(-) create mode 100644 test/mock/init/fs_manager/fs_dm_mock.c create mode 100644 test/mock/init/include/fs_dm_mock.h rename test/{unittest/single_test/fstab_mount => mock/libs}/include/func_wrapper.h (68%) rename test/{unittest/single_test/fstab_mount => mock/libs}/src/func_wrapper.cpp (62%) create mode 100644 test/unittest/single_test/libfs_dm_snapshot/BUILD.gn create mode 100644 test/unittest/single_test/libfs_dm_snapshot/include/libfs_dm_snapshot_test.h create mode 100644 test/unittest/single_test/libfs_dm_snapshot/include/libfs_dm_test.h create mode 100644 test/unittest/single_test/libfs_dm_snapshot/src/libfs_dm_snapshot_test.cpp create mode 100644 test/unittest/single_test/libfs_dm_snapshot/src/libfs_dm_test.cpp diff --git a/interfaces/innerkits/fs_manager/libfs_dm/fs_dm_snapshot.c b/interfaces/innerkits/fs_manager/libfs_dm/fs_dm_snapshot.c index 89f960fc7..9838199c3 100644 --- a/interfaces/innerkits/fs_manager/libfs_dm/fs_dm_snapshot.c +++ b/interfaces/innerkits/fs_manager/libfs_dm/fs_dm_snapshot.c @@ -99,7 +99,7 @@ int FsDmSwitchToSnapshotMerge(const char *devName, DmSnapshotTarget *target) return rc; } -static bool ParseStatusText(char *data, StatusInfo *processInfo) +INIT_STATIC bool ParseStatusText(char *data, StatusInfo *processInfo) { BEGET_LOGI("ParseStatusText start \"%s\"", data); int args = sscanf_s(data, "%lu %lu %lu", &processInfo->sectors_allocated, diff --git a/interfaces/innerkits/include/fs_manager/fs_manager.h b/interfaces/innerkits/include/fs_manager/fs_manager.h index a77a3a349..55a5f2d9b 100644 --- a/interfaces/innerkits/include/fs_manager/fs_manager.h +++ b/interfaces/innerkits/include/fs_manager/fs_manager.h @@ -25,6 +25,16 @@ extern "C" { #endif #endif +#ifndef STARTUP_INIT_TEST +#ifndef INIT_STATIC +#define INIT_STATIC static +#endif +#else +#ifndef INIT_STATIC +#define INIT_STATIC +#endif +#endif + /* Fs manager flags definition */ #define FS_MANAGER_CHECK 0x00000001 #define FS_MANAGER_WAIT 0x00000002 diff --git a/test/BUILD.gn b/test/BUILD.gn index b18aa1153..7d05ec86d 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -26,6 +26,7 @@ group("testgroup") { "unittest:init_unittest", "unittest/single_test/init_firststage:init_firststage_unittest", "unittest/single_test/fstab_mount:fstab_mount_unittest", + "unittest/single_test/libfs_dm_snapshot:fs_dm_snapshot_unittest", ] } else { if (ohos_kernel_type == "liteos_a" || ohos_kernel_type == "linux") { diff --git a/test/mock/init/fs_manager/fs_dm_mock.c b/test/mock/init/fs_manager/fs_dm_mock.c new file mode 100644 index 000000000..b3d7dc8c9 --- /dev/null +++ b/test/mock/init/fs_manager/fs_dm_mock.c @@ -0,0 +1,187 @@ +/* + * 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 "fs_dm_mock.h" + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif +#endif + +// start wrap FsDmInitDmDev +static FsdminitdmdevFunc g_FsDmInitDmDev = NULL; +void UpdateFsdminitdmdevFunc(FsdminitdmdevFunc func) +{ + g_FsDmInitDmDev = func; +} + +int __wrap_FsDmInitDmDev(char *devPath, bool useSocket) +{ + if (g_FsDmInitDmDev) { + return g_FsDmInitDmDev(devPath, useSocket); + } else { + return __real_FsDmInitDmDev(devPath, useSocket); + } +} + +// start wrap FsDmCreateDevice +static FsdmcreatedeviceFunc g_FsDmCreateDevice = NULL; +void UpdateFsdmcreatedeviceFunc(FsdmcreatedeviceFunc func) +{ + g_FsDmCreateDevice = func; +} + +int __wrap_FsDmCreateDevice(char **dmDevPath, const char *devName, DmVerityTarget *target) +{ + if (g_FsDmCreateDevice) { + return g_FsDmCreateDevice(dmDevPath, devName, target); + } else { + return __real_FsDmCreateDevice(dmDevPath, devName, target); + } +} + +// start wrap FsDmRemoveDevice +static FsdmremovedeviceFunc g_FsDmRemoveDevice = NULL; +void UpdateFsdmremovedeviceFunc(FsdmremovedeviceFunc func) +{ + g_FsDmRemoveDevice = func; +} + +int __wrap_FsDmRemoveDevice(const char *devName) +{ + if (g_FsDmRemoveDevice) { + return g_FsDmRemoveDevice(devName); + } else { + return __real_FsDmRemoveDevice(devName); + } +} + +// start wrap LoadDmDeviceTable +static LoaddmdevicetableFunc g_LoadDmDeviceTable = NULL; +void UpdateLoaddmdevicetableFunc(LoaddmdevicetableFunc func) +{ + g_LoadDmDeviceTable = func; +} + +int __wrap_LoadDmDeviceTable(int fd, const char *devName, DmVerityTarget *target, int dmType) +{ + if (g_LoadDmDeviceTable) { + return g_LoadDmDeviceTable(fd, devName, target, dmType); + } else { + return __real_LoadDmDeviceTable(fd, devName, target, dmType); + } +} + +// start wrap DmGetDeviceName +static DmgetdevicenameFunc g_DmGetDeviceName = NULL; +void UpdateDmgetdevicenameFunc(DmgetdevicenameFunc func) +{ + g_DmGetDeviceName = func; +} + +int __wrap_DmGetDeviceName(int fd, const char *devName, char *outDevName, const uint64_t outDevNameLen) +{ + if (g_DmGetDeviceName) { + return g_DmGetDeviceName(fd, devName, outDevName, outDevNameLen); + } else { + return __real_DmGetDeviceName(fd, devName, outDevName, outDevNameLen); + } +} + +// start wrap ActiveDmDevice +static ActivedmdeviceFunc g_ActiveDmDevice = NULL; +void UpdateActivedmdeviceFunc(ActivedmdeviceFunc func) +{ + g_ActiveDmDevice = func; +} + +int __wrap_ActiveDmDevice(int fd, const char *devName) +{ + if (g_ActiveDmDevice) { + return g_ActiveDmDevice(fd, devName); + } else { + return __real_ActiveDmDevice(fd, devName); + } +} + +// start wrap InitDmIo +static InitdmioFunc g_InitDmIo = NULL; +void UpdateInitdmioFunc(InitdmioFunc func) +{ + g_InitDmIo = func; +} + +int __wrap_InitDmIo(struct dm_ioctl *io, const char *devName) +{ + if (g_InitDmIo) { + return g_InitDmIo(io, devName); + } else { + return __real_InitDmIo(io, devName); + } +} + +// start wrap CreateDmDev +static CreatedmdevFunc g_CreateDmDev = NULL; +void UpdateCreatedmdevFunc(CreatedmdevFunc func) +{ + g_CreateDmDev = func; +} + +int __wrap_CreateDmDev(int fd, const char *devName) +{ + if (g_CreateDmDev) { + return g_CreateDmDev(fd, devName); + } else { + return __real_CreateDmDev(fd, devName); + } +} + +// start wrap GetDmDevPath +static GetdmdevpathFunc g_GetDmDevPath = NULL; +void UpdateGetdmdevpathFunc(GetdmdevpathFunc func) +{ + g_GetDmDevPath = func; +} + +int __wrap_GetDmDevPath(int fd, char **dmDevPath, const char *devName) +{ + if (g_GetDmDevPath) { + return g_GetDmDevPath(fd, dmDevPath, devName); + } else { + return __real_GetDmDevPath(fd, dmDevPath, devName); + } +} + +// start wrap GetDmStatusInfo +static GetdmstatusinfoFunc g_GetDmStatusInfo = NULL; +void UpdateGetdmstatusinfoFunc(GetdmstatusinfoFunc func) +{ + g_GetDmStatusInfo = func; +} + +bool __wrap_GetDmStatusInfo(const char *name, struct dm_ioctl *io) +{ + if (g_GetDmStatusInfo) { + return g_GetDmStatusInfo(name, io); + } else { + return __real_GetDmStatusInfo(name, io); + } +} + +#ifdef __cplusplus +#if __cplusplus +} +#endif +#endif diff --git a/test/mock/init/include/fs_dm_mock.h b/test/mock/init/include/fs_dm_mock.h new file mode 100644 index 000000000..f373f6635 --- /dev/null +++ b/test/mock/init/include/fs_dm_mock.h @@ -0,0 +1,84 @@ +/* + * 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. + */ +#ifndef FS_DM_MOCK_H +#define FS_DM_MOCK_H +#include "fs_dm.h" +#include +#include +#include +#include +#include +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif +#endif + +// for wrapper FsDmInitDmDev; +int __real_FsDmInitDmDev(char *devPath, bool useSocket); +typedef int (*FsdminitdmdevFunc)(char *devPath, bool useSocket); +void UpdateFsdminitdmdevFunc(FsdminitdmdevFunc func); + +// for wrapper FsDmCreateDevice; +int __real_FsDmCreateDevice(char **dmDevPath, const char *devName, DmVerityTarget *target); +typedef int (*FsdmcreatedeviceFunc)(char **dmDevPath, const char *devName, DmVerityTarget *target); +void UpdateFsdmcreatedeviceFunc(FsdmcreatedeviceFunc func); + +// for wrapper FsDmRemoveDevice; +int __real_FsDmRemoveDevice(const char *devName); +typedef int (*FsdmremovedeviceFunc)(const char *devName); +void UpdateFsdmremovedeviceFunc(FsdmremovedeviceFunc func); + +// for wrapper LoadDmDeviceTable; +int __real_LoadDmDeviceTable(int fd, const char *devName, DmVerityTarget *target, int dmType); +typedef int (*LoaddmdevicetableFunc)(int fd, const char *devName, DmVerityTarget *target, int dmType); +void UpdateLoaddmdevicetableFunc(LoaddmdevicetableFunc func); + +// for wrapper DmGetDeviceName; +int __real_DmGetDeviceName(int fd, const char *devName, char *outDevName, const uint64_t outDevNameLen); +typedef int (*DmgetdevicenameFunc)(int fd, const char *devName, char *outDevName, const uint64_t outDevNameLen); +void UpdateDmgetdevicenameFunc(DmgetdevicenameFunc func); + +// for wrapper ActiveDmDevice; +int __real_ActiveDmDevice(int fd, const char *devName); +typedef int (*ActivedmdeviceFunc)(int fd, const char *devName); +void UpdateActivedmdeviceFunc(ActivedmdeviceFunc func); + +// for wrapper InitDmIo; +int __real_InitDmIo(struct dm_ioctl *io, const char *devName); +typedef int (*InitdmioFunc)(struct dm_ioctl *io, const char *devName); +void UpdateInitdmioFunc(InitdmioFunc func); + +// for wrapper CreateDmDev; +int __real_CreateDmDev(int fd, const char *devName); +typedef int (*CreatedmdevFunc)(int fd, const char *devName); +void UpdateCreatedmdevFunc(CreatedmdevFunc func); + +// for wrapper GetDmDevPath; +int __real_GetDmDevPath(int fd, char **dmDevPath, const char *devName); +typedef int (*GetdmdevpathFunc)(int fd, char **dmDevPath, const char *devName); +void UpdateGetdmdevpathFunc(GetdmdevpathFunc func); + +// for wrapper GetDmStatusInfo; +bool __real_GetDmStatusInfo(const char *name, struct dm_ioctl *io); +typedef bool (*GetdmstatusinfoFunc)(const char *name, struct dm_ioctl *io); +void UpdateGetdmstatusinfoFunc(GetdmstatusinfoFunc func); + +#ifdef __cplusplus +#if __cplusplus +} +#endif +#endif +#endif // TEST_WRAPPER_H \ No newline at end of file diff --git a/test/unittest/single_test/fstab_mount/include/func_wrapper.h b/test/mock/libs/include/func_wrapper.h similarity index 68% rename from test/unittest/single_test/fstab_mount/include/func_wrapper.h rename to test/mock/libs/include/func_wrapper.h index 477c53eb5..43b84f8ac 100644 --- a/test/unittest/single_test/fstab_mount/include/func_wrapper.h +++ b/test/mock/libs/include/func_wrapper.h @@ -19,6 +19,7 @@ #include #include #include +#include #ifdef __cplusplus #if __cplusplus extern "C" { @@ -57,10 +58,39 @@ typedef int (*StatFunc)(const char *pathname, struct stat *buf); void UpdateStatFunc(StatFunc func); // for wrapper snprintf_s; -size_t __real_snprintf_s(char *strDest, size_t destMax, size_t count, const char *format, ...); -typedef size_t (*SnprintfSFunc)(char *strDest, size_t destMax, size_t count, const char *format, ...); +typedef size_t (*SnprintfSFunc)(char *strDest, size_t destMax, size_t count, const char *format, va_list args); void UpdateSnprintfSFunc(SnprintfSFunc func); +// for wrapper open; +int __real_open(const char *pathname, int flag); +typedef int (*OpenFunc)(const char *pathname, int flag); +void UpdateOpenFunc(OpenFunc func); + +// for wrapper close; +int __real_close(int fd); +typedef int (*CloseFunc)(int fd); +void UpdateCloseFunc(CloseFunc func); + +// for wrapper strcpy_s; +int __real_strcpy_s(char *dest, size_t destMax, const char *src); +typedef int (*StrcpySFunc)(char *dest, size_t destMax, const char *src); +void UpdateStrcpySFunc(StrcpySFunc func); + +// for wrapper ioctl; +int __real_ioctl(int fd, int req, ...); +typedef int (*IoctlFunc)(int fd, int req, va_list args); +void UpdateIoctlFunc(IoctlFunc func); + +// for wrapper calloc; +void* __real_calloc(size_t m, size_t n); +typedef void* (*CallocFunc)(size_t m, size_t n); +void UpdateCallocFunc(CallocFunc func); + +// for wrapper minor; +int __real_minor(dev_t dev); +typedef int (*MinorFunc)(dev_t dev); +void UpdateMinorFunc(MinorFunc func); + #ifdef __cplusplus #if __cplusplus } diff --git a/test/unittest/single_test/fstab_mount/src/func_wrapper.cpp b/test/mock/libs/src/func_wrapper.cpp similarity index 62% rename from test/unittest/single_test/fstab_mount/src/func_wrapper.cpp rename to test/mock/libs/src/func_wrapper.cpp index 911809441..60a510857 100644 --- a/test/unittest/single_test/fstab_mount/src/func_wrapper.cpp +++ b/test/mock/libs/src/func_wrapper.cpp @@ -13,7 +13,7 @@ * limitations under the License. */ #include "func_wrapper.h" - +#include "securec.h" #ifdef __cplusplus #if __cplusplus extern "C" { @@ -26,6 +26,7 @@ void UpdateStrdupFunc(StrdupFunc func) { g_strdup = func; } + char* __wrap_strdup(const char* string) { if (g_strdup) { @@ -41,6 +42,7 @@ void UpdateMallocFunc(MallocFunc func) { g_malloc = func; } + void* __wrap_malloc(size_t size) { if (g_malloc) { @@ -56,6 +58,7 @@ void UpdateStrncatSFunc(StrncatSFunc func) { g_strncat_s = func; } + int __wrap_strncat_s(char *strDest, size_t destMax, const char *strSrc, size_t count) { if (g_strncat_s) { @@ -71,6 +74,7 @@ void UpdateMkdirFunc(MkdirFunc func) { g_mkdir = func; } + int __wrap_mkdir(const char *path, mode_t mode) { if (g_mkdir) { @@ -86,6 +90,7 @@ void UpdateMountFunc(MountFunc func) { g_mount = func; } + int __wrap_mount(const char *source, const char *target, const char *fsType, unsigned long flags, const void *data) { @@ -102,6 +107,7 @@ void UpdateStatFunc(StatFunc func) { g_stat = func; } + int __wrap_stat(const char *pathname, struct stat *buf) { if (g_stat) { @@ -117,6 +123,7 @@ void UpdateSnprintfSFunc(SnprintfSFunc func) { g_snprintf_s = func; } + size_t __wrap_snprintf_s(char *strDest, size_t destMax, size_t count, const char *format, ...) { va_list args; @@ -125,12 +132,113 @@ size_t __wrap_snprintf_s(char *strDest, size_t destMax, size_t count, const char if (g_snprintf_s) { rc = g_snprintf_s(strDest, destMax, count, format, args); } else { - rc = __real_snprintf_s(strDest, destMax, count, format, args); + rc = vsnprintf_s(strDest, destMax, count, format, args); } va_end(args); return rc; } +// start wrap open +static OpenFunc g_open = NULL; +void UpdateOpenFunc(OpenFunc func) +{ + g_open = func; +} + +int __wrap_open(const char *pathname, int flag) +{ + if (g_open) { + return g_open(pathname, flag); + } else { + return __real_open(pathname, flag); + } +} + +// start wrap close +static CloseFunc g_close = NULL; +void UpdateCloseFunc(CloseFunc func) +{ + g_close = func; +} + +int __wrap_close(int fd) +{ + if (g_close) { + return g_close(fd); + } else { + return __real_close(fd); + } +} + +// start wrap strcpy_s +static StrcpySFunc g_strcpy_s = NULL; +void UpdateStrcpySFunc(StrcpySFunc func) +{ + g_strcpy_s = func; +} + +int __wrap_strcpy_s(char *dest, size_t destMax, const char *src) +{ + if (g_strcpy_s) { + return g_strcpy_s(dest, destMax, src); + } else { + return __real_strcpy_s(dest, destMax, src); + } +} + +// start wrap ioctl +static IoctlFunc g_ioctl = NULL; +void UpdateIoctlFunc(IoctlFunc func) +{ + g_ioctl = func; +} + +int __wrap_ioctl(int fd, int req, ...) +{ + va_list args; + va_start(args, req); + int rc; + if (g_ioctl) { + rc = g_ioctl(fd, req, args); + } else { + rc = __real_ioctl(fd, req, args); + } + va_end(args); + return rc; +} + +// start wrap calloc +static CallocFunc g_calloc = NULL; +void UpdateCallocFunc(CallocFunc func) +{ + g_calloc = func; +} + +void* __wrap_calloc(size_t m, size_t n) +{ + if (g_calloc) { + return g_calloc(m, n); + } else { + return __real_calloc(m, n); + } +} + +// start wrap minor +static MinorFunc g_minor = NULL; +void UpdateMinorFunc(MinorFunc func) +{ + g_minor = func; +} + +int __wrap_minor(dev_t dev) +{ + if (g_minor) { + return g_minor(dev); + } else { + return __real_minor(dev); + } +} + #ifdef __cplusplus #if __cplusplus } diff --git a/test/unittest/single_test/fstab_mount/BUILD.gn b/test/unittest/single_test/fstab_mount/BUILD.gn index 8edf40317..d7eb98ad4 100644 --- a/test/unittest/single_test/fstab_mount/BUILD.gn +++ b/test/unittest/single_test/fstab_mount/BUILD.gn @@ -24,13 +24,13 @@ ohos_unittest("fstab_mount_unittest") { "//base/startup/init/ueventd/include", "//base/startup/init/interfaces/innerkits/fs_manager/switch_root/include", "//base/startup/init/interfaces/innerkits/init_module_engine/include", - "//base/startup/init/test/mock/libc/include" + "//base/startup/init/test/mock/libs/include" ] sources = [ "//base/startup/init/interfaces/innerkits/fs_manager/fstab_mount.c", "//base/startup/init/test/unittest/single_test/fstab_mount/src/fstab_mount_test.cpp", - "//base/startup/init/test/unittest/single_test/fstab_mount/src/func_wrapper.cpp", + "//base/startup/init/test/mock/libs/src/func_wrapper.cpp", ] cflags_cc = [ "-fexceptions" ] diff --git a/test/unittest/single_test/fstab_mount/src/fstab_mount_test.cpp b/test/unittest/single_test/fstab_mount/src/fstab_mount_test.cpp index dc1975374..b77807cb5 100644 --- a/test/unittest/single_test/fstab_mount/src/fstab_mount_test.cpp +++ b/test/unittest/single_test/fstab_mount/src/fstab_mount_test.cpp @@ -29,7 +29,7 @@ extern "C" { #endif #endif -size_t SnprintfSReturnZero(char *strDest, size_t destMax, size_t count, const char *format, ...) +size_t SnprintfSReturnZero(char *strDest, size_t destMax, size_t count, const char *format, va_list args) { return 0; } diff --git a/test/unittest/single_test/libfs_dm_snapshot/BUILD.gn b/test/unittest/single_test/libfs_dm_snapshot/BUILD.gn new file mode 100644 index 000000000..6db57cfb8 --- /dev/null +++ b/test/unittest/single_test/libfs_dm_snapshot/BUILD.gn @@ -0,0 +1,88 @@ +# 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. +import("//base/startup/init/begetd.gni") +import("//build/test.gni") + +ohos_unittest("fs_dm_snapshot_unittest") { + module_out_path = "init/init" + include_dirs = [ + "//base/startup/init/test/mock/libs/include", + "//base/startup/init/test/mock/init/include", + "//base/startup/init/test/unittest/single_test/libfs_dm_snapshot/include", + "//base/startup/init/interfaces/innerkits/fs_manager/libfs_dm/include", + "//base/startup/init/interfaces/innerkits/include/fs_manager", + "//base/startup/init/ueventd/include" + ] + + sources = [ + "//base/startup/init/test/mock/libs/src/func_wrapper.cpp", + "//base/startup/init/test/mock/init/fs_manager/fs_dm_mock.c", + "//base/startup/init/test/unittest/single_test/libfs_dm_snapshot/src/libfs_dm_snapshot_test.cpp", + "//base/startup/init/test/unittest/single_test/libfs_dm_snapshot/src/libfs_dm_test.cpp", + "//base/startup/init/interfaces/innerkits/fs_manager/libfs_dm/fs_dm_snapshot.c", + "//base/startup/init/interfaces/innerkits/fs_manager/libfs_dm/fs_dm.c", + ] + + cflags_cc = [ "-fexceptions" ] + ldflags = [ + "-Wl,--wrap=strdup", + "-Wl,--wrap=stat", + "-Wl,--wrap=mount", + "-Wl,--wrap=mkdir", + "-Wl,--wrap=malloc", + "-Wl,--wrap=strncat_s", + "-Wl,--wrap=snprintf_s", + "-Wl,--wrap=open", + "-Wl,--wrap=close", + "-Wl,--wrap=strcpy_s", + "-Wl,--wrap=InitDmIo", + "-Wl,--wrap=ioctl", + "-Wl,--wrap=calloc", + "-Wl,--wrap=minor", + # fs_dm + "-Wl,--wrap=FsDmInitDmDev", + "-Wl,--wrap=FsDmCreateDevice", + "-Wl,--wrap=FsDmRemoveDevice", + "-Wl,--wrap=FsDmCreateLinearDevice", + "-Wl,--wrap=LoadDmDeviceTable", + "-Wl,--wrap=DmGetDeviceName", + "-Wl,--wrap=ActiveDmDevice", + "-Wl,--wrap=InitDmIo", + "-Wl,--wrap=CreateDmDev", + "-Wl,--wrap=GetDmDevPath", + "-Wl,--wrap=GetDmStatusInfo", + ] + cflags = [ + "-DRetriggerDmUeventByPath=RetriggerDmUeventByPathStub", + "-DUeventdSocketInit=UeventdSocketInitStub", + ] + deps = [ + "//base/startup/init/interfaces/innerkits:libbegetutil", + ] + + defines = [ + "STARTUP_INIT_UT_PATH =\"/data/init_ut\"", + "ASAN_DETECTOR", + "STARTUP_INIT_TEST", + ] + + configs = [] + + external_deps = [ + "bounds_checking_function:libsec_static", + "cJSON:cjson", + "c_utils:utils", + "googletest:gtest", + "hilog:libhilog", + ] +} diff --git a/test/unittest/single_test/libfs_dm_snapshot/include/libfs_dm_snapshot_test.h b/test/unittest/single_test/libfs_dm_snapshot/include/libfs_dm_snapshot_test.h new file mode 100644 index 000000000..633aaf9f2 --- /dev/null +++ b/test/unittest/single_test/libfs_dm_snapshot/include/libfs_dm_snapshot_test.h @@ -0,0 +1,29 @@ +/* + * 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. + */ + +#ifndef LIBFS_DM_SNAPSHOT_TEST_H +#define LIBFS_DM_SNAPSHOT_TEST_H +#include "fs_dm_snapshot.h" + +#ifdef __cplusplus +extern "C" { +#endif + +bool ParseStatusText(char *data, StatusInfo *processInfo); + +#ifdef __cplusplus +} +#endif +#endif \ No newline at end of file diff --git a/test/unittest/single_test/libfs_dm_snapshot/include/libfs_dm_test.h b/test/unittest/single_test/libfs_dm_snapshot/include/libfs_dm_test.h new file mode 100644 index 000000000..6074c4660 --- /dev/null +++ b/test/unittest/single_test/libfs_dm_snapshot/include/libfs_dm_test.h @@ -0,0 +1,27 @@ +/* + * 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. + */ + +#ifndef LIBFS_DM_TEST_H +#define LIBFS_DM_TEST_H +#include "fs_dm.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef __cplusplus +} +#endif +#endif \ No newline at end of file diff --git a/test/unittest/single_test/libfs_dm_snapshot/src/libfs_dm_snapshot_test.cpp b/test/unittest/single_test/libfs_dm_snapshot/src/libfs_dm_snapshot_test.cpp new file mode 100644 index 000000000..5a0fabc95 --- /dev/null +++ b/test/unittest/single_test/libfs_dm_snapshot/src/libfs_dm_snapshot_test.cpp @@ -0,0 +1,637 @@ +/* + * 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 "libfs_dm_snapshot_test.h" +#include +#include "func_wrapper.h" +#include "fs_dm_mock.h" +#include +#include +#include +#include "init_utils.h" +#include "securec.h" +using namespace testing; +using namespace testing::ext; +#define STRSIZE 64 +#define TESTIODATA "10 10 10" +#ifdef __cplusplus +#if __cplusplus +extern "C" +{ +#endif +#endif + +#ifdef __cplusplus +#if __cplusplus +} +#endif +#endif +const static int FD_ID = 100000; +namespace OHOS { +class LibFsDmSnapshotTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void LibFsDmSnapshotTest::SetUpTestCase() +{ + GTEST_LOG_(INFO) << "LibFsDmSnapshotTest SetUpTestCase"; +} + +void LibFsDmSnapshotTest::TearDownTestCase() +{ + GTEST_LOG_(INFO) << "LibFsDmSnapshotTest TearDownTestCase"; +} + +void LibFsDmSnapshotTest::SetUp() +{ + GTEST_LOG_(INFO) << "LibFsDmSnapshotTest SetUp"; +} + +void LibFsDmSnapshotTest::TearDown() +{ + GTEST_LOG_(INFO) << "LibFsDmSnapshotTest TearDown"; +} + +HWTEST_F(LibFsDmSnapshotTest, FsDmCreateSnapshotDevice_001, TestSize.Level0) +{ + int ret = FsDmCreateSnapshotDevice(nullptr, nullptr, 0, nullptr); + EXPECT_EQ(ret, -1); +} + +HWTEST_F(LibFsDmSnapshotTest, FsDmCreateSnapshotDevice_002, TestSize.Level0) +{ + OpenFunc func = [](const char *path, int flags) -> int { + return -1; + }; + UpdateOpenFunc(func); + DmSnapshotTarget *target = (DmSnapshotTarget *)malloc(sizeof(DmSnapshotTarget)); + char devpath[PATH_MAX] = "devpath"; + int ret = FsDmCreateSnapshotDevice("devName", devpath, strlen(devpath), target); + UpdateOpenFunc(nullptr); + + free(target); + EXPECT_EQ(ret, -1); +} + +HWTEST_F(LibFsDmSnapshotTest, FsDmCreateSnapshotDevice_003, TestSize.Level0) +{ + OpenFunc func = [](const char *path, int flags) -> int { + return 1; + }; + CreatedmdevFunc dmFunc = [](int, const char *) { + return -1; + }; + CloseFunc closeFunc = [](int) -> int { + return 0; + }; + UpdateCloseFunc(closeFunc); + UpdateOpenFunc(func); + UpdateCreatedmdevFunc(dmFunc); + DmSnapshotTarget *target = (DmSnapshotTarget *)malloc(sizeof(DmSnapshotTarget)); + char devpath[PATH_MAX] = "devpath"; + int ret = FsDmCreateSnapshotDevice("devName", devpath, strlen(devpath), target); + UpdateOpenFunc(nullptr); + UpdateCreatedmdevFunc(nullptr); + UpdateCloseFunc(nullptr); + free(target); + EXPECT_EQ(ret, -1); +} + +HWTEST_F(LibFsDmSnapshotTest, FsDmCreateSnapshotDevice_004, TestSize.Level0) +{ + OpenFunc func = [](const char *path, int flags) -> int { + return 1; + }; + CreatedmdevFunc dmFunc = [](int, const char *) { + return 0; + }; + LoaddmdevicetableFunc loadFunc = [](int, const char *, DmVerityTarget *, int) -> int { + return -1; + }; + CloseFunc closeFunc = [](int) -> int { + return 0; + }; + UpdateCloseFunc(closeFunc); + UpdateOpenFunc(func); + UpdateCreatedmdevFunc(dmFunc); + UpdateLoaddmdevicetableFunc(loadFunc); + DmSnapshotTarget *target = (DmSnapshotTarget *)malloc(sizeof(DmSnapshotTarget)); + char devpath[PATH_MAX] = "devpath"; + int ret = FsDmCreateSnapshotDevice("devName", devpath, strlen(devpath), target); + UpdateOpenFunc(nullptr); + UpdateCreatedmdevFunc(nullptr); + UpdateLoaddmdevicetableFunc(nullptr); + UpdateCloseFunc(nullptr); + + free(target); + EXPECT_EQ(ret, -1); +} + +HWTEST_F(LibFsDmSnapshotTest, FsDmCreateSnapshotDevice_005, TestSize.Level0) +{ + OpenFunc func = [](const char *path, int flags) -> int { + return 1; + }; + CreatedmdevFunc dmFunc = [](int, const char *) -> int { + return 0; + }; + LoaddmdevicetableFunc loadFunc = [](int, const char *, DmVerityTarget *, int) -> int { + return 0; + }; + ActivedmdeviceFunc activeDmDeviceFunc = [](int, const char *) -> int { + return -1; + }; + CloseFunc closeFunc = [](int) -> int { + return 0; + }; + UpdateOpenFunc(func); + UpdateCreatedmdevFunc(dmFunc); + UpdateActivedmdeviceFunc(activeDmDeviceFunc); + UpdateLoaddmdevicetableFunc(loadFunc); + UpdateCloseFunc(closeFunc); + + DmSnapshotTarget *target = (DmSnapshotTarget *)malloc(sizeof(DmSnapshotTarget)); + char devpath[PATH_MAX] = "devpath"; + int ret = FsDmCreateSnapshotDevice("devName", devpath, strlen(devpath), target); + UpdateOpenFunc(nullptr); + UpdateCreatedmdevFunc(nullptr); + UpdateActivedmdeviceFunc(nullptr); + UpdateLoaddmdevicetableFunc(nullptr); + UpdateCloseFunc(nullptr); + + free(target); + EXPECT_EQ(ret, -1); +} + +HWTEST_F(LibFsDmSnapshotTest, FsDmCreateSnapshotDevice_006, TestSize.Level0) +{ + OpenFunc func = [](const char *path, int flags) -> int { + return 1; + }; + CreatedmdevFunc dmFunc = [](int, const char *) -> int { + return 0; + }; + LoaddmdevicetableFunc loadFunc = [](int, const char *, DmVerityTarget *, int) -> int { + return 0; + }; + ActivedmdeviceFunc activeDmDeviceFunc = [](int, const char *) -> int { + return 0; + }; + DmgetdevicenameFunc deviceNameFunc = [](int, const char *, char *, const uint64_t) -> int { + return -1; + }; + CloseFunc closeFunc = [](int) -> int { + return 0; + }; + UpdateOpenFunc(func); + UpdateCreatedmdevFunc(dmFunc); + UpdateActivedmdeviceFunc(activeDmDeviceFunc); + UpdateDmgetdevicenameFunc(deviceNameFunc); + UpdateLoaddmdevicetableFunc(loadFunc); + UpdateCloseFunc(closeFunc); + + DmSnapshotTarget *target = (DmSnapshotTarget *)malloc(sizeof(DmSnapshotTarget)); + char devpath[PATH_MAX] = "devpath"; + int ret = FsDmCreateSnapshotDevice("devName", devpath, strlen(devpath), target); + UpdateOpenFunc(nullptr); + UpdateCreatedmdevFunc(nullptr); + UpdateActivedmdeviceFunc(nullptr); + UpdateDmgetdevicenameFunc(nullptr); + UpdateLoaddmdevicetableFunc(nullptr); + UpdateCloseFunc(nullptr); + free(target); + EXPECT_EQ(ret, -1); +} + +HWTEST_F(LibFsDmSnapshotTest, FsDmCreateSnapshotDevice_007, TestSize.Level0) +{ + OpenFunc func = [](const char *path, int flags) -> int { + return 1; + }; + CreatedmdevFunc dmFunc = [](int, const char *) -> int { + return 0; + }; + + LoaddmdevicetableFunc loadFunc = [](int, const char *, DmVerityTarget *, int) -> int { + return 0; + }; + ActivedmdeviceFunc activeDmDeviceFunc = [](int, const char *) -> int { + return 0; + }; + DmgetdevicenameFunc deviceNameFunc = [](int, const char *, char *, const uint64_t) -> int { + return 0; + }; + CloseFunc closeFunc = [](int) -> int { + return 0; + }; + UpdateOpenFunc(func); + UpdateCreatedmdevFunc(dmFunc); + UpdateActivedmdeviceFunc(activeDmDeviceFunc); + UpdateDmgetdevicenameFunc(deviceNameFunc); + UpdateLoaddmdevicetableFunc(loadFunc); + UpdateCloseFunc(closeFunc); + + DmSnapshotTarget *target = (DmSnapshotTarget *)malloc(sizeof(DmSnapshotTarget)); + char devpath[PATH_MAX] = "devpath"; + int ret = FsDmCreateSnapshotDevice("devName", devpath, strlen(devpath), target); + UpdateOpenFunc(nullptr); + UpdateCreatedmdevFunc(nullptr); + UpdateActivedmdeviceFunc(nullptr); + UpdateDmgetdevicenameFunc(nullptr); + UpdateLoaddmdevicetableFunc(nullptr); + UpdateCloseFunc(nullptr); + + free(target); + EXPECT_EQ(ret, 0); +} + +HWTEST_F(LibFsDmSnapshotTest, FsDmSwitchToSnapshotMerge_001, TestSize.Level0) +{ + int ret = FsDmSwitchToSnapshotMerge(nullptr, nullptr); + EXPECT_EQ(ret, -1); +} + +HWTEST_F(LibFsDmSnapshotTest, FsDmSwitchToSnapshotMerge_002, TestSize.Level0) +{ + OpenFunc openFunc = [](const char*, int) -> int { + return -1; + }; + UpdateOpenFunc(openFunc); + char devName[PATH_MAX] = "devName"; + DmSnapshotTarget *target = (DmSnapshotTarget *)malloc(sizeof(target)); + int ret = FsDmSwitchToSnapshotMerge(devName, target); + free(target); + UpdateOpenFunc(nullptr); + EXPECT_EQ(ret, -1); +} + +HWTEST_F(LibFsDmSnapshotTest, FsDmSwitchToSnapshotMerge_003, TestSize.Level0) +{ + OpenFunc openFunc = [](const char*, int) -> int { + return 0; + }; + UpdateOpenFunc(openFunc); + LoaddmdevicetableFunc loadFunc = [](int, const char*, DmSnapshotTarget *, int) -> int { + return -1; + }; + UpdateLoaddmdevicetableFunc(loadFunc); + CloseFunc closeFunc = [](int) -> int { + return 0; + }; + UpdateCloseFunc(closeFunc); + + char devName[PATH_MAX] = "devName"; + DmSnapshotTarget *target = (DmSnapshotTarget *)malloc(sizeof(target)); + int ret = FsDmSwitchToSnapshotMerge(devName, target); + free(target); + + UpdateOpenFunc(nullptr); + UpdateLoaddmdevicetableFunc(nullptr); + UpdateCloseFunc(nullptr); + + EXPECT_EQ(ret, -1); +} + + +HWTEST_F(LibFsDmSnapshotTest, FsDmSwitchToSnapshotMerge_004, TestSize.Level0) +{ + OpenFunc openFunc = [](const char*, int) -> int { + return 0; + }; + UpdateOpenFunc(openFunc); + LoaddmdevicetableFunc loadFunc = [](int, const char*, DmSnapshotTarget *, int) -> int { + return 0; + }; + UpdateLoaddmdevicetableFunc(loadFunc); + CloseFunc closeFunc = [](int) -> int { + return 0; + }; + UpdateCloseFunc(closeFunc); + ActivedmdeviceFunc activeDmDeviceFunc = [](int, const char *) -> int { + return -1; + }; + UpdateActivedmdeviceFunc(activeDmDeviceFunc); + char devName[PATH_MAX] = "devName"; + DmSnapshotTarget *target = (DmSnapshotTarget *)malloc(sizeof(target)); + int ret = FsDmSwitchToSnapshotMerge(devName, target); + free(target); + + UpdateOpenFunc(nullptr); + UpdateLoaddmdevicetableFunc(nullptr); + UpdateCloseFunc(nullptr); + UpdateActivedmdeviceFunc(nullptr); + + EXPECT_EQ(ret, -1); +} + +HWTEST_F(LibFsDmSnapshotTest, FsDmSwitchToSnapshotMerge_005, TestSize.Level0) +{ + OpenFunc openFunc = [](const char*, int) -> int { + return 0; + }; + UpdateOpenFunc(openFunc); + LoaddmdevicetableFunc loadFunc = [](int, const char*, DmSnapshotTarget *, int) -> int { + return 0; + }; + UpdateLoaddmdevicetableFunc(loadFunc); + CloseFunc closeFunc = [](int) -> int { + return 0; + }; + UpdateCloseFunc(closeFunc); + ActivedmdeviceFunc activeDmDeviceFunc = [](int, const char *) -> int { + return 0; + }; + UpdateActivedmdeviceFunc(activeDmDeviceFunc); + char devName[PATH_MAX] = "devName"; + DmSnapshotTarget *target = (DmSnapshotTarget *)malloc(sizeof(target)); + int ret = FsDmSwitchToSnapshotMerge(devName, target); + free(target); + + UpdateOpenFunc(nullptr); + UpdateLoaddmdevicetableFunc(nullptr); + UpdateCloseFunc(nullptr); + UpdateActivedmdeviceFunc(nullptr); + + EXPECT_EQ(ret, 0); +} + +HWTEST_F(LibFsDmSnapshotTest, ParseStatusText_001, TestSize.Level0) +{ + StatusInfo *info = (StatusInfo *)malloc(sizeof(StatusInfo)); + char data[PATH_MAX] = "10 10 10"; + bool ret = ParseStatusText(data, info); + EXPECT_EQ(ret, true); +} + +HWTEST_F(LibFsDmSnapshotTest, ParseStatusText_002, TestSize.Level0) +{ + StatusInfo *info = (StatusInfo *)malloc(sizeof(StatusInfo)); + char data[PATH_MAX] = "10 1011asasdfdasfasdfasdfasdfas"; + bool ret = ParseStatusText(data, info); + EXPECT_EQ(ret, false); +} + +HWTEST_F(LibFsDmSnapshotTest, ParseStatusText_003, TestSize.Level0) +{ + StrcpySFunc strcpyFunc = [](char *, size_t, const char *) -> int { + return -1; + }; + UpdateStrcpySFunc(strcpyFunc); + StatusInfo *info = (StatusInfo *)malloc(sizeof(StatusInfo)); + char data[PATH_MAX] = "10 10"; + bool ret = ParseStatusText(data, info); + UpdateStrcpySFunc(nullptr); + EXPECT_EQ(ret, false); +} + +HWTEST_F(LibFsDmSnapshotTest, ParseStatusText_004, TestSize.Level0) +{ + StatusInfo *info = (StatusInfo *)malloc(sizeof(StatusInfo)); + + char data[PATH_MAX] = "Invalid"; + bool ret = ParseStatusText(data, info); + EXPECT_EQ(ret, true); +} + +HWTEST_F(LibFsDmSnapshotTest, ParseStatusText_005, TestSize.Level0) +{ + StatusInfo *info = (StatusInfo *)malloc(sizeof(StatusInfo)); + + char data[PATH_MAX] = "otherError"; + bool ret = ParseStatusText(data, info); + EXPECT_EQ(ret, false); +} + +HWTEST_F(LibFsDmSnapshotTest, GetDmSnapshotStatus_001, TestSize.Level0) +{ + int ret = GetDmSnapshotStatus(nullptr, nullptr, nullptr); + EXPECT_EQ(ret, false); +} + +HWTEST_F(LibFsDmSnapshotTest, GetDmSnapshotStatus_002, TestSize.Level0) +{ + OpenFunc openFunc = [](const char*, int) -> int { + return FD_ID; + }; + UpdateOpenFunc(openFunc); + InitdmioFunc initdmioFunc = [](struct dm_ioctl *, const char*) -> int { + return -1; + }; + UpdateInitdmioFunc(initdmioFunc); + char name[PATH_MAX] = "name"; + char targetType[PATH_MAX] = "targetType"; + StatusInfo *info = (StatusInfo *)malloc(sizeof(StatusInfo)); + int ret = GetDmSnapshotStatus(name, targetType, info); + UpdateOpenFunc(nullptr); + UpdateInitdmioFunc(nullptr); + + EXPECT_EQ(ret, false); +} + +HWTEST_F(LibFsDmSnapshotTest, GetDmSnapshotStatus_003, TestSize.Level0) +{ + OpenFunc openFunc = [](const char*, int) -> int { + return FD_ID; + }; + UpdateOpenFunc(openFunc); + InitdmioFunc initdmioFunc = [](struct dm_ioctl *, const char*) -> int { + return 0; + }; + UpdateInitdmioFunc(initdmioFunc); + + IoctlFunc ioctlFunc = [](int, int, va_list args) -> int { + return -1; + }; + UpdateIoctlFunc(ioctlFunc); + + char name[PATH_MAX] = "name"; + char targetType[PATH_MAX] = "targetType"; + StatusInfo *info = (StatusInfo *)malloc(sizeof(StatusInfo)); + int ret = GetDmSnapshotStatus(name, targetType, info); + UpdateOpenFunc(nullptr); + UpdateInitdmioFunc(nullptr); + UpdateIoctlFunc(nullptr); + + EXPECT_EQ(ret, false); +} + +HWTEST_F(LibFsDmSnapshotTest, GetDmSnapshotStatus_004, TestSize.Level0) +{ + OpenFunc openFunc = [](const char*, int) -> int { + return FD_ID; + }; + UpdateOpenFunc(openFunc); + InitdmioFunc initdmioFunc = [](struct dm_ioctl *, const char*) -> int { + return 0; + }; + UpdateInitdmioFunc(initdmioFunc); + + IoctlFunc ioctlFunc = [](int fd, int req, va_list args) -> int { + struct dm_ioctl *io = va_arg(args, struct dm_ioctl *); + io->flags = DM_BUFFER_FULL_FLAG; + return 0; + }; + UpdateIoctlFunc(ioctlFunc); + char name[PATH_MAX] = "name"; + char targetType[PATH_MAX] = "targetType"; + StatusInfo *info = (StatusInfo *)malloc(sizeof(StatusInfo)); + int ret = GetDmSnapshotStatus(name, targetType, info); + UpdateOpenFunc(nullptr); + UpdateInitdmioFunc(nullptr); + UpdateIoctlFunc(nullptr); + + EXPECT_EQ(ret, false); +} + +HWTEST_F(LibFsDmSnapshotTest, GetDmSnapshotStatus_005, TestSize.Level0) +{ + OpenFunc openFunc = [](const char*, int) -> int { + return FD_ID; + }; + UpdateOpenFunc(openFunc); + InitdmioFunc initdmioFunc = [](struct dm_ioctl *, const char*) -> int { + return 0; + }; + UpdateInitdmioFunc(initdmioFunc); + + IoctlFunc ioctlFunc = [](int fd, int req, va_list args) -> int { + struct dm_ioctl *io = va_arg(args, struct dm_ioctl *); + io->data_start = sizeof(struct dm_ioctl *); + io->data_size =sizeof (struct dm_ioctl *) + sizeof(struct dm_target_spec) - 1; + return 0; + }; + UpdateIoctlFunc(ioctlFunc); + char name[PATH_MAX] = "name"; + char targetType[PATH_MAX] = "targetType"; + StatusInfo *info = (StatusInfo *)malloc(sizeof(StatusInfo)); + int ret = GetDmSnapshotStatus(name, targetType, info); + UpdateOpenFunc(nullptr); + UpdateInitdmioFunc(nullptr); + UpdateIoctlFunc(nullptr); + + EXPECT_EQ(ret, false); +} + +HWTEST_F(LibFsDmSnapshotTest, GetDmSnapshotStatus_006, TestSize.Level0) +{ + OpenFunc openFunc = [](const char*, int) -> int { + return FD_ID; + }; + UpdateOpenFunc(openFunc); + InitdmioFunc initdmioFunc = [](struct dm_ioctl *, const char*) -> int { + return 0; + }; + UpdateInitdmioFunc(initdmioFunc); + + IoctlFunc ioctlFunc = [](int fd, int req, va_list args) -> int { + struct dm_ioctl *io = va_arg(args, struct dm_ioctl *); + io->data_start = sizeof(struct dm_ioctl *); + io->data_size = sizeof(struct dm_ioctl *) + sizeof(struct dm_target_spec); + struct dm_target_spec* spec = (struct dm_target_spec*)(&((char*)io)[sizeof(struct dm_ioctl *)]); + spec->next = sizeof(struct dm_target_spec); + return 0; + }; + UpdateIoctlFunc(ioctlFunc); + char name[PATH_MAX] = "name"; + char targetType[PATH_MAX] = "targetType"; + StatusInfo *info = (StatusInfo *)malloc(sizeof(StatusInfo)); + int ret = GetDmSnapshotStatus(name, targetType, info); + UpdateOpenFunc(nullptr); + UpdateInitdmioFunc(nullptr); + UpdateIoctlFunc(nullptr); + + EXPECT_EQ(ret, false); +} + +HWTEST_F(LibFsDmSnapshotTest, GetDmSnapshotStatus_007, TestSize.Level0) +{ + OpenFunc openFunc = [](const char*, int) -> int { + return FD_ID; + }; + UpdateOpenFunc(openFunc); + InitdmioFunc initdmioFunc = [](struct dm_ioctl *, const char*) -> int { + return 0; + }; + UpdateInitdmioFunc(initdmioFunc); + + IoctlFunc ioctlFunc = [](int fd, int req, va_list args) -> int { + struct dm_ioctl *io = va_arg(args, struct dm_ioctl *); + io->data_start = sizeof(struct dm_ioctl *); + io->data_size = sizeof(struct dm_ioctl *) + sizeof(struct dm_target_spec); + struct dm_target_spec* spec = (struct dm_target_spec*)(&((char*)io)[sizeof(struct dm_ioctl *)]); + spec->next = sizeof(struct dm_target_spec) + 1; + return 0; + }; + UpdateIoctlFunc(ioctlFunc); + char name[PATH_MAX] = "name"; + char targetType[PATH_MAX] = "targetType"; + StatusInfo *info = (StatusInfo *)malloc(sizeof(StatusInfo)); + int ret = GetDmSnapshotStatus(name, targetType, info); + UpdateOpenFunc(nullptr); + UpdateInitdmioFunc(nullptr); + UpdateIoctlFunc(nullptr); + + EXPECT_EQ(ret, false); +} + +HWTEST_F(LibFsDmSnapshotTest, GetDmSnapshotStatus_008, TestSize.Level0) +{ + OpenFunc openFunc = [](const char*, int) -> int { + return FD_ID; + }; + UpdateOpenFunc(openFunc); + InitdmioFunc initdmioFunc = [](struct dm_ioctl *, const char*) -> int { + return 0; + }; + UpdateInitdmioFunc(initdmioFunc); + + IoctlFunc ioctlFunc = [](int fd, int req, va_list args) -> int { + struct dm_ioctl *io = va_arg(args, struct dm_ioctl *); + io->data_start = sizeof(struct dm_ioctl *); + io->data_size = sizeof(struct dm_ioctl *) + sizeof(struct dm_target_spec); + struct dm_target_spec* spec = (struct dm_target_spec*)(&((char*)io)[sizeof(struct dm_ioctl *)]); + spec->next = sizeof(struct dm_target_spec) + 1 + strlen(TESTIODATA); + char *data = (char *)io + sizeof(struct dm_target_spec) + io->data_start; + int ret = strcpy_s(spec->target_type, DM_MAX_TYPE_NAME, "targetType"); + if (ret < 0) { + return -1; + } + ret = strcpy_s(data, spec->next - sizeof(struct dm_target_spec), TESTIODATA); + if (ret < 0) { + return -1; + } + return 0; + }; + UpdateIoctlFunc(ioctlFunc); + char name[PATH_MAX] = "name"; + char targetType[PATH_MAX] = "targetType"; + StatusInfo *info = (StatusInfo *)malloc(sizeof(StatusInfo)); + int ret = GetDmSnapshotStatus(name, targetType, info); + UpdateOpenFunc(nullptr); + UpdateInitdmioFunc(nullptr); + UpdateIoctlFunc(nullptr); + + EXPECT_EQ(ret, true); +} + +HWTEST_F(LibFsDmSnapshotTest, GetDmMergeProcess_001, TestSize.Level0) +{ + bool ret = GetDmMergeProcess(nullptr, nullptr); + EXPECT_EQ(ret, false); +} +} \ No newline at end of file diff --git a/test/unittest/single_test/libfs_dm_snapshot/src/libfs_dm_test.cpp b/test/unittest/single_test/libfs_dm_snapshot/src/libfs_dm_test.cpp new file mode 100644 index 000000000..803dc681a --- /dev/null +++ b/test/unittest/single_test/libfs_dm_snapshot/src/libfs_dm_test.cpp @@ -0,0 +1,614 @@ +/* + * 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 "libfs_dm_test.h" +#include +#include "func_wrapper.h" +#include "fs_dm_mock.h" +#include +#include +#include +#include "init_utils.h" +#include "securec.h" +using namespace testing; +using namespace testing::ext; +#define STRSIZE 64 +#define TESTIODATA "10 10 10" +#define FD_ID 10000 +#ifdef __cplusplus +#if __cplusplus +static int g_socketFd = -1; +extern "C" +{ +#endif +#endif +void RetriggerDmUeventByPathStub(int sockFd, char *path, char **devices, int num) +{ + printf("RetriggerDmUeventByPathStub"); +} + +int UeventdSocketInitStub() +{ + return g_socketFd; +} + +#ifdef __cplusplus +#if __cplusplus +} +#endif +#endif +namespace OHOS { +class LibFsDmTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void LibFsDmTest::SetUpTestCase() +{ + GTEST_LOG_(INFO) << "LibFsDmTest SetUpTestCase"; +} + +void LibFsDmTest::TearDownTestCase() +{ + GTEST_LOG_(INFO) << "LibFsDmTest TearDownTestCase"; +} + +void LibFsDmTest::SetUp() +{ + GTEST_LOG_(INFO) << "LibFsDmTest SetUp"; +} + +void LibFsDmTest::TearDown() +{ + GTEST_LOG_(INFO) << "LibFsDmTest TearDown"; +} + +HWTEST_F(LibFsDmTest, InitDmIo_001, TestSize.Level0) +{ + int ret = InitDmIo(nullptr, nullptr); + EXPECT_EQ(ret, -1); +} + +HWTEST_F(LibFsDmTest, InitDmIo_002, TestSize.Level0) +{ + struct dm_ioctl io = {}; + int ret = InitDmIo(&io, nullptr); + EXPECT_EQ(ret, 0); +} + +HWTEST_F(LibFsDmTest, InitDmIo_003, TestSize.Level0) +{ + struct dm_ioctl io = {}; + StrcpySFunc strcpysFunc = [](char *, size_t, const char *) -> int { + return -1; + }; + + UpdateStrcpySFunc(strcpysFunc); + int ret = InitDmIo(&io, "devname"); + UpdateStrcpySFunc(nullptr); + + EXPECT_EQ(ret, -1); +} + +HWTEST_F(LibFsDmTest, InitDmIo_004, TestSize.Level0) +{ + struct dm_ioctl io = {}; + StrcpySFunc strcpysFunc = [](char *, size_t, const char *) -> int { + return 0; + }; + + UpdateStrcpySFunc(strcpysFunc); + int ret = InitDmIo(&io, "devname"); + UpdateStrcpySFunc(nullptr); + EXPECT_EQ(ret, 0); +} + + +HWTEST_F(LibFsDmTest, CreateDmDev_001, TestSize.Level0) +{ + InitdmioFunc initDmio = [](struct dm_ioctl *, const char *) -> int { + return -1; + }; + UpdateInitdmioFunc(initDmio); + int ret = CreateDmDev(0, "devname"); + UpdateInitdmioFunc(nullptr); + EXPECT_EQ(ret, -1); +} + +HWTEST_F(LibFsDmTest, CreateDmDev_002, TestSize.Level0) +{ + int ret = CreateDmDev(0, "devname"); + EXPECT_EQ(ret, -1); +} + +HWTEST_F(LibFsDmTest, CreateDmDev_003, TestSize.Level0) +{ + IoctlFunc ioctlFunc = [](int fd, int req, va_list args) -> int { + return 0; + }; + UpdateIoctlFunc(ioctlFunc); + int ret = CreateDmDev(0, "devname"); + UpdateIoctlFunc(nullptr); + EXPECT_EQ(ret, 0); +} + +HWTEST_F(LibFsDmTest, LoadDmDeviceTable_001, TestSize.Level0) +{ + int dmTpyeIndex = MAXNUMTYPE + 1; + DmVerityTarget target = {}; + target.paras = (char *)malloc(sizeof(char) * PATH_MAX); + target.paras_len = sizeof(char) * PATH_MAX; + int ret = LoadDmDeviceTable(FD_ID, nullptr, &target, dmTpyeIndex); + EXPECT_EQ(ret, -1); + free(target.paras); +} + + +HWTEST_F(LibFsDmTest, LoadDmDeviceTable_002, TestSize.Level0) +{ + int dmTpyeIndex = VERIFY; + DmVerityTarget target = {}; + target.paras = (char *)malloc(sizeof(char) * PATH_MAX); + target.paras_len = sizeof(char) * PATH_MAX; + + CallocFunc callocFunc = [](size_t, size_t) -> void* { + return nullptr; + }; + UpdateCallocFunc(callocFunc); + int ret = LoadDmDeviceTable(FD_ID, nullptr, &target, dmTpyeIndex); + EXPECT_EQ(ret, -1); + free(target.paras); + UpdateCallocFunc(nullptr); +} + +HWTEST_F(LibFsDmTest, LoadDmDeviceTable_003, TestSize.Level0) +{ + int dmTpyeIndex = VERIFY; + DmVerityTarget target = {}; + target.paras = (char *)malloc(sizeof(char) * PATH_MAX); + target.paras_len = sizeof(char) * PATH_MAX; + + InitdmioFunc initDmIoFunc = [](struct dm_ioctl *, const char *) -> int { + return -1; + }; + UpdateInitdmioFunc(initDmIoFunc); + int ret = LoadDmDeviceTable(FD_ID, "devname", &target, dmTpyeIndex); + EXPECT_EQ(ret, -1); + UpdateInitdmioFunc(nullptr); + free(target.paras); +} + +HWTEST_F(LibFsDmTest, LoadDmDeviceTable_004, TestSize.Level0) +{ + int dmTpyeIndex = VERIFY; + DmVerityTarget target = {}; + target.paras = (char *)malloc(sizeof(char) * PATH_MAX); + target.paras_len = sizeof(char) * PATH_MAX; + + int ret = LoadDmDeviceTable(FD_ID, "devname", &target, dmTpyeIndex); + EXPECT_EQ(ret, -1); + free(target.paras); +} + +HWTEST_F(LibFsDmTest, LoadDmDeviceTable_005, TestSize.Level0) +{ + int dmTpyeIndex = SNAPSHOT; + DmVerityTarget target = {}; + target.paras = (char *)malloc(sizeof(char) * PATH_MAX); + target.paras_len = sizeof(char) * PATH_MAX; + StrcpySFunc strcpysFunc = [](char *, size_t, const char *) -> int { + static int count = 0; + return (count ++ & 1) ? 0 : -1; + }; + UpdateStrcpySFunc(strcpysFunc); + InitdmioFunc initDmIoFunc = [](struct dm_ioctl *, const char *) -> int { + return 0; + }; + UpdateInitdmioFunc(initDmIoFunc); + int ret = LoadDmDeviceTable(FD_ID, "devname", &target, dmTpyeIndex); + EXPECT_EQ(ret, -1); + ret = LoadDmDeviceTable(FD_ID, "devname", &target, dmTpyeIndex); + EXPECT_EQ(ret, -1); + UpdateStrcpySFunc(nullptr); + UpdateInitdmioFunc(nullptr); + free(target.paras); +} + +HWTEST_F(LibFsDmTest, LoadDmDeviceTable_006, TestSize.Level0) +{ + int dmTpyeIndex = SNAPSHOT; + DmVerityTarget target = {}; + target.paras = (char *)malloc(sizeof(char) * PATH_MAX); + target.paras_len = sizeof(char) * PATH_MAX; + + InitdmioFunc initDmIoFunc = [](struct dm_ioctl *, const char *) -> int { + return 0; + }; + UpdateInitdmioFunc(initDmIoFunc); + + IoctlFunc ioctlFunc = [](int, int, va_list args) -> int { + return -1; + }; + UpdateIoctlFunc(ioctlFunc); + int ret = LoadDmDeviceTable(FD_ID, "devname", &target, dmTpyeIndex); + EXPECT_EQ(ret, -1); + UpdateIoctlFunc(nullptr); + UpdateInitdmioFunc(nullptr); + free(target.paras); +} + +HWTEST_F(LibFsDmTest, LoadDmDeviceTable_007, TestSize.Level0) +{ + int dmTpyeIndex = SNAPSHOT; + DmVerityTarget target = {}; + target.paras = (char *)malloc(sizeof(char) * PATH_MAX); + target.paras_len = sizeof(char) * PATH_MAX; + + InitdmioFunc initDmIoFunc = [](struct dm_ioctl *, const char *) -> int { + return 0; + }; + UpdateInitdmioFunc(initDmIoFunc); + + IoctlFunc ioctlFunc = [](int, int, va_list args) -> int { + return 0; + }; + UpdateIoctlFunc(ioctlFunc); + int ret = LoadDmDeviceTable(FD_ID, "devname", &target, dmTpyeIndex); + EXPECT_EQ(ret, 0); + UpdateIoctlFunc(nullptr); + UpdateInitdmioFunc(nullptr); + free(target.paras); +} + +HWTEST_F(LibFsDmTest, ActiveDmDevice_001, TestSize.Level0) +{ + InitdmioFunc initDmIoFunc = [](struct dm_ioctl *, const char *) -> int { + return -1; + }; + UpdateInitdmioFunc(initDmIoFunc); + int ret = ActiveDmDevice(FD_ID, "devname"); + EXPECT_EQ(ret, -1); + UpdateInitdmioFunc(nullptr); +} + +HWTEST_F(LibFsDmTest, ActiveDmDevice_002, TestSize.Level0) +{ + InitdmioFunc initDmIoFunc = [](struct dm_ioctl *, const char *) -> int { + return 0; + }; + UpdateInitdmioFunc(initDmIoFunc); + IoctlFunc ioctlFunc = [](int, int, va_list) -> int { + return -1; + }; + UpdateIoctlFunc(ioctlFunc); + int ret = ActiveDmDevice(FD_ID, "devname"); + EXPECT_EQ(ret, -1); + UpdateInitdmioFunc(nullptr); + UpdateIoctlFunc(nullptr); +} + +HWTEST_F(LibFsDmTest, ActiveDmDevice_004, TestSize.Level0) +{ + InitdmioFunc initDmIoFunc = [](struct dm_ioctl *, const char *) -> int { + return 0; + }; + UpdateInitdmioFunc(initDmIoFunc); + IoctlFunc ioctlFunc = [](int, int, va_list) -> int { + return 0; + }; + UpdateIoctlFunc(ioctlFunc); + int ret = ActiveDmDevice(FD_ID, "devname"); + EXPECT_EQ(ret, 0); + UpdateInitdmioFunc(nullptr); + UpdateIoctlFunc(nullptr); +} + +HWTEST_F(LibFsDmTest, GetDmDevPath_001, TestSize.Level0) +{ + InitdmioFunc initDmIoFunc = [](struct dm_ioctl *, const char *) -> int { + return -1; + }; + UpdateInitdmioFunc(initDmIoFunc); + char *dmDevPath = NULL; + int ret = GetDmDevPath(FD_ID, &dmDevPath, "devname"); + EXPECT_EQ(ret, -1); + UpdateInitdmioFunc(nullptr); +} + +HWTEST_F(LibFsDmTest, GetDmDevPath_002, TestSize.Level0) +{ + InitdmioFunc initDmIoFunc = [](struct dm_ioctl *, const char *) -> int { + return 0; + }; + UpdateInitdmioFunc(initDmIoFunc); + IoctlFunc ioctlFunc = [](int, int, va_list) -> int { + return -1; + }; + UpdateIoctlFunc(ioctlFunc); + char *dmDevPath = NULL; + int ret = GetDmDevPath(FD_ID, &dmDevPath, "devname"); + EXPECT_EQ(ret, -1); + UpdateInitdmioFunc(nullptr); + UpdateIoctlFunc(nullptr); +} + +HWTEST_F(LibFsDmTest, GetDmDevPath_003, TestSize.Level0) +{ + InitdmioFunc initDmIoFunc = [](struct dm_ioctl *, const char *) -> int { + return 0; + }; + UpdateInitdmioFunc(initDmIoFunc); + IoctlFunc ioctlFunc = [](int, int, va_list) -> int { + return 0; + }; + UpdateIoctlFunc(ioctlFunc); + CallocFunc callocFunc = [](size_t, size_t) -> void* { + return nullptr; + }; + UpdateCallocFunc(callocFunc); + char *dmDevPath = NULL; + int ret = GetDmDevPath(FD_ID, &dmDevPath, "devname"); + EXPECT_EQ(ret, -1); + UpdateInitdmioFunc(nullptr); + UpdateIoctlFunc(nullptr); + UpdateCallocFunc(nullptr); +} + +HWTEST_F(LibFsDmTest, GetDmDevPath_004, TestSize.Level0) +{ + InitdmioFunc initDmIoFunc = [](struct dm_ioctl *, const char *) -> int { + return 0; + }; + UpdateInitdmioFunc(initDmIoFunc); + IoctlFunc ioctlFunc = [](int, int, va_list) -> int { + return 0; + }; + UpdateIoctlFunc(ioctlFunc); + SnprintfSFunc snprintfsFunc = [](char *, size_t, size_t, const char *, va_list) -> size_t { + return -1; + }; + UpdateSnprintfSFunc(snprintfsFunc); + char *dmDevPath = NULL; + int ret = GetDmDevPath(FD_ID, &dmDevPath, "devname"); + EXPECT_EQ(ret, -1); + UpdateInitdmioFunc(nullptr); + UpdateIoctlFunc(nullptr); + UpdateSnprintfSFunc(nullptr); +} + +HWTEST_F(LibFsDmTest, GetDmDevPath_005, TestSize.Level0) +{ + InitdmioFunc initDmIoFunc = [](struct dm_ioctl *, const char *) -> int { + return 0; + }; + UpdateInitdmioFunc(initDmIoFunc); + IoctlFunc ioctlFunc = [](int, int, va_list) -> int { + return 0; + }; + UpdateIoctlFunc(ioctlFunc); + SnprintfSFunc snprintfsFunc = [](char *, size_t, size_t, const char *, va_list) -> size_t { + return 0; + }; + UpdateSnprintfSFunc(snprintfsFunc); + char *dmDevPath = NULL; + int ret = GetDmDevPath(FD_ID, &dmDevPath, "devname"); + EXPECT_EQ(ret, 0); + UpdateInitdmioFunc(nullptr); + UpdateIoctlFunc(nullptr); + UpdateSnprintfSFunc(nullptr); +} + +HWTEST_F(LibFsDmTest, FsDmCreateDevice_001, TestSize.Level0) +{ + OpenFunc openFunc = [](const char *, int) -> int { + return -1; + }; + UpdateOpenFunc(openFunc); + char *dmDevPath = nullptr; + int ret = FsDmCreateDevice(&dmDevPath, "devname", nullptr); + EXPECT_EQ(ret, -1); + UpdateOpenFunc(nullptr); +} + +HWTEST_F(LibFsDmTest, FsDmCreateDevice_002, TestSize.Level0) +{ + OpenFunc openFunc = [](const char *, int) -> int { + return FD_ID; + }; + UpdateOpenFunc(openFunc); + CreatedmdevFunc createDmDevFunc = [](int, const char*) -> int { + return -1; + }; + UpdateCreatedmdevFunc(createDmDevFunc); + char *dmDevPath = nullptr; + int ret = FsDmCreateDevice(&dmDevPath, "devname", nullptr); + UpdateOpenFunc(nullptr); + UpdateCreatedmdevFunc(nullptr); + EXPECT_EQ(ret, -1); +} + +HWTEST_F(LibFsDmTest, FsDmCreateDevice_003, TestSize.Level0) +{ + OpenFunc openFunc = [](const char *, int) -> int { + return FD_ID; + }; + UpdateOpenFunc(openFunc); + CreatedmdevFunc createDmDevFunc = [](int, const char*) -> int { + return 0; + }; + UpdateCreatedmdevFunc(createDmDevFunc); + LoaddmdevicetableFunc loadDmFunc = [](int, const char *, DmVerityTarget *, int) -> int { + return -1; + }; + UpdateLoaddmdevicetableFunc(loadDmFunc); + char *dmDevPath = nullptr; + int ret = FsDmCreateDevice(&dmDevPath, "devname", nullptr); + UpdateOpenFunc(nullptr); + UpdateCreatedmdevFunc(nullptr); + UpdateLoaddmdevicetableFunc(nullptr); + EXPECT_EQ(ret, -1); +} + +HWTEST_F(LibFsDmTest, FsDmCreateDevice_004, TestSize.Level0) +{ + OpenFunc openFunc = [](const char *, int) -> int { + return FD_ID; + }; + UpdateOpenFunc(openFunc); + CreatedmdevFunc createDmDevFunc = [](int, const char*) -> int { + return 0; + }; + UpdateCreatedmdevFunc(createDmDevFunc); + LoaddmdevicetableFunc loadDmFunc = [](int, const char *, DmVerityTarget *, int) -> int { + return 0; + }; + UpdateLoaddmdevicetableFunc(loadDmFunc); + ActivedmdeviceFunc activeDeviceFunc = [](int, const char*) -> int { + return -1; + }; + UpdateActivedmdeviceFunc(activeDeviceFunc); + char *dmDevPath = nullptr; + int ret = FsDmCreateDevice(&dmDevPath, "devname", nullptr); + UpdateOpenFunc(nullptr); + UpdateCreatedmdevFunc(nullptr); + UpdateLoaddmdevicetableFunc(nullptr); + UpdateActivedmdeviceFunc(nullptr); + EXPECT_EQ(ret, -1); +} + +HWTEST_F(LibFsDmTest, FsDmCreateDevice_005, TestSize.Level0) +{ + OpenFunc openFunc = [](const char *, int) -> int { + return FD_ID; + }; + UpdateOpenFunc(openFunc); + CreatedmdevFunc createDmDevFunc = [](int, const char*) -> int { + return 0; + }; + UpdateCreatedmdevFunc(createDmDevFunc); + LoaddmdevicetableFunc loadDmFunc = [](int, const char *, DmVerityTarget *, int) -> int { + return 0; + }; + UpdateLoaddmdevicetableFunc(loadDmFunc); + ActivedmdeviceFunc activeDeviceFunc = [](int, const char*) -> int { + return 0; + }; + UpdateActivedmdeviceFunc(activeDeviceFunc); + GetdmdevpathFunc getDmDevPathFunc = [](int fd, char **dmDevPath, const char *devName) -> int { + return -1; + }; + UpdateGetdmdevpathFunc(getDmDevPathFunc); + char *dmDevPath = nullptr; + int ret = FsDmCreateDevice(&dmDevPath, "devname", nullptr); + UpdateOpenFunc(nullptr); + UpdateCreatedmdevFunc(nullptr); + UpdateLoaddmdevicetableFunc(nullptr); + UpdateActivedmdeviceFunc(nullptr); + UpdateGetdmdevpathFunc(nullptr); + EXPECT_EQ(ret, -1); +} + +HWTEST_F(LibFsDmTest, FsDmCreateDevice_006, TestSize.Level0) +{ + OpenFunc openFunc = [](const char *, int) -> int { + return FD_ID; + }; + UpdateOpenFunc(openFunc); + CreatedmdevFunc createDmDevFunc = [](int, const char*) -> int { + return 0; + }; + UpdateCreatedmdevFunc(createDmDevFunc); + LoaddmdevicetableFunc loadDmFunc = [](int, const char *, DmVerityTarget *, int) -> int { + return 0; + }; + UpdateLoaddmdevicetableFunc(loadDmFunc); + ActivedmdeviceFunc activeDeviceFunc = [](int, const char*) -> int { + return 0; + }; + UpdateActivedmdeviceFunc(activeDeviceFunc); + GetdmdevpathFunc getDmDevPathFunc = [](int fd, char **dmDevPath, const char *devName) -> int { + return 0; + }; + UpdateGetdmdevpathFunc(getDmDevPathFunc); + char *dmDevPath = nullptr; + int ret = FsDmCreateDevice(&dmDevPath, "devname", nullptr); + UpdateOpenFunc(nullptr); + UpdateCreatedmdevFunc(nullptr); + UpdateLoaddmdevicetableFunc(nullptr); + UpdateActivedmdeviceFunc(nullptr); + UpdateGetdmdevpathFunc(nullptr); + EXPECT_EQ(ret, 0); +} + +HWTEST_F(LibFsDmTest, FsDmInitDmDev_001, TestSize.Level0) +{ + int ret = FsDmInitDmDev(nullptr, false); + EXPECT_EQ(ret, -1); +} + +HWTEST_F(LibFsDmTest, FsDmInitDmDev_002, TestSize.Level0) +{ + SnprintfSFunc snprintfsFunc = [](char *, size_t, size_t, const char *, va_list) -> size_t { + return -1; + }; + UpdateSnprintfSFunc(snprintfsFunc); + char devpath[PATH_MAX] = "devpath"; + int ret = FsDmInitDmDev(devpath, false); + EXPECT_EQ(ret, -1); + UpdateSnprintfSFunc(nullptr); +} + +HWTEST_F(LibFsDmTest, FsDmInitDmDev_003, TestSize.Level0) +{ + CallocFunc callocFunc = [](size_t, size_t) -> void* { + return nullptr; + }; + UpdateCallocFunc(callocFunc); + char devpath[PATH_MAX] = "devpath"; + int ret = FsDmInitDmDev(devpath, false); + EXPECT_EQ(ret, -1); + UpdateCallocFunc(nullptr); +} + +HWTEST_F(LibFsDmTest, FsDmInitDmDev_004, TestSize.Level0) +{ + StrdupFunc strdupFunc = [](const char*) -> char* { + return nullptr; + }; + UpdateStrdupFunc(strdupFunc); + char devpath[PATH_MAX] = "devpath"; + int ret = FsDmInitDmDev(devpath, false); + EXPECT_EQ(ret, -1); + UpdateStrdupFunc(nullptr); +} + +HWTEST_F(LibFsDmTest, FsDmInitDmDev_005, TestSize.Level0) +{ + g_socketFd = -1; + char devpath[PATH_MAX] = "devpath"; + int ret = FsDmInitDmDev(devpath, true); + EXPECT_EQ(ret, -1); +} + +HWTEST_F(LibFsDmTest, FsDmInitDmDev_006, TestSize.Level0) +{ + g_socketFd = FD_ID; + char devpath[PATH_MAX] = "devpath"; + int ret = FsDmInitDmDev(devpath, true); + EXPECT_EQ(ret, 0); +} +} \ No newline at end of file -- Gitee