Skip to content

Commit 1870f05

Browse files
author
roman
committed
test tls UPDATE add tls keylog test
1 parent ea1fc0a commit 1870f05

File tree

1 file changed

+67
-1
lines changed

1 file changed

+67
-1
lines changed

tests/test_tls.c

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
#include "ln2_test.h"
2727

28+
#define KEYLOG_FILENAME "ln2_test_tls_keylog.txt"
29+
2830
int TEST_PORT = 10050;
2931
const char *TEST_PORT_STR = "10050";
3032

@@ -99,6 +101,69 @@ test_nc_tls_ec_key(void **state)
99101
}
100102
}
101103

104+
static void
105+
check_keylog_file(const char *filename)
106+
{
107+
char buf[256];
108+
FILE *f;
109+
int cli_random, cli_hs, cli_traffic, srv_hs, srv_traffic;
110+
111+
cli_random = cli_hs = cli_traffic = srv_hs = srv_traffic = 0;
112+
113+
f = fopen(filename, "r");
114+
assert_non_null(f);
115+
116+
while (fgets(buf, sizeof(buf), f)) {
117+
if (!strncmp(buf, "CLIENT_RANDOM", 13)) {
118+
cli_random++;
119+
} else if (!strncmp(buf, "CLIENT_HANDSHAKE_TRAFFIC_SECRET", 31)) {
120+
cli_hs++;
121+
} else if (!strncmp(buf, "CLIENT_TRAFFIC_SECRET_0", 23)) {
122+
cli_traffic++;
123+
} else if (!strncmp(buf, "SERVER_HANDSHAKE_TRAFFIC_SECRET", 31)) {
124+
srv_hs++;
125+
} else if (!strncmp(buf, "SERVER_TRAFFIC_SECRET_0", 23)) {
126+
srv_traffic++;
127+
}
128+
}
129+
130+
fclose(f);
131+
132+
if (cli_random) {
133+
/* tls 1.2 */
134+
assert_int_equal(cli_random, 1);
135+
assert_int_equal(cli_hs + cli_traffic + srv_hs + srv_traffic, 0);
136+
} else {
137+
/* tls 1.3 */
138+
assert_int_equal(cli_hs + cli_traffic + srv_hs + srv_traffic, 4);
139+
}
140+
}
141+
142+
static void
143+
test_nc_tls_keylog(void **state)
144+
{
145+
int ret, i;
146+
pthread_t tids[2];
147+
148+
assert_non_null(state);
149+
150+
unlink(KEYLOG_FILENAME);
151+
152+
ret = setenv("SSLKEYLOGFILE", KEYLOG_FILENAME, 1);
153+
assert_int_equal(ret, 0);
154+
155+
ret = pthread_create(&tids[0], NULL, client_thread, *state);
156+
assert_int_equal(ret, 0);
157+
ret = pthread_create(&tids[1], NULL, ln2_glob_test_server_thread, *state);
158+
assert_int_equal(ret, 0);
159+
160+
for (i = 0; i < 2; i++) {
161+
pthread_join(tids[i], NULL);
162+
}
163+
164+
check_keylog_file(KEYLOG_FILENAME);
165+
}
166+
102167
static void
103168
test_nc_tls_free_test_data(void *test_data)
104169
{
@@ -154,7 +219,8 @@ main(void)
154219
{
155220
const struct CMUnitTest tests[] = {
156221
cmocka_unit_test_setup_teardown(test_nc_tls, setup_f, ln2_glob_test_teardown),
157-
cmocka_unit_test_setup_teardown(test_nc_tls_ec_key, setup_f, ln2_glob_test_teardown)
222+
cmocka_unit_test_setup_teardown(test_nc_tls_ec_key, setup_f, ln2_glob_test_teardown),
223+
cmocka_unit_test_setup_teardown(test_nc_tls_keylog, setup_f, ln2_glob_test_teardown)
158224
};
159225

160226
/* try to get ports from the environment, otherwise use the default */

0 commit comments

Comments
 (0)