Now working for simple, non-encrypted requests

making requests to something like example.com over a non-encrypted
connection now works. Binary files are unlikely to work at the moment
although I haven't tried. Also, non-encrypted doesn't work.

I have also changed a little about how tests work. Requests tests now
display much better.
This commit is contained in:
Jonathan Hodgson 2022-01-18 21:45:58 +00:00
parent a91a264a7a
commit 8a5bfe9b36
18 changed files with 725 additions and 148 deletions

View file

@ -4,12 +4,12 @@
#include "munit/munit.h"
#include "config.test.c"
#include "request.test.c"
#include "response.test.c"
#ifndef REQUESTRESPONSE_C
#define REQUESTRESPONSE_C value
#include "requestresponse.test.c"
#endif /* ifndef REQUESTRESPONSE_C */
#include "request.test.c"
#include "response.test.c"
#include <string.h>
#include <stdio.h>

View file

@ -1,31 +1,41 @@
#ifndef REQUEST_TEST
#define REQUEST_TEST
#include "munit/munit.h"
#include "../src/readline.c"
#include "../src/request.c"
#ifndef READLINE_C
#define READLINE_C
#include "../src/readline.h"
#endif
#include "../src/request.h"
#ifndef REQUESTRESPONSE_C
#define REQUESTRESPONSE_C
#include "../src/requestresponse.c"
#include "../src/requestresponse.h"
#endif /* ifndef REQUESTRESPONSE_C */
typedef struct {
char *fullLine;
char *method;
char *host;
int port;
char *protocol;
double version;
char *path;
char *queryString;
} firstLine;
} requestTestFirstLine;
static firstLine line1Examples[] = {
//Full Line, Method Host Protocol Version Path
{ "GET /search?q=test HTTP/2", "GET", "", "", 2, "/search", "?q=test"},
{ "POST http://example.com/test HTTP/1.1", "POST", "example.com", "http", 1.1, "/test", "" },
{ "POST https://example.com/test/test2/test3 HTTP/1.1", "POST", "example.com", "https", 1.1, "/test/test2/test3", ""},
{ NULL, NULL, NULL, NULL, 0, NULL, NULL }
static requestTestFirstLine requestLine1Examples[] = {
//Full Line, Method Host Port Protocol Version Path Query string
{ "GET /search?q=test HTTP/2", "GET", "", 80, "http", 2, "/search", "?q=test"},
{ "GET / HTTP/2", "GET", "", 80, "http", 2, "/", ""},
{ "POST http://example.com/test HTTP/1.1", "POST", "example.com", 80, "http", 1.1, "/test", "" },
{ "CONNECT example.com:443 HTTP/1.1", "CONNECT", "example.com", 443, "https", 1.1, "", "" },
{ "POST https://example.com/test/test2/test3 HTTP/1.1", "POST", "example.com", 443, "https", 1.1, "/test/test2/test3", ""},
{ "POST https://example.com:4444/test/ HTTP/1.1", "POST", "example.com", 4444, "https", 1.1, "/test/", ""},
{ NULL, NULL, NULL, 80, NULL, 0, NULL, NULL }
};
@ -34,72 +44,133 @@ static firstLine line1Examples[] = {
MunitResult testFirstLineProtocols(const MunitParameter params[],
void* user_data_or_fixture){
Request *req;
for ( firstLine *line = line1Examples; line->fullLine != NULL; line++ ){
req = newRequest();
requestFirstLine( req, line->fullLine );
munit_assert_string_equal( req->protocol, line->protocol );
free( req );
}
const char *firstLine = munit_parameters_get(params, "L1" );
requestTestFirstLine *line = requestLine1Examples;
while ( line->fullLine != NULL && strcmp( line->fullLine, firstLine ) != 0 )
line++;
if ( line->fullLine == NULL ) return MUNIT_ERROR;
req = newRequest();
requestFirstLine( req, line->fullLine );
munit_assert_not_null( req->protocol );
munit_assert_string_equal( req->protocol, line->protocol );
free( req );
return MUNIT_OK;
}
MunitResult testFirstLineMethod(const MunitParameter params[],
void* user_data_or_fixture){
Request *req;
for ( firstLine *line = line1Examples; line->fullLine != NULL; line++ ){
req = newRequest();
requestFirstLine( req, line->fullLine );
munit_assert_string_equal( req->method, line->method );
free( req );
}
const char *firstLine = munit_parameters_get(params, "L1" );
requestTestFirstLine *line = requestLine1Examples;
while ( line->fullLine != NULL && strcmp( line->fullLine, firstLine ) != 0 )
line++;
if ( line->fullLine == NULL ) return MUNIT_ERROR;
req = newRequest();
requestFirstLine( req, line->fullLine );
munit_assert_not_null( req->method );
munit_assert_string_equal( req->method, line->method );
free( req );
return MUNIT_OK;
}
MunitResult testFirstLineHosts(const MunitParameter params[],
void* user_data_or_fixture){
Request *req;
for ( firstLine *line = line1Examples; line->fullLine != NULL; line++ ){
req = newRequest();
requestFirstLine( req, line->fullLine );
munit_assert_string_equal( req->host, line->host );
free( req );
}
const char *firstLine = munit_parameters_get(params, "L1" );
requestTestFirstLine *line = requestLine1Examples;
while ( line->fullLine != NULL && strcmp( line->fullLine, firstLine ) != 0 )
line++;
if ( line->fullLine == NULL ) return MUNIT_ERROR;
req = newRequest();
requestFirstLine( req, line->fullLine );
munit_assert_not_null( req->host );
munit_assert_string_equal( req->host, line->host );
free( req );
return MUNIT_OK;
}
MunitResult testFirstLinePorts(const MunitParameter params[],
void* user_data_or_fixture){
Request *req;
const char *firstLine = munit_parameters_get(params, "L1" );
requestTestFirstLine *line = requestLine1Examples;
while ( line->fullLine != NULL && strcmp( line->fullLine, firstLine ) != 0 )
line++;
if ( line->fullLine == NULL ) return MUNIT_ERROR;
req = newRequest();
requestFirstLine( req, line->fullLine );
munit_assert_int( req->port, ==, line->port );
free( req );
return MUNIT_OK;
}
MunitResult testFirstLinePaths(const MunitParameter params[],
void* user_data_or_fixture){
Request *req;
for ( firstLine *line = line1Examples; line->fullLine != NULL; line++ ){
req = newRequest();
requestFirstLine( req, line->fullLine );
munit_assert_string_equal( req->path, line->path );
free( req );
}
const char *firstLine = munit_parameters_get(params, "L1" );
requestTestFirstLine *line = requestLine1Examples;
while ( line->fullLine != NULL && strcmp( line->fullLine, firstLine ) != 0 )
line++;
if ( line->fullLine == NULL ) return MUNIT_ERROR;
req = newRequest();
requestFirstLine( req, line->fullLine );
munit_assert_not_null( req->path );
munit_assert_string_equal( req->path, line->path );
free( req );
return MUNIT_OK;
}
MunitResult testFirstLineVersions(const MunitParameter params[],
void* user_data_or_fixture){
Request *req;
for ( firstLine *line = line1Examples; line->fullLine != NULL; line++ ){
req = newRequest();
requestFirstLine( req, line->fullLine );
munit_assert_float( req->version, ==, line->version );
free( req );
}
const char *firstLine = munit_parameters_get(params, "L1" );
requestTestFirstLine *line = requestLine1Examples;
while ( line->fullLine != NULL && strcmp( line->fullLine, firstLine ) != 0 )
line++;
if ( line->fullLine == NULL ) return MUNIT_ERROR;
req = newRequest();
requestFirstLine( req, line->fullLine );
munit_assert_float( req->version, ==, line->version );
free( req );
return MUNIT_OK;
}
MunitResult testFirstLineQueryString(const MunitParameter params[],
void* user_data_or_fixture){
Request *req;
for ( firstLine *line = line1Examples; line->fullLine != NULL; line++ ){
req = newRequest();
requestFirstLine( req, line->fullLine );
munit_assert_string_equal( req->queryString, line->queryString );
free( req );
}
const char *firstLine = munit_parameters_get(params, "L1" );
requestTestFirstLine *line = requestLine1Examples;
while ( line->fullLine != NULL && strcmp( line->fullLine, firstLine ) != 0 )
line++;
if ( line->fullLine == NULL ) return MUNIT_ERROR;
req = newRequest();
requestFirstLine( req, line->fullLine );
munit_assert_not_null( req->queryString );
munit_assert_string_equal( req->queryString, line->queryString );
free( req );
return MUNIT_OK;
}
@ -118,13 +189,15 @@ MunitResult testRequestToString(const MunitParameter params[],
void* user_data_or_fixture){
Request *req = newRequest();
requestFirstLine( req, "GET /search?q=test HTTP/1.1" );
munit_assert_string_equal( requestToString( req, 0 ), "GET /search?q=test HTTP/1.1\r\n" );
munit_assert_string_equal( requestToString( req ), "GET /search?q=test HTTP/1.1\r\n\r\n" );
requestAddHeader( req, "Host: example.com" );
munit_assert_string_equal( requestToString( req, 0 ), "GET /search?q=test HTTP/1.1\r\nHost: example.com\r\n" );
munit_assert_string_equal( requestToString( req ), "GET /search?q=test HTTP/1.1\r\nHost: example.com\r\n\r\n" );
return MUNIT_OK;
}
MunitParameterEnum test_first_line_params[2] = {NULL, NULL};
static MunitTest request_tests[] = {
{
"/line1/versions", /* name */
@ -132,42 +205,49 @@ static MunitTest request_tests[] = {
NULL, /* setup */
NULL, /* tear_down */
MUNIT_TEST_OPTION_NONE, /* options */
NULL /* parameters */
test_first_line_params /* parameters */
}, {
"/line1/methods", /* name */
testFirstLineMethod, /* test */
NULL, /* setup */
NULL, /* tear_down */
MUNIT_TEST_OPTION_NONE, /* options */
NULL /* parameters */
test_first_line_params /* parameters */
},{
"/line1/protocols", /* name */
testFirstLineProtocols, /* test */
NULL, /* setup */
NULL, /* tear_down */
MUNIT_TEST_OPTION_NONE, /* options */
NULL /* parameters */
test_first_line_params /* parameters */
},{
"/line1/hosts", /* name */
testFirstLineHosts, /* test */
NULL, /* setup */
NULL, /* tear_down */
MUNIT_TEST_OPTION_NONE, /* options */
NULL /* parameters */
test_first_line_params /* parameters */
},{
"/line1/ports", /* name */
testFirstLinePorts, /* test */
NULL, /* setup */
NULL, /* tear_down */
MUNIT_TEST_OPTION_NONE, /* options */
test_first_line_params /* parameters */
},{
"/line1/paths", /* name */
testFirstLinePaths, /* test */
NULL, /* setup */
NULL, /* tear_down */
MUNIT_TEST_OPTION_NONE, /* options */
NULL /* parameters */
test_first_line_params /* parameters */
},{
"/line1/queryStrings", /* name */
testFirstLineQueryString, /* test */
NULL, /* setup */
NULL, /* tear_down */
MUNIT_TEST_OPTION_NONE, /* options */
NULL /* parameters */
test_first_line_params /* parameters */
},{
"/headers/add", /* name */
testRequestAddHeader, /* test */
@ -189,18 +269,36 @@ static MunitTest request_tests[] = {
};
MunitSuite request_test_suite = {
"/request", /* name */
request_tests, /* tests */
NULL, /* suites */
1, /* iterations */
MUNIT_SUITE_OPTION_NONE /* options */
"/request", /* name */
request_tests, /* tests */
NULL, /* suites */
1, /* iterations */
MUNIT_SUITE_OPTION_NONE /* options */
};
#ifndef MAINTEST
#define MAINTEST
int main (int argc, char* argv[]) {
return munit_suite_main(&request_test_suite, NULL, argc, argv);
static char** firstLines;
unsigned int count = 1;
requestTestFirstLine *current = requestLine1Examples;
while ( current->fullLine != NULL ){
count++;
current++;
}
printf("There are %i request lines to test", count);
firstLines = malloc( sizeof( char * ) * ( count ) );
for( unsigned int i = 0; i < count; i++ ){
firstLines[i] = requestLine1Examples[i].fullLine;
}
test_first_line_params[0].name = "L1";
test_first_line_params[0].values = firstLines;
return munit_suite_main(&request_test_suite, NULL, argc, argv);
}
#endif /* ifndef MAINTEST */

View file

@ -10,19 +10,26 @@ typedef struct {
char *value;
} HeaderTestCase;
//Not that some of these have trailing \r and \n in the full lines
//These should be removed when turning it into a header struct
static HeaderTestCase testCases[] = {
{ "Content-Encoding: gzip", "Content-Encoding", "gzip" },
{ "Accept-Ranges: bytes", "Accept-Ranges", "bytes" },
{ "Age: 186432", "Age", "186432" },
{ "Age: 186432\n", "Age", "186432" },
{ "Cache-Control: max-age=604800", "Cache-Control", "max-age=604800" },
{ "Content-Type: text/html; charset=UTF-8", "Content-Type", "text/html; charset=UTF-8" },
{ "Date: Thu, 06 Jan 2022 18:52:13 GMT", "Date", "Thu, 06 Jan 2022 18:52:13 GMT" },
{ "Date: Thu, 06 Jan 2022 18:52:13 GMT\r", "Date", "Thu, 06 Jan 2022 18:52:13 GMT" },
{ "Etag: \"3147526947+ident\"", "Etag", "\"3147526947+ident\"" },
{ "Expires: Thu, 13 Jan 2022 18:52:13 GMT", "Expires", "Thu, 13 Jan 2022 18:52:13 GMT" },
{ "Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT", "Last-Modified", "Thu, 17 Oct 2019 07:18:26 GMT" },
{ "Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT\r\n\r\n", "Last-Modified", "Thu, 17 Oct 2019 07:18:26 GMT" },
{ "Server: ECS (nyb/1D13)", "Server", "ECS (nyb/1D13)" },
{ "X-Cache: HIT", "X-Cache", "HIT" },
{ "Content-Length: 648", "Content-Length", "648" }
{ "Content-Length: 648", "Content-Length", "648" },
//These are request heaedrs but should work the same way
{ "Host: example.com\r\n", "Host", "example.com" },
{ "User-Agent: curl/7.80.0", "User-Agent", "curl/7.80.0" },
{ "Accept: */*", "Accept", "*/*" },
{ "Accept: */*", "Accept", "*/*" },
};
MunitResult testHeadersName(const MunitParameter params[],

View file

@ -1,17 +1,46 @@
#ifndef RESPONSE_TEST
#define RESPONSE_TEST value
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>
#include <stdbool.h>
#include "munit/munit.h"
#ifndef READLINE_C
#define READLINE_C
#include "../src/readline.c"
#endif
#ifndef REQUESTRESPONSE_C
#define REQUESTRESPONSE_C value
#define REQUESTRESPONSE_C
#include "../src/requestresponse.c"
#endif /* ifndef REQUESTRESPONSE_C */
#include "../src/response.c"
typedef struct {
char *fullLine;
float version;
int statusCode;
char *statusMessage;
} firstLine;
static firstLine line1Examples[] = {
//Full Line, version status Code Status Message
{ "HTTP/1.1 200 OK", 1.1, 200, "OK" },
{ "HTTP/2.0 404 Not Found", 2.0, 404, "Not Found" },
{ NULL, 0, 0, NULL }
};
MunitResult testResponseNewStatus(const MunitParameter params[],
void* user_data_or_fixture){
Response *rsp = newResponse();
responseBarebones(rsp);
munit_assert_int( rsp->statusCode, ==, 200 );
return MUNIT_OK;
}
@ -19,6 +48,7 @@ MunitResult testResponseNewStatus(const MunitParameter params[],
MunitResult testResponseNewStatusMessage(const MunitParameter params[],
void* user_data_or_fixture){
Response *rsp = newResponse();
responseBarebones(rsp);
munit_assert_string_equal( rsp->statusMessage, "OK" );
return MUNIT_OK;
}
@ -26,6 +56,7 @@ MunitResult testResponseNewStatusMessage(const MunitParameter params[],
MunitResult testResponseNewVersion(const MunitParameter params[],
void* user_data_or_fixture){
Response *rsp = newResponse();
responseBarebones(rsp);
munit_assert_float( rsp->version, ==, 1.1 );
return MUNIT_OK;
}
@ -33,6 +64,7 @@ MunitResult testResponseNewVersion(const MunitParameter params[],
MunitResult testResponseSetBody(const MunitParameter params[],
void* user_data_or_fixture){
Response *rsp = newResponse();
responseBarebones(rsp);
responseSetBody( rsp, "Testing", 1 );
munit_assert_string_equal( rsp->body, "Testing" );
munit_assert_string_equal( getHeader( rsp->headers, "content-length" )->value, "7" );
@ -42,11 +74,122 @@ MunitResult testResponseSetBody(const MunitParameter params[],
MunitResult testResponseToString(const MunitParameter params[],
void* user_data_or_fixture){
Response *rsp = newResponse();
responseBarebones(rsp);
munit_assert_string_equal( responseToString( rsp ), "HTTP/1.1 200 OK\r\nContent-Length: 0\r\nContent-Type: text/plain\r\n\r\n" );
responseSetBody( rsp, "Testing", 1 );
munit_assert_string_equal( responseToString( rsp ), "HTTP/1.1 200 OK\r\nContent-Length: 7\r\nContent-Type: text/plain\r\n\r\nTesting" );
munit_assert_string_equal( responseToString( rsp ), "HTTP/1.1 200 OK\r\nContent-Length: 7\r\nContent-Type: text/plain\r\n\r\nTesting\r\n" );
return MUNIT_OK;
}
MunitResult testResponseFirstLineStatusCode(const MunitParameter params[],
void* user_data_or_fixture){
Response *rsp;
for ( firstLine *line = line1Examples; line->fullLine != NULL; line++ ){
rsp = newResponse();
responseFirstLine( rsp, line->fullLine );
munit_assert_int( rsp->statusCode, ==, line->statusCode );
free( rsp );
}
return MUNIT_OK;
}
MunitResult testResponseFirstLineStatusMessage(const MunitParameter params[],
void* user_data_or_fixture){
Response *rsp;
for ( firstLine *line = line1Examples; line->fullLine != NULL; line++ ){
rsp = newResponse();
responseFirstLine( rsp, line->fullLine );
munit_assert_string_equal( rsp->statusMessage, line->statusMessage );
free( rsp );
}
return MUNIT_OK;
}
MunitResult testResponseFirstLineVersion(const MunitParameter params[],
void* user_data_or_fixture){
Response *rsp;
for ( firstLine *line = line1Examples; line->fullLine != NULL; line++ ){
rsp = newResponse();
responseFirstLine( rsp, line->fullLine );
munit_assert_float( rsp->version, ==, line->version );
free( rsp );
}
return MUNIT_OK;
}
static void* setupSocketTests(const MunitParameter params[], void* user_data) {
int client_fd = 0;
struct sockaddr_in address;
memset( &address, 0, sizeof(address) );
struct hostent *host = gethostbyname("example.com");
Response *rsp = NULL;
if ((client_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0){
perror("socket failed");
return NULL;
}
address.sin_family = AF_INET;
address.sin_port = htons( 80 );
// We want the request to go out to whatever the host was resolved to
memcpy( &address.sin_addr, host->h_addr_list[0], host->h_length );
if((connect(client_fd, (struct sockaddr *)&address, sizeof(address)))<0) {
perror("connect failed");
return NULL;
}
char *toSend = "GET / HTTP/1.1\r\nHost: example.com\r\n\r\n";
if ( write( client_fd, toSend, strlen(toSend) ) != strlen(toSend) ){
perror( "Write Error" );
return NULL;
}
rsp = newResponseFromSocket( client_fd );
return rsp;
}
MunitResult testResponseFromSocketHeaderLength(const MunitParameter params[],
void* user_data_or_fixture){
Response *rsp = ( Response * )user_data_or_fixture;
if ( rsp == NULL ) return MUNIT_ERROR;
//Check there are the right nubmer of haeders
// Unfortunately it changes from request to request so this is a "loose"
// check
munit_assert_int( countHeaders( rsp->headers ), >, 7 );
return MUNIT_OK;
}
MunitResult testResponseFromSocketStatusCode(const MunitParameter params[],
void* user_data_or_fixture){
Response *rsp = ( Response * )user_data_or_fixture;
if ( rsp == NULL ) return MUNIT_ERROR;
//Check there are the right nubmer of haeders
munit_assert_int( rsp->statusCode, ==, 200 );
return MUNIT_OK;
}
MunitResult testResponseFromSocketBody(const MunitParameter params[],
void* user_data_or_fixture){
Response *rsp = ( Response * )user_data_or_fixture;
Header *contentLength = getHeader( rsp->headers, "content-length" );
unsigned int length = 0;
if ( contentLength == NULL ) return MUNIT_ERROR;
munit_assert_int( atoi( contentLength->value ), ==, strlen( (char *)rsp->body ) );
//Check there are the right nubmer of haeders
munit_assert_int( rsp->statusCode, ==, 200 );
return MUNIT_OK;
}
static MunitTest response_tests[] = {
{
@ -84,6 +227,48 @@ static MunitTest response_tests[] = {
NULL, /* tear_down */
MUNIT_TEST_OPTION_NONE, /* options */
NULL /* parameters */
}, {
"/line1/statusCode", /* name */
testResponseFirstLineStatusCode, /* test */
NULL, /* setup */
NULL, /* tear_down */
MUNIT_TEST_OPTION_NONE, /* options */
NULL /* parameters */
}, {
"/line1/statusMessage", /* name */
testResponseFirstLineStatusMessage, /* test */
NULL, /* setup */
NULL, /* tear_down */
MUNIT_TEST_OPTION_NONE, /* options */
NULL /* parameters */
}, {
"/line1/version", /* name */
testResponseFirstLineVersion, /* test */
NULL, /* setup */
NULL, /* tear_down */
MUNIT_TEST_OPTION_NONE, /* options */
NULL /* parameters */
}, {
"/fromSocket/headerLength", /* name */
testResponseFromSocketHeaderLength, /* test */
setupSocketTests, /* setup */
NULL, /* tear_down */
MUNIT_TEST_OPTION_NONE, /* options */
NULL /* parameters */
}, {
"/fromSocket/statusCode", /* name */
testResponseFromSocketStatusCode, /* test */
setupSocketTests, /* setup */
NULL, /* tear_down */
MUNIT_TEST_OPTION_NONE, /* options */
NULL /* parameters */
}, {
"/fromSocket/body", /* name */
testResponseFromSocketBody, /* test */
setupSocketTests, /* setup */
NULL, /* tear_down */
MUNIT_TEST_OPTION_NONE, /* options */
NULL /* parameters */
},
/* Mark the end of the array with an entry where the test
* function is NULL */

51
tests/util.test.c Normal file
View file

@ -0,0 +1,51 @@
#ifndef UTIL_TEST
#define UTIL_TEST
#include "munit/munit.h"
#include "../src/util.h"
MunitResult testStrPos(const MunitParameter params[],
void* user_data_or_fixture){
munit_assert_int( strpos( "thisisatest", "test" ), ==, 7 );
munit_assert_int( strpos( "testthisisatest", "test" ), ==, 0 );
munit_assert_int( strpos( "blar", "test" ), ==, -1 );
return MUNIT_OK;
}
static MunitTest util_tests[] = {
{
"/util/versions", /* name */
testStrPos, /* test */
NULL, /* setup */
NULL, /* tear_down */
MUNIT_TEST_OPTION_NONE, /* options */
NULL /* parameters */
},
/* Mark the end of the array with an entry where the test
* function is NULL */
{ NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
};
MunitSuite util_test_suite = {
"/request", /* name */
util_tests, /* tests */
NULL, /* suites */
1, /* iterations */
MUNIT_SUITE_OPTION_NONE /* options */
};
#ifndef MAINTEST
#define MAINTEST
int main (int argc, char* argv[]) {
return munit_suite_main(&util_test_suite, NULL, argc, argv);
}
#endif /* ifndef MAINTEST */
#endif /* ifndef REQUEST_TEST */