Commit a44ff9ca authored by Ivan Tyagov's avatar Ivan Tyagov

Feature/unit test

See merge request !22
parents 38cda1e9 75713a47
...@@ -90,6 +90,12 @@ static int getDigitalInputState(int i2c_addr, char **digital_input) ...@@ -90,6 +90,12 @@ static int getDigitalInputState(int i2c_addr, char **digital_input)
*/ */
int file; int file;
char filename[20]; char filename[20];
if (I2C_VIRTUAL_MODE)
{
// we're in a virtual mode, likely on x86 platform or without I2C support
// simply do nothing
return 0;
}
// step 1: open device // step 1: open device
file = open(I2C_BLOCK_DEVICE_NAME, O_RDWR); file = open(I2C_BLOCK_DEVICE_NAME, O_RDWR);
...@@ -137,6 +143,12 @@ static int getAnalogInputStateAIN(int i2c_addr, int **analog_input, uint8_t read ...@@ -137,6 +143,12 @@ static int getAnalogInputStateAIN(int i2c_addr, int **analog_input, uint8_t read
*/ */
int file; int file;
char filename[20]; char filename[20];
if (I2C_VIRTUAL_MODE)
{
// we're in a virtual mode, likely on x86 platform or without I2C support
// simply do nothing
return 0;
}
// step 1: open device // step 1: open device
file = open(I2C_BLOCK_DEVICE_NAME, O_RDWR); file = open(I2C_BLOCK_DEVICE_NAME, O_RDWR);
......
...@@ -70,7 +70,7 @@ static void stopHandler(int sign) ...@@ -70,7 +70,7 @@ static void stopHandler(int sign)
running = false; running = false;
} }
#ifndef DOING_UNIT_TESTS
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
// init dictionary only once$ // init dictionary only once$
...@@ -194,3 +194,4 @@ int main(int argc, char **argv) ...@@ -194,3 +194,4 @@ int main(int argc, char **argv)
return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE; return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
} }
#endif
\ No newline at end of file
CC=gcc
CFLAGS= -l:libopen62541.so -L/usr/local/lib -Wall -Wno-missing-braces -ggdb `pkg-config --cflags criterion` -I /usr/local/include/ -I ~/open62541/src/pubsub/ -I ~/open62541/deps/
LDFLAGS= `pkg-config --libs criterion` -lmbedcrypto -lmbedx509
OUT_DIR=build/
all: test_common test_modio_i2c test_keep_alive test_keep_alive_publisher test_keep_alive_subscriber
test_common: test_common.o
@mkdir -p $(OUT_DIR)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^
@mv $@ $(OUT_DIR)
test_modio_i2c: test_modio_i2c.o
@mkdir -p $(OUT_DIR)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^
@mv $@ $(OUT_DIR)
test_keep_alive: test_keep_alive.o
@mkdir -p $(OUT_DIR)
$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)
@mv $@ $(OUT_DIR)
test_keep_alive_publisher: test_keep_alive_publisher.o
@mkdir -p $(OUT_DIR)
$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)
@mv $@ $(OUT_DIR)
test_keep_alive_subscriber: test_keep_alive_subscriber.o
@mkdir -p $(OUT_DIR)
$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)
@mv $@ $(OUT_DIR)
run: all
@${OUT_DIR}/test_common --tap=${OUT_DIR}/test_common.tap
@${OUT_DIR}/test_modio_i2c --tap=${OUT_DIR}/test_modio_i2c.tap
@${OUT_DIR}/test_keep_alive --tap=${OUT_DIR}/test_keep_alive.tap
@${OUT_DIR}/test_keep_alive_publisher --tap=${OUT_DIR}/test_keep_alive_publisher.tap
@${OUT_DIR}/test_keep_alive_subscriber --tap=${OUT_DIR}/test_keep_alive_subscriber.tap
clean:
@rm $(OUT_DIR)test_common 2>/dev/null || true
@rm $(OUT_DIR)test_common.tap 2>/dev/null || true
@rm $(OUT_DIR)test_modio_i2c 2>/dev/null || true
@rm $(OUT_DIR)test_modio_i2c.tap 2>/dev/null || true
@rm $(OUT_DIR)test_keep_alive 2>/dev/null || true
@rm $(OUT_DIR)test_keep_alive.tap 2>/dev/null || true
@rm $(OUT_DIR)test_keep_alive_publisher 2>/dev/null || true
@rm $(OUT_DIR)test_keep_alive_publisher.tap 2>/dev/null || true
@rm $(OUT_DIR)test_keep_alive_subscriber 2>/dev/null || true
@rm $(OUT_DIR)test_keep_alive_subscriber.tap 2>/dev/null || true
@rm *.o 2>/dev/null || true
.PHONY: clean debug all
/* ================ Includes ===================== */
#include <criterion/criterion.h>
#include <string.h>
#include <sys/time.h>
#include "../../coupler/opc-ua-server/common.h"
/* ================ Function Tests =============== */
// ############# get milliseconds since epoch ##############
Test(common, getMilliSecondsSinceEpoch) {
unsigned long int ms, retval;
struct timeval current_time;
gettimeofday(&current_time, NULL);
ms = current_time.tv_sec * 1000 + current_time.tv_usec / 1000;
retval = getMilliSecondsSinceEpoch();
cr_expect_eq(retval, ms);
}
// ############# load File parses the certificate file ##############
// ############# Generate a random String ##############
Test(common, randomString) {
char *result = "PKdht";
char *retval = randomString(5);
cr_expect_str_eq(retval, result);
}
// ############# Convert integer to String ##############
Test(common, convertInt2Str) {
char *result = "5";
char *retval = convertInt2Str(5);
cr_expect_str_eq(retval, result);
}
// ############# Convert a long integer to String ##############
Test(common, convertLongInt2Str) {
char *result = "5";
char *retval = convertLongInt2Str(5);
cr_expect_str_eq(retval, result);
}
// ############# A key/value dictionary system ##############
Test(common, dict) {
/* Create a dict */
dict_t **dict = dictAlloc();
char *result = "bar";
/* lets add foo to the dict */
addItem(dict, "foo", "bar");
/* and print their values */
char *retval = (char *)getItem(*dict, "foo");
cr_expect_str_eq(retval, result);
/* lets delete them */
delItem(dict, "foo");
/* see, their gone, there NULL */
retval = (char *)getItem(*dict, "foo");
cr_expect_null(retval);
/* add them again to proof it works */
addItem(dict, "foo", "bar");
/* see, here */
retval = (char *)getItem(*dict, "foo");
cr_expect_str_eq(retval, result);
delItem(dict, "foo");
dictDealloc(dict);
}
\ No newline at end of file
/* ================ Includes ===================== */
#include <criterion/criterion.h>
#include <linux/i2c-dev.h>
#include <open62541/plugin/log_stdout.h>
#include "../../coupler/opc-ua-server/mod_io_i2c.h"
#include "../../coupler/opc-ua-server/keep_alive.h"
/* ================ Function Tests =============== */
// ############# test safe mode ##############
Test(keepalive, gotoSafeMode) {
int result = 1;
gotoSafeMode();
cr_expect_eq(I2C_VIRTUAL_MODE, result);
}
// ############# test normal mode ##############
Test(keepalive, gotoNormalMode) {
int result = 0;
gotoNormalMode();
cr_expect_eq(I2C_VIRTUAL_MODE, result);
}
\ No newline at end of file
/* ================ Includes ===================== */
#define DOING_UNIT_TESTS
#include <criterion/criterion.h>
#include "../../coupler/opc-ua-server/server.c"
/* ================ Function Tests =============== */
// ############# check heart beats of the server ##############
Test(keepalivepublisher, callbackTicHeartBeat) {
int result = 1;
server = UA_Server_new();
callbackTicHeartBeat();
UA_Server_delete(server);
cr_expect_geq(HEART_BEATS, result);
}
/* ================ Includes ===================== */
#define DOING_UNIT_TESTS
#include <criterion/criterion.h>
#include "../../coupler/opc-ua-server/server.c"
/* ================ Function Tests =============== */
// ############# check heart beats of the server ##############
Test(keepalivepublisher, callbackTicHeartBeat) {
int result = 0;
server = UA_Server_new();
callbackCheckHeartBeat();
UA_Server_delete(server);
cr_expect_geq(HEART_BEAT_ID_LIST[0], result);
}
\ No newline at end of file
/* ================ Includes ===================== */
#include <criterion/criterion.h>
#include <stdint.h>
#include <linux/i2c-dev.h>
#include <fcntl.h>
#include "../../coupler/opc-ua-server/mod_io_i2c.h"
/* ================ Function Tests =============== */
// ############# get I2C Slave List Length ##############
Test(modioi2c, getI2CSlaveListLength) {
int result, retval;
retval = getI2CSlaveListLength();
cr_expect_eq(retval, result);
}
// ############# Set Relay State (only virtual mode) ##############
Test(modioi2c, setRelayState) {
int result, retval, command = 0x00, i2c_addr = 0x58;
retval = setRelayState(command, i2c_addr);
cr_expect_eq(retval, result);
}
// ############# Get Digital Input State (only virtual mode) ##############
Test(modioi2c, getDigitalInputState) {
int result = 0, retval, i2c_addr = 0x58;
char *syscall_str = NULL;
retval = getDigitalInputState(i2c_addr, &syscall_str);
cr_expect_eq(retval, result);
}
// ############# Get Analog Input State AIN (only virtual mode) ##############
Test(modioi2c, getAnalogInputStateAIN) {
int result = 0, retval, i2c_addr = 0x58;
int *syscall_int = NULL;
uint8_t read_reg = 0x30;
retval = getAnalogInputStateAIN(i2c_addr, &syscall_int, read_reg);
cr_expect_eq(retval, result);
}
// ############# Safe Shutdown I2C Slave List ##############
Test(modioi2c, safeShutdownI2CSlaveList) {
int result = 0, retval;
safeShutdownI2CSlaveList();
cr_expect_eq(retval, result);
}
\ No newline at end of file
# Unit Tests
## Framework
Criterion: https://github.com/Snaipe/Criterion
## Criterion installation for Ubuntu (>=21.04)/Debian (>=11)
apt-get install libcriterion-dev
## Compile
```
make all
```
## Execute Tests
For `test_common` to execute:
```
./test_common
```
For `test_common` to execute and get a `tap` conform output execute:
```
./test_common --tap=test.tap
```
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment