提交 fd929a54 编写于 作者: Z zhuchunyi

update code

上级 6b30b504
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
......
# 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()
......@@ -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 <hw.huiwang@huawei.com> - 0.0.1
......
......@@ -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",
......
......@@ -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": [
......
../../defs.json
\ No newline at end of file
......@@ -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",
......
......@@ -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": [
......
......@@ -33,7 +33,6 @@ CODE = '''// Auto generated file. Do not edit!
# include <stdbool.h>
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <stdint.h>
# include <yajl/yajl_tree.h>
# include <yajl/yajl_gen.h>
......
......@@ -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;
}
......
......@@ -20,6 +20,11 @@
#include <sys/types.h>
#include <stdio.h>
#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 */
project(lcr_LLT)
# setup testing
find_package(Threads REQUIRED)
find_package(GTest REQUIRED)
add_subdirectory(utils)
/*
* 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 <stdbool.h>
#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 */
#! /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 <option> use cmake genenate Makefile, eg: -m(default), -mcoverage, --cmake, --cmake=coverage"
echo " -c, --compile Enable compile"
echo " -e, --empty Enable compile empty(make clean)"
echo
echo "TestRun Options"
echo " -r, --run-llt <option> Run all llt, eg: -r, -rscreen(default), -rxml, --run-llt, --run-llt=screen, --run-llt=xml"
echo " -s, --specify-llt FILE Only Run specify llt executable FILE, eg: -smain_llt, --specify-llt=main_llt"
echo
echo "Coverage Options"
echo " -t, --cover-report <option> Enable coverage report. eg: -t, -thtml(default), -ttxt, --cover-report, --cover-report=html, --cover-report=txt"
echo " -f, --cover-file FILE Specified FILE coverage report, eg: -fmain.c, --cover-file=main.c"
echo
}
ARGS=`getopt -o "hcer::m::t::s:f:" -l "help,cmake::,empty,cover-report::,run-llt::,specify-llt:,cover-file:" -n "run_llt.sh" -- "$@"`
if [ $? != 0 ]; then
usage
exit
fi
eval set -- "${ARGS}"
if [ x"$ARGS" = x" --" ]; then
#set default value
COMPILE_ENABLE=no
COVERAGE_ENABLE=no
EMPTY_ENABLE=no
RUN_LLT=yes
RUN_MODE=screen #value: screen or xml
COVER_REPORT_ENABLE=no
fi
while true; do
case "${1}" in
-h|--help)
usage; exit 0;;
-m|--cmake)
CMAKE_ENABLE=yes
case "$2" in
"") shift 2 ;;
coverage) COVERAGE_ENABLE=yes; shift 2 ;;
*) echo "Error param: $2";exit 1;;
esac;;
-c|--compile)
COMPILE_ENABLE=yes
shift;;
-e|--empty)
EMPTY_ENABLE=yes
shift;;
-r|--run-llt)
RUN_LLT=yes
case "$2" in
"") RUN_MODE=screen;shift 2 ;;
screen) RUN_MODE=screen;shift 2 ;;
xml) RUN_MODE=xml;shift 2 ;;
*)echo "Error param: $2";exit 1;;
esac;;
-t|--cover-report)
COVER_REPORT_ENABLE=yes
case "$2" in
"") COVER_STYLE=html;shift 2 ;;
html) COVER_STYLE=html;shift 2 ;;
txt) COVER_STYLE=txt;shift 2 ;;
*)echo "Error param: $2";exit 1;;
esac;;
-s|--specify-llt)
SPECIFY_LLT=$2
shift 2;;
-f|--cover-file)
COVER_FILE=$2
shift 2;;
--)
shift;break;;
esac
done
function llt_empty()
{
if [ x"${EMPTY_ENABLE}" = x"yes" ]; then
echo ---------------------- llt empty begin ----------------------
set -x
make clean
find -name "*.gcda" |xargs rm -f
find -name "*.gcno" |xargs rm -f
find -name "*.gcov" |xargs rm -f
find ../ -name "cmake_install.cmake" |xargs rm -f
find ../ -name "Makefile" |xargs rm -f
find ../ -name "CMakeFiles" |xargs rm -rf
find ../ -name "CMakeCache.txt"|xargs rm -f
find ../ -name "CTestTestfile.cmake"|xargs rm -f
rm -rf ../conf ../grpc ../json
rm coverage -rf
rm test_result.log -f
set +x
echo ---------------------- llt empty end ------------------------
fi
}
function llt_cmake()
{
ret=0
if [ x"${CMAKE_ENABLE}" = x"yes" ]; then
echo ---------------------- llt cmake begin ----------------------
cd ..
if [ x"${COVERAGE_ENABLE}" = x"yes" ]; then
cmake ./ -DCMAKE_BUILD_TYPE=Debug -DENABLE_COVERAGE=1 -DENABLE_LLT=ON
ret=$?
else
cmake -DENABLE_LLT=ON ./
ret=$?
fi
cd -
echo ---------------------- llt cmake end ------------------------
echo
fi
return $ret
}
function llt_compile()
{
ret=0
if [ x"${COMPILE_ENABLE}" = x"yes" ]; then
echo ---------------------- llt compile begin ----------------------
make -j
ret=$?
echo ---------------------- llt compile end ------------------------
echo
fi
return $ret
}
function llt_run_all_test()
{
if [ x"${RUN_LLT}" = x"yes" ]; then
echo ---------------------- llt run begin --------------------------
if [ x"${RUN_MODE}" = x"screen" ]; then
RUN_MODE=0
elif [ x"${RUN_MODE}" = x"xml" ]; then
RUN_MODE=1
elif [ x"${RUN_MODE}" = x"" ]; then
RUN_MODE=0
else
echo "not suport run mode <${RUN_MODE}>"
usage
exit 1
fi
if [ x"${SPECIFY_LLT}" = x"" ]; then
SPECIFY_LLT=`find -name "*_llt"` # run all test
else
SPECIFY_LLT=`find -name "${SPECIFY_LLT}"`
fi
TEST_LOG=test_result.log
>$TEST_LOG
ret=0
for TEST in $SPECIFY_LLT
do
echo $TEST
$TEST $RUN_MODE
if [ $? != 0 ];then
echo $TEST FAILED >> $TEST_LOG
ret=1
else
echo $TEST success >> $TEST_LOG
fi
done
echo ""
echo '######################test result begin######################'
cat $TEST_LOG
echo '#######################test result end#######################'
echo ""
echo ---------------------- llt run end --------------------------
echo
return $ret
fi
}
function llt_coverage()
{
if [ x"${COVER_REPORT_ENABLE}" = x"yes" ]; then
echo ------------------ llt generate coverage begin --------------
if [ x"${COVER_STYLE}" = x"txt" ]; then
GCDAS=`find -name "${COVER_FILE}.gcda"`
if [ x"$GCDAS" = x"" ]; then
echo "not find ${COVER_FILE}.gcda"
echo
exit 1
fi
for GCDA in $GCDAS
do
gcov $GCDA
done
find -name "*.h.gcov" | xargs rm -f
echo '#################################'
find -name "${COVER_FILE}.gcov"
echo '#################################'
exit
elif [ x"${COVER_STYLE}" = x"html" ]; then
if [ -d coverage ]; then
rm -rf coverage
fi
mkdir coverage
if [ x"${COVER_FILE}" = x"" ]; then
LCOV_CMD="-d ./"
else
GCDAS=`find -name "${COVER_FILE}.gcda"`
if [ $? != 0 ]; then
echo "not match ${COVER_FILE}.gcda"
exit 1
fi
for GCDA in ${GCDAS}
do
TMP_STR=" -d ${GCDA}";
LCOV_CMD="${LCOV_CMD} ${TMP_STR}";
done
fi
#lcov -c ${LCOV_CMD} -o coverage/coverage.info --exclude '*_llt.c' --include '*.c' --include '*.cpp' --include '*.cc' --rc lcov_branch_coverage=1 --ignore-errors gcov --ignore-errors source --ignore-errors graph
lcov -c ${LCOV_CMD} -b $(dirname $(pwd)) --no-external --exclude '*_llt.c' -o coverage/coverage.info --rc lcov_branch_coverage=1 --ignore-errors gcov --ignore-errors source --ignore-errors graph
if [ $? != 0 ]; then
echo "lcov generate coverage.info fail."
exit 1
fi
genhtml coverage/coverage.info -o coverage/html --branch-coverage --rc lcov_branch_coverage=1 -s --legend --ignore-errors source
if [ $? != 0 ]; then
echo "genhtml fail."
exit 1
fi
chmod 755 -R coverage
fi
echo ------------------ llt generate coverage end ----------------
fi
}
llt_cmake
if [[ $? -ne 0 ]];then
exit 1
fi
llt_empty
llt_compile
if [[ $? -ne 0 ]];then
exit 1
fi
llt_run_all_test
if [[ $? -ne 0 ]];then
exit 1
fi
llt_coverage
project(lcr_LLT)
SET(EXE utils_llt)
add_executable(${EXE}
${CMAKE_CURRENT_SOURCE_DIR}/../../src/utils.c
${CMAKE_CURRENT_SOURCE_DIR}/../../src/log.c
${CMAKE_BINARY_DIR}/json/json_common.c
utils_llt.cc)
target_include_directories(${EXE} PUBLIC
${GTEST_INCLUDE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/../include
${CMAKE_CURRENT_SOURCE_DIR}/../../src
${CMAKE_BINARY_DIR}/json
)
set_target_properties(${EXE} PROPERTIES LINK_FLAGS "-Wl,--wrap,util_strdup_s -Wl,--wrap,calloc -Wl,--wrap,strcat_s")
target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} -lyajl -lsecurec -lz)
/*
* 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: utils_string llt
* Author: tanyifeng
* Create: 2019-07-08
*/
#include <stdlib.h>
#include <stdio.h>
#include <securec.h>
#include <gtest/gtest.h>
#include "mock.h"
#include "utils.h"
extern "C" {
DECLARE_WRAPPER(util_strdup_s, char *, (const char *str));
DEFINE_WRAPPER(util_strdup_s, char *, (const char *str), (str));
DECLARE_WRAPPER(calloc, void *, (size_t nmemb, size_t size));
DEFINE_WRAPPER(calloc, void *, (size_t nmemb, size_t size), (nmemb, size));
DECLARE_WRAPPER_V(strcat_s, errno_t, (char *strDest, size_t destMax, const char *strSrc));
DEFINE_WRAPPER_V(strcat_s, errno_t, (char *strDest, size_t destMax, const char *strSrc),
(strDest, destMax, strSrc));
}
static int g_strcat_s_cnt = 0;
static errno_t strcat_s_fail(char *strDest, size_t destMax, const char *strSrc)
{
return (errno_t)EINVAL;
}
static errno_t strcat_s_second_fail(char *strDest, size_t destMax, const char *strSrc)
{
g_strcat_s_cnt++;
if (g_strcat_s_cnt == 1) {
return __real_strcat_s(strDest, destMax, strSrc);
}
return (errno_t)EINVAL;
}
TEST(utils_string_llt, test_str_skip_str)
{
const char *str = "abcdefghij1234567890";
const char *substr = "abcdefgh";
const char *result = nullptr;
result = str_skip_str(str, substr);
ASSERT_STREQ(result, "ij1234567890");
result = str_skip_str(str, "habc");
ASSERT_STREQ(result, nullptr);
result = str_skip_str(str, "");
ASSERT_STREQ(result, str);
result = str_skip_str(str, nullptr);
ASSERT_STREQ(result, nullptr);
result = str_skip_str("a", "a");
ASSERT_STREQ(result, "");
result = str_skip_str("", "");
ASSERT_STREQ(result, "");
result = str_skip_str(nullptr, "");
ASSERT_STREQ(result, nullptr);
}
TEST(utils_string_llt, test_util_string_join)
{
const char *array_long[] = { "abcd", "1234", "5678", "", "&^%abc" };
size_t array_long_len = sizeof(array_long) / sizeof(array_long[0]);
const char *array_short[] = { "abcd" };
size_t array_short_len = sizeof(array_short) / sizeof(array_short[0]);
const char *array_nullptr[] = { nullptr };
size_t array_nullptr_len = sizeof(array_nullptr) / sizeof(array_nullptr[0]);
char *result = nullptr;
result = util_string_join(" ", array_long, array_long_len);
ASSERT_STREQ(result, "abcd 1234 5678 &^%abc");
free(result);
result = util_string_join(" ", array_short, array_short_len);
ASSERT_STREQ(result, "abcd");
free(result);
result = util_string_join(" ", array_nullptr, array_nullptr_len);
ASSERT_EQ(result, nullptr);
result = util_string_join(" ", nullptr, 0);
ASSERT_EQ(result, nullptr);
result = util_string_join("", array_long, array_long_len);
ASSERT_STREQ(result, "abcd12345678&^%abc");
free(result);
result = util_string_join(nullptr, array_long, array_long_len);
ASSERT_STREQ(result, nullptr);
MOCK_SET_V(strcat_s, strcat_s_fail);
result = util_string_join(" ", array_short, array_short_len);
ASSERT_STREQ(result, nullptr);
MOCK_CLEAR(strcat_s);
MOCK_SET_V(strcat_s, strcat_s_fail);
result = util_string_join(" ", array_long, array_long_len);
ASSERT_STREQ(result, nullptr);
MOCK_CLEAR(strcat_s);
g_strcat_s_cnt = 0;
MOCK_SET_V(strcat_s, strcat_s_second_fail);
result = util_string_join(" ", array_long, array_long_len);
ASSERT_STREQ(result, nullptr);
MOCK_CLEAR(strcat_s);
}
TEST(utils_string_llt, test_util_string_append)
{
char *result = nullptr;
result = util_string_append("abc", "123");
ASSERT_STREQ(result, "123abc");
free(result);
result = util_string_append("abc", "");
ASSERT_STREQ(result, "abc");
free(result);
result = util_string_append("abc", nullptr);
ASSERT_STREQ(result, "abc");
free(result);
result = util_string_append("", "123");
ASSERT_STREQ(result, "123");
free(result);
result = util_string_append("", "");
ASSERT_STREQ(result, "");
free(result);
result = util_string_append("", nullptr);
ASSERT_STREQ(result, "");
free(result);
result = util_string_append(nullptr, "123");
ASSERT_STREQ(result, "123");
free(result);
result = util_string_append(nullptr, "");
ASSERT_STREQ(result, "");
free(result);
result = util_string_append(nullptr, nullptr);
ASSERT_STREQ(result, nullptr);
MOCK_SET(calloc, nullptr);
result = util_string_append("abc", "123");
ASSERT_STREQ(result, nullptr);
MOCK_CLEAR(calloc);
MOCK_SET_V(strcat_s, strcat_s_fail);
result = util_string_append("abc", "123");
ASSERT_STREQ(result, nullptr);
MOCK_CLEAR(strcat_s);
g_strcat_s_cnt = 0;
MOCK_SET_V(strcat_s, strcat_s_second_fail);
result = util_string_append("abc", "123");
ASSERT_STREQ(result, nullptr);
MOCK_CLEAR(strcat_s);
}
......@@ -47,7 +47,7 @@ function usage() {
echo " -i, --incremental-check Perform incremental check"
echo " -f, --quick-format Incremental format code by astyle/clang-format"
echo " -k, --style-check Check code style by astyle"
echo " --cpp-check Check code style by Cppcheck"
echo " --cpp-check Use Cppcheck check code style"
echo " -h, --help Script help information"
}
......@@ -82,7 +82,7 @@ function pclint_check() {
local start_time=$(date +%s)
local files
if [[ ${1} == "all" ]]; then
files=$(find ./src -regextype posix-extended -regex ".*\.(c)")
files=$(find ./src ./test -regextype posix-extended -regex ".*\.(c|cc)")
else
files=$(git diff --name-only HEAD | grep -E "*.c$")
fi
......@@ -148,7 +148,7 @@ function codestyle_check() {
local start_time=$(date +%s)
local files
if [[ ${1} == "all" ]]; then
files=$(find ./src -regextype posix-extended -regex ".*\.(h|c|cc)")
files=$(find ./src ./test -regextype posix-extended -regex ".*\.(h|c|cc)")
else
files=$(git diff --name-only HEAD | grep -E "*.h$|*.c$|*.cc$")
fi
......@@ -203,7 +203,7 @@ function cpp_check() {
printf "\n"
local check_rule=$(echo ${CPPCHRECK_RULE[@]} | sed -e "s/ /,/g")
local start_time=$(date +%s)
result=$(cppcheck --enable=${check_rule} -I ./src -I ./build -i ./build -i ./isula_testcases -i ./CI ./ 2>&1 \
result=$(cppcheck --enable=${check_rule} -I ./src -i ./build -i ./isula_testcases -i ./CI ./ 2>&1 \
| grep -vE "^Checking|done$|Cppcheck cannot find all the include files")
nums=$(echo "${result}" | wc -l)
echo "${result}"
......@@ -304,7 +304,7 @@ function astyle_format() {
/_/ |_|/____/ /_/ /_//_____//_____/ /_/ \____//_/ |_|/_/ /_//_/ |_|/_/ \033[0m]
================================================================================================="
local start_time=$(date +%s)
local files=$(find ./src -regextype posix-extended -regex ".*\.(h|c|cc)")
local files=$(find ./src ./test -regextype posix-extended -regex ".*\.(h|c|cc)")
files=(${files// / })
local total=${#files[@]}
local failure_num=0
......@@ -359,7 +359,7 @@ echo -e "\
local start_time=$(date +%s)
local files
if [[ ${1} == "all" ]]; then
files=$(find ./src -regextype posix-extended -regex ".*\.(h|c|cc)")
files=$(find ./src ./test -regextype posix-extended -regex ".*\.(h|c|cc)")
else
files=$(git diff --name-only HEAD | grep -E "*.h$|*.c$|*.cc$")
fi
......@@ -419,7 +419,7 @@ function cmetrics_check() {
local start_time=$(date +%s)
local files
if [[ ${1} == "all" ]]; then
files=$(find ./src -regextype posix-extended -regex ".*\.(h|c|cc)")
files=$(find ./src ./test -regextype posix-extended -regex ".*\.(h|c|cc)")
else
files=$(git diff --name-only HEAD | grep -E "*.h$|*.c$|*.cc$")
fi
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册