Skip to content

Commit 4fada02

Browse files
authored
Merge pull request #16 from danielinux/unit-tests-cov
Added more unit tests, test coverage reports
2 parents bd96da6 + cc2367e commit 4fada02

8 files changed

Lines changed: 10111 additions & 800 deletions

File tree

Makefile

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,11 @@ TAP_PIE_OBJ:=$(patsubst src/%.c,build/pie/%.o,$(TAP_SRC))
6565
ifeq ($(UNAME_S),Darwin)
6666
BEGIN_GROUP:=
6767
END_GROUP:=
68+
OPEN_CMD?=open
6869
else
6970
BEGIN_GROUP:=-Wl,--start-group
7071
END_GROUP:=-Wl,--end-group
72+
OPEN_CMD?=xdg-open
7173
endif
7274

7375
CHECK_PKG_CFLAGS:=$(shell pkg-config --cflags check 2>/dev/null)
@@ -76,6 +78,7 @@ CHECK_PKG_LIBS:=$(shell pkg-config --libs check 2>/dev/null)
7678
ifneq ($(CHECK_PKG_CFLAGS),)
7779
UNIT_CFLAGS+=$(CHECK_PKG_CFLAGS)
7880
endif
81+
UNIT_CFLAGS+=-Isrc/test/unit/mocks
7982

8083
CPPCHECK=cppcheck
8184
CPPCHECK_FLAGS=--enable=warning,performance,portability,missingInclude \
@@ -271,17 +274,40 @@ unit: build/test/unit
271274
build/test/unit:
272275
@mkdir -p build/test/
273276
@echo "[CC] unit.c"
274-
@$(CC) $(CFLAGS) $(UNIT_CFLAGS) -c src/test/unit/unit.c -o build/test/unit.o
277+
@$(CC) $(UNIT_CFLAGS) $(CFLAGS) -c src/test/unit/unit.c -o build/test/unit.o
275278
@echo "[LD] $@"
276279
@$(CC) build/test/unit.o -o build/test/unit $(UNIT_LDFLAGS) $(LDFLAGS)
277280

281+
COV_DIR:=build/coverage
282+
COV_UNIT:=$(COV_DIR)/unit
283+
COV_UNIT_O:=$(COV_DIR)/unit.o
284+
285+
$(COV_UNIT_O): src/test/unit/unit.c
286+
@mkdir -p $(COV_DIR)
287+
@echo "[CC] unit.c (coverage)"
288+
@$(CC) $(UNIT_CFLAGS) $(CFLAGS) --coverage -c src/test/unit/unit.c -o $(COV_UNIT_O)
289+
290+
$(COV_UNIT): LDFLAGS+=--coverage $(UNIT_LIBS)
291+
$(COV_UNIT): $(COV_UNIT_O)
292+
@echo "[LD] $@"
293+
@$(CC) $(COV_UNIT_O) -o $(COV_UNIT) $(UNIT_LDFLAGS) $(LDFLAGS)
294+
295+
cov: unit $(COV_UNIT)
296+
@echo "[RUN] unit (coverage)"
297+
@rm -f $(COV_DIR)/*.gcda
298+
@$(COV_UNIT)
299+
@echo "[COV] gcovr html"
300+
@mkdir -p build/coverage
301+
@gcovr -r . --exclude "src/test/unit/unit.c" --html-details -o build/coverage/index.html
302+
@$(OPEN_CMD) build/coverage/index.html
303+
278304
# Install dynamic library to re-link linux applications
279305
#
280306
install:
281307
install libwolfip.so $(PREFIX)/lib
282308
ldconfig
283309

284-
.PHONY: clean all static cppcheck
310+
.PHONY: clean all static cppcheck cov
285311

286312
cppcheck:
287313
$(CPPCHECK) $(CPPCHECK_FLAGS) src/ 2>cppcheck_results.xml

README.md

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,38 @@ A single network interface can be associated with the device.
1313

1414
## Features supported
1515

16-
- ARP (RFC 826)
17-
- IPv4 (RFC 791)
18-
- ICMP (RFC 792): only ping replies
19-
- DHCP (RFC 2131): client only
20-
- DNS (RFC 1035): client only
21-
- UDP (RFC 768): unicast only
22-
- TCP (RFC 793)
23-
- TCP options supported: Timestamps, Maximum Segment Size
2416
- BSD-like, non blocking socket API, with custom callbacks
2517
- No dynamic memory allocation
2618
- Fixed number of concurrent sockets
2719
- Pre-allocated buffers for packet processing in static memory
2820

21+
## Protocols and RFCs
22+
23+
| Layer | Protocol | Features | RFC(s) |
24+
|-------|----------|----------|--------|
25+
| **Data Link** | Ethernet II | Frame encapsulation | [IEEE 802.3](https://standards.ieee.org/ieee/802.3/10422/) |
26+
| **Data Link** | ARP | Address resolution, request/reply | [RFC 826](https://datatracker.ietf.org/doc/html/rfc826) |
27+
| **Network** | IPv4 | Datagram delivery, TTL handling | [RFC 791](https://datatracker.ietf.org/doc/html/rfc791) |
28+
| **Network** | IPv4 Forwarding | Multi-interface routing (optional) | [RFC 1812](https://datatracker.ietf.org/doc/html/rfc1812) |
29+
| **Network** | ICMP | Echo request/reply, TTL exceeded | [RFC 792](https://datatracker.ietf.org/doc/html/rfc792) |
30+
| **Transport** | UDP | Unicast datagrams, checksum | [RFC 768](https://datatracker.ietf.org/doc/html/rfc768) |
31+
| **Transport** | TCP | Connection management, reliable delivery | [RFC 793](https://datatracker.ietf.org/doc/html/rfc793), [RFC 9293](https://datatracker.ietf.org/doc/html/rfc9293) |
32+
| **Transport** | TCP Options: MSS | Maximum Segment Size negotiation | [RFC 793](https://datatracker.ietf.org/doc/html/rfc793) |
33+
| **Transport** | TCP Options: Timestamps | RTT measurement, PAWS | [RFC 7323](https://datatracker.ietf.org/doc/html/rfc7323) |
34+
| **Transport** | TCP Congestion Control | Slow start, congestion avoidance | [RFC 5681](https://datatracker.ietf.org/doc/html/rfc5681) |
35+
| **Transport** | TCP Fast Retransmit | Triple duplicate ACK detection | [RFC 5681](https://datatracker.ietf.org/doc/html/rfc5681) |
36+
| **Application** | DHCP | Client only (DORA) | [RFC 2131](https://datatracker.ietf.org/doc/html/rfc2131) |
37+
| **Application** | DNS | A and PTR record queries (client) | [RFC 1035](https://datatracker.ietf.org/doc/html/rfc1035) |
38+
| **Application** | HTTP/HTTPS | Server with wolfSSL TLS support | [RFC 9110](https://datatracker.ietf.org/doc/html/rfc9110) |
39+
40+
### Notes
41+
42+
- **TCP Congestion Control**: Implements slow start and congestion avoidance with `cwnd`, `ssthresh` tracking.
43+
- **TCP Fast Retransmit**: Detects triple duplicate ACKs and retransmits lost segments.
44+
- **ICMP**: Responds to ping requests; sends TTL exceeded messages when forwarding is enabled.
45+
- **DHCP**: Full DORA (Discover, Offer, Request, Acknowledge) state machine with retry logic.
46+
- **DNS**: Supports A record (forward) and PTR record (reverse) lookups.
47+
2948

3049
## Functional tests with `LD_PRELOAD`
3150

src/port/wolfssl_io.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include <wolfssl/ssl.h>
2626
#include <wolfssl/wolfcrypt/memory.h>
2727

28-
2928
#ifndef MAX_WOLFIP_CTX
3029
#define MAX_WOLFIP_CTX 8 /* Default value */
3130
#endif

src/test/unit/mocks/wolfssl/ssl.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* Mock wolfssl/ssl.h for unit tests.
2+
* Only includes pieces needed by src/port/wolfssl_io.c tests.
3+
*/
4+
#ifndef WOLFSSL_SSL_H
5+
#define WOLFSSL_SSL_H
6+
7+
#ifdef __cplusplus
8+
extern "C" {
9+
#endif
10+
11+
typedef struct WOLFSSL_CTX {
12+
int id;
13+
} WOLFSSL_CTX;
14+
15+
typedef struct WOLFSSL {
16+
WOLFSSL_CTX *ctx;
17+
void *rctx;
18+
void *wctx;
19+
} WOLFSSL;
20+
21+
typedef int (*CallbackIORecv)(WOLFSSL *ssl, char *buf, int sz, void *ctx);
22+
typedef int (*CallbackIOSend)(WOLFSSL *ssl, char *buf, int sz, void *ctx);
23+
24+
#define WOLFSSL_CBIO_ERR_GENERAL (-1)
25+
#define WOLFSSL_CBIO_ERR_WANT_READ (-2)
26+
#define WOLFSSL_CBIO_ERR_WANT_WRITE (-2)
27+
#define WOLFSSL_CBIO_ERR_CONN_CLOSE (-5)
28+
29+
int wolfSSL_SetIORecv(WOLFSSL_CTX *ctx, CallbackIORecv cb);
30+
int wolfSSL_SetIOSend(WOLFSSL_CTX *ctx, CallbackIOSend cb);
31+
int wolfSSL_SetIOReadCtx(WOLFSSL *ssl, void *ctx);
32+
int wolfSSL_SetIOWriteCtx(WOLFSSL *ssl, void *ctx);
33+
WOLFSSL_CTX *wolfSSL_get_SSL_CTX(WOLFSSL *ssl);
34+
35+
#ifdef __cplusplus
36+
}
37+
#endif
38+
39+
#endif /* WOLFSSL_SSL_H */
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/* Mock wolfssl/wolfcrypt/memory.h for unit tests. */
2+
#ifndef WOLFSSL_WOLFCRYPT_MEMORY_H
3+
#define WOLFSSL_WOLFCRYPT_MEMORY_H
4+
5+
#endif /* WOLFSSL_WOLFCRYPT_MEMORY_H */
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/* Mock wolfssl/wolfcrypt/settings.h for unit tests. */
2+
#ifndef WOLFSSL_WOLFCRYPT_SETTINGS_H
3+
#define WOLFSSL_WOLFCRYPT_SETTINGS_H
4+
5+
#endif /* WOLFSSL_WOLFCRYPT_SETTINGS_H */

0 commit comments

Comments
 (0)