Debian Codegraph @

test/lib/endpoint.c

raw
1 #include <errno.h>
2 #include <fcntl.h>
3 #include <stdio.h>
4 #include <unistd.h>
5
6 #include "endpoint.h"
7
8 static int getFamily(const MunitParameter params[])
9 {
10 const char *family = NULL;
11 if (params != NULL) {
12 family = munit_parameters_get(params, TEST_ENDPOINT_FAMILY);
13 }
14 if (family == NULL) {
15 family = "unix";
16 }
17 if (strcmp(family, "tcp") == 0) {
18 return AF_INET;
19 } else if (strcmp(family, "unix") == 0) {
20 return AF_UNIX;
21 }
22 munit_errorf("unexpected socket family: %s", family);
23 return -1;
24 }
25
26 void test_endpoint_setup(struct test_endpoint *e, const MunitParameter params[])
27 {
28 struct sockaddr *address;
29 socklen_t size;
30 int rv;
31 e->family = getFamily(params);
32
33 /* Initialize the appropriate socket address structure, depending on the
34 * selected socket family. */
35 switch (e->family) {
36 case AF_INET:
37 /* TCP socket on loopback device */
38 memset(&e->in_address, 0, sizeof e->in_address);
39 e->in_address.sin_family = AF_INET;
40 e->in_address.sin_addr.s_addr = inet_addr("127.0.0.1");
41 e->in_address.sin_port = 0; /* Get a random free port */
42 address = (struct sockaddr *)(&e->in_address);
43 size = sizeof e->in_address;
44 break;
45 case AF_UNIX:
46 /* Abstract Unix socket */
47 memset(&e->un_address, 0, sizeof e->un_address);
48 e->un_address.sun_family = AF_UNIX;
49 strcpy(e->un_address.sun_path, ""); /* Random address */
50 address = (struct sockaddr *)(&e->un_address);
51 size = sizeof e->un_address;
52 break;
53 default:
54 munit_errorf("unexpected socket family: %d", e->family);
55 }
56
57 /* Create the listener fd. */
58 e->fd = socket(e->family, SOCK_STREAM, 0);
59 if (e->fd < 0) {
60 munit_errorf("socket(): %s", strerror(errno));
61 }
62
63 /* Bind the listener fd. */
64 rv = bind(e->fd, address, size);
65 if (rv != 0) {
66 munit_errorf("bind(): %s", strerror(errno));
67 }
68
69 /* Get the actual addressed assigned by the kernel and save it back in
70 * the relevant struct server field (pointed to by address). */
71 rv = getsockname(e->fd, address, &size);
72 if (rv != 0) {
73 munit_errorf("getsockname(): %s", strerror(errno));
74 }
75
76 /* Render the endpoint address. */
77 switch (e->family) {
78 case AF_INET:
79 sprintf(e->address, "127.0.0.1:%d",
80 htons(e->in_address.sin_port));
81 break;
82 case AF_UNIX:
83 /* TODO */
84 break;
85 }
86 }
87
88 void test_endpoint_tear_down(struct test_endpoint *e)
89 {
90 close(e->fd);
91 }
92
93 int test_endpoint_connect(struct test_endpoint *e)
94 {
95 struct sockaddr *address;
96 socklen_t size;
97 int fd;
98 int rv;
99
100 switch (e->family) {
101 case AF_INET:
102 address = (struct sockaddr *)&e->in_address;
103 size = sizeof e->in_address;
104 break;
105 case AF_UNIX:
106 address = (struct sockaddr *)&e->un_address;
107 size = sizeof e->un_address;
108 break;
109 }
110
111 /* Create the socket. */
112 fd = socket(e->family, SOCK_STREAM, 0);
113 if (fd < 0) {
114 munit_errorf("socket(): %s", strerror(errno));
115 }
116
117 /* Connect to the server */
118 rv = connect(fd, address, size);
119 if (rv != 0 && errno != ECONNREFUSED) {
120 munit_errorf("connect(): %s", strerror(errno));
121 }
122
123 return fd;
124 }
125
126 int test_endpoint_accept(struct test_endpoint *e)
127 {
128 struct sockaddr_in in_address;
129 struct sockaddr_un un_address;
130 struct sockaddr *address;
131 socklen_t size;
132 int fd;
133 int rv;
134
135 switch (e->family) {
136 case AF_INET:
137 address = (struct sockaddr *)&in_address;
138 size = sizeof in_address;
139 break;
140 case AF_UNIX:
141 address = (struct sockaddr *)&un_address;
142 size = sizeof un_address;
143 break;
144 }
145
146 /* Accept the client connection. */
147 fd = accept(e->fd, address, &size);
148 if (fd < 0) {
149 /* Check if the endpoint has been closed, so this is benign. */
150 if (errno == EBADF || errno == EINVAL || errno == ENOTSOCK) {
151 return -1;
152 }
153 munit_errorf("accept(): %s", strerror(errno));
154 }
155
156 /* Set non-blocking mode */
157 rv = fcntl(fd, F_SETFL, O_NONBLOCK);
158 if (rv != 0) {
159 munit_errorf("set non-blocking mode: %s", strerror(errno));
160 }
161
162 return fd;
163 }
164
165 void test_endpoint_pair(struct test_endpoint *e, int *server, int *client)
166 {
167 *client = test_endpoint_connect(e);
168 *server = test_endpoint_accept(e);
169 }
170
171 const char *test_endpoint_address(struct test_endpoint *e)
172 {
173 return e->address;
174 }
175
176 char *test_endpoint_family_values[] = {"tcp", "unix", NULL};