Skip to content

Commit

Permalink
tests: move server specific code out of common
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cpu committed Dec 12, 2024
1 parent a05115b commit b94cb0c
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 102 deletions.
80 changes: 0 additions & 80 deletions tests/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,21 +108,6 @@ write_cb(void *userdata, const unsigned char *buf, const size_t len,
return 0;
}

rustls_io_result
write_tls(rustls_connection *rconn, conndata *conn, size_t *n)
{
#ifdef _WIN32
return rustls_connection_write_tls(rconn, write_cb, conn, n);
#else
if(getenv("VECTORED_IO")) {
return rustls_connection_write_tls_vectored(
rconn, write_vectored_cb, conn, n);
}

return rustls_connection_write_tls(rconn, write_cb, conn, n);
#endif /* _WIN32 */
}

#ifndef _WIN32
rustls_io_result
write_vectored_cb(void *userdata, const rustls_iovec *iov, size_t count,
Expand Down Expand Up @@ -183,60 +168,6 @@ bytevec_ensure_available(bytevec *vec, const size_t n)
return DEMO_OK;
}

/*
* Do one read from the socket, and process all resulting bytes into the
* rustls_connection.
* Returns:
* - DEMO_OK for success
* - DEMO_AGAIN if we got an EAGAIN or EWOULDBLOCK reading from the
* socket
* - DEMO_EOF if we got EOF
* - DEMO_ERROR for other errors.
*/
demo_result
do_read(conndata *conn, rustls_connection *rconn)
{
size_t n = 0;
char buf[1];
const int err = rustls_connection_read_tls(rconn, read_cb, conn, &n);
if(err == EWOULDBLOCK) {
LOG("reading from socket: EAGAIN or EWOULDBLOCK: %s", strerror(errno));
return DEMO_AGAIN;
}
else if(err != 0) {
LOG("reading from socket: errno %d", err);
return DEMO_ERROR;
}
else if(n == 0) {
return DEMO_EOF;
}
LOG("read %zu bytes from socket", n);

const rustls_result rr = rustls_connection_process_new_packets(rconn);
if(rr != RUSTLS_RESULT_OK) {
print_error("in process_new_packets", rr);
return DEMO_ERROR;
}

const demo_result dr = copy_plaintext_to_buffer(conn);
if(dr != DEMO_EOF) {
return dr;
}

/* If we got an EOF on the plaintext stream (peer closed connection cleanly),
* verify that the sender then closed the TCP connection. */
const ssize_t signed_n = read(conn->fd, buf, sizeof(buf));
if(signed_n > 0) {
LOG("error: read returned %zu bytes after receiving close_notify", n);
return DEMO_ERROR;
}
else if(signed_n < 0 && errno != EWOULDBLOCK) {
LOG("wrong error after receiving close_notify: %s", strerror(errno));
return DEMO_ERROR;
}
return DEMO_EOF;
}

/**
* Copy all available plaintext from rustls into our own buffer, growing
* our buffer as much as needed.
Expand Down Expand Up @@ -320,17 +251,6 @@ memmem(const void *haystack, size_t haystacklen, const void *needle,
return NULL;
}

char *
body_beginning(const bytevec *vec)
{
const void *result = memmem(vec->data, vec->len, "\r\n\r\n", 4);
if(result == NULL) {
return NULL;
}

return (char *)result + 4;
}

void
log_cb(void *userdata, const rustls_log_params *params)
{
Expand Down
22 changes: 0 additions & 22 deletions tests/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,6 @@ demo_result nonblock(int sockfd);
/* A callback that reads bytes from the network. */
int read_cb(void *userdata, uint8_t *buf, uintptr_t len, uintptr_t *out_n);

/* Invoke rustls_connection_write_tls with either a vectored or unvectored
callback, depending on environment variable. */
rustls_io_result write_tls(rustls_connection *rconn, conndata *conn,
size_t *n);

/* A callback that writes bytes to the network. */
int write_cb(void *userdata, const uint8_t *buf, uintptr_t len,
uintptr_t *out_n);
Expand All @@ -92,18 +87,6 @@ void bytevec_consume(bytevec *vec, size_t n);
* DEMO_ERROR. */
demo_result bytevec_ensure_available(bytevec *vec, size_t n);

/*
* Do one read from the socket, and process all resulting bytes into the
* rustls_connection.
* Returns:
* - DEMO_OK for success
* - DEMO_AGAIN if we got an EAGAIN or EWOULDBLOCK reading from the
* socket
* - DEMO_EOF if we got EOF
* - DEMO_ERROR for other errors.
*/
demo_result do_read(conndata *conn, rustls_connection *rconn);

/* Read all available bytes from the rustls_connection until EOF.
* Note that EOF here indicates "no more bytes until
* process_new_packets", not "stream is closed".
Expand All @@ -118,11 +101,6 @@ demo_result copy_plaintext_to_buffer(conndata *conn);
void *memmem(const void *haystack, size_t haystacklen, const void *needle,
size_t needlelen);

/* If headers are done (received \r\n\r\n), return a pointer to the beginning
* of the body. Otherwise return NULL.
*/
char *body_beginning(const bytevec *vec);

void log_cb(void *userdata, const rustls_log_params *params);

demo_result read_file(const char *filename, char *buf, size_t buflen,
Expand Down
85 changes: 85 additions & 0 deletions tests/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,91 @@ send_response(const conndata *conn)
return DEMO_OK;
}

/* Invoke rustls_connection_write_tls with either a vectored or unvectored
callback, depending on environment variable. */
rustls_io_result
write_tls(rustls_connection *rconn, conndata *conn, size_t *n)
{
#ifdef _WIN32
return rustls_connection_write_tls(rconn, write_cb, conn, n);
#else
if(getenv("VECTORED_IO")) {
return rustls_connection_write_tls_vectored(
rconn, write_vectored_cb, conn, n);
}

return rustls_connection_write_tls(rconn, write_cb, conn, n);
#endif /* _WIN32 */
}

/*
* Do one read from the socket, and process all resulting bytes into the
* rustls_connection.
* Returns:
* - DEMO_OK for success
* - DEMO_AGAIN if we got an EAGAIN or EWOULDBLOCK reading from the
* socket
* - DEMO_EOF if we got EOF
* - DEMO_ERROR for other errors.
*/
demo_result
do_read(conndata *conn, rustls_connection *rconn)
{
size_t n = 0;
char buf[1];
const int err = rustls_connection_read_tls(rconn, read_cb, conn, &n);
if(err == EWOULDBLOCK) {
LOG("reading from socket: EAGAIN or EWOULDBLOCK: %s", strerror(errno));
return DEMO_AGAIN;
}
else if(err != 0) {
LOG("reading from socket: errno %d", err);
return DEMO_ERROR;
}
else if(n == 0) {
return DEMO_EOF;
}
LOG("read %zu bytes from socket", n);

const rustls_result rr = rustls_connection_process_new_packets(rconn);
if(rr != RUSTLS_RESULT_OK) {
print_error("in process_new_packets", rr);
return DEMO_ERROR;
}

const demo_result dr = copy_plaintext_to_buffer(conn);
if(dr != DEMO_EOF) {
return dr;
}

/* If we got an EOF on the plaintext stream (peer closed connection cleanly),
* verify that the sender then closed the TCP connection. */
const ssize_t signed_n = read(conn->fd, buf, sizeof(buf));
if(signed_n > 0) {
LOG("error: read returned %zu bytes after receiving close_notify", n);
return DEMO_ERROR;
}
else if(signed_n < 0 && errno != EWOULDBLOCK) {
LOG("wrong error after receiving close_notify: %s", strerror(errno));
return DEMO_ERROR;
}
return DEMO_EOF;
}

/* If headers are done (received \r\n\r\n), return a pointer to the beginning
* of the body. Otherwise return NULL.
*/
char *
body_beginning(const bytevec *vec)
{
const void *result = memmem(vec->data, vec->len, "\r\n\r\n", 4);
if(result == NULL) {
return NULL;
}

return (char *)result + 4;
}

void
handle_conn(conndata *conn)
{
Expand Down

0 comments on commit b94cb0c

Please sign in to comment.