Skip to content

Commit 083f30e

Browse files
committed
Add unit tests
Add initial unit tests, and automate testing via GitHub workflows
1 parent a3ce3a7 commit 083f30e

File tree

5 files changed

+145
-97
lines changed

5 files changed

+145
-97
lines changed

.github/workflows/build-test.yml

-95
This file was deleted.

.github/workflows/unit-tests.yml

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
name: Build, installation, and unit tests
2+
3+
on:
4+
push:
5+
branches:
6+
- '**'
7+
pull_request:
8+
branches:
9+
- '**'
10+
11+
jobs:
12+
UbuntuLatest:
13+
name: Test on Ubuntu-latest
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- uses: actions/checkout@v3
18+
- name: Install dependencies
19+
run: sudo apt install -y libpcap-dev
20+
- name: Build and install tools
21+
run: make install
22+
- name: Unit tests
23+
run: make unit_tests
24+
25+
Ubuntu2204:
26+
name: Test on Ubuntu 22.04
27+
runs-on: ubuntu-22.04
28+
29+
steps:
30+
- uses: actions/checkout@v3
31+
- name: Install dependencies
32+
run: sudo apt install -y libpcap-dev
33+
- name: Build and install tools
34+
run: make install
35+
- name: Unit tests
36+
run: make unit_tests
37+
38+
Ubuntu2004:
39+
name: Test on Ubuntu 20.04
40+
runs-on: ubuntu-20.04
41+
42+
steps:
43+
- uses: actions/checkout@v3
44+
- name: Install dependencies
45+
run: sudo apt install -y libpcap-dev
46+
- name: Build and install tools
47+
run: make install
48+
- name: Unit tests
49+
run: make unit_tests
50+
51+
MacOSLatest:
52+
name: Test on MacOS-latest
53+
runs-on: macos-latest
54+
55+
steps:
56+
- uses: actions/checkout@v3
57+
- name: Install dependencies
58+
run: |
59+
brew update
60+
brew install libpcap
61+
- name: Build and install tools
62+
run: make install
63+
- name: Unit tests
64+
run: make unit_tests
65+
66+
MacOS14:
67+
name: Test on MacOS 14
68+
runs-on: macos-14
69+
70+
steps:
71+
- uses: actions/checkout@v3
72+
- name: Install dependencies
73+
run: |
74+
brew update
75+
brew install libpcap
76+
- name: Build and install tools
77+
run: make install
78+
- name: Unit tests
79+
run: make unit_tests
80+
81+
MacOS13:
82+
name: Test on MacOS 13
83+
runs-on: macos-13
84+
85+
steps:
86+
- uses: actions/checkout@v3
87+
- name: Install dependencies
88+
run: |
89+
brew update
90+
brew install libpcap
91+
- name: Build and install tools
92+
run: make install
93+
- name: Unit tests
94+
run: make unit_tests
95+
96+
MacOS12:
97+
name: Test on MacOS Monterey 12
98+
runs-on: macos-12
99+
100+
steps:
101+
- uses: actions/checkout@v3
102+
- name: Install dependencies
103+
run: |
104+
brew update
105+
brew install libpcap
106+
- name: Build and install tools
107+
run: make install
108+
- name: Unit tests
109+
run: make unit_tests

GNUmakefile

+10-1
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,13 @@ DATAPATH= $(DESTDIR)$(PREFIX)/share/ipv6toolkit
4646
BINPATH= $(DESTDIR)$(PREFIX)/bin
4747
SBINPATH= $(DESTDIR)$(PREFIX)/sbin
4848
SRCPATH= tools
49+
TESTSPATH= tests
4950

5051

5152
SBINTOOLS= blackhole6 flow6 frag6 icmp6 jumbo6 messi mldq6 na6 ni6 ns6 path6 ra6 rd6 rs6 scan6 script6 tcp6 udp6
5253
BINTOOLS= addr6
5354
TOOLS= $(BINTOOLS) $(SBINTOOLS)
55+
TESTS= tests_libipv6
5456
LIBS= libipv6.o
5557

5658
all: $(TOOLS) data/ipv6toolkit.conf
@@ -106,6 +108,11 @@ scan6: $(SRCPATH)/scan6.c $(SRCPATH)/scan6.h $(SRCPATH)/ipv6toolkit.h $(LIBS) $(
106108
script6: $(SRCPATH)/script6
107109
cp $(SRCPATH)/script6 ./
108110

111+
tests: $(TESTS)
112+
113+
tests_libipv6: $(TESTSPATH)/tests_libipv6.c libipv6.o
114+
$(CC) $(CPPFLAGS) $(CFLAGS) -o tests_libipv6 $(TESTSPATH)/tests_libipv6.c $(LIBS) $(LDFLAGS)
115+
109116
tcp6: $(SRCPATH)/tcp6.c $(SRCPATH)/tcp6.h $(SRCPATH)/ipv6toolkit.h $(LIBS) $(SRCPATH)/libipv6.h
110117
$(CC) $(CPPFLAGS) $(CFLAGS) -o tcp6 $(SRCPATH)/tcp6.c $(LIBS) $(LDFLAGS)
111118

@@ -140,7 +147,7 @@ data/ipv6toolkit.conf:
140147
data/ipv6toolkit.conf
141148

142149
clean:
143-
rm -f $(TOOLS) $(LIBS)
150+
rm -f $(TOOLS) $(LIBS) $(TESTS)
144151
rm -f data/ipv6toolkit.conf
145152

146153
install: all
@@ -326,3 +333,5 @@ uninstall:
326333
rm -f $(MANPATH)/man5/ipv6toolkit.conf.5
327334
rm -f $(MANPATH)/man7/ipv6toolkit.7
328335

336+
unit_tests: tests
337+
./tests_libipv6

Makefile

+10-1
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,13 @@ DATAPATH= $(DESTDIR)$(PREFIX)/share/ipv6toolkit
3939
BINPATH= $(DESTDIR)$(PREFIX)/bin
4040
SBINPATH= $(DESTDIR)$(PREFIX)/sbin
4141
SRCPATH= tools
42+
TESTSPATH= tests
4243

4344

4445
SBINTOOLS= blackhole6 flow6 frag6 icmp6 jumbo6 messi mldq6 na6 ni6 ns6 path6 ra6 rd6 rs6 scan6 script6 tcp6 udp6
4546
BINTOOLS= addr6
4647
TOOLS= $(BINTOOLS) $(SBINTOOLS)
48+
TESTS= tests_libipv6
4749
LIBS= libipv6.o
4850

4951
all: $(TOOLS) data/ipv6toolkit.conf
@@ -102,6 +104,11 @@ script6: $(SRCPATH)/script6
102104
tcp6: $(SRCPATH)/tcp6.c $(SRCPATH)/tcp6.h $(SRCPATH)/ipv6toolkit.h $(LIBS) $(SRCPATH)/libipv6.h
103105
$(CC) $(CPPFLAGS) $(CFLAGS) -o tcp6 $(SRCPATH)/tcp6.c $(LIBS) $(LDFLAGS)
104106

107+
tests: $(TESTS)
108+
109+
tests_libipv6: $(TESTSPATH)/tests_libipv6.c libipv6.o
110+
$(CC) $(CPPFLAGS) $(CFLAGS) -o tests_libipv6 $(TESTSPATH)/tests_libipv6.c $(LIBS) $(LDFLAGS)
111+
105112
udp6: $(SRCPATH)/udp6.c $(SRCPATH)/udp6.h $(SRCPATH)/ipv6toolkit.h $(LIBS) $(SRCPATH)/libipv6.h
106113
$(CC) $(CPPFLAGS) $(CFLAGS) -o udp6 $(SRCPATH)/udp6.c $(LIBS) $(LDFLAGS)
107114

@@ -133,7 +140,7 @@ data/ipv6toolkit.conf:
133140
data/ipv6toolkit.conf
134141

135142
clean:
136-
rm -f $(TOOLS) $(LIBS)
143+
rm -f $(TOOLS) $(LIBS) $(TESTS)
137144
rm -f data/ipv6toolkit.conf
138145

139146
install: all
@@ -234,3 +241,5 @@ uninstall:
234241
rm -f $(MANPATH)/man5/ipv6toolkit.conf.5
235242
rm -f $(MANPATH)/man7/ipv6toolkit.7
236243

244+
unit_tests: tests
245+
./tests_libipv6

tests/tests_libipv6.c

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <assert.h>
2+
#include <stdlib.h>
3+
#include "../tools/libipv6.h"
4+
5+
void test_is_service_port(void);
6+
7+
int main(void){
8+
test_is_service_port();
9+
exit(EXIT_SUCCESS);
10+
}
11+
12+
void test_is_service_port(void){
13+
assert(is_service_port(443));
14+
assert(is_service_port(1000) == FALSE);
15+
return;
16+
}

0 commit comments

Comments
 (0)