Skip to content

Commit dffcfdc

Browse files
committed
tests: move server specific code out of common
After the client rewrite some of the pieces in common are now server specific. Move these bits into `server.c`. For now I've put them into `server.c` in the order required for compilation pending a larger top-down order rewrite.
1 parent 5abd728 commit dffcfdc

File tree

3 files changed

+85
-102
lines changed

3 files changed

+85
-102
lines changed

tests/common.c

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -108,21 +108,6 @@ write_cb(void *userdata, const unsigned char *buf, const size_t len,
108108
return 0;
109109
}
110110

111-
rustls_io_result
112-
write_tls(rustls_connection *rconn, conndata *conn, size_t *n)
113-
{
114-
#ifdef _WIN32
115-
return rustls_connection_write_tls(rconn, write_cb, conn, n);
116-
#else
117-
if(getenv("VECTORED_IO")) {
118-
return rustls_connection_write_tls_vectored(
119-
rconn, write_vectored_cb, conn, n);
120-
}
121-
122-
return rustls_connection_write_tls(rconn, write_cb, conn, n);
123-
#endif /* _WIN32 */
124-
}
125-
126111
#ifndef _WIN32
127112
rustls_io_result
128113
write_vectored_cb(void *userdata, const rustls_iovec *iov, size_t count,
@@ -183,60 +168,6 @@ bytevec_ensure_available(bytevec *vec, const size_t n)
183168
return DEMO_OK;
184169
}
185170

186-
/*
187-
* Do one read from the socket, and process all resulting bytes into the
188-
* rustls_connection.
189-
* Returns:
190-
* - DEMO_OK for success
191-
* - DEMO_AGAIN if we got an EAGAIN or EWOULDBLOCK reading from the
192-
* socket
193-
* - DEMO_EOF if we got EOF
194-
* - DEMO_ERROR for other errors.
195-
*/
196-
demo_result
197-
do_read(conndata *conn, rustls_connection *rconn)
198-
{
199-
size_t n = 0;
200-
char buf[1];
201-
const int err = rustls_connection_read_tls(rconn, read_cb, conn, &n);
202-
if(err == EWOULDBLOCK) {
203-
LOG("reading from socket: EAGAIN or EWOULDBLOCK: %s", strerror(errno));
204-
return DEMO_AGAIN;
205-
}
206-
else if(err != 0) {
207-
LOG("reading from socket: errno %d", err);
208-
return DEMO_ERROR;
209-
}
210-
else if(n == 0) {
211-
return DEMO_EOF;
212-
}
213-
LOG("read %zu bytes from socket", n);
214-
215-
const rustls_result rr = rustls_connection_process_new_packets(rconn);
216-
if(rr != RUSTLS_RESULT_OK) {
217-
print_error("in process_new_packets", rr);
218-
return DEMO_ERROR;
219-
}
220-
221-
const demo_result dr = copy_plaintext_to_buffer(conn);
222-
if(dr != DEMO_EOF) {
223-
return dr;
224-
}
225-
226-
/* If we got an EOF on the plaintext stream (peer closed connection cleanly),
227-
* verify that the sender then closed the TCP connection. */
228-
const ssize_t signed_n = read(conn->fd, buf, sizeof(buf));
229-
if(signed_n > 0) {
230-
LOG("error: read returned %zu bytes after receiving close_notify", n);
231-
return DEMO_ERROR;
232-
}
233-
else if(signed_n < 0 && errno != EWOULDBLOCK) {
234-
LOG("wrong error after receiving close_notify: %s", strerror(errno));
235-
return DEMO_ERROR;
236-
}
237-
return DEMO_EOF;
238-
}
239-
240171
/**
241172
* Copy all available plaintext from rustls into our own buffer, growing
242173
* our buffer as much as needed.
@@ -320,17 +251,6 @@ memmem(const void *haystack, size_t haystacklen, const void *needle,
320251
return NULL;
321252
}
322253

323-
char *
324-
body_beginning(const bytevec *vec)
325-
{
326-
const void *result = memmem(vec->data, vec->len, "\r\n\r\n", 4);
327-
if(result == NULL) {
328-
return NULL;
329-
}
330-
331-
return (char *)result + 4;
332-
}
333-
334254
void
335255
log_cb(void *userdata, const rustls_log_params *params)
336256
{

tests/common.h

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,6 @@ demo_result nonblock(int sockfd);
6464
/* A callback that reads bytes from the network. */
6565
int read_cb(void *userdata, uint8_t *buf, uintptr_t len, uintptr_t *out_n);
6666

67-
/* Invoke rustls_connection_write_tls with either a vectored or unvectored
68-
callback, depending on environment variable. */
69-
rustls_io_result write_tls(rustls_connection *rconn, conndata *conn,
70-
size_t *n);
71-
7267
/* A callback that writes bytes to the network. */
7368
int write_cb(void *userdata, const uint8_t *buf, uintptr_t len,
7469
uintptr_t *out_n);
@@ -92,18 +87,6 @@ void bytevec_consume(bytevec *vec, size_t n);
9287
* DEMO_ERROR. */
9388
demo_result bytevec_ensure_available(bytevec *vec, size_t n);
9489

95-
/*
96-
* Do one read from the socket, and process all resulting bytes into the
97-
* rustls_connection.
98-
* Returns:
99-
* - DEMO_OK for success
100-
* - DEMO_AGAIN if we got an EAGAIN or EWOULDBLOCK reading from the
101-
* socket
102-
* - DEMO_EOF if we got EOF
103-
* - DEMO_ERROR for other errors.
104-
*/
105-
demo_result do_read(conndata *conn, rustls_connection *rconn);
106-
10790
/* Read all available bytes from the rustls_connection until EOF.
10891
* Note that EOF here indicates "no more bytes until
10992
* process_new_packets", not "stream is closed".
@@ -118,11 +101,6 @@ demo_result copy_plaintext_to_buffer(conndata *conn);
118101
void *memmem(const void *haystack, size_t haystacklen, const void *needle,
119102
size_t needlelen);
120103

121-
/* If headers are done (received \r\n\r\n), return a pointer to the beginning
122-
* of the body. Otherwise return NULL.
123-
*/
124-
char *body_beginning(const bytevec *vec);
125-
126104
void log_cb(void *userdata, const rustls_log_params *params);
127105

128106
demo_result read_file(const char *filename, char *buf, size_t buflen,

tests/server.c

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,91 @@ send_response(const conndata *conn)
6161
return DEMO_OK;
6262
}
6363

64+
/* Invoke rustls_connection_write_tls with either a vectored or unvectored
65+
callback, depending on environment variable. */
66+
rustls_io_result
67+
write_tls(rustls_connection *rconn, conndata *conn, size_t *n)
68+
{
69+
#ifdef _WIN32
70+
return rustls_connection_write_tls(rconn, write_cb, conn, n);
71+
#else
72+
if(getenv("VECTORED_IO")) {
73+
return rustls_connection_write_tls_vectored(
74+
rconn, write_vectored_cb, conn, n);
75+
}
76+
77+
return rustls_connection_write_tls(rconn, write_cb, conn, n);
78+
#endif /* _WIN32 */
79+
}
80+
81+
/*
82+
* Do one read from the socket, and process all resulting bytes into the
83+
* rustls_connection.
84+
* Returns:
85+
* - DEMO_OK for success
86+
* - DEMO_AGAIN if we got an EAGAIN or EWOULDBLOCK reading from the
87+
* socket
88+
* - DEMO_EOF if we got EOF
89+
* - DEMO_ERROR for other errors.
90+
*/
91+
demo_result
92+
do_read(conndata *conn, rustls_connection *rconn)
93+
{
94+
size_t n = 0;
95+
char buf[1];
96+
const int err = rustls_connection_read_tls(rconn, read_cb, conn, &n);
97+
if(err == EWOULDBLOCK) {
98+
LOG("reading from socket: EAGAIN or EWOULDBLOCK: %s", strerror(errno));
99+
return DEMO_AGAIN;
100+
}
101+
else if(err != 0) {
102+
LOG("reading from socket: errno %d", err);
103+
return DEMO_ERROR;
104+
}
105+
else if(n == 0) {
106+
return DEMO_EOF;
107+
}
108+
LOG("read %zu bytes from socket", n);
109+
110+
const rustls_result rr = rustls_connection_process_new_packets(rconn);
111+
if(rr != RUSTLS_RESULT_OK) {
112+
print_error("in process_new_packets", rr);
113+
return DEMO_ERROR;
114+
}
115+
116+
const demo_result dr = copy_plaintext_to_buffer(conn);
117+
if(dr != DEMO_EOF) {
118+
return dr;
119+
}
120+
121+
/* If we got an EOF on the plaintext stream (peer closed connection cleanly),
122+
* verify that the sender then closed the TCP connection. */
123+
const ssize_t signed_n = read(conn->fd, buf, sizeof(buf));
124+
if(signed_n > 0) {
125+
LOG("error: read returned %zu bytes after receiving close_notify", n);
126+
return DEMO_ERROR;
127+
}
128+
else if(signed_n < 0 && errno != EWOULDBLOCK) {
129+
LOG("wrong error after receiving close_notify: %s", strerror(errno));
130+
return DEMO_ERROR;
131+
}
132+
return DEMO_EOF;
133+
}
134+
135+
/* If headers are done (received \r\n\r\n), return a pointer to the beginning
136+
* of the body. Otherwise return NULL.
137+
*/
138+
char *
139+
body_beginning(const bytevec *vec)
140+
{
141+
const void *result = memmem(vec->data, vec->len, "\r\n\r\n", 4);
142+
if(result == NULL) {
143+
return NULL;
144+
}
145+
146+
return (char *)result + 4;
147+
}
148+
64149
void
65150
handle_conn(conndata *conn)
66151
{

0 commit comments

Comments
 (0)