|
25 | 25 |
|
26 | 26 | #include "ln2_test.h" |
27 | 27 |
|
| 28 | +#define KEYLOG_FILENAME "ln2_test_tls_keylog.txt" |
| 29 | + |
28 | 30 | int TEST_PORT = 10050; |
29 | 31 | const char *TEST_PORT_STR = "10050"; |
30 | 32 |
|
@@ -99,6 +101,64 @@ test_nc_tls_ec_key(void **state) |
99 | 101 | } |
100 | 102 | } |
101 | 103 |
|
| 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 | + ret = pthread_create(&tids[0], NULL, client_thread, *state); |
| 151 | + assert_int_equal(ret, 0); |
| 152 | + ret = pthread_create(&tids[1], NULL, ln2_glob_test_server_thread, *state); |
| 153 | + assert_int_equal(ret, 0); |
| 154 | + |
| 155 | + for (i = 0; i < 2; i++) { |
| 156 | + pthread_join(tids[i], NULL); |
| 157 | + } |
| 158 | + |
| 159 | + check_keylog_file(KEYLOG_FILENAME); |
| 160 | +} |
| 161 | + |
102 | 162 | static void |
103 | 163 | test_nc_tls_free_test_data(void *test_data) |
104 | 164 | { |
@@ -149,12 +209,22 @@ setup_f(void **state) |
149 | 209 | return 0; |
150 | 210 | } |
151 | 211 |
|
| 212 | +static int |
| 213 | +keylog_setup_f(void **state) |
| 214 | +{ |
| 215 | + unlink(KEYLOG_FILENAME); |
| 216 | + setenv("SSLKEYLOGFILE", KEYLOG_FILENAME, 1); |
| 217 | + |
| 218 | + return setup_f(state); |
| 219 | +} |
| 220 | + |
152 | 221 | int |
153 | 222 | main(void) |
154 | 223 | { |
155 | 224 | const struct CMUnitTest tests[] = { |
156 | 225 | 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) |
| 226 | + cmocka_unit_test_setup_teardown(test_nc_tls_ec_key, setup_f, ln2_glob_test_teardown), |
| 227 | + cmocka_unit_test_setup_teardown(test_nc_tls_keylog, keylog_setup_f, ln2_glob_test_teardown) |
158 | 228 | }; |
159 | 229 |
|
160 | 230 | /* try to get ports from the environment, otherwise use the default */ |
|
0 commit comments