The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
Data Structures | Functions | Variables
bio_fd_tests.c File Reference

Tests for the FD bio. More...

#include <freeradius-devel/util/test/acutest_common_init.h>
#include <freeradius-devel/util/test/acutest_helpers.h>
#include <freeradius-devel/bio/bio_priv.h>
#include <freeradius-devel/bio/fd.h>
#include <freeradius-devel/bio/mem.h>
#include <freeradius-devel/server/cf_parse.h>
#include <freeradius-devel/server/cf_util.h>
#include <freeradius-devel/util/syserror.h>
#include <poll.h>
+ Include dependency graph for bio_fd_tests.c:

Go to the source code of this file.

Data Structures

struct  cfg_pair_t
 One key / value pair in the transport subsection of a test configuration. More...
 

Functions

static int blocked_connect (TALLOC_CTX *ctx, int *listen_out, fr_bio_t **bio_out)
 Open a non-blocking TCP bio to a listening socket which never calls accept()
 
static void cb_connected (fr_bio_t *bio)
 
static void cb_error (fr_bio_t *bio)
 
static void cb_timeout (fr_bio_t *bio)
 
static int cb_write_blocked (fr_bio_t *bio)
 
static int ipv6_loopback_usable (void)
 See if the IPv6 loopback address can be used.
 
static int loopback_listen (uint16_t *port)
 Open a TCP socket listening on loopback.
 
static int parse_client_config (TALLOC_CTX *ctx, fr_bio_fd_config_t *cfg, fr_bio_fd_type_t type, char const *transport, cfg_pair_t const *pairs)
 Parse "transport" and the transport subsection with fr_bio_fd_client_config.
 
static void recvfromto_test (int af)
 Read one packet through an unconnected UDP bio bound to the wildcard address.
 
static void test_client_config_connected_tcp (void)
 A client configuration for a connected socket still takes the destination of the "tcp" subsection.
 
static void test_client_config_unconnected_rejects_other_transports (void)
 A client configuration for an unconnected socket rejects every transport except "udp" and "tcp".
 
static void test_client_config_unconnected_tcp (void)
 A client configuration for an unconnected socket accepts "tcp".
 
static void test_client_config_unconnected_tcp_options (void)
 The "tcp" subsection of an unconnected socket takes the stream socket options.
 
static void test_client_config_unconnected_udp (void)
 A client configuration for an unconnected socket accepts "udp".
 
static void test_deferred_connect_success_calls_connected_cb (void)
 A deferred connect which succeeds has to call the connected callback.
 
static void test_eagain_calls_fd_write_blocked_cb (void)
 The write_blocked callback of an FD bio with no other bios still runs on EAGAIN.
 
static void test_eagain_calls_head_write_blocked_cb (void)
 A write which blocks with no bytes accepted has to run the write_blocked callback of the head bio.
 
static void test_listen_accept (void)
 Accept a TCP connection through a listening bio.
 
static void test_recvfromto4 (void)
 
static void test_recvfromto6 (void)
 
static ssize_t write_until_blocked (fr_bio_t *bio)
 Write one byte at a time until the socket has no room.
 

Variables

static int blocked_count
 
static int connected_count
 
static int error_count
 
 TEST_LIST
 
static int timeout_count
 

Detailed Description

Tests for the FD bio.

Definition in file bio_fd_tests.c.


Data Structure Documentation

◆ cfg_pair_t

struct cfg_pair_t

One key / value pair in the transport subsection of a test configuration.

Definition at line 615 of file bio_fd_tests.c.

Data Fields
char const * name
char const * value

Function Documentation

◆ blocked_connect()

static int blocked_connect ( TALLOC_CTX *  ctx,
int *  listen_out,
fr_bio_t **  bio_out 
)
static

Open a non-blocking TCP bio to a listening socket which never calls accept()

The listening socket has a small receive buffer, and the bio has a small send buffer, so a few kilobytes of writes fill both buffers. The kernel then has no room for another byte.

Parameters
[in]ctxto allocate the bio in.
[out]listen_outthe listening socket. The caller closes the socket.
[out]bio_outthe connected FD bio.
Returns
  • 0 on success.
  • <0 on error.

< the FD bio keeps a pointer to this, and outlives this function

Definition at line 432 of file bio_fd_tests.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ cb_connected()

static void cb_connected ( fr_bio_t *  bio)
static

Definition at line 39 of file bio_fd_tests.c.

+ Here is the caller graph for this function:

◆ cb_error()

static void cb_error ( fr_bio_t *  bio)
static

Definition at line 40 of file bio_fd_tests.c.

+ Here is the caller graph for this function:

◆ cb_timeout()

static void cb_timeout ( fr_bio_t *  bio)
static

Definition at line 41 of file bio_fd_tests.c.

+ Here is the caller graph for this function:

◆ cb_write_blocked()

static int cb_write_blocked ( fr_bio_t *  bio)
static

Definition at line 418 of file bio_fd_tests.c.

+ Here is the caller graph for this function:

◆ ipv6_loopback_usable()

static int ipv6_loopback_usable ( void  )
static

See if the IPv6 loopback address can be used.

Some hosts and containers turn IPv6 off. The socket() or bind() fails there.

Returns
  • 0 the IPv6 loopback address can be used.
  • the errno from the socket() or bind() which failed. The errno is saved before close() can change it.

Definition at line 176 of file bio_fd_tests.c.

+ Here is the caller graph for this function:

◆ loopback_listen()

static int loopback_listen ( uint16_t *  port)
static

Open a TCP socket listening on loopback.

The kernel completes the TCP handshake for any listening socket, and the test needs only the handshake. The test never calls accept(), so the kernel holds the new connection in the backlog of the listening socket.

Parameters
[out]portthe port which the kernel picked.
Returns
  • >=0 the listening socket.
  • <0 on error.

Definition at line 54 of file bio_fd_tests.c.

+ Here is the caller graph for this function:

◆ parse_client_config()

static int parse_client_config ( TALLOC_CTX *  ctx,
fr_bio_fd_config_t *  cfg,
fr_bio_fd_type_t  type,
char const *  transport,
cfg_pair_t const *  pairs 
)
static

Parse "transport" and the transport subsection with fr_bio_fd_client_config.

The caller sets the socket type before the parse. rlm_radius sets the type of the socket in the same way, as the "mode" decides if the sockets are connected.

Parameters
[in]ctxto allocate the configuration in.
[out]cfgwhere to write the parsed configuration.
[in]typeof the socket, set before the parse.
[in]transportname of the transport, which is also the name of the subsection.
[in]pairsfor the subsection, terminated by an entry with a NULL name. May be NULL.
Returns
  • 0 on success.
  • <0 on error.

Definition at line 634 of file bio_fd_tests.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ recvfromto_test()

static void recvfromto_test ( int  af)
static

Read one packet through an unconnected UDP bio bound to the wildcard address.

The local address of a bio bound to the wildcard address does not identify the address that a packet was sent to. fr_bio_fd_init_common() therefore installs fr_bio_fd_recvfromto4() or fr_bio_fd_recvfromto6(). Those two functions read the destination address from a control message (data which recvmsg() returns beside the packet), and copy the destination port from the port of the bio.

fr_bio_fd_common_datagram() also enables receive timestamps, so each packet carries two control messages. If cbuf in fr_bio_fd_t is too small for both control messages, then the bio drops every packet.

Definition at line 206 of file bio_fd_tests.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ test_client_config_connected_tcp()

static void test_client_config_connected_tcp ( void  )
static

A client configuration for a connected socket still takes the destination of the "tcp" subsection.

The unconnected sockets have their own "tcp" subsection with no destination, and the change must leave the connected sockets alone.

Definition at line 759 of file bio_fd_tests.c.

+ Here is the call graph for this function:

◆ test_client_config_unconnected_rejects_other_transports()

static void test_client_config_unconnected_rejects_other_transports ( void  )
static

A client configuration for an unconnected socket rejects every transport except "udp" and "tcp".

Definition at line 737 of file bio_fd_tests.c.

+ Here is the call graph for this function:

◆ test_client_config_unconnected_tcp()

static void test_client_config_unconnected_tcp ( void  )
static

A client configuration for an unconnected socket accepts "tcp".

The destination of the socket is not in the configuration, as the caller sets the destination later.

Definition at line 686 of file bio_fd_tests.c.

+ Here is the call graph for this function:

◆ test_client_config_unconnected_tcp_options()

static void test_client_config_unconnected_tcp_options ( void  )
static

The "tcp" subsection of an unconnected socket takes the stream socket options.

Definition at line 714 of file bio_fd_tests.c.

+ Here is the call graph for this function:

◆ test_client_config_unconnected_udp()

static void test_client_config_unconnected_udp ( void  )
static

A client configuration for an unconnected socket accepts "udp".

Definition at line 663 of file bio_fd_tests.c.

+ Here is the call graph for this function:

◆ test_deferred_connect_success_calls_connected_cb()

static void test_deferred_connect_success_calls_connected_cb ( void  )
static

A deferred connect which succeeds has to call the connected callback.

A non-blocking connect() to a listening loopback port can return EINPROGRESS. On EINPROGRESS, fr_bio_fd_connect_full() defers the connect, and the event loop calls fr_bio_fd_el_connect() once the socket is writable. fr_bio_fd_el_connect() then calls connect() a second time. If the handshake has finished, the second connect() can fail with EISCONN, so fr_bio_fd_try_connect() has to treat EISCONN as success.

The test needs a connect which succeeds, because a connect which fails may not reach fr_bio_fd_el_connect(). kqueue can report a refused connect with EV_EOF, and event.c passes EV_EOF to the error callback, fr_bio_fd_el_error().

Definition at line 89 of file bio_fd_tests.c.

+ Here is the call graph for this function:

◆ test_eagain_calls_fd_write_blocked_cb()

static void test_eagain_calls_fd_write_blocked_cb ( void  )
static

The write_blocked callback of an FD bio with no other bios still runs on EAGAIN.

The FD bio is the head of the chain, so the head callback is the callback of the FD bio.

Definition at line 584 of file bio_fd_tests.c.

+ Here is the call graph for this function:

◆ test_eagain_calls_head_write_blocked_cb()

static void test_eagain_calls_head_write_blocked_cb ( void  )
static

A write which blocks with no bytes accepted has to run the write_blocked callback of the head bio.

The application sets write_blocked on the head of the chain, not on the FD bio. The FD bio runs fr_bio_write_blocked() when a write is partial. When the socket has no room at all, the write fails with EAGAIN, and the FD bio has to run fr_bio_write_blocked() for that case, too.

Definition at line 544 of file bio_fd_tests.c.

+ Here is the call graph for this function:

◆ test_listen_accept()

static void test_listen_accept ( void  )
static

Accept a TCP connection through a listening bio.

fr_bio_fd_accept() returns 0 when no connection is waiting, and 1 with a new connected bio when one is. The listening bio must not change: the new socket, its name, and its read and write routines all belong to the new bio.

Definition at line 310 of file bio_fd_tests.c.

+ Here is the call graph for this function:

◆ test_recvfromto4()

static void test_recvfromto4 ( void  )
static

Definition at line 294 of file bio_fd_tests.c.

+ Here is the call graph for this function:

◆ test_recvfromto6()

static void test_recvfromto6 ( void  )
static

Definition at line 299 of file bio_fd_tests.c.

+ Here is the call graph for this function:

◆ write_until_blocked()

static ssize_t write_until_blocked ( fr_bio_t *  bio)
static

Write one byte at a time until the socket has no room.

A one byte write cannot be partial. The kernel either takes the byte, or fails the write with EAGAIN. The test therefore exercises the "no bytes accepted" path of the FD bio, which is different from the "some bytes accepted" path.

Returns
  • the number of bytes written before the bio blocked.
  • <0 if the bio never blocked, or returned an error other than "would block".

Definition at line 520 of file bio_fd_tests.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ blocked_count

int blocked_count
static

Definition at line 416 of file bio_fd_tests.c.

◆ connected_count

int connected_count
static

Definition at line 35 of file bio_fd_tests.c.

◆ error_count

int error_count
static

Definition at line 36 of file bio_fd_tests.c.

◆ TEST_LIST

TEST_LIST
Initial value:
= {
{ "deferred_connect_success_calls_connected_cb", test_deferred_connect_success_calls_connected_cb },
{ "recvfromto4", test_recvfromto4 },
{ "recvfromto6", test_recvfromto6 },
{ "listen_accept", test_listen_accept },
{ "eagain_calls_head_write_blocked_cb", test_eagain_calls_head_write_blocked_cb },
{ "eagain_calls_fd_write_blocked_cb", test_eagain_calls_fd_write_blocked_cb },
{ "client_config_unconnected_udp", test_client_config_unconnected_udp },
{ "client_config_unconnected_tcp", test_client_config_unconnected_tcp },
{ "client_config_unconnected_tcp_options", test_client_config_unconnected_tcp_options },
{ "client_config_unconnected_rejects_other_transports", test_client_config_unconnected_rejects_other_transports },
{ "client_config_connected_tcp", test_client_config_connected_tcp },
}
#define TEST_TERMINATOR
Definition acutest.h:77
static void test_deferred_connect_success_calls_connected_cb(void)
A deferred connect which succeeds has to call the connected callback.
static void test_recvfromto6(void)
static void test_eagain_calls_fd_write_blocked_cb(void)
The write_blocked callback of an FD bio with no other bios still runs on EAGAIN.
static void test_recvfromto4(void)
static void test_eagain_calls_head_write_blocked_cb(void)
A write which blocks with no bytes accepted has to run the write_blocked callback of the head bio.
static void test_listen_accept(void)
Accept a TCP connection through a listening bio.
static void test_client_config_unconnected_tcp_options(void)
The "tcp" subsection of an unconnected socket takes the stream socket options.
static void test_client_config_unconnected_tcp(void)
A client configuration for an unconnected socket accepts "tcp".
static void test_client_config_connected_tcp(void)
A client configuration for a connected socket still takes the destination of the "tcp" subsection.
static void test_client_config_unconnected_udp(void)
A client configuration for an unconnected socket accepts "udp".
static void test_client_config_unconnected_rejects_other_transports(void)
A client configuration for an unconnected socket rejects every transport except "udp" and "tcp".

Definition at line 780 of file bio_fd_tests.c.

◆ timeout_count

int timeout_count
static

Definition at line 37 of file bio_fd_tests.c.