| 1 |
/* Helpers to create and connect Unix or TCP sockets. */ |
| 2 |
|
| 3 |
#ifndef TEST_ENDPOINT_H |
| 4 |
#define TEST_ENDPOINT_H |
| 5 |
|
| 6 |
#include <arpa/inet.h> |
| 7 |
#include <sys/un.h> |
| 8 |
|
| 9 |
#include "munit.h" |
| 10 |
|
| 11 |
/* A few tests depend on knowing that certain reads and writes will not be short |
| 12 |
* and will happen immediately. */ |
| 13 |
#define TEST_SOCKET_MIN_BUF_SIZE 4096 |
| 14 |
|
| 15 |
/* Munit parameter defining the socket type to use in test_endpoint_setup. |
| 16 |
* |
| 17 |
* If set to "unix" a pair of unix abstract sockets will be created. If set to |
| 18 |
* "tcp" a pair of TCP sockets using the loopback interface will be created. */ |
| 19 |
#define TEST_ENDPOINT_FAMILY "endpoint-family" |
| 20 |
|
| 21 |
/* Null-terminated list of legal values for TEST_ENDPOINT_FAMILY. Currently |
| 22 |
* "unix" and "tcp". */ |
| 23 |
extern char *test_endpoint_family_values[]; |
| 24 |
|
| 25 |
/* Listening socket endpoint. */ |
| 26 |
struct test_endpoint |
| 27 |
{ |
| 28 |
char address[256]; /* Rendered address string. */ |
| 29 |
sa_family_t family; /* Address family (either AF_INET or AF_UNIX) */ |
| 30 |
int fd; /* Listening socket. */ |
| 31 |
union { /* Server address (either a TCP or Unix) */ |
| 32 |
struct sockaddr_in in_address; |
| 33 |
struct sockaddr_un un_address; |
| 34 |
}; |
| 35 |
}; |
| 36 |
|
| 37 |
/* Create a listening endpoint. |
| 38 |
* |
| 39 |
* This will bind a random address and start listening to it. */ |
| 40 |
void test_endpoint_setup(struct test_endpoint *e, |
| 41 |
const MunitParameter params[]); |
| 42 |
|
| 43 |
/* Tear down a listening endpoint. */ |
| 44 |
void test_endpoint_tear_down(struct test_endpoint *e); |
| 45 |
|
| 46 |
/* Establish a new client connection. */ |
| 47 |
int test_endpoint_connect(struct test_endpoint *e); |
| 48 |
|
| 49 |
/* Accept a new client connection. */ |
| 50 |
int test_endpoint_accept(struct test_endpoint *e); |
| 51 |
|
| 52 |
/* Connect and accept a connection, returning the pair of connected sockets. */ |
| 53 |
void test_endpoint_pair(struct test_endpoint *e, int *server, int *client); |
| 54 |
|
| 55 |
/* Return the endpoint address. */ |
| 56 |
const char *test_endpoint_address(struct test_endpoint *e); |
| 57 |
|
| 58 |
#endif /* TEST_ENDPOINT_H */ |