| 1 |
/* |
| 2 |
* Copyright (C) Internet Systems Consortium, Inc. ("ISC") |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: MPL-2.0 |
| 5 |
* |
| 6 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 7 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 |
* file, you can obtain one at https://mozilla.org/MPL/2.0/. |
| 9 |
* |
| 10 |
* See the COPYRIGHT file distributed with this work for additional |
| 11 |
* information regarding copyright ownership. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include <inttypes.h> |
| 15 |
#include <sched.h> /* IWYU pragma: keep */ |
| 16 |
#include <setjmp.h> |
| 17 |
#include <signal.h> |
| 18 |
#include <stdarg.h> |
| 19 |
#include <stdlib.h> |
| 20 |
#include <unistd.h> |
| 21 |
|
| 22 |
/* |
| 23 |
* As a workaround, include an OpenSSL header file before including cmocka.h, |
| 24 |
* because OpenSSL 3.1.0 uses __attribute__(malloc), conflicting with a |
| 25 |
* redefined malloc in cmocka.h. |
| 26 |
*/ |
| 27 |
#include <openssl/err.h> |
| 28 |
|
| 29 |
#define UNIT_TESTING |
| 30 |
#include <cmocka.h> |
| 31 |
|
| 32 |
#include "netmgr_common.h" |
| 33 |
|
| 34 |
#include <tests/isc.h> |
| 35 |
|
| 36 |
/* |
| 37 |
* FIXME: This really needs two network managers, so there's predictable result |
| 38 |
* when shuttingdown the netmgr - right now there's a race whether the listening |
| 39 |
* or connecting sockets gets shutdown first |
| 40 |
*/ |
| 41 |
|
| 42 |
static void |
| 43 |
shutdownconnect_connectcb(isc_nmhandle_t *handle, isc_result_t eresult, |
| 44 |
void *cbarg) { |
| 45 |
F(); |
| 46 |
|
| 47 |
assert_non_null(handle); |
| 48 |
assert_int_equal(eresult, ISC_R_SHUTTINGDOWN); |
| 49 |
assert_null(cbarg); |
| 50 |
|
| 51 |
isc_refcount_decrement(&active_cconnects); |
| 52 |
|
| 53 |
atomic_fetch_add(&cconnects, 1); |
| 54 |
} |
| 55 |
|
| 56 |
int |
| 57 |
stream_shutdownconnect_setup(void **state ISC_ATTR_UNUSED) { |
| 58 |
int r = setup_netmgr_test(state); |
| 59 |
return r; |
| 60 |
} |
| 61 |
|
| 62 |
int |
| 63 |
proxystream_shutdownconnect_setup(void **state) { |
| 64 |
stream_use_PROXY = true; |
| 65 |
return stream_shutdownconnect_setup(state); |
| 66 |
} |
| 67 |
|
| 68 |
int |
| 69 |
proxystream_shutdownconnect_teardown(void **state) { |
| 70 |
int r = stream_shutdownconnect_teardown(state); |
| 71 |
stream_use_PROXY = false; |
| 72 |
return r; |
| 73 |
} |
| 74 |
|
| 75 |
int |
| 76 |
proxystreamtls_shutdownconnect_setup(void **state) { |
| 77 |
stream_PROXY_over_TLS = true; |
| 78 |
return proxystream_shutdownconnect_setup(state); |
| 79 |
} |
| 80 |
|
| 81 |
int |
| 82 |
proxystreamtls_shutdownconnect_teardown(void **state) { |
| 83 |
int r = proxystream_shutdownconnect_teardown(state); |
| 84 |
stream_PROXY_over_TLS = false; |
| 85 |
return r; |
| 86 |
} |
| 87 |
|
| 88 |
void |
| 89 |
stream_shutdownconnect(void **state ISC_ATTR_UNUSED) { |
| 90 |
isc_result_t result = stream_listen(stream_accept_cb, NULL, 128, NULL, |
| 91 |
&listen_sock); |
| 92 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 93 |
isc_loop_teardown(mainloop, stop_listening, listen_sock); |
| 94 |
|
| 95 |
/* Schedule the shutdown before the connect */ |
| 96 |
isc_loopmgr_shutdown(loopmgr); |
| 97 |
|
| 98 |
stream_connect(shutdownconnect_connectcb, NULL, T_CONNECT); |
| 99 |
} |
| 100 |
|
| 101 |
int |
| 102 |
stream_shutdownconnect_teardown(void **state ISC_ATTR_UNUSED) { |
| 103 |
X(cconnects); |
| 104 |
X(csends); |
| 105 |
X(creads); |
| 106 |
|
| 107 |
atomic_assert_int_eq(cconnects, 1); |
| 108 |
atomic_assert_int_eq(csends, 0); |
| 109 |
atomic_assert_int_eq(creads, 0); |
| 110 |
|
| 111 |
return teardown_netmgr_test(state); |
| 112 |
} |
| 113 |
|
| 114 |
/* Issue the shutdown before reading */ |
| 115 |
|
| 116 |
static void |
| 117 |
shutdownread_readcb(isc_nmhandle_t *handle, isc_result_t eresult, |
| 118 |
isc_region_t *region, void *cbarg) { |
| 119 |
F(); |
| 120 |
assert_non_null(handle); |
| 121 |
assert_true(eresult == ISC_R_SHUTTINGDOWN || |
| 122 |
eresult == ISC_R_CONNECTIONRESET || eresult == ISC_R_EOF); |
| 123 |
assert_non_null(region); |
| 124 |
assert_null(cbarg); |
| 125 |
|
| 126 |
atomic_fetch_add(&creads, 1); |
| 127 |
isc_nmhandle_detach(&handle); |
| 128 |
isc_refcount_decrement(&active_creads); |
| 129 |
} |
| 130 |
|
| 131 |
static void |
| 132 |
shutdownread_sendcb(isc_nmhandle_t *handle, isc_result_t eresult, void *cbarg) { |
| 133 |
F(); |
| 134 |
assert_non_null(handle); |
| 135 |
assert_true(eresult == ISC_R_SUCCESS || eresult == ISC_R_SHUTTINGDOWN || |
| 136 |
eresult == ISC_R_CONNECTIONRESET || eresult == ISC_R_EOF); |
| 137 |
assert_null(cbarg); |
| 138 |
|
| 139 |
atomic_fetch_add(&csends, 1); |
| 140 |
|
| 141 |
isc_nmhandle_detach(&handle); |
| 142 |
isc_refcount_decrement(&active_csends); |
| 143 |
} |
| 144 |
|
| 145 |
static void |
| 146 |
shutdownread_connectcb(isc_nmhandle_t *handle, isc_result_t eresult, |
| 147 |
void *cbarg) { |
| 148 |
F(); |
| 149 |
|
| 150 |
assert_non_null(handle); |
| 151 |
assert_int_equal(eresult, ISC_R_SUCCESS); |
| 152 |
assert_null(cbarg); |
| 153 |
|
| 154 |
isc_refcount_decrement(&active_cconnects); |
| 155 |
|
| 156 |
atomic_fetch_add(&cconnects, 1); |
| 157 |
|
| 158 |
/* Schedule the shutdown before read and send */ |
| 159 |
isc_loopmgr_shutdown(loopmgr); |
| 160 |
|
| 161 |
isc_refcount_increment0(&active_creads); |
| 162 |
isc_nmhandle_ref(handle); |
| 163 |
isc_nm_read(handle, shutdownread_readcb, cbarg); |
| 164 |
|
| 165 |
isc_refcount_increment0(&active_csends); |
| 166 |
isc_nmhandle_ref(handle); |
| 167 |
isc_nm_send(handle, (isc_region_t *)&send_msg, shutdownread_sendcb, |
| 168 |
cbarg); |
| 169 |
} |
| 170 |
|
| 171 |
int |
| 172 |
stream_shutdownread_setup(void **state ISC_ATTR_UNUSED) { |
| 173 |
int r = setup_netmgr_test(state); |
| 174 |
return r; |
| 175 |
} |
| 176 |
|
| 177 |
int |
| 178 |
proxystream_shutdownread_setup(void **state) { |
| 179 |
stream_use_PROXY = true; |
| 180 |
return stream_shutdownread_setup(state); |
| 181 |
} |
| 182 |
|
| 183 |
int |
| 184 |
proxystream_shutdownread_teardown(void **state) { |
| 185 |
int r = stream_shutdownread_teardown(state); |
| 186 |
stream_use_PROXY = false; |
| 187 |
return r; |
| 188 |
} |
| 189 |
|
| 190 |
int |
| 191 |
proxystreamtls_shutdownread_setup(void **state) { |
| 192 |
stream_PROXY_over_TLS = true; |
| 193 |
return proxystream_shutdownread_setup(state); |
| 194 |
} |
| 195 |
|
| 196 |
int |
| 197 |
proxystreamtls_shutdownread_teardown(void **state) { |
| 198 |
int r = proxystream_shutdownread_teardown(state); |
| 199 |
stream_PROXY_over_TLS = false; |
| 200 |
return r; |
| 201 |
} |
| 202 |
|
| 203 |
void |
| 204 |
stream_shutdownread(void **state ISC_ATTR_UNUSED) { |
| 205 |
isc_result_t result = stream_listen(stream_accept_cb, NULL, 128, NULL, |
| 206 |
&listen_sock); |
| 207 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 208 |
isc_loop_teardown(mainloop, stop_listening, listen_sock); |
| 209 |
|
| 210 |
stream_connect(shutdownread_connectcb, NULL, T_CONNECT); |
| 211 |
} |
| 212 |
|
| 213 |
int |
| 214 |
stream_shutdownread_teardown(void **state ISC_ATTR_UNUSED) { |
| 215 |
X(cconnects); |
| 216 |
X(csends); |
| 217 |
X(creads); |
| 218 |
|
| 219 |
atomic_assert_int_eq(cconnects, 1); |
| 220 |
atomic_assert_int_eq(csends, 1); |
| 221 |
atomic_assert_int_eq(creads, 1); |
| 222 |
|
| 223 |
return teardown_netmgr_test(state); |
| 224 |
} |