test: Implement common mocks

In unit tests there are a lot of mocks duplication for mocks related
with memory management and panic routine.
As long as implementation doesn't have any module specific routines
and may be reused, it should be mocked in common mock space.

Marked each mock as WEAK to make it possible to overwrite this
implementation in specific test.

To use assert_false in __panic function, added dependence of
universal_mock from cmock.

Signed-off-by: Karol Trzcinski <karolx.trzcinski@linux.intel.com>
This commit is contained in:
Karol Trzcinski 2020-01-21 12:07:45 +01:00 committed by Liam Girdwood
parent b7c92f18e6
commit 816f8919db
2 changed files with 52 additions and 3 deletions

View File

@ -55,6 +55,8 @@ add_custom_command(OUTPUT ${memory_mock_lds_out}
add_custom_target(ld_script_memory_mock DEPENDS ${memory_mock_lds_out})
SET(arch_src ${PROJECT_SOURCE_DIR}/src/arch/xtensa/idc.c)
add_library(common_mock STATIC ${PROJECT_SOURCE_DIR}/test/cmocka/src/common_mocks.c)
add_dependencies(common_mock cmocka)
target_include_directories(common_mock PRIVATE ${CMOCKA_INCLUDE_DIR})
target_link_libraries(common_mock PRIVATE sof_options)
link_libraries(common_mock)
sof_append_relative_path_definitions(common_mock)

View File

@ -1,14 +1,56 @@
// SPDX-License-Identifier: BSD-3-Clause
//
// Copyright(c) 2019 Intel Corporation. All rights reserved.
// Copyright(c) 2019-2020 Intel Corporation. All rights reserved.
//
// Author: Jakub Dabek <jakub.dabek@linux.intel.com>
// Author: Karol Trzcinski <karolx.trzcinski@linux.intel.com>
#include <errno.h>
#include <sof/lib/alloc.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <stdint.h>
#include <cmocka.h>
int memcpy_s(void *dest, size_t dest_size,
const void *src, size_t src_size)
#define WEAK __attribute__((weak))
void WEAK *_balloc(uint32_t flags, uint32_t caps, size_t bytes,
uint32_t alignment)
{
(void)flags;
(void)caps;
return malloc(bytes);
}
void WEAK *_zalloc(enum mem_zone zone, uint32_t flags, uint32_t caps,
size_t bytes)
{
(void)zone;
(void)flags;
(void)caps;
return calloc(bytes, 1);
}
void WEAK *_brealloc(void *ptr, uint32_t flags, uint32_t caps, size_t bytes,
uint32_t alignment)
{
(void)flags;
(void)caps;
return realloc(ptr, bytes);
}
void WEAK rfree(void *ptr)
{
free(ptr);
}
int WEAK memcpy_s(void *dest, size_t dest_size,
const void *src, size_t src_size)
{
if (!dest || !src)
return -EINVAL;
@ -24,3 +66,8 @@ int memcpy_s(void *dest, size_t dest_size,
return 0;
}
void WEAK __panic(uint32_t p, char *filename, uint32_t linenum)
{
fail_msg("panic: %s:%d (code 0x%X)\n", filename, linenum, p);
}