diff --git a/CMakeLists.txt b/CMakeLists.txt index 4460e049cafac0ccf7dd3288b5b6a8bd2ad22fb9..dffdbf5aad877d8f16b421223deecb090a34496c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,8 @@ cmake_minimum_required (VERSION 3.12.1) project (lcr) +include(cmake/set_build_flags.cmake) + option(VERSION "set lcr version" ON) if (VERSION STREQUAL "ON") set(LCR_VERSION "1.0.15") @@ -65,7 +67,22 @@ else () message("-- Build static library") endif() +# llt and coverage +SET(CMAKE_VERBOSE_MAKEFILE OFF) +OPTION(ENABLE_COVERAGE "coverage switch" OFF) +IF(ENABLE_COVERAGE) + MESSAGE(STATUS "Enable coverage compile option") + SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -fprofile-arcs -ftest-coverage") + SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g -fprofile-arcs -ftest-coverage") + SET(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -g -fprofile-arcs -ftest-coverage -lgcov") +ENDIF(ENABLE_COVERAGE) + add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/src) +OPTION(ENABLE_LLT "llt switch" OFF) +IF(ENABLE_LLT) + enable_testing() + add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/test) +ENDIF(ENABLE_LLT) # install all files install(FILES ${CMAKE_BINARY_DIR}/conf/lcr.pc diff --git a/cmake/set_build_flags.cmake b/cmake/set_build_flags.cmake new file mode 100644 index 0000000000000000000000000000000000000000..1a803331d61c56d61346545774c7cd4a7f00c116 --- /dev/null +++ b/cmake/set_build_flags.cmake @@ -0,0 +1,14 @@ +# set common FLAGS +set(CMAKE_C_FLAGS "-fPIC -fstack-protector-all -D_FORTIFY_SOURCE=2 -O2 -Wall -Werror -rdynamic") +set(CMAKE_CXX_FLAGS "-fPIC -std=c++11 -fstack-protector-all -D_FORTIFY_SOURCE=2 -O2 -Wall -Werror") +set(CMAKE_SHARED_LINKER_FLAGS "-Wl,-E -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -Wtrampolines -fPIE -pie -shared -pthread") +set(CMAKE_EXE_LINKER_FLAGS "-Wl,-E -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -Wtrampolines -fPIE -pie") + +if (LCR_GCOV) + set(CMAKE_C_FLAGS_DEBUG "-Wall -fprofile-arcs -ftest-coverage") + set(CMAKE_CXX_FLAGS_DEBUG "-Wall -fprofile-arcs -ftest-coverage") + message("-----CXXFLAGS: " ${CMAKE_CXX_FLAGS_DEBUG}) + message("------compile with gcov-------------") + message("-----CFLAGS: " ${CMAKE_C_FLAGS_DEBUG}) + message("------------------------------------") +endif() diff --git a/lcr.spec b/lcr.spec index 6538d7f2942663a99c9e7402b280b99a8b1016c9..eface963bf1cd64572a289df863893dbfc785bb9 100644 --- a/lcr.spec +++ b/lcr.spec @@ -2,13 +2,13 @@ %global _release 20190612.105034.gitefd3b7ac Name: lcr Version: %{_version} -Release: %{_release}%{?dist} +Release: %{_release} URL: http://code.huawei.com/containers/lcr -Source: %{name}-1.0.tar.gz +Source: lcr-1.0.tar.gz Summary: Lightweight Container Runtime Group: Applications/System License: Mulan PSL v1 -BuildRoot: %{_tmppath}/%{name}-%{version} +BuildRoot: %{_tmppath}/lcr-%{version} BuildRequires: cmake BuildRequires: lxc @@ -38,7 +38,7 @@ lxc-based containers. %global debug_package %{nil} %prep -%setup -c -n %{name}-%{version} +%setup -c -n lcr-%{version} %build mkdir -p build @@ -49,10 +49,10 @@ cd build %install rm -rf %{buildroot} cd build -mkdir -p %{buildroot}/{%{_libdir},%{_libdir}/pkgconfig,%{_includedir}/%{name},%{_bindir}} +mkdir -p %{buildroot}/{%{_libdir},%{_libdir}/pkgconfig,%{_includedir}/lcr,%{_bindir}} install -m 0644 ./src/liblcr.so %{buildroot}/%{_libdir}/liblcr.so install -m 0644 ./conf/lcr.pc %{buildroot}/%{_libdir}/pkgconfig/lcr.pc -install -m 0644 ../src/lcrcontainer.h %{buildroot}/%{_includedir}/%{name}/lcrcontainer.h +install -m 0644 ../src/lcrcontainer.h %{buildroot}/%{_includedir}/lcr/lcrcontainer.h find %{buildroot} -type f -name '*.la' -exec rm -f {} ';' find %{buildroot} -name '*.a' -exec rm -f {} ';' @@ -70,8 +70,8 @@ rm -rf %{buildroot} %files %defattr(-,root,root,-) %{_libdir}/* -%{_libdir}/pkgconfig/%{name}.pc -%{_includedir}/%{name}/lcrcontainer.h +%{_libdir}/pkgconfig/lcr.pc +%{_includedir}/lcr/lcrcontainer.h %changelog * Fri Apr 14 2017 Hui Wang - 0.0.1 diff --git a/src/json/schema/schema/oci/runtime/config-linux.json b/src/json/schema/schema/oci/runtime/config-linux.json index 95935f52881722615b119322cc4ba0207d65da25..70b05e0d959ef7eff6b08434f6bb403d3184aacb 100644 --- a/src/json/schema/schema/oci/runtime/config-linux.json +++ b/src/json/schema/schema/oci/runtime/config-linux.json @@ -15,14 +15,14 @@ "id": "https://opencontainers.org/schema/bundle/linux/uidMappings", "type": "array", "items": { - "$ref": "defs.json#/definitions/IDMapping" + "$ref": "../../defs.json#/definitions/IDMapping" } }, "gidMappings": { "id": "https://opencontainers.org/schema/bundle/linux/gidMappings", "type": "array", "items": { - "$ref": "defs.json#/definitions/IDMapping" + "$ref": "../../defs.json#/definitions/IDMapping" } }, "namespaces": { @@ -53,7 +53,7 @@ "properties": { "limit": { "id": "https://opencontainers.org/schema/bundle/linux/resources/pids/limit", - "$ref": "defs.json#/definitions/int64" + "$ref": "../../defs.json#/definitions/int64" } }, "required": [ @@ -123,23 +123,23 @@ }, "period": { "id": "https://opencontainers.org/schema/bundle/linux/resources/cpu/period", - "$ref": "defs.json#/definitions/uint64" + "$ref": "../../defs.json#/definitions/uint64" }, "quota": { "id": "https://opencontainers.org/schema/bundle/linux/resources/cpu/quota", - "$ref": "defs.json#/definitions/int64" + "$ref": "../../defs.json#/definitions/int64" }, "realtimePeriod": { "id": "https://opencontainers.org/schema/bundle/linux/resources/cpu/realtimePeriod", - "$ref": "defs.json#/definitions/uint64" + "$ref": "../../defs.json#/definitions/uint64" }, "realtimeRuntime": { "id": "https://opencontainers.org/schema/bundle/linux/resources/cpu/realtimeRuntime", - "$ref": "defs.json#/definitions/int64" + "$ref": "../../defs.json#/definitions/int64" }, "shares": { "id": "https://opencontainers.org/schema/bundle/linux/resources/cpu/shares", - "$ref": "defs.json#/definitions/uint64" + "$ref": "../../defs.json#/definitions/uint64" } } }, @@ -153,7 +153,7 @@ "type": "string" }, "limit": { - "$ref": "defs.json#/definitions/uint64" + "$ref": "../../defs.json#/definitions/uint64" } }, "required": [ @@ -168,27 +168,27 @@ "properties": { "kernel": { "id": "https://opencontainers.org/schema/bundle/linux/resources/memory/kernel", - "$ref": "defs.json#/definitions/int64" + "$ref": "../../defs.json#/definitions/int64" }, "kernelTCP": { "id": "https://opencontainers.org/schema/bundle/linux/resources/memory/kernelTCP", - "$ref": "defs.json#/definitions/int64" + "$ref": "../../defs.json#/definitions/int64" }, "limit": { "id": "https://opencontainers.org/schema/bundle/linux/resources/memory/limit", - "$ref": "defs.json#/definitions/int64" + "$ref": "../../defs.json#/definitions/int64" }, "reservation": { "id": "https://opencontainers.org/schema/bundle/linux/resources/memory/reservation", - "$ref": "defs.json#/definitions/int64" + "$ref": "../../defs.json#/definitions/int64" }, "swap": { "id": "https://opencontainers.org/schema/bundle/linux/resources/memory/swap", - "$ref": "defs.json#/definitions/int64" + "$ref": "../../defs.json#/definitions/int64" }, "swappiness": { "id": "https://opencontainers.org/schema/bundle/linux/resources/memory/swappiness", - "$ref": "defs.json#/definitions/uint64" + "$ref": "../../defs.json#/definitions/uint64" }, "disableOOMKiller": { "id": "https://opencontainers.org/schema/bundle/linux/resources/memory/disableOOMKiller", @@ -202,7 +202,7 @@ "properties": { "classID": { "id": "https://opencontainers.org/schema/bundle/linux/resources/network/classId", - "$ref": "defs.json#/definitions/uint32" + "$ref": "../../defs.json#/definitions/uint32" }, "priorities": { "id": "https://opencontainers.org/schema/bundle/linux/resources/network/priorities", @@ -252,15 +252,15 @@ }, "sysctl": { "id": "https://opencontainers.org/schema/bundle/linux/sysctl", - "$ref": "defs.json#/definitions/mapStringString" + "$ref": "../../defs.json#/definitions/mapStringString" }, "maskedPaths": { "id": "https://opencontainers.org/schema/bundle/linux/maskedPaths", - "$ref": "defs.json#/definitions/ArrayOfStrings" + "$ref": "../../defs.json#/definitions/ArrayOfStrings" }, "readonlyPaths": { "id": "https://opencontainers.org/schema/bundle/linux/readonlyPaths", - "$ref": "defs.json#/definitions/ArrayOfStrings" + "$ref": "../../defs.json#/definitions/ArrayOfStrings" }, "mountLabel": { "id": "https://opencontainers.org/schema/bundle/linux/mountLabel", diff --git a/src/json/schema/schema/oci/runtime/defs-linux.json b/src/json/schema/schema/oci/runtime/defs-linux.json index 4d9620a4a2155acb715e021c9cf01ea05153bf48..ccbe3c7eb9fb2ef67a4e0e2c50b0934712ff8457 100644 --- a/src/json/schema/schema/oci/runtime/defs-linux.json +++ b/src/json/schema/schema/oci/runtime/defs-linux.json @@ -58,13 +58,13 @@ "type": "object", "properties": { "index": { - "$ref": "defs.json#/definitions/uint32" + "$ref": "../../defs.json#/definitions/uint32" }, "value": { - "$ref": "defs.json#/definitions/uint64" + "$ref": "../../defs.json#/definitions/uint64" }, "valueTwo": { - "$ref": "defs.json#/definitions/uint64" + "$ref": "../../defs.json#/definitions/uint64" }, "op": { "$ref": "#/definitions/SeccompOperators" @@ -103,11 +103,11 @@ }, "Major": { "description": "major device number", - "$ref": "defs.json#/definitions/int64" + "$ref": "../../defs.json#/definitions/int64" }, "Minor": { "description": "minor device number", - "$ref": "defs.json#/definitions/int64" + "$ref": "../../defs.json#/definitions/int64" }, "FileMode": { "description": "File permissions mode (typically an octal value)", @@ -131,7 +131,7 @@ "$ref": "#/definitions/FileType" }, "path": { - "$ref": "defs.json#/definitions/FilePath" + "$ref": "../../defs.json#/definitions/FilePath" }, "fileMode": { "$ref": "#/definitions/FileMode" @@ -143,10 +143,10 @@ "$ref": "#/definitions/Minor" }, "uid": { - "$ref": "defs.json#/definitions/UID" + "$ref": "../../defs.json#/definitions/UID" }, "gid": { - "$ref": "defs.json#/definitions/GID" + "$ref": "../../defs.json#/definitions/GID" } } }, @@ -196,7 +196,7 @@ "type": "object", "properties": { "rate": { - "$ref": "defs.json#/definitions/uint64" + "$ref": "../../defs.json#/definitions/uint64" } } } @@ -232,7 +232,7 @@ "type": "string" }, "priority": { - "$ref": "defs.json#/definitions/uint32" + "$ref": "../../defs.json#/definitions/uint32" } }, "required": [ @@ -259,7 +259,7 @@ "$ref": "#/definitions/NamespaceType" }, "path": { - "$ref": "defs.json#/definitions/FilePath" + "$ref": "../../defs.json#/definitions/FilePath" } }, "required": [ diff --git a/src/json/schema/schema/oci/runtime/defs.json b/src/json/schema/schema/oci/runtime/defs.json deleted file mode 120000 index 78180b582b22e715cfb44782b20e2ca1b50fa233..0000000000000000000000000000000000000000 --- a/src/json/schema/schema/oci/runtime/defs.json +++ /dev/null @@ -1 +0,0 @@ -../../defs.json \ No newline at end of file diff --git a/src/json/schema/schema/oci/runtime/spec.json b/src/json/schema/schema/oci/runtime/spec.json index d083274a054dc850bf2afa99a0fb405802b7c5cf..61eacbd81f14cbec23a4fcbf2588b9adfabf90b8 100644 --- a/src/json/schema/schema/oci/runtime/spec.json +++ b/src/json/schema/schema/oci/runtime/spec.json @@ -6,25 +6,25 @@ "properties": { "ociVersion": { "id": "https://opencontainers.org/schema/bundle/ociVersion", - "$ref": "defs.json#/definitions/ociVersion" + "$ref": "../../defs.json#/definitions/ociVersion" }, "hooks": { "id": "https://opencontainers.org/schema/bundle/hooks", "type": "object", "properties": { "prestart": { - "$ref": "defs.json#/definitions/ArrayOfHooks" + "$ref": "../../defs.json#/definitions/ArrayOfHooks" }, "poststart": { - "$ref": "defs.json#/definitions/ArrayOfHooks" + "$ref": "../../defs.json#/definitions/ArrayOfHooks" }, "poststop": { - "$ref": "defs.json#/definitions/ArrayOfHooks" + "$ref": "../../defs.json#/definitions/ArrayOfHooks" } } }, "annotations": { - "$ref": "defs.json#/definitions/annotations" + "$ref": "../../defs.json#/definitions/annotations" }, "hostname": { "id": "https://opencontainers.org/schema/bundle/hostname", @@ -34,7 +34,7 @@ "id": "https://opencontainers.org/schema/bundle/mounts", "type": "array", "items": { - "$ref": "defs.json#/definitions/Mount" + "$ref": "../../defs.json#/definitions/Mount" } }, "root": { @@ -47,7 +47,7 @@ "properties": { "path": { "id": "https://opencontainers.org/schema/bundle/root/path", - "$ref": "defs.json#/definitions/FilePath" + "$ref": "../../defs.json#/definitions/FilePath" }, "readonly": { "id": "https://opencontainers.org/schema/bundle/root/readonly", @@ -65,7 +65,7 @@ "properties": { "args": { "id": "https://opencontainers.org/schema/bundle/process/args", - "$ref": "defs.json#/definitions/ArrayOfStrings" + "$ref": "../../defs.json#/definitions/ArrayOfStrings" }, "consoleSize": { "id": "https://opencontainers.org/schema/bundle/process/consoleSize", @@ -77,11 +77,11 @@ "properties": { "height": { "id": "https://opencontainers.org/schema/bundle/process/consoleSize/height", - "$ref": "defs.json#/definitions/uint64" + "$ref": "../../defs.json#/definitions/uint64" }, "width": { "id": "https://opencontainers.org/schema/bundle/process/consoleSize/width", - "$ref": "defs.json#/definitions/uint64" + "$ref": "../../defs.json#/definitions/uint64" } } }, @@ -91,7 +91,7 @@ }, "env": { "id": "https://opencontainers.org/schema/bundle/process/env", - "$ref": "defs.json#/definitions/Env" + "$ref": "../../defs.json#/definitions/Env" }, "terminal": { "id": "https://opencontainers.org/schema/bundle/process/terminal", @@ -103,15 +103,15 @@ "properties": { "uid": { "id": "https://opencontainers.org/schema/bundle/process/user/uid", - "$ref": "defs.json#/definitions/UID" + "$ref": "../../defs.json#/definitions/UID" }, "gid": { "id": "https://opencontainers.org/schema/bundle/process/user/gid", - "$ref": "defs.json#/definitions/GID" + "$ref": "../../defs.json#/definitions/GID" }, "additionalGids": { "id": "https://opencontainers.org/schema/bundle/process/user/additionalGids", - "$ref": "defs.json#/definitions/ArrayOfGIDs" + "$ref": "../../defs.json#/definitions/ArrayOfGIDs" }, "username": { "id": "https://opencontainers.org/schema/bundle/process/user/username", @@ -190,11 +190,11 @@ "properties": { "hard": { "id": "https://opencontainers.org/schema/bundle/linux/rlimits/0/hard", - "$ref": "defs.json#/definitions/uint64" + "$ref": "../../defs.json#/definitions/uint64" }, "soft": { "id": "https://opencontainers.org/schema/bundle/linux/rlimits/0/soft", - "$ref": "defs.json#/definitions/uint64" + "$ref": "../../defs.json#/definitions/uint64" }, "type": { "id": "https://opencontainers.org/schema/bundle/linux/rlimits/0/type", diff --git a/src/json/schema/schema/oci/runtime/state.json b/src/json/schema/schema/oci/runtime/state.json index 563a619f024b056c14c1005809b5b81b7d27304e..93b816011b741e51fd91f1355286d2535a5d3874 100644 --- a/src/json/schema/schema/oci/runtime/state.json +++ b/src/json/schema/schema/oci/runtime/state.json @@ -6,7 +6,7 @@ "properties": { "ociVersion": { "id": "https://opencontainers.org/schema/runtime/state/ociVersion", - "$ref": "defs.json#/definitions/ociVersion" + "$ref": "../../defs.json#/definitions/ociVersion" }, "id": { "id": "https://opencontainers.org/schema/runtime/state/id", @@ -33,7 +33,7 @@ "type": "string" }, "annotations": { - "$ref": "defs.json#/definitions/annotations" + "$ref": "../../defs.json#/definitions/annotations" } }, "required": [ diff --git a/src/json/schema/src/common_h.py b/src/json/schema/src/common_h.py index e2e04ce1d4d42f2160f6a6fd532be09731aa6b4d..eb33f632fb9001754dc688fdb2ecd16b2515487d 100644 --- a/src/json/schema/src/common_h.py +++ b/src/json/schema/src/common_h.py @@ -33,7 +33,6 @@ CODE = '''// Auto generated file. Do not edit! # include # include # include -# include # include # include # include diff --git a/src/utils.c b/src/utils.c index 41fbba20520494a96b207127688931fae7b5a817..05a3ebc0c92d717d341609b4fa49d1b8eea74b2a 100644 --- a/src/utils.c +++ b/src/utils.c @@ -475,12 +475,12 @@ void *util_common_calloc_s(size_t size) return calloc(1, size); } -int mem_realloc(void **new, size_t newsize, void *old, size_t oldsize) +int mem_realloc(void **newptr, size_t newsize, void *oldptr, size_t oldsize) { int nret = 0; void *addr = NULL; - if (new == NULL) { + if (newptr == NULL) { return -1; } @@ -493,16 +493,16 @@ int mem_realloc(void **new, size_t newsize, void *old, size_t oldsize) goto err_out; } - if (old != NULL) { - nret = memcpy_s(addr, newsize, old, oldsize); + if (oldptr != NULL) { + nret = memcpy_s(addr, newsize, oldptr, oldsize); if (nret != EOK) { free(addr); goto err_out; } - free(old); + free(oldptr); } - *new = addr; + *newptr = addr; return 0; err_out: @@ -1095,32 +1095,34 @@ restart: } /* util string append */ -char *util_string_append(const char *dst, const char *src) +char *util_string_append(const char *post, const char *pre) { char *res_string = NULL; size_t length = 0; - if ((dst == NULL) && (src == NULL)) { + if (post == NULL && pre == NULL) { return NULL; } - if (src == NULL) { - return util_strdup_s(dst); + if (pre == NULL) { + return util_strdup_s(post); } - if (dst == NULL) { - return util_strdup_s(src); + if (post == NULL) { + return util_strdup_s(pre); } - - if (strlen(dst) > ((SIZE_MAX - strlen(src)) - 1)) { + if (strlen(post) > ((SIZE_MAX - strlen(pre)) - 1)) { + ERROR("String is too long to be appended"); return NULL; } - length = strlen(dst) + strlen(src) + 1; + length = strlen(post) + strlen(pre) + 1; res_string = util_common_calloc_s(length); if (res_string == NULL) { return NULL; } - - if (sprintf_s(res_string, length, "%s%s", src, dst) < 0) { - ERROR("Failed to get mount option"); + if (strcat_s(res_string, length, pre) != EOK) { + free(res_string); + return NULL; + } + if (strcat_s(res_string, length, post) != EOK) { free(res_string); return NULL; } @@ -1249,6 +1251,7 @@ void util_free_array(char **array) { char **p = NULL; + /* free a null pointer may cause codex error */ if (array == NULL) { return; } diff --git a/src/utils.h b/src/utils.h index 81eacaca0207596cd0143a7e12ba01acc2fb3d0a..5e82fba9092e9222faa1d8902114d729c63dd47f 100644 --- a/src/utils.h +++ b/src/utils.h @@ -20,6 +20,11 @@ #include #include +#ifdef __cplusplus +extern "C" { +#endif + + #ifndef O_CLOEXEC #define O_CLOEXEC 02000000 #endif @@ -159,7 +164,7 @@ char **lcr_string_split_and_trim(const char *str, char _sep); void lcr_free_array(void **array); int lcr_grow_array(void ***array, size_t *capacity, size_t new_size, size_t capacity_increment); size_t lcr_array_len(void **array); -int mem_realloc(void **new, size_t newsize, void *old, size_t oldsize); +int mem_realloc(void **newptr, size_t newsize, void *oldptr, size_t oldsize); bool util_valid_cmd_arg(const char *arg); int util_safe_ullong(const char *numstr, unsigned long long *converted); int util_safe_strtod(const char *numstr, double *converted); @@ -176,7 +181,7 @@ FILE *util_fopen(const char *filename, const char *mode); void *util_common_calloc_s(size_t size); int util_safe_int(const char *numstr, int *converted); int util_check_inherited(bool closeall, int fd_to_ignore); -char *util_string_append(const char *dst, const char *src); +char *util_string_append(const char *post, const char *pre); char *util_string_split_prefix(size_t prefix_len, const char *file); int util_build_dir(const char *name); @@ -199,4 +204,8 @@ bool util_write_file(const char *filepath, const char *content, size_t len, bool int util_atomic_write_file(const char *filepath, const char *content); +#ifdef __cplusplus +} +#endif + #endif /*__LCR_UTILS_H */ diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..4320971a7963663dce3892f1b02c032118901721 --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,7 @@ +project(lcr_LLT) + +# setup testing +find_package(Threads REQUIRED) +find_package(GTest REQUIRED) + +add_subdirectory(utils) diff --git a/test/include/mock.h b/test/include/mock.h new file mode 100644 index 0000000000000000000000000000000000000000..fe537d10f896d1d35d3f8bfd8ca82b4cbf0a32dd --- /dev/null +++ b/test/include/mock.h @@ -0,0 +1,126 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2019-2019. All rights reserved. + * lcr licensed under the Mulan PSL v1. + * You can use this software according to the terms and conditions of the Mulan PSL v1. + * You may obtain a copy of Mulan PSL v1 at: + * http://license.coscl.org.cn/MulanPSL + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR + * PURPOSE. + * See the Mulan PSL v1 for more details. + * Description: define mock method + * Author: wangyushui + * Create: 2019-6-10 + */ + +#ifndef MOCK_H +#define MOCK_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define MOCK_STRUCT_INIT(...) \ + { __VA_ARGS__ } + +#define DEFINE_RETURN_MOCK(fn, ret) \ + bool ut_ ## fn ## _mocked = false; \ + ret ut_ ## fn + +#define DEFINE_RETURN_MOCK_V(fn, ret, dargs) \ + bool ut_ ## fn ## _mocked = false; \ + ret(* ut_ ## fn) dargs +/* + * For controlling mocked function behavior, setting + * and getting values from the stub, the _P macros are + * for mocking functions that return pointer values. + */ +#define MOCK_SET(fn, val) \ + ut_ ## fn ## _mocked = true; \ + ut_ ## fn = val + +#define MOCK_SET_V(fn, fun) \ + ut_ ## fn ## _mocked = true; \ + ut_ ## fn = fun + +#define MOCK_GET(fn) \ + ut_ ## fn + +#define MOCK_GET_V(fn, args) \ + ut_ ## fn args + +#define MOCK_CLEAR(fn) \ + ut_ ## fn ## _mocked = false; + +#define MOCK_CLEAR_P(fn) \ + ut_ ## fn ## _mocked = false; \ + ut_ ## fn = NULL; + +/* for declaring function protoypes for wrappers */ +#define DECLARE_WRAPPER(fn, ret, args) \ + extern bool ut_ ## fn ## _mocked; \ + extern ret ut_ ## fn; \ + ret __wrap_ ## fn args; \ + ret __real_ ## fn args; + +#define DECLARE_WRAPPER_V(fn, ret, args) \ + extern bool ut_ ## fn ## _mocked; \ + extern ret(* ut_ ## fn) args; \ + ret __wrap_ ## fn args; \ + ret __real_ ## fn args; + +/* for defining the implmentation of wrappers for syscalls */ +#define DEFINE_WRAPPER(fn, ret, dargs, pargs) \ + DEFINE_RETURN_MOCK(fn, ret); \ + ret __wrap_ ## fn dargs \ + { \ + if (!ut_ ## fn ## _mocked) { \ + return __real_ ## fn pargs; \ + } else { \ + return MOCK_GET(fn); \ + } \ + } + +#define DEFINE_WRAPPER_V(fn, ret, dargs, pargs) \ + DEFINE_RETURN_MOCK_V(fn, ret, dargs); \ + __attribute__((used)) ret __wrap_ ## fn dargs \ + { \ + if (!ut_ ## fn ## _mocked) { \ + return __real_ ## fn pargs; \ + } else { \ + return MOCK_GET_V(fn, pargs); \ + } \ + } + +/* DEFINE_STUB is for defining the implmentation of stubs for funcs. */ +#define DEFINE_STUB(fn, ret, dargs, val) \ + bool ut_ ## fn ## _mocked = true; \ + ret ut_ ## fn = val; \ + ret fn dargs; \ + ret fn dargs \ + { \ + return MOCK_GET(fn); \ + } + +/* DEFINE_STUB_V macro is for stubs that don't have a return value */ +#define DEFINE_STUB_V(fn, dargs) \ + void fn dargs; \ + void fn dargs \ + { \ + } + +#define HANDLE_RETURN_MOCK(fn) \ + if (ut_ ## fn ## _mocked) { \ + return ut_ ## fn; \ + } + +#ifdef __cplusplus +} +#endif + +#endif /* MOCK_H */ + + + diff --git a/test/llt.sh b/test/llt.sh new file mode 100644 index 0000000000000000000000000000000000000000..a5705a08b6aa49d6dfa9ae416437766223c8d01b --- /dev/null +++ b/test/llt.sh @@ -0,0 +1,267 @@ +#! /bin/bash + +#set -xe + +usage() +{ + echo "Usage: sh llt.sh [OPTIONS]" + echo "Use llt.sh to control llt operation" + echo "" + echo "Misc:" + echo " -h, --help Print this help, then exit" + echo + echo "Compile Options:" + echo " -m, --cmake