Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
osie
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nexedi
osie
Commits
a44ff9ca
Commit
a44ff9ca
authored
Nov 17, 2022
by
Ivan Tyagov
Browse files
Options
Browse Files
Download
Plain Diff
Feature/unit test
See merge request
!22
parents
38cda1e9
75713a47
Changes
9
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
308 additions
and
1 deletion
+308
-1
coupler/opc-ua-server/mod_io_i2c.h
coupler/opc-ua-server/mod_io_i2c.h
+12
-0
coupler/opc-ua-server/server.c
coupler/opc-ua-server/server.c
+2
-1
tests/unit_test/Makefile
tests/unit_test/Makefile
+55
-0
tests/unit_test/test_common.c
tests/unit_test/test_common.c
+84
-0
tests/unit_test/test_keep_alive.c
tests/unit_test/test_keep_alive.c
+30
-0
tests/unit_test/test_keep_alive_publisher.c
tests/unit_test/test_keep_alive_publisher.c
+19
-0
tests/unit_test/test_keep_alive_subscriber.c
tests/unit_test/test_keep_alive_subscriber.c
+19
-0
tests/unit_test/test_modio_i2c.c
tests/unit_test/test_modio_i2c.c
+62
-0
tests/unit_test/unit_test.md
tests/unit_test/unit_test.md
+25
-0
No files found.
coupler/opc-ua-server/mod_io_i2c.h
View file @
a44ff9ca
...
...
@@ -90,6 +90,12 @@ static int getDigitalInputState(int i2c_addr, char **digital_input)
*/
int
file
;
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
file
=
open
(
I2C_BLOCK_DEVICE_NAME
,
O_RDWR
);
...
...
@@ -137,6 +143,12 @@ static int getAnalogInputStateAIN(int i2c_addr, int **analog_input, uint8_t read
*/
int
file
;
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
file
=
open
(
I2C_BLOCK_DEVICE_NAME
,
O_RDWR
);
...
...
coupler/opc-ua-server/server.c
View file @
a44ff9ca
...
...
@@ -70,7 +70,7 @@ static void stopHandler(int sign)
running
=
false
;
}
#ifndef DOING_UNIT_TESTS
int
main
(
int
argc
,
char
**
argv
)
{
// init dictionary only once$
...
...
@@ -194,3 +194,4 @@ int main(int argc, char **argv)
return
retval
==
UA_STATUSCODE_GOOD
?
EXIT_SUCCESS
:
EXIT_FAILURE
;
}
#endif
\ No newline at end of file
tests/unit_test/Makefile
0 → 100644
View file @
a44ff9ca
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
tests/unit_test/test_common.c
0 → 100644
View file @
a44ff9ca
/* ================ 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
tests/unit_test/test_keep_alive.c
0 → 100644
View file @
a44ff9ca
/* ================ 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
tests/unit_test/test_keep_alive_publisher.c
0 → 100644
View file @
a44ff9ca
/* ================ 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
);
}
tests/unit_test/test_keep_alive_subscriber.c
0 → 100644
View file @
a44ff9ca
/* ================ 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
tests/unit_test/test_modio_i2c.c
0 → 100644
View file @
a44ff9ca
/* ================ 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
tests/unit_test/unit_test.md
0 → 100644
View file @
a44ff9ca
# 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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment