Skip to content

Commit 9bb7b05

Browse files
author
Seppo Takalo
committed
Implement unittests for Linux
1 parent 1976b68 commit 9bb7b05

File tree

5 files changed

+128
-0
lines changed

5 files changed

+128
-0
lines changed

test/mbed-client-randlib/unittest/randlib/test_randlib.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "test_randlib.h"
55
#include <string.h>
66
#include <inttypes.h>
7+
#include "randLIB.h"
78

89
bool test_randLIB_seed_random()
910
{
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
include ../makefile_defines.txt
2+
3+
COMPONENT_NAME = randLIB_linux_unit
4+
5+
#This must be changed manually
6+
SRC_FILES = \
7+
../../../../linux/randLIB.c
8+
9+
TEST_SRC_FILES = \
10+
main.cpp \
11+
randlibtest.cpp \
12+
../randlib/test_randlib.c \
13+
../stubs/random_stub.c \
14+
../stubs/open_stub.c \
15+
16+
include ../MakefileWorker.mk
17+
18+
CPPUTESTFLAGS += -DFEA_TRACE_SUPPORT
19+
20+
CPPUTEST_LDFLAGS = -Wl,--wrap,open
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* Copyright (c) 2015 ARM. All rights reserved.
3+
*/
4+
5+
#include "CppUTest/CommandLineTestRunner.h"
6+
#include "CppUTest/TestPlugin.h"
7+
#include "CppUTest/TestRegistry.h"
8+
#include "CppUTestExt/MockSupportPlugin.h"
9+
int main(int ac, char** av)
10+
{
11+
return CommandLineTestRunner::RunAllTests(ac, av);
12+
}
13+
14+
IMPORT_TEST_GROUP(randLIB_linux);
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (c) 2016 ARM. All rights reserved.
3+
*/
4+
#include "CppUTest/TestHarness.h"
5+
#include "../randlib/test_randlib.h"
6+
#include "randLIB.h"
7+
8+
TEST_GROUP(randLIB_linux)
9+
{
10+
void setup()
11+
{
12+
}
13+
14+
void teardown()
15+
{
16+
}
17+
};
18+
19+
extern bool allow_open;
20+
21+
TEST(randLIB_linux, test_randLIB_seed_random)
22+
{
23+
allow_open = true;
24+
CHECK(test_randLIB_seed_random());
25+
}
26+
27+
TEST(randLIB_linux, test_randLIB_get_8bit)
28+
{
29+
allow_open = true;
30+
CHECK(test_randLIB_get_8bit());
31+
}
32+
33+
TEST(randLIB_linux, test_randLIB_get_16bit)
34+
{
35+
allow_open = true;
36+
CHECK(test_randLIB_get_16bit());
37+
}
38+
39+
TEST(randLIB_linux, test_randLIB_get_32bit)
40+
{
41+
allow_open = true;
42+
CHECK(test_randLIB_get_32bit());
43+
}
44+
45+
TEST(randLIB_linux, test_randLIB_get_n_bytes_random)
46+
{
47+
allow_open = true;
48+
CHECK(test_randLIB_get_n_bytes_random());
49+
}
50+
51+
TEST(randLIB_linux, test_randLIB_get_random_in_range)
52+
{
53+
allow_open = true;
54+
CHECK(test_randLIB_get_random_in_range());
55+
}
56+
57+
TEST(randLIB_linux, test_randLIB_randomise_base)
58+
{
59+
allow_open = true;
60+
CHECK(test_randLIB_randomise_base());
61+
}
62+
63+
TEST(randLIB_linux, test_fail_to_open)
64+
{
65+
uint8_t buf[4];
66+
allow_open = false;
67+
CHECK(-1 == randLIB_get_n_bytes_random(buf, 4));
68+
allow_open = true;
69+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <stdbool.h>
2+
#include <sys/types.h>
3+
#include <sys/stat.h>
4+
#include <fcntl.h>
5+
#include <stdarg.h>
6+
7+
bool allow_open = true;
8+
int __real_open(const char *path, int flags, ...);
9+
int __wrap_open(const char *path, int flags, ...)
10+
{
11+
if (allow_open) {
12+
if (flags & O_CREAT) {
13+
va_list vl;
14+
va_start(vl,flags);
15+
mode_t mode = va_arg(vl, mode_t);
16+
va_end(vl);
17+
return __real_open(path, flags, mode);
18+
} else {
19+
return __real_open(path, flags);
20+
}
21+
} else {
22+
return -1;
23+
}
24+
}

0 commit comments

Comments
 (0)