Skip to content

Commit 8dbfcbf

Browse files
authored
Merge pull request #3014 from cesanta/test
improve TLS testing
2 parents f71b3b9 + 317591b commit 8dbfcbf

File tree

13 files changed

+23928
-57
lines changed

13 files changed

+23928
-57
lines changed

mongoose.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10435,7 +10435,7 @@ static int mg_tls_recv_record(struct mg_connection *c) {
1043510435
r = msgsz - 16 - 1;
1043610436
tls->content_type = msg[msgsz - 16 - 1];
1043710437
tls->recv_offset = (size_t) msg - (size_t) rio->buf;
10438-
tls->recv_len = msgsz - 16 - 1;
10438+
tls->recv_len = (size_t) msgsz - 16 - 1;
1043910439
c->is_client ? tls->enc.sseq++ : tls->enc.cseq++;
1044010440
return r;
1044110441
}

mongoose.h

Lines changed: 31 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2690,25 +2690,6 @@ bool mg_ota_end(void); // Stop writing
26902690

26912691

26922692

2693-
#if MG_OTA != MG_OTA_NONE && MG_OTA != MG_OTA_CUSTOM
2694-
2695-
struct mg_flash {
2696-
void *start; // Address at which flash starts
2697-
size_t size; // Flash size
2698-
size_t secsz; // Sector size
2699-
size_t align; // Write alignment
2700-
bool (*write_fn)(void *, const void *, size_t); // Write function
2701-
bool (*swap_fn)(void); // Swap partitions
2702-
};
2703-
2704-
bool mg_ota_flash_begin(size_t new_firmware_size, struct mg_flash *flash);
2705-
bool mg_ota_flash_write(const void *buf, size_t len, struct mg_flash *flash);
2706-
bool mg_ota_flash_end(struct mg_flash *flash);
2707-
2708-
#endif
2709-
2710-
2711-
27122693

27132694

27142695

@@ -3195,22 +3176,14 @@ struct mg_tcpip_driver_tms570_data {
31953176

31963177

31973178

3198-
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_XMC) && MG_ENABLE_DRIVER_XMC
3179+
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_W5500) && MG_ENABLE_DRIVER_W5500
31993180

3200-
struct mg_tcpip_driver_xmc_data {
3201-
// 13.2.8.1 Station Management Functions
3202-
// MDC clock divider (). MDC clock is derived from ETH MAC clock
3203-
// It must not exceed 2.5MHz
3204-
// ETH Clock range DIVIDER mdc_cr VALUE
3205-
// --------------------------------------------
3206-
// -1 <-- tell driver to guess the value
3207-
// 60-100 MHz ETH Clock/42 0
3208-
// 100-150 MHz ETH Clock/62 1
3209-
// 20-35 MHz ETH Clock/16 2
3210-
// 35-60 MHz ETH Clock/26 3
3211-
// 150-250 MHz ETH Clock/102 4
3212-
// 250-300 MHz ETH Clock/124 5
3213-
// 110, 111 Reserved
3181+
#endif
3182+
3183+
3184+
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_XMC7) && MG_ENABLE_DRIVER_XMC7
3185+
3186+
struct mg_tcpip_driver_xmc7_data {
32143187
int mdc_cr; // Valid values: -1, 0, 1, 2, 3, 4, 5
32153188
uint8_t phy_addr;
32163189
};
@@ -3220,31 +3193,45 @@ struct mg_tcpip_driver_xmc_data {
32203193
#endif
32213194

32223195
#ifndef MG_DRIVER_MDC_CR
3223-
#define MG_DRIVER_MDC_CR 4
3196+
#define MG_DRIVER_MDC_CR 3
32243197
#endif
32253198

32263199
#define MG_TCPIP_DRIVER_INIT(mgr) \
32273200
do { \
3228-
static struct mg_tcpip_driver_xmc_data driver_data_; \
3201+
static struct mg_tcpip_driver_xmc7_data driver_data_; \
32293202
static struct mg_tcpip_if mif_; \
32303203
driver_data_.mdc_cr = MG_DRIVER_MDC_CR; \
32313204
driver_data_.phy_addr = MG_TCPIP_PHY_ADDR; \
32323205
mif_.ip = MG_TCPIP_IP; \
32333206
mif_.mask = MG_TCPIP_MASK; \
32343207
mif_.gw = MG_TCPIP_GW; \
3235-
mif_.driver = &mg_tcpip_driver_xmc; \
3208+
mif_.driver = &mg_tcpip_driver_xmc7; \
32363209
mif_.driver_data = &driver_data_; \
32373210
MG_SET_MAC_ADDRESS(mif_.mac); \
32383211
mg_tcpip_init(mgr, &mif_); \
3239-
MG_INFO(("Driver: xmc, MAC: %M", mg_print_mac, mif_.mac)); \
3212+
MG_INFO(("Driver: xmc7, MAC: %M", mg_print_mac, mif_.mac)); \
32403213
} while (0)
32413214

32423215
#endif
32433216

32443217

3245-
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_XMC7) && MG_ENABLE_DRIVER_XMC7
32463218

3247-
struct mg_tcpip_driver_xmc7_data {
3219+
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_XMC) && MG_ENABLE_DRIVER_XMC
3220+
3221+
struct mg_tcpip_driver_xmc_data {
3222+
// 13.2.8.1 Station Management Functions
3223+
// MDC clock divider (). MDC clock is derived from ETH MAC clock
3224+
// It must not exceed 2.5MHz
3225+
// ETH Clock range DIVIDER mdc_cr VALUE
3226+
// --------------------------------------------
3227+
// -1 <-- tell driver to guess the value
3228+
// 60-100 MHz ETH Clock/42 0
3229+
// 100-150 MHz ETH Clock/62 1
3230+
// 20-35 MHz ETH Clock/16 2
3231+
// 35-60 MHz ETH Clock/26 3
3232+
// 150-250 MHz ETH Clock/102 4
3233+
// 250-300 MHz ETH Clock/124 5
3234+
// 110, 111 Reserved
32483235
int mdc_cr; // Valid values: -1, 0, 1, 2, 3, 4, 5
32493236
uint8_t phy_addr;
32503237
};
@@ -3254,28 +3241,27 @@ struct mg_tcpip_driver_xmc7_data {
32543241
#endif
32553242

32563243
#ifndef MG_DRIVER_MDC_CR
3257-
#define MG_DRIVER_MDC_CR 3
3244+
#define MG_DRIVER_MDC_CR 4
32583245
#endif
32593246

32603247
#define MG_TCPIP_DRIVER_INIT(mgr) \
32613248
do { \
3262-
static struct mg_tcpip_driver_xmc7_data driver_data_; \
3249+
static struct mg_tcpip_driver_xmc_data driver_data_; \
32633250
static struct mg_tcpip_if mif_; \
32643251
driver_data_.mdc_cr = MG_DRIVER_MDC_CR; \
32653252
driver_data_.phy_addr = MG_TCPIP_PHY_ADDR; \
32663253
mif_.ip = MG_TCPIP_IP; \
32673254
mif_.mask = MG_TCPIP_MASK; \
32683255
mif_.gw = MG_TCPIP_GW; \
3269-
mif_.driver = &mg_tcpip_driver_xmc7; \
3256+
mif_.driver = &mg_tcpip_driver_xmc; \
32703257
mif_.driver_data = &driver_data_; \
32713258
MG_SET_MAC_ADDRESS(mif_.mac); \
32723259
mg_tcpip_init(mgr, &mif_); \
3273-
MG_INFO(("Driver: xmc7, MAC: %M", mg_print_mac, mif_.mac)); \
3260+
MG_INFO(("Driver: xmc, MAC: %M", mg_print_mac, mif_.mac)); \
32743261
} while (0)
32753262

32763263
#endif
32773264

3278-
32793265
#ifdef __cplusplus
32803266
}
32813267
#endif

src/tls_builtin.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ static int mg_tls_recv_record(struct mg_connection *c) {
486486
r = msgsz - 16 - 1;
487487
tls->content_type = msg[msgsz - 16 - 1];
488488
tls->recv_offset = (size_t) msg - (size_t) rio->buf;
489-
tls->recv_len = msgsz - 16 - 1;
489+
tls->recv_len = (size_t) msgsz - 16 - 1;
490490
c->is_client ? tls->enc.sseq++ : tls->enc.cseq++;
491491
return r;
492492
}

test/Makefile

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ mip_test: mip_test.c mongoose.c mongoose.h packed_fs.c Makefile
9090
$(CC) mip_test.c packed_fs.c $(CFLAGS) $(LDFLAGS) -o $@
9191
ASAN_OPTIONS=$(ASAN_OPTIONS) $(RUN) ./$@
9292

93-
mip_tap_test: mip_tap_test.c mongoose.c mongoose.h packed_fs.c Makefile
93+
mip_tap_test: mip_tap_test.c mongoose.c mongoose.h packed_fs.c Makefile tls_multirec/server
9494
$(CC) mip_tap_test.c packed_fs.c $(CFLAGS) $(LDFLAGS) -o $@
9595
ASAN_OPTIONS=$(ASAN_OPTIONS) $(RUN) ./$@
9696

@@ -123,10 +123,16 @@ fuzz2: mongoose.c mongoose.h Makefile fuzz.c
123123
$(CC) fuzz.c -DMAIN $(OPTS) $(WARN) $(ASAN) $(INCS) -o fuzzer
124124
$(RUN) ./fuzzer $(FUZZDATA)
125125

126-
test: Makefile mongoose.h $(SRCS)
126+
test: Makefile mongoose.h $(SRCS) tls_multirec/server
127127
$(CC) $(SRCS) $(CFLAGS) $(LDFLAGS) -o unit_test
128128
ASAN_OPTIONS=$(ASAN_OPTIONS) $(RUN) ./unit_test
129129

130+
tls_multirec/server: FORCE
131+
$(MAKE) -C tls_multirec CC=gcc
132+
133+
FORCE:
134+
true
135+
130136
coverage: CFLAGS += -coverage
131137
coverage: test
132138
gcov -l -n *.gcno | sed '/^$$/d' | sed 'N;s/\n/ /'

test/mip_tap_test.c

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#define MIPTAPTEST_USING_DHCP 1
55

6-
#define FETCH_BUF_SIZE (8 * 1024)
6+
#define FETCH_BUF_SIZE (16 * 1024)
77

88

99
#include <sys/socket.h>
@@ -64,6 +64,8 @@ static const char *s_ca_cert =
6464
#endif
6565

6666

67+
static char *host_ip;
68+
6769
static int s_num_tests = 0;
6870

6971
#define ABORT() \
@@ -79,6 +81,22 @@ static int s_num_tests = 0;
7981
} \
8082
} while (0)
8183

84+
static struct mg_http_message gethm(const char *buf) {
85+
struct mg_http_message hm;
86+
memset(&hm, 0, sizeof(hm));
87+
mg_http_parse(buf, strlen(buf), &hm);
88+
return hm;
89+
}
90+
91+
static int cmpbody(const char *buf, const char *str) {
92+
struct mg_str s = mg_str(str);
93+
struct mg_http_message hm = gethm(buf);
94+
size_t len = strlen(buf);
95+
if (hm.body.len > len) hm.body.len = len - (size_t) (hm.body.buf - buf);
96+
return mg_strcmp(hm.body, s);
97+
}
98+
99+
82100
// MIP TUNTAP driver
83101
static size_t tap_rx(void *buf, size_t len, struct mg_tcpip_if *ifp) {
84102
ssize_t received = read(*(int *) ifp->driver_data, buf, len);
@@ -144,6 +162,11 @@ static void fcb(struct mg_connection *c, int ev, void *ev_data) {
144162
memset(&opts, 0, sizeof(opts)); // read CA from packed_fs
145163
opts.name = mg_url_host(fd->url);
146164
opts.ca = mg_unpacked("/data/ca.pem");
165+
if (host_ip != NULL && strstr(fd->url, host_ip) != NULL) {
166+
MG_DEBUG(("Local connection, using self-signed certificates"));
167+
opts.name = mg_str_s("localhost");
168+
opts.ca = mg_unpacked("/certs/ca.crt");
169+
}
147170
mg_tls_init(c, &opts);
148171
}
149172
} else if (ev == MG_EV_HTTP_MSG) {
@@ -277,7 +300,7 @@ static void *poll_thread(void *p) {
277300
return NULL;
278301
}
279302

280-
static void test_http_server(struct mg_mgr *mgr, uint32_t ip) {
303+
static void test_http_server(struct mg_mgr *mgr) {
281304
struct mg_connection *c;
282305
char *cmd;
283306
pthread_t thread_id = (pthread_t) 0;
@@ -288,10 +311,10 @@ static void test_http_server(struct mg_mgr *mgr, uint32_t ip) {
288311
opts.cert = mg_unpacked("/certs/server.crt");
289312
opts.key = mg_unpacked("/certs/server.key");
290313
c = mg_http_listen(mgr, "https://0.0.0.0:12347", eh1, &opts);
291-
cmd = mg_mprintf("./mip_curl.sh --insecure https://%M:12347", mg_print_ip4, &ip);
314+
cmd = mg_mprintf("./mip_curl.sh --insecure https://%M:12347", mg_print_ip4, &mgr->ifp->ip);
292315
#else
293316
c = mg_http_listen(mgr, "http://0.0.0.0:12347", eh1, NULL);
294-
cmd = mg_mprintf("./mip_curl.sh http://%M:12347", mg_print_ip4, &ip);
317+
cmd = mg_mprintf("./mip_curl.sh http://%M:12347", mg_print_ip4, &mgr->ifp->ip);
295318
#endif
296319
ASSERT(c != NULL);
297320
pthread_create(&thread_id, NULL, poll_thread, mgr); // simpler this way, no concurrency anyway
@@ -303,10 +326,36 @@ static void test_http_server(struct mg_mgr *mgr, uint32_t ip) {
303326
free(cmd);
304327
}
305328

329+
static void test_tls(struct mg_mgr *mgr) {
330+
#if MG_TLS
331+
char *url;
332+
char buf[FETCH_BUF_SIZE]; // make sure it can hold Makefile
333+
struct mg_str data = mg_unpacked("/Makefile");
334+
if (host_ip == NULL) {
335+
MG_INFO(("No HOST_IP provided, skipping tests"));
336+
return;
337+
}
338+
MG_DEBUG(("HOST_IP: %s", host_ip));
339+
// - POST a large file, make sure we drain TLS buffers and read all: done at server test, using curl as POSTing client
340+
// - Fire patched server, test multiple TLS records per TCP segment handling
341+
url = mg_mprintf("https://%s:8443", host_ip); // for historic reasons
342+
ASSERT(system("tls_multirec/server -d tls_multirec &") == 0);
343+
sleep(1);
344+
ASSERT(fetch(mgr, buf, url, "GET /thefile HTTP/1.0\n\n") == 200);
345+
ASSERT(cmpbody(buf, data.buf) == 0); // "thefile" links to Makefile
346+
system("killall tls_multirec/server");
347+
free(url);
348+
#else
349+
(void) mgr;
350+
(void) ip;
351+
#endif
352+
}
353+
306354
int main(void) {
355+
const char *debug_level = getenv("V");
307356
// Setup interface
308357
const char *iface = "tap0"; // Network iface
309-
const char *mac = "00:00:01:02:03:78"; // MAC address
358+
const char *mac = "02:00:01:02:03:78"; // MAC address
310359
#ifndef __OpenBSD__
311360
const char *tuntap_device = "/dev/net/tun";
312361
#else
@@ -334,6 +383,11 @@ int main(void) {
334383
MG_INFO(("Opened TAP interface: %s", iface));
335384
usleep(200000); // 200 ms
336385

386+
if (debug_level == NULL) debug_level = "3";
387+
mg_log_set(atoi(debug_level));
388+
389+
host_ip = getenv("HOST_IP");
390+
337391
// Events
338392
struct mg_mgr mgr; // Event manager
339393
mg_log_set(MG_LL_DEBUG);
@@ -383,12 +437,18 @@ int main(void) {
383437
if (!mif.ip) MG_ERROR(("No ip assigned (DHCP lease may have failed).\n"));
384438
ASSERT(mif.ip); // We have an IP (lease or static)
385439
#endif
440+
while (mif.state != MG_TCPIP_STATE_READY) {
441+
mg_mgr_poll(&mgr, 100);
442+
usleep(10000); // 10 ms
443+
}
386444

387445
// RUN TESTS
388446
usleep(500000); // 500 ms
389447
test_http_client(&mgr);
390448
usleep(500000); // 500 ms
391-
test_http_server(&mgr, mif.ip);
449+
test_http_server(&mgr);
450+
usleep(500000); // 500 ms
451+
test_tls(&mgr);
392452
usleep(500000); // 500 ms
393453
test_mqtt_connsubpub(&mgr);
394454
usleep(500000); // 500 ms

test/setup_ga_network.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ echo
4949
# Setup DHCP server
5050
echo "Network configuration script: DHCP server"
5151
echo "Serving from $BRIDGE_IP"
52+
export HOST_IP = $BRIDGE_IP
5253
echo "dhcpd.conf:"
5354
cat test/dhcpd.conf
5455
echo

test/tls_multirec/Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CFLAGS = -W -Wall -Wextra -g -I. # Build options
2+
CFLAGS_MONGOOSE += -DMG_ENABLE_LINES=1 -DMG_ENABLE_IPV6=1
3+
CFLAGS_EXTRA ?= -DMG_TLS=MG_TLS_BUILTIN
4+
5+
server: main.c patched_mongoose.c mongoose.h Makefile
6+
$(CC) main.c patched_mongoose.c $(CFLAGS) $(CFLAGS_MONGOOSE) $(CFLAGS_EXTRA) -o $@
7+
8+
clean:
9+
rm -f server

0 commit comments

Comments
 (0)