Implement unittests for Linux

pull/3240/head
Seppo Takalo 2016-05-24 14:00:20 +03:00
parent 1976b688ed
commit 9bb7b055de
5 changed files with 128 additions and 0 deletions

View File

@ -4,6 +4,7 @@
#include "test_randlib.h"
#include <string.h>
#include <inttypes.h>
#include "randLIB.h"
bool test_randLIB_seed_random()
{

View File

@ -0,0 +1,20 @@
include ../makefile_defines.txt
COMPONENT_NAME = randLIB_linux_unit
#This must be changed manually
SRC_FILES = \
../../../../linux/randLIB.c
TEST_SRC_FILES = \
main.cpp \
randlibtest.cpp \
../randlib/test_randlib.c \
../stubs/random_stub.c \
../stubs/open_stub.c \
include ../MakefileWorker.mk
CPPUTESTFLAGS += -DFEA_TRACE_SUPPORT
CPPUTEST_LDFLAGS = -Wl,--wrap,open

View File

@ -0,0 +1,14 @@
/*
* Copyright (c) 2015 ARM. All rights reserved.
*/
#include "CppUTest/CommandLineTestRunner.h"
#include "CppUTest/TestPlugin.h"
#include "CppUTest/TestRegistry.h"
#include "CppUTestExt/MockSupportPlugin.h"
int main(int ac, char** av)
{
return CommandLineTestRunner::RunAllTests(ac, av);
}
IMPORT_TEST_GROUP(randLIB_linux);

View File

@ -0,0 +1,69 @@
/*
* Copyright (c) 2016 ARM. All rights reserved.
*/
#include "CppUTest/TestHarness.h"
#include "../randlib/test_randlib.h"
#include "randLIB.h"
TEST_GROUP(randLIB_linux)
{
void setup()
{
}
void teardown()
{
}
};
extern bool allow_open;
TEST(randLIB_linux, test_randLIB_seed_random)
{
allow_open = true;
CHECK(test_randLIB_seed_random());
}
TEST(randLIB_linux, test_randLIB_get_8bit)
{
allow_open = true;
CHECK(test_randLIB_get_8bit());
}
TEST(randLIB_linux, test_randLIB_get_16bit)
{
allow_open = true;
CHECK(test_randLIB_get_16bit());
}
TEST(randLIB_linux, test_randLIB_get_32bit)
{
allow_open = true;
CHECK(test_randLIB_get_32bit());
}
TEST(randLIB_linux, test_randLIB_get_n_bytes_random)
{
allow_open = true;
CHECK(test_randLIB_get_n_bytes_random());
}
TEST(randLIB_linux, test_randLIB_get_random_in_range)
{
allow_open = true;
CHECK(test_randLIB_get_random_in_range());
}
TEST(randLIB_linux, test_randLIB_randomise_base)
{
allow_open = true;
CHECK(test_randLIB_randomise_base());
}
TEST(randLIB_linux, test_fail_to_open)
{
uint8_t buf[4];
allow_open = false;
CHECK(-1 == randLIB_get_n_bytes_random(buf, 4));
allow_open = true;
}

View File

@ -0,0 +1,24 @@
#include <stdbool.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdarg.h>
bool allow_open = true;
int __real_open(const char *path, int flags, ...);
int __wrap_open(const char *path, int flags, ...)
{
if (allow_open) {
if (flags & O_CREAT) {
va_list vl;
va_start(vl,flags);
mode_t mode = va_arg(vl, mode_t);
va_end(vl);
return __real_open(path, flags, mode);
} else {
return __real_open(path, flags);
}
} else {
return -1;
}
}