pgbackrest.git / summary / log / commit / refs

commit    71ce6375571bf9253b7aebad5ee44774fb3cbb07
Author:   David Steele <david@pgmasters.net>
Date:     Sun Apr 12 13:01:41 2020 +0000

    Use standard HARNESS_FORK*() macros to fork test servers.
    
    These forks were done in a custom way (not sure why) and lack the capability of the standard macros for the parent to wait for child exit.
    
    This mean that the server would continue to run after the tests were complete and that multiple servers could run at once. This caused subtle timing and connection issues that required larger timeouts to resolve.
    
    Don't change the timeouts here since they need to be adjusted in future commits anyway.


test/src/module/common/ioHttpTest.c | 826 +++++++++++----------- test/src/module/common/ioTlsTest.c | 338 ++++----- test/src/module/storage/s3Test.c | 1322 ++++++++++++++++++----------------- 3 files changed, 1262 insertions(+), 1224 deletions(-) diff --git a/test/src/module/common/ioHttpTest.c b/test/src/module/common/ioHttpTest.c index 3d40fbf43..c154cba08 100644 --- a/test/src/module/common/ioHttpTest.c +++ b/test/src/module/common/ioHttpTest.c @@ -3,9 +3,11 @@ Test Http ***********************************************************************************************************************************/ #include <unistd.h> -#include "common/harnessTls.h" #include "common/time.h" +#include "common/harnessFork.h" +#include "common/harnessTls.h" + /*********************************************************************************************************************************** Test server ***********************************************************************************************************************************/ @@ -14,311 +16,303 @@ testHttpServer(void) { FUNCTION_HARNESS_VOID(); - if (fork() == 0) - { - // Change log process id to aid in debugging - hrnLogProcessIdSet(1); + harnessTlsServerInitDefault(); - harnessTlsServerInitDefault(); + // Test no output from server + harnessTlsServerAccept(); - // Test no output from server - harnessTlsServerAccept(); + harnessTlsServerExpect( + "GET / HTTP/1.1\r\n" + "\r\n"); - harnessTlsServerExpect( - "GET / HTTP/1.1\r\n" - "\r\n"); + sleepMSec(600); + harnessTlsServerClose(); - sleepMSec(600); - harnessTlsServerClose(); + // Test invalid http version + harnessTlsServerAccept(); - // Test invalid http version - harnessTlsServerAccept(); + harnessTlsServerExpect( + "GET / HTTP/1.1\r\n" + "\r\n"); - harnessTlsServerExpect( - "GET / HTTP/1.1\r\n" - "\r\n"); + harnessTlsServerReply( + "HTTP/1.0 200 OK\r\n"); - harnessTlsServerReply( - "HTTP/1.0 200 OK\r\n"); + harnessTlsServerClose(); - harnessTlsServerClose(); + // Test no space in status + harnessTlsServerAccept(); - // Test no space in status - harnessTlsServerAccept(); + harnessTlsServerExpect( + "GET / HTTP/1.1\r\n" + "\r\n"); - harnessTlsServerExpect( - "GET / HTTP/1.1\r\n" - "\r\n"); + harnessTlsServerReply( + "HTTP/1.1 200OK\r\n"); - harnessTlsServerReply( - "HTTP/1.1 200OK\r\n"); + harnessTlsServerClose(); - harnessTlsServerClose(); + // Test unexpected end of headers + harnessTlsServerAccept(); - // Test unexpected end of headers - harnessTlsServerAccept(); + harnessTlsServerExpect( + "GET / HTTP/1.1\r\n" + "\r\n"); - harnessTlsServerExpect( - "GET / HTTP/1.1\r\n" - "\r\n"); + harnessTlsServerReply( + "HTTP/1.1 200 OK\r\n"); - harnessTlsServerReply( - "HTTP/1.1 200 OK\r\n"); + harnessTlsServerClose(); - harnessTlsServerClose(); + // Test missing colon in header + harnessTlsServerAccept(); - // Test missing colon in header - harnessTlsServerAccept(); + harnessTlsServerExpect( + "GET / HTTP/1.1\r\n" + "\r\n"); - harnessTlsServerExpect( - "GET / HTTP/1.1\r\n" - "\r\n"); + harnessTlsServerReply( + "HTTP/1.1 200 OK\r\n" + "header-value\r\n"); - harnessTlsServerReply( - "HTTP/1.1 200 OK\r\n" - "header-value\r\n"); + harnessTlsServerClose(); - harnessTlsServerClose(); + // Test invalid transfer encoding + harnessTlsServerAccept(); - // Test invalid transfer encoding - harnessTlsServerAccept(); + harnessTlsServerExpect( + "GET / HTTP/1.1\r\n" + "\r\n"); - harnessTlsServerExpect( - "GET / HTTP/1.1\r\n" - "\r\n"); + harnessTlsServerReply( + "HTTP/1.1 200 OK\r\n" + "transfer-encoding:bogus\r\n"); - harnessTlsServerReply( - "HTTP/1.1 200 OK\r\n" - "transfer-encoding:bogus\r\n"); + harnessTlsServerClose(); - harnessTlsServerClose(); + // Test content length and transfer encoding both set + harnessTlsServerAccept(); - // Test content length and transfer encoding both set - harnessTlsServerAccept(); + harnessTlsServerExpect( + "GET / HTTP/1.1\r\n" + "\r\n"); - harnessTlsServerExpect( - "GET / HTTP/1.1\r\n" - "\r\n"); + harnessTlsServerReply( + "HTTP/1.1 200 OK\r\n" + "transfer-encoding:chunked\r\n" + "content-length:777\r\n" + "\r\n"); - harnessTlsServerReply( - "HTTP/1.1 200 OK\r\n" - "transfer-encoding:chunked\r\n" - "content-length:777\r\n" - "\r\n"); + harnessTlsServerClose(); - harnessTlsServerClose(); + // Test 5xx error with no retry + harnessTlsServerAccept(); - // Test 5xx error with no retry - harnessTlsServerAccept(); + harnessTlsServerExpect( + "GET / HTTP/1.1\r\n" + "\r\n"); - harnessTlsServerExpect( - "GET / HTTP/1.1\r\n" - "\r\n"); + harnessTlsServerReply( + "HTTP/1.1 503 Slow Down\r\n" + "\r\n"); - harnessTlsServerReply( - "HTTP/1.1 503 Slow Down\r\n" - "\r\n"); + harnessTlsServerClose(); - harnessTlsServerClose(); + // Request with no content (with an internal error) + harnessTlsServerAccept(); - // Request with no content (with an internal error) - harnessTlsServerAccept(); + harnessTlsServerExpect( + "GET /?name=%2Fpath%2FA%20Z.txt&type=test HTTP/1.1\r\n" + "host:myhost.com\r\n" + "\r\n"); - harnessTlsServerExpect( - "GET /?name=%2Fpath%2FA%20Z.txt&type=test HTTP/1.1\r\n" - "host:myhost.com\r\n" - "\r\n"); + harnessTlsServerReply( + "HTTP/1.1 500 Internal Error\r\n" + "Connection:close\r\n" + "\r\n"); - harnessTlsServerReply( - "HTTP/1.1 500 Internal Error\r\n" - "Connection:close\r\n" - "\r\n"); + harnessTlsServerClose(); - harnessTlsServerClose(); + harnessTlsServerAccept(); - harnessTlsServerAccept(); + harnessTlsServerExpect( + "GET /?name=%2Fpath%2FA%20Z.txt&type=test HTTP/1.1\r\n" + "host:myhost.com\r\n" + "\r\n"); - harnessTlsServerExpect( - "GET /?name=%2Fpath%2FA%20Z.txt&type=test HTTP/1.1\r\n" - "host:myhost.com\r\n" - "\r\n"); + harnessTlsServerReply( + "HTTP/1.1 200 OK\r\n" + "key1:0\r\n" + " key2 : value2\r\n" + "Connection:ack\r\n" + "\r\n"); - harnessTlsServerReply( - "HTTP/1.1 200 OK\r\n" - "key1:0\r\n" - " key2 : value2\r\n" - "Connection:ack\r\n" - "\r\n"); + // Head request with content-length but no content + harnessTlsServerExpect( + "HEAD / HTTP/1.1\r\n" + "\r\n"); - // Head request with content-length but no content - harnessTlsServerExpect( - "HEAD / HTTP/1.1\r\n" - "\r\n"); + harnessTlsServerReply( + "HTTP/1.1 200 OK\r\n" + "content-length:380\r\n" + "\r\n"); - harnessTlsServerReply( - "HTTP/1.1 200 OK\r\n" - "content-length:380\r\n" - "\r\n"); + // Head request with transfer encoding but no content + harnessTlsServerExpect( + "HEAD / HTTP/1.1\r\n" + "\r\n"); - // Head request with transfer encoding but no content - harnessTlsServerExpect( - "HEAD / HTTP/1.1\r\n" - "\r\n"); + harnessTlsServerReply( + "HTTP/1.1 200 OK\r\n" + "Transfer-Encoding: chunked\r\n" + "\r\n"); - harnessTlsServerReply( - "HTTP/1.1 200 OK\r\n" - "Transfer-Encoding: chunked\r\n" - "\r\n"); + // Head request with connection close but no content + harnessTlsServerExpect( + "HEAD / HTTP/1.1\r\n" + "\r\n"); - // Head request with connection close but no content - harnessTlsServerExpect( - "HEAD / HTTP/1.1\r\n" - "\r\n"); + harnessTlsServerReply( + "HTTP/1.1 200 OK\r\n" + "Connection:close\r\n" + "\r\n"); - harnessTlsServerReply( - "HTTP/1.1 200 OK\r\n" - "Connection:close\r\n" - "\r\n"); + harnessTlsServerClose(); - harnessTlsServerClose(); + harnessTlsServerAccept(); - harnessTlsServerAccept(); + // Error with content (with a few slow down errors) + harnessTlsServerExpect( + "GET / HTTP/1.1\r\n" + "\r\n"); - // Error with content (with a few slow down errors) - harnessTlsServerExpect( - "GET / HTTP/1.1\r\n" - "\r\n"); + harnessTlsServerReply( + "HTTP/1.1 503 Slow Down\r\n" + "content-length:3\r\n" + "Connection:close\r\n" + "\r\n" + "123"); - harnessTlsServerReply( - "HTTP/1.1 503 Slow Down\r\n" - "content-length:3\r\n" - "Connection:close\r\n" - "\r\n" - "123"); + harnessTlsServerClose(); - harnessTlsServerClose(); + harnessTlsServerAccept(); - harnessTlsServerAccept(); + harnessTlsServerExpect( + "GET / HTTP/1.1\r\n" + "\r\n"); - harnessTlsServerExpect( - "GET / HTTP/1.1\r\n" - "\r\n"); + harnessTlsServerReply( + "HTTP/1.1 503 Slow Down\r\n" + "Transfer-Encoding:chunked\r\n" + "Connection:close\r\n" + "\r\n" + "0\r\n" + "\r\n"); + + harnessTlsServerClose(); + + harnessTlsServerAccept(); + + harnessTlsServerExpect( + "GET / HTTP/1.1\r\n" + "\r\n"); + + harnessTlsServerReply( + "HTTP/1.1 404 Not Found\r\n" + "content-length:0\r\n" + "\r\n"); + + // Error with content + harnessTlsServerAccept(); + + harnessTlsServerExpect( + "GET / HTTP/1.1\r\n" + "\r\n"); + + harnessTlsServerReply( + "HTTP/1.1 403 Auth Error\r\n" + "content-length:7\r\n" + "\r\n" + "CONTENT"); + + // Request with content + harnessTlsServerAccept(); + + harnessTlsServerExpect( + "GET /path/file%201.txt HTTP/1.1\r\n" + "content-length:30\r\n" + "\r\n" + "012345678901234567890123456789"); + + harnessTlsServerReply( + "HTTP/1.1 200 OK\r\n" + "Connection:close\r\n" + "\r\n" + "01234567890123456789012345678901"); + + harnessTlsServerClose(); - harnessTlsServerReply( - "HTTP/1.1 503 Slow Down\r\n" - "Transfer-Encoding:chunked\r\n" - "Connection:close\r\n" - "\r\n" - "0\r\n" - "\r\n"); - - harnessTlsServerClose(); - - harnessTlsServerAccept(); - - harnessTlsServerExpect( - "GET / HTTP/1.1\r\n" - "\r\n"); - - harnessTlsServerReply( - "HTTP/1.1 404 Not Found\r\n" - "content-length:0\r\n" - "\r\n"); - - // Error with content - harnessTlsServerAccept(); - - harnessTlsServerExpect( - "GET / HTTP/1.1\r\n" - "\r\n"); - - harnessTlsServerReply( - "HTTP/1.1 403 Auth Error\r\n" - "content-length:7\r\n" - "\r\n" - "CONTENT"); - - // Request with content - harnessTlsServerAccept(); - - harnessTlsServerExpect( - "GET /path/file%201.txt HTTP/1.1\r\n" - "content-length:30\r\n" - "\r\n" - "012345678901234567890123456789"); - - harnessTlsServerReply( - "HTTP/1.1 200 OK\r\n" - "Connection:close\r\n" - "\r\n" - "01234567890123456789012345678901"); - - harnessTlsServerClose(); - - // Request with eof before content complete with retry - harnessTlsServerAccept(); - - harnessTlsServerExpect( - "GET /path/file%201.txt HTTP/1.1\r\n" - "\r\n"); - - harnessTlsServerReply( - "HTTP/1.1 200 OK\r\n" - "content-length:32\r\n" - "\r\n" - "0123456789012345678901234567890"); + // Request with eof before content complete with retry + harnessTlsServerAccept(); + + harnessTlsServerExpect( + "GET /path/file%201.txt HTTP/1.1\r\n" + "\r\n"); + + harnessTlsServerReply( + "HTTP/1.1 200 OK\r\n" + "content-length:32\r\n" + "\r\n" + "0123456789012345678901234567890"); - harnessTlsServerClose(); + harnessTlsServerClose(); - harnessTlsServerAccept(); + harnessTlsServerAccept(); - harnessTlsServerExpect( - "GET /path/file%201.txt HTTP/1.1\r\n" - "\r\n"); + harnessTlsServerExpect( + "GET /path/file%201.txt HTTP/1.1\r\n" + "\r\n"); - harnessTlsServerReply( - "HTTP/1.1 200 OK\r\n" - "content-length:32\r\n" - "\r\n" - "01234567890123456789012345678901"); + harnessTlsServerReply( + "HTTP/1.1 200 OK\r\n" + "content-length:32\r\n" + "\r\n" + "01234567890123456789012345678901"); - // Request with eof before content complete - harnessTlsServerAccept(); - - harnessTlsServerExpect( - "GET /path/file%201.txt HTTP/1.1\r\n" - "\r\n"); + // Request with eof before content complete + harnessTlsServerAccept(); + + harnessTlsServerExpect( + "GET /path/file%201.txt HTTP/1.1\r\n" + "\r\n"); - harnessTlsServerReply( - "HTTP/1.1 200 OK\r\n" - "content-length:32\r\n" - "\r\n" - "0123456789012345678901234567890"); + harnessTlsServerReply( + "HTTP/1.1 200 OK\r\n" + "content-length:32\r\n" + "\r\n" + "0123456789012345678901234567890"); - harnessTlsServerClose(); + harnessTlsServerClose(); - // Request with chunked content - harnessTlsServerAccept(); + // Request with chunked content + harnessTlsServerAccept(); - harnessTlsServerExpect( - "GET / HTTP/1.1\r\n" - "\r\n"); + harnessTlsServerExpect( + "GET / HTTP/1.1\r\n" + "\r\n"); - harnessTlsServerReply( - "HTTP/1.1 200 OK\r\n" - "Transfer-Encoding:chunked\r\n" - "\r\n" - "20\r\n" - "01234567890123456789012345678901\r\n" - "10\r\n" - "0123456789012345\r\n" - "0\r\n" - "\r\n"); - - harnessTlsServerClose(); - - exit(0); - } + harnessTlsServerReply( + "HTTP/1.1 200 OK\r\n" + "Transfer-Encoding:chunked\r\n" + "\r\n" + "20\r\n" + "01234567890123456789012345678901\r\n" + "10\r\n" + "0123456789012345\r\n" + "0\r\n" + "\r\n"); + + harnessTlsServerClose(); FUNCTION_HARNESS_RESULT_VOID(); } @@ -446,176 +440,198 @@ testRun(void) // Reset statistics httpClientStatLocal = (HttpClientStat){0}; + TEST_RESULT_PTR(httpClientStatStr(), NULL, "no stats yet"); TEST_ASSIGN( - client, httpClientNew(strNew("localhost"), harnessTlsTestPort(), 500, testContainer(), NULL, NULL), "new client"); + client, httpClientNew(strNew("localhost"), harnessTlsTestPort(), 500, testContainer(), NULL, NULL), + "new client"); TEST_ERROR_FMT( httpClientRequest(client, strNew("GET"), strNew("/"), NULL, NULL, NULL, false), HostConnectError, "unable to connect to 'localhost:%u': [111] Connection refused", harnessTlsTestPort()); - // Start http test server - TEST_RESULT_VOID(testHttpServer(), "http server begin"); - - // Test no output from server - TEST_ASSIGN( - client, httpClientNew(harnessTlsTestHost(), harnessTlsTestPort(), 500, testContainer(), NULL, NULL), "new client"); - client->timeout = 0; - - TEST_ERROR_FMT( - httpClientRequest(client, strNew("GET"), strNew("/"), NULL, NULL, NULL, false), FileReadError, - "timeout after 500ms waiting for read from '%s:%u'", strPtr(harnessTlsTestHost()), harnessTlsTestPort()); - - // Test invalid http version - TEST_ERROR( - httpClientRequest(client, strNew("GET"), strNew("/"), NULL, NULL, NULL, false), FormatError, - "http version of response 'HTTP/1.0 200 OK' must be HTTP/1.1"); - - // Test no space in status - TEST_ERROR( - httpClientRequest(client, strNew("GET"), strNew("/"), NULL, NULL, NULL, false), FormatError, - "response status '200OK' must have a space"); - - // Test unexpected end of headers - TEST_ERROR( - httpClientRequest(client, strNew("GET"), strNew("/"), NULL, NULL, NULL, false), FileReadError, - "unexpected eof while reading line"); - - // Test missing colon in header - TEST_ERROR( - httpClientRequest(client, strNew("GET"), strNew("/"), NULL, NULL, NULL, false), FormatError, - "header 'header-value' missing colon"); - - // Test invalid transfer encoding - TEST_ERROR( - httpClientRequest(client, strNew("GET"), strNew("/"), NULL, NULL, NULL, false), FormatError, - "only 'chunked' is supported for 'transfer-encoding' header"); - - // Test content length and transfer encoding both set - TEST_ERROR( - httpClientRequest(client, strNew("GET"), strNew("/"), NULL, NULL, NULL, false), FormatError, - "'transfer-encoding' and 'content-length' headers are both set"); - - // Test 5xx error with no retry - TEST_ERROR(httpClientRequest(client, strNew("GET"), strNew("/"), NULL, NULL, NULL, false), ServiceError, "[503] Slow Down"); - - // Request with no content - client->timeout = 2000; - - HttpHeader *headerRequest = httpHeaderNew(NULL); - httpHeaderAdd(headerRequest, strNew("host"), strNew("myhost.com")); - - HttpQuery *query = httpQueryNew(); - httpQueryAdd(query, strNew("name"), strNew("/path/A Z.txt")); - httpQueryAdd(query, strNew("type"), strNew("test")); - - TEST_RESULT_VOID( - httpClientRequest(client, strNew("GET"), strNew("/"), query, headerRequest, NULL, false), "request with no content"); - TEST_RESULT_UINT(httpClientResponseCode(client), 200, " check response code"); - TEST_RESULT_STR_Z(httpClientResponseMessage(client), "OK", " check response message"); - TEST_RESULT_UINT(httpClientEof(client), true, " io is eof"); - TEST_RESULT_STR_Z( - httpHeaderToLog(httpClientResponseHeader(client)), "{connection: 'ack', key1: '0', key2: 'value2'}", - " check response headers"); - - // Head request with content-length but no content - TEST_RESULT_VOID( - httpClientRequest(client, strNew("HEAD"), strNew("/"), NULL, httpHeaderNew(NULL), NULL, true), - "head request with content-length"); - TEST_RESULT_UINT(httpClientResponseCode(client), 200, " check response code"); - TEST_RESULT_STR_Z(httpClientResponseMessage(client), "OK", " check response message"); - TEST_RESULT_BOOL(httpClientEof(client), true, " io is eof"); - TEST_RESULT_BOOL(httpClientBusy(client), false, " client is not busy"); - TEST_RESULT_STR_Z( - httpHeaderToLog(httpClientResponseHeader(client)), "{content-length: '380'}", " check response headers"); - - // Head request with transfer encoding but no content - TEST_RESULT_VOID( - httpClientRequest(client, strNew("HEAD"), strNew("/"), NULL, httpHeaderNew(NULL), NULL, true), - "head request with transfer encoding"); - TEST_RESULT_UINT(httpClientResponseCode(client), 200, " check response code"); - TEST_RESULT_STR_Z(httpClientResponseMessage(client), "OK", " check response message"); - TEST_RESULT_BOOL(httpClientEof(client), true, " io is eof"); - TEST_RESULT_BOOL(httpClientBusy(client), false, " client is not busy"); - TEST_RESULT_STR_Z( - httpHeaderToLog(httpClientResponseHeader(client)), "{transfer-encoding: 'chunked'}", " check response headers"); - - // Head request with connection close but no content - TEST_RESULT_VOID( - httpClientRequest(client, strNew("HEAD"), strNew("/"), NULL, httpHeaderNew(NULL), NULL, true), - "head request with connection close"); - TEST_RESULT_UINT(httpClientResponseCode(client), 200, " check response code"); - TEST_RESULT_STR_Z(httpClientResponseMessage(client), "OK", " check response message"); - TEST_RESULT_BOOL(httpClientEof(client), true, " io is eof"); - TEST_RESULT_BOOL(httpClientBusy(client), false, " client is not busy"); - TEST_RESULT_STR_Z( - httpHeaderToLog(httpClientResponseHeader(client)), "{connection: 'close'}", " check response headers"); - - // Error with content length 0 - TEST_RESULT_VOID( - httpClientRequest(client, strNew("GET"), strNew("/"), NULL, NULL, NULL, false), "error with content length 0"); - TEST_RESULT_UINT(httpClientResponseCode(client), 404, " check response code"); - TEST_RESULT_STR_Z(httpClientResponseMessage(client), "Not Found", " check response message"); - TEST_RESULT_STR_Z( - httpHeaderToLog(httpClientResponseHeader(client)), "{content-length: '0'}", " check response headers"); - - // Error with content - Buffer *buffer = NULL; - - TEST_ASSIGN( - buffer, httpClientRequest(client, strNew("GET"), strNew("/"), NULL, NULL, NULL, false), "error with content length"); - TEST_RESULT_UINT(httpClientResponseCode(client), 403, " check response code"); - TEST_RESULT_STR_Z(httpClientResponseMessage(client), "Auth Error", " check response message"); - TEST_RESULT_STR_Z( - httpHeaderToLog(httpClientResponseHeader(client)), "{content-length: '7'}", " check response headers"); - TEST_RESULT_STR_Z(strNewBuf(buffer), "CONTENT", " check response"); - - // Request with content using content-length - ioBufferSizeSet(30); - - TEST_ASSIGN( - buffer, - httpClientRequest( - client, strNew("GET"), strNew("/path/file 1.txt"), NULL, - httpHeaderAdd(httpHeaderNew(NULL), strNew("content-length"), strNew("30")), - BUFSTRDEF("012345678901234567890123456789"), true), - "request with content length"); - TEST_RESULT_STR_Z( - httpHeaderToLog(httpClientResponseHeader(client)), "{connection: 'close'}", - " check response headers"); - TEST_RESULT_STR_Z(strNewBuf(buffer), "01234567890123456789012345678901", " check response"); - TEST_RESULT_UINT(httpClientRead(client, bufNew(1), true), 0, " call internal read to check eof"); - - // Request with eof before content complete with retry - TEST_ASSIGN( - buffer, httpClientRequest(client, strNew("GET"), strNew("/path/file 1.txt"), NULL, NULL, NULL, true), - "request with content length retry"); - TEST_RESULT_STR_Z(strNewBuf(buffer), "01234567890123456789012345678901", " check response"); - TEST_RESULT_UINT(httpClientRead(client, bufNew(1), true), 0, " call internal read to check eof"); - - // Request with eof before content and error - buffer = bufNew(32); - TEST_RESULT_VOID( - httpClientRequest(client, strNew("GET"), strNew("/path/file 1.txt"), NULL, NULL, NULL, false), - "request with content length error"); - TEST_RESULT_BOOL(httpClientBusy(client), true, " client is busy"); - TEST_ERROR( - ioRead(httpClientIoRead(client), buffer), FileReadError, "unexpected EOF reading HTTP content"); - - // Request with content using chunked encoding - TEST_RESULT_VOID( - httpClientRequest(client, strNew("GET"), strNew("/"), NULL, NULL, NULL, false), "request with chunked encoding"); - TEST_RESULT_STR_Z( - httpHeaderToLog(httpClientResponseHeader(client)), "{transfer-encoding: 'chunked'}", " check response headers"); - - buffer = bufNew(35); - TEST_RESULT_VOID(ioRead(httpClientIoRead(client), buffer), " read response"); - TEST_RESULT_STR_Z(strNewBuf(buffer), "01234567890123456789012345678901012", " check response"); - - TEST_RESULT_BOOL(httpClientStatStr() != NULL, true, "check statistics exist"); - - TEST_RESULT_VOID(httpClientFree(client), "free client"); + HARNESS_FORK_BEGIN() + { + HARNESS_FORK_CHILD_BEGIN(0, false) + { + // Start http test server + TEST_RESULT_VOID(testHttpServer(), "http server begin"); + } + HARNESS_FORK_CHILD_END(); + + HARNESS_FORK_PARENT_BEGIN() + { + // Test no output from server + TEST_ASSIGN( + client, httpClientNew(harnessTlsTestHost(), harnessTlsTestPort(), 500, testContainer(), NULL, NULL), + "new client"); + client->timeout = 0; + + TEST_ERROR_FMT( + httpClientRequest(client, strNew("GET"), strNew("/"), NULL, NULL, NULL, false), FileReadError, + "timeout after 500ms waiting for read from '%s:%u'", strPtr(harnessTlsTestHost()), harnessTlsTestPort()); + + // Test invalid http version + TEST_ERROR( + httpClientRequest(client, strNew("GET"), strNew("/"), NULL, NULL, NULL, false), FormatError, + "http version of response 'HTTP/1.0 200 OK' must be HTTP/1.1"); + + // Test no space in status + TEST_ERROR( + httpClientRequest(client, strNew("GET"), strNew("/"), NULL, NULL, NULL, false), FormatError, + "response status '200OK' must have a space"); + + // Test unexpected end of headers + TEST_ERROR( + httpClientRequest(client, strNew("GET"), strNew("/"), NULL, NULL, NULL, false), FileReadError, + "unexpected eof while reading line"); + + // Test missing colon in header + TEST_ERROR( + httpClientRequest(client, strNew("GET"), strNew("/"), NULL, NULL, NULL, false), FormatError, + "header 'header-value' missing colon"); + + // Test invalid transfer encoding + TEST_ERROR( + httpClientRequest(client, strNew("GET"), strNew("/"), NULL, NULL, NULL, false), FormatError, + "only 'chunked' is supported for 'transfer-encoding' header"); + + // Test content length and transfer encoding both set + TEST_ERROR( + httpClientRequest(client, strNew("GET"), strNew("/"), NULL, NULL, NULL, false), FormatError, + "'transfer-encoding' and 'content-length' headers are both set"); + + // Test 5xx error with no retry + TEST_ERROR( + httpClientRequest(client, strNew("GET"), strNew("/"), NULL, NULL, NULL, false), ServiceError, + "[503] Slow Down"); + + // Request with no content + client->timeout = 2000; + + HttpHeader *headerRequest = httpHeaderNew(NULL); + httpHeaderAdd(headerRequest, strNew("host"), strNew("myhost.com")); + + HttpQuery *query = httpQueryNew(); + httpQueryAdd(query, strNew("name"), strNew("/path/A Z.txt")); + httpQueryAdd(query, strNew("type"), strNew("test")); + + TEST_RESULT_VOID( + httpClientRequest(client, strNew("GET"), strNew("/"), query, headerRequest, NULL, false), + "request with no content"); + TEST_RESULT_UINT(httpClientResponseCode(client), 200, " check response code"); + TEST_RESULT_STR_Z(httpClientResponseMessage(client), "OK", " check response message"); + TEST_RESULT_UINT(httpClientEof(client), true, " io is eof"); + TEST_RESULT_STR_Z( + httpHeaderToLog(httpClientResponseHeader(client)), "{connection: 'ack', key1: '0', key2: 'value2'}", + " check response headers"); + + // Head request with content-length but no content + TEST_RESULT_VOID( + httpClientRequest(client, strNew("HEAD"), strNew("/"), NULL, httpHeaderNew(NULL), NULL, true), + "head request with content-length"); + TEST_RESULT_UINT(httpClientResponseCode(client), 200, " check response code"); + TEST_RESULT_STR_Z(httpClientResponseMessage(client), "OK", " check response message"); + TEST_RESULT_BOOL(httpClientEof(client), true, " io is eof"); + TEST_RESULT_BOOL(httpClientBusy(client), false, " client is not busy"); + TEST_RESULT_STR_Z( + httpHeaderToLog(httpClientResponseHeader(client)), "{content-length: '380'}", " check response headers"); + + // Head request with transfer encoding but no content + TEST_RESULT_VOID( + httpClientRequest(client, strNew("HEAD"), strNew("/"), NULL, httpHeaderNew(NULL), NULL, true), + "head request with transfer encoding"); + TEST_RESULT_UINT(httpClientResponseCode(client), 200, " check response code"); + TEST_RESULT_STR_Z(httpClientResponseMessage(client), "OK", " check response message"); + TEST_RESULT_BOOL(httpClientEof(client), true, " io is eof"); + TEST_RESULT_BOOL(httpClientBusy(client), false, " client is not busy"); + TEST_RESULT_STR_Z( + httpHeaderToLog(httpClientResponseHeader(client)), "{transfer-encoding: 'chunked'}", + " check response headers"); + + // Head request with connection close but no content + TEST_RESULT_VOID( + httpClientRequest(client, strNew("HEAD"), strNew("/"), NULL, httpHeaderNew(NULL), NULL, true), + "head request with connection close"); + TEST_RESULT_UINT(httpClientResponseCode(client), 200, " check response code"); + TEST_RESULT_STR_Z(httpClientResponseMessage(client), "OK", " check response message"); + TEST_RESULT_BOOL(httpClientEof(client), true, " io is eof"); + TEST_RESULT_BOOL(httpClientBusy(client), false, " client is not busy"); + TEST_RESULT_STR_Z( + httpHeaderToLog(httpClientResponseHeader(client)), "{connection: 'close'}", " check response headers"); + + // Error with content length 0 + TEST_RESULT_VOID( + httpClientRequest(client, strNew("GET"), strNew("/"), NULL, NULL, NULL, false), "error with content length 0"); + TEST_RESULT_UINT(httpClientResponseCode(client), 404, " check response code"); + TEST_RESULT_STR_Z(httpClientResponseMessage(client), "Not Found", " check response message"); + TEST_RESULT_STR_Z( + httpHeaderToLog(httpClientResponseHeader(client)), "{content-length: '0'}", " check response headers"); + + // Error with content + Buffer *buffer = NULL; + + TEST_ASSIGN( + buffer, httpClientRequest(client, strNew("GET"), strNew("/"), NULL, NULL, NULL, false), + "error with content length"); + TEST_RESULT_UINT(httpClientResponseCode(client), 403, " check response code"); + TEST_RESULT_STR_Z(httpClientResponseMessage(client), "Auth Error", " check response message"); + TEST_RESULT_STR_Z( + httpHeaderToLog(httpClientResponseHeader(client)), "{content-length: '7'}", " check response headers"); + TEST_RESULT_STR_Z(strNewBuf(buffer), "CONTENT", " check response"); + + // Request with content using content-length + ioBufferSizeSet(30); + + TEST_ASSIGN( + buffer, + httpClientRequest( + client, strNew("GET"), strNew("/path/file 1.txt"), NULL, + httpHeaderAdd(httpHeaderNew(NULL), strNew("content-length"), strNew("30")), + BUFSTRDEF("012345678901234567890123456789"), true), + "request with content length"); + TEST_RESULT_STR_Z( + httpHeaderToLog(httpClientResponseHeader(client)), "{connection: 'close'}", + " check response headers"); + TEST_RESULT_STR_Z(strNewBuf(buffer), "01234567890123456789012345678901", " check response"); + TEST_RESULT_UINT(httpClientRead(client, bufNew(1), true), 0, " call internal read to check eof"); + + // Request with eof before content complete with retry + TEST_ASSIGN( + buffer, httpClientRequest(client, strNew("GET"), strNew("/path/file 1.txt"), NULL, NULL, NULL, true), + "request with content length retry"); + TEST_RESULT_STR_Z(strNewBuf(buffer), "01234567890123456789012345678901", " check response"); + TEST_RESULT_UINT(httpClientRead(client, bufNew(1), true), 0, " call internal read to check eof"); + + // Request with eof before content and error + buffer = bufNew(32); + TEST_RESULT_VOID( + httpClientRequest(client, strNew("GET"), strNew("/path/file 1.txt"), NULL, NULL, NULL, false), + "request with content length error"); + TEST_RESULT_BOOL(httpClientBusy(client), true, " client is busy"); + TEST_ERROR( + ioRead(httpClientIoRead(client), buffer), FileReadError, "unexpected EOF reading HTTP content"); + + // Request with content using chunked encoding + TEST_RESULT_VOID( + httpClientRequest(client, strNew("GET"), strNew("/"), NULL, NULL, NULL, false), + "request with chunked encoding"); + TEST_RESULT_STR_Z( + httpHeaderToLog(httpClientResponseHeader(client)), "{transfer-encoding: 'chunked'}", + " check response headers"); + + buffer = bufNew(35); + TEST_RESULT_VOID(ioRead(httpClientIoRead(client), buffer), " read response"); + TEST_RESULT_STR_Z(strNewBuf(buffer), "01234567890123456789012345678901012", " check response"); + + TEST_RESULT_BOOL(httpClientStatStr() != NULL, true, "check statistics exist"); + + TEST_RESULT_VOID(httpClientFree(client), "free client"); + } + HARNESS_FORK_PARENT_END(); + } + HARNESS_FORK_END(); } // ***************************************************************************************************************************** diff --git a/test/src/module/common/ioTlsTest.c b/test/src/module/common/ioTlsTest.c index cda3a630c..3dab88820 100644 --- a/test/src/module/common/ioTlsTest.c +++ b/test/src/module/common/ioTlsTest.c @@ -6,6 +6,7 @@ Test Tls Client #include "common/time.h" +#include "common/harnessFork.h" #include "common/harnessTls.h" /*********************************************************************************************************************************** @@ -16,46 +17,38 @@ testTlsServerAltName(void) { FUNCTION_HARNESS_VOID(); - if (fork() == 0) - { - // Change log process id to aid in debugging - hrnLogProcessIdSet(1); + harnessTlsServerInit( + harnessTlsTestPort(), + strPtr(strNewFmt("%s/" TEST_CERTIFICATE_PREFIX "-alt-name.crt", testRepoPath())), + strPtr(strNewFmt("%s/" TEST_CERTIFICATE_PREFIX ".key", testRepoPath()))); - harnessTlsServerInit( - harnessTlsTestPort(), - strPtr(strNewFmt("%s/" TEST_CERTIFICATE_PREFIX "-alt-name.crt", testRepoPath())), - strPtr(strNewFmt("%s/" TEST_CERTIFICATE_PREFIX ".key", testRepoPath()))); + // Certificate error on invalid ca path + harnessTlsServerAccept(); + harnessTlsServerClose(); - // Certificate error on invalid ca path + if (testContainer()) + { + // Success on valid ca file and match common name harnessTlsServerAccept(); harnessTlsServerClose(); - if (testContainer()) - { - // Success on valid ca file and match common name - harnessTlsServerAccept(); - harnessTlsServerClose(); - - // Success on valid ca file and match alt name - harnessTlsServerAccept(); - harnessTlsServerClose(); - - // Unable to find matching hostname in certificate - harnessTlsServerAccept(); - harnessTlsServerClose(); - } - - // Certificate error + // Success on valid ca file and match alt name harnessTlsServerAccept(); harnessTlsServerClose(); - // Certificate ignored + // Unable to find matching hostname in certificate harnessTlsServerAccept(); harnessTlsServerClose(); - - exit(0); } + // Certificate error + harnessTlsServerAccept(); + harnessTlsServerClose(); + + // Certificate ignored + harnessTlsServerAccept(); + harnessTlsServerClose(); + FUNCTION_HARNESS_RESULT_VOID(); } @@ -67,40 +60,32 @@ testTlsServer(void) { FUNCTION_HARNESS_VOID(); - if (fork() == 0) - { - // Change log process id to aid in debugging - hrnLogProcessIdSet(1); + harnessTlsServerInitDefault(); - harnessTlsServerInitDefault(); + // First protocol exchange + harnessTlsServerAccept(); - // First protocol exchange - harnessTlsServerAccept(); + harnessTlsServerExpect("some protocol info"); + harnessTlsServerReply("something:0\n"); - harnessTlsServerExpect("some protocol info"); - harnessTlsServerReply("something:0\n"); + sleepMSec(100); + harnessTlsServerReply("some "); - sleepMSec(100); - harnessTlsServerReply("some "); + sleepMSec(100); + harnessTlsServerReply("contentAND MORE"); - sleepMSec(100); - harnessTlsServerReply("contentAND MORE"); + // This will cause the client to disconnect + sleepMSec(500); - // This will cause the client to disconnect - sleepMSec(500); + // Second protocol exchange + harnessTlsServerExpect("more protocol info"); + harnessTlsServerReply("0123456789AB"); + harnessTlsServerClose(); - // Second protocol exchange - harnessTlsServerExpect("more protocol info"); - harnessTlsServerReply("0123456789AB"); - harnessTlsServerClose(); - - // Need data in read buffer to test tlsWriteContinue() - harnessTlsServerAccept(); - harnessTlsServerReply("0123456789AB"); - harnessTlsServerClose(); - - exit(0); - } + // Need data in read buffer to test tlsWriteContinue() + harnessTlsServerAccept(); + harnessTlsServerReply("0123456789AB"); + harnessTlsServerClose(); FUNCTION_HARNESS_RESULT_VOID(); } @@ -289,57 +274,72 @@ testRun(void) } } - // Start server to test various certificate errors - TEST_RESULT_VOID(testTlsServerAltName(), "tls alt name server begin"); - - TEST_ERROR( - tlsClientOpen( - tlsClientNew( - sckClientNew(strNew("localhost"), harnessTlsTestPort(), 500), 500, true, strNew("bogus.crt"), - strNew("/bogus"))), - CryptoError, "unable to set user-defined CA certificate location: [33558530] No such file or directory"); - TEST_ERROR_FMT( - tlsClientOpen( - tlsClientNew(sckClientNew(strNew("localhost"), harnessTlsTestPort(), 500), 500, true, NULL, strNew("/bogus"))), - CryptoError, "unable to verify certificate presented by 'localhost:%u': [20] unable to get local issuer certificate", - harnessTlsTestPort()); - - if (testContainer()) + HARNESS_FORK_BEGIN() { - TEST_RESULT_VOID( - tlsClientOpen( - tlsClientNew( - sckClientNew(strNew("test.pgbackrest.org"), harnessTlsTestPort(), 500), 500, true, - strNewFmt("%s/" TEST_CERTIFICATE_PREFIX "-ca.crt", testRepoPath()), NULL)), - "success on valid ca file and match common name"); - TEST_RESULT_VOID( - tlsClientOpen( - tlsClientNew( - sckClientNew(strNew("host.test2.pgbackrest.org"), harnessTlsTestPort(), 500), 500, true, - strNewFmt("%s/" TEST_CERTIFICATE_PREFIX "-ca.crt", testRepoPath()), NULL)), - "success on valid ca file and match alt name"); - TEST_ERROR( - tlsClientOpen( - tlsClientNew( - sckClientNew(strNew("test3.pgbackrest.org"), harnessTlsTestPort(), 500), 500, true, - strNewFmt("%s/" TEST_CERTIFICATE_PREFIX "-ca.crt", testRepoPath()), NULL)), - CryptoError, - "unable to find hostname 'test3.pgbackrest.org' in certificate common name or subject alternative names"); - } - - TEST_ERROR_FMT( - tlsClientOpen( - tlsClientNew( - sckClientNew(strNew("localhost"), harnessTlsTestPort(), 500), 500, true, - strNewFmt("%s/" TEST_CERTIFICATE_PREFIX ".crt", testRepoPath()), - NULL)), - CryptoError, "unable to verify certificate presented by 'localhost:%u': [20] unable to get local issuer certificate", - harnessTlsTestPort()); + HARNESS_FORK_CHILD_BEGIN(0, false) + { + // Start server to test various certificate errors + TEST_RESULT_VOID(testTlsServerAltName(), "tls alt name server begin"); + } + HARNESS_FORK_CHILD_END(); - TEST_RESULT_VOID( - tlsClientOpen( - tlsClientNew(sckClientNew(strNew("localhost"), harnessTlsTestPort(), 500), 500, false, NULL, NULL)), - "success on no verify"); + HARNESS_FORK_PARENT_BEGIN() + { + TEST_ERROR( + tlsClientOpen( + tlsClientNew( + sckClientNew(strNew("localhost"), harnessTlsTestPort(), 500), 500, true, strNew("bogus.crt"), + strNew("/bogus"))), + CryptoError, "unable to set user-defined CA certificate location: [33558530] No such file or directory"); + TEST_ERROR_FMT( + tlsClientOpen( + tlsClientNew( + sckClientNew(strNew("localhost"), harnessTlsTestPort(), 500), 500, true, NULL, strNew("/bogus"))), + CryptoError, + "unable to verify certificate presented by 'localhost:%u': [20] unable to get local issuer certificate", + harnessTlsTestPort()); + + if (testContainer()) + { + TEST_RESULT_VOID( + tlsClientOpen( + tlsClientNew( + sckClientNew(strNew("test.pgbackrest.org"), harnessTlsTestPort(), 500), 500, true, + strNewFmt("%s/" TEST_CERTIFICATE_PREFIX "-ca.crt", testRepoPath()), NULL)), + "success on valid ca file and match common name"); + TEST_RESULT_VOID( + tlsClientOpen( + tlsClientNew( + sckClientNew(strNew("host.test2.pgbackrest.org"), harnessTlsTestPort(), 500), 500, true, + strNewFmt("%s/" TEST_CERTIFICATE_PREFIX "-ca.crt", testRepoPath()), NULL)), + "success on valid ca file and match alt name"); + TEST_ERROR( + tlsClientOpen( + tlsClientNew( + sckClientNew(strNew("test3.pgbackrest.org"), harnessTlsTestPort(), 500), 500, true, + strNewFmt("%s/" TEST_CERTIFICATE_PREFIX "-ca.crt", testRepoPath()), NULL)), + CryptoError, + "unable to find hostname 'test3.pgbackrest.org' in certificate common name or subject alternative names"); + } + + TEST_ERROR_FMT( + tlsClientOpen( + tlsClientNew( + sckClientNew(strNew("localhost"), harnessTlsTestPort(), 500), 500, true, + strNewFmt("%s/" TEST_CERTIFICATE_PREFIX ".crt", testRepoPath()), + NULL)), + CryptoError, + "unable to verify certificate presented by 'localhost:%u': [20] unable to get local issuer certificate", + harnessTlsTestPort()); + + TEST_RESULT_VOID( + tlsClientOpen( + tlsClientNew(sckClientNew(strNew("localhost"), harnessTlsTestPort(), 500), 500, false, NULL, NULL)), + "success on no verify"); + } + HARNESS_FORK_PARENT_END(); + } + HARNESS_FORK_END(); } // ***************************************************************************************************************************** @@ -353,65 +353,79 @@ testRun(void) tlsClientStatLocal = (TlsClientStat){0}; TEST_RESULT_PTR(tlsClientStatStr(), NULL, "no stats yet"); - TEST_RESULT_VOID(testTlsServer(), "tls server begin"); - ioBufferSizeSet(12); - - TEST_ASSIGN( - client, tlsClientNew(sckClientNew(harnessTlsTestHost(), harnessTlsTestPort(), 500), 500, testContainer(), NULL, NULL), - "new client"); - TEST_RESULT_VOID(tlsClientOpen(client), "open client"); - - const Buffer *input = BUFSTRDEF("some protocol info"); - TEST_RESULT_VOID(ioWrite(tlsClientIoWrite(client), input), "write input"); - ioWriteFlush(tlsClientIoWrite(client)); - - TEST_RESULT_STR_Z(ioReadLine(tlsClientIoRead(client)), "something:0", "read line"); - TEST_RESULT_BOOL(ioReadEof(tlsClientIoRead(client)), false, " check eof = false"); - - Buffer *output = bufNew(12); - TEST_RESULT_UINT(ioRead(tlsClientIoRead(client), output), 12, "read output"); - TEST_RESULT_STR_Z(strNewBuf(output), "some content", " check output"); - TEST_RESULT_BOOL(ioReadEof(tlsClientIoRead(client)), false, " check eof = false"); - - output = bufNew(8); - TEST_RESULT_UINT(ioRead(tlsClientIoRead(client), output), 8, "read output"); - TEST_RESULT_STR_Z(strNewBuf(output), "AND MORE", " check output"); - TEST_RESULT_BOOL(ioReadEof(tlsClientIoRead(client)), false, " check eof = false"); - - output = bufNew(12); - TEST_ERROR_FMT( - ioRead(tlsClientIoRead(client), output), FileReadError, - "timeout after 500ms waiting for read from '%s:%u'", strPtr(harnessTlsTestHost()), harnessTlsTestPort()); - - // ------------------------------------------------------------------------------------------------------------------------- - input = BUFSTRDEF("more protocol info"); - TEST_RESULT_VOID(tlsClientOpen(client), "open client again (it is already open)"); - TEST_RESULT_VOID(ioWrite(tlsClientIoWrite(client), input), "write input"); - ioWriteFlush(tlsClientIoWrite(client)); - - output = bufNew(12); - TEST_RESULT_UINT(ioRead(tlsClientIoRead(client), output), 12, "read output"); - TEST_RESULT_STR_Z(strNewBuf(output), "0123456789AB", " check output"); - TEST_RESULT_BOOL(ioReadEof(tlsClientIoRead(client)), false, " check eof = false"); - - output = bufNew(12); - TEST_RESULT_UINT(ioRead(tlsClientIoRead(client), output), 0, "read no output after eof"); - TEST_RESULT_BOOL(ioReadEof(tlsClientIoRead(client)), true, " check eof = true"); - - // ------------------------------------------------------------------------------------------------------------------------- - TEST_RESULT_VOID(tlsClientOpen(client), "open client again (was closed by server)"); - TEST_RESULT_BOOL(tlsWriteContinue(client, -1, SSL_ERROR_WANT_READ, 1), true, "continue on WANT_READ"); - TEST_RESULT_BOOL(tlsWriteContinue(client, 0, SSL_ERROR_NONE, 1), true, "continue on WANT_READ"); - TEST_ERROR( - tlsWriteContinue(client, 77, 0, 88), FileWriteError, - "unable to write to tls, write size 77 does not match expected size 88"); - TEST_ERROR(tlsWriteContinue(client, 0, SSL_ERROR_ZERO_RETURN, 1), FileWriteError, "unable to write to tls [6]"); - - // ------------------------------------------------------------------------------------------------------------------------- - TEST_RESULT_BOOL(sckClientStatStr() != NULL, true, "check statistics exist"); - TEST_RESULT_BOOL(tlsClientStatStr() != NULL, true, "check statistics exist"); + HARNESS_FORK_BEGIN() + { + HARNESS_FORK_CHILD_BEGIN(0, false) + { + TEST_RESULT_VOID(testTlsServer(), "tls server begin"); + } + HARNESS_FORK_CHILD_END(); - TEST_RESULT_VOID(tlsClientFree(client), "free client"); + HARNESS_FORK_PARENT_BEGIN() + { + ioBufferSizeSet(12); + + TEST_ASSIGN( + client, + tlsClientNew(sckClientNew(harnessTlsTestHost(), harnessTlsTestPort(), 500), 500, testContainer(), NULL, NULL), + "new client"); + TEST_RESULT_VOID(tlsClientOpen(client), "open client"); + + const Buffer *input = BUFSTRDEF("some protocol info"); + TEST_RESULT_VOID(ioWrite(tlsClientIoWrite(client), input), "write input"); + ioWriteFlush(tlsClientIoWrite(client)); + + TEST_RESULT_STR_Z(ioReadLine(tlsClientIoRead(client)), "something:0", "read line"); + TEST_RESULT_BOOL(ioReadEof(tlsClientIoRead(client)), false, " check eof = false"); + + Buffer *output = bufNew(12); + TEST_RESULT_UINT(ioRead(tlsClientIoRead(client), output), 12, "read output"); + TEST_RESULT_STR_Z(strNewBuf(output), "some content", " check output"); + TEST_RESULT_BOOL(ioReadEof(tlsClientIoRead(client)), false, " check eof = false"); + + output = bufNew(8); + TEST_RESULT_UINT(ioRead(tlsClientIoRead(client), output), 8, "read output"); + TEST_RESULT_STR_Z(strNewBuf(output), "AND MORE", " check output"); + TEST_RESULT_BOOL(ioReadEof(tlsClientIoRead(client)), false, " check eof = false"); + + output = bufNew(12); + TEST_ERROR_FMT( + ioRead(tlsClientIoRead(client), output), FileReadError, + "timeout after 500ms waiting for read from '%s:%u'", strPtr(harnessTlsTestHost()), harnessTlsTestPort()); + + // ----------------------------------------------------------------------------------------------------------------- + input = BUFSTRDEF("more protocol info"); + TEST_RESULT_VOID(tlsClientOpen(client), "open client again (it is already open)"); + TEST_RESULT_VOID(ioWrite(tlsClientIoWrite(client), input), "write input"); + ioWriteFlush(tlsClientIoWrite(client)); + + output = bufNew(12); + TEST_RESULT_UINT(ioRead(tlsClientIoRead(client), output), 12, "read output"); + TEST_RESULT_STR_Z(strNewBuf(output), "0123456789AB", " check output"); + TEST_RESULT_BOOL(ioReadEof(tlsClientIoRead(client)), false, " check eof = false"); + + output = bufNew(12); + TEST_RESULT_UINT(ioRead(tlsClientIoRead(client), output), 0, "read no output after eof"); + TEST_RESULT_BOOL(ioReadEof(tlsClientIoRead(client)), true, " check eof = true"); + + // ----------------------------------------------------------------------------------------------------------------- + TEST_RESULT_VOID(tlsClientOpen(client), "open client again (was closed by server)"); + TEST_RESULT_BOOL(tlsWriteContinue(client, -1, SSL_ERROR_WANT_READ, 1), true, "continue on WANT_READ"); + TEST_RESULT_BOOL(tlsWriteContinue(client, 0, SSL_ERROR_NONE, 1), true, "continue on WANT_READ"); + TEST_ERROR( + tlsWriteContinue(client, 77, 0, 88), FileWriteError, + "unable to write to tls, write size 77 does not match expected size 88"); + TEST_ERROR(tlsWriteContinue(client, 0, SSL_ERROR_ZERO_RETURN, 1), FileWriteError, "unable to write to tls [6]"); + + // ----------------------------------------------------------------------------------------------------------------- + TEST_RESULT_BOOL(sckClientStatStr() != NULL, true, "check statistics exist"); + TEST_RESULT_BOOL(tlsClientStatStr() != NULL, true, "check statistics exist"); + + TEST_RESULT_VOID(tlsClientFree(client), "free client"); + } + HARNESS_FORK_PARENT_END(); + } + HARNESS_FORK_END(); } FUNCTION_HARNESS_RESULT_VOID(); diff --git a/test/src/module/storage/s3Test.c b/test/src/module/storage/s3Test.c index f17b36f2c..ae257ab34 100644 --- a/test/src/module/storage/s3Test.c +++ b/test/src/module/storage/s3Test.c @@ -4,6 +4,7 @@ Test S3 Storage #include <unistd.h> #include "common/harnessConfig.h" +#include "common/harnessFork.h" #include "common/harnessStorage.h" #include "common/harnessTls.h" @@ -87,441 +88,434 @@ testS3Server(void) { FUNCTION_HARNESS_VOID(); - if (fork() == 0) - { - // Change log process id to aid in debugging - hrnLogProcessIdSet(1); - - harnessTlsServerInitDefault(); - harnessTlsServerAccept(); - - // storageS3NewRead() and StorageS3FileRead - // ------------------------------------------------------------------------------------------------------------------------- - // Ignore missing file - harnessTlsServerExpect(testS3ServerRequest(HTTP_VERB_GET, "/fi%26le.txt", NULL, storageS3UriStyleHost)); - harnessTlsServerReply(testS3ServerResponse(404, "Not Found", NULL, NULL)); - - // Error on missing file - harnessTlsServerExpect(testS3ServerRequest(HTTP_VERB_GET, "/file.txt", NULL, storageS3UriStyleHost)); - harnessTlsServerReply(testS3ServerResponse(404, "Not Found", NULL, NULL)); - - // Get file - harnessTlsServerExpect(testS3ServerRequest(HTTP_VERB_GET, "/file.txt", NULL, storageS3UriStyleHost)); - harnessTlsServerReply(testS3ServerResponse(200, "OK", NULL, "this is a sample file")); - - // Get zero-length file - harnessTlsServerExpect(testS3ServerRequest(HTTP_VERB_GET, "/file0.txt", NULL, storageS3UriStyleHost)); - harnessTlsServerReply(testS3ServerResponse(200, "OK", NULL, NULL)); - - // Throw non-404 error - harnessTlsServerExpect(testS3ServerRequest(HTTP_VERB_GET, "/file.txt", NULL, storageS3UriStyleHost)); - harnessTlsServerReply(testS3ServerResponse(303, "Some bad status", NULL, "CONTENT")); - - // storageS3NewWrite() and StorageWriteS3 - // ------------------------------------------------------------------------------------------------------------------------- - // File is written all at once - harnessTlsServerExpect(testS3ServerRequest(HTTP_VERB_PUT, "/file.txt", "ABCD", storageS3UriStyleHost)); - harnessTlsServerReply(testS3ServerResponse( - 403, "Forbidden", NULL, + harnessTlsServerInitDefault(); + harnessTlsServerAccept(); + + // storageS3NewRead() and StorageS3FileRead + // ------------------------------------------------------------------------------------------------------------------------- + // Ignore missing file + harnessTlsServerExpect(testS3ServerRequest(HTTP_VERB_GET, "/fi%26le.txt", NULL, storageS3UriStyleHost)); + harnessTlsServerReply(testS3ServerResponse(404, "Not Found", NULL, NULL)); + + // Error on missing file + harnessTlsServerExpect(testS3ServerRequest(HTTP_VERB_GET, "/file.txt", NULL, storageS3UriStyleHost)); + harnessTlsServerReply(testS3ServerResponse(404, "Not Found", NULL, NULL)); + + // Get file + harnessTlsServerExpect(testS3ServerRequest(HTTP_VERB_GET, "/file.txt", NULL, storageS3UriStyleHost)); + harnessTlsServerReply(testS3ServerResponse(200, "OK", NULL, "this is a sample file")); + + // Get zero-length file + harnessTlsServerExpect(testS3ServerRequest(HTTP_VERB_GET, "/file0.txt", NULL, storageS3UriStyleHost)); + harnessTlsServerReply(testS3ServerResponse(200, "OK", NULL, NULL)); + + // Throw non-404 error + harnessTlsServerExpect(testS3ServerRequest(HTTP_VERB_GET, "/file.txt", NULL, storageS3UriStyleHost)); + harnessTlsServerReply(testS3ServerResponse(303, "Some bad status", NULL, "CONTENT")); + + // storageS3NewWrite() and StorageWriteS3 + // ------------------------------------------------------------------------------------------------------------------------- + // File is written all at once + harnessTlsServerExpect(testS3ServerRequest(HTTP_VERB_PUT, "/file.txt", "ABCD", storageS3UriStyleHost)); + harnessTlsServerReply(testS3ServerResponse( + 403, "Forbidden", NULL, + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<Error>" + "<Code>RequestTimeTooSkewed</Code>" + "<Message>The difference between the request time and the current time is too large.</Message>" + "<RequestTime>20190726T221748Z</RequestTime>" + "<ServerTime>2019-07-26T22:33:27Z</ServerTime>" + "<MaxAllowedSkewMilliseconds>900000</MaxAllowedSkewMilliseconds>" + "<RequestId>601AA1A7F7E37AE9</RequestId>" + "<HostId>KYMys77PoloZrGCkiQRyOIl0biqdHsk4T2EdTkhzkH1l8x00D4lvv/py5uUuHwQXG9qz6NRuldQ=</HostId>" + "</Error>")); + + harnessTlsServerAccept(); + harnessTlsServerExpect(testS3ServerRequest(HTTP_VERB_PUT, "/file.txt", "ABCD", storageS3UriStyleHost)); + harnessTlsServerReply(testS3ServerResponse(200, "OK", NULL, NULL)); + + // Zero-length file + harnessTlsServerExpect(testS3ServerRequest(HTTP_VERB_PUT, "/file.txt", "", storageS3UriStyleHost)); + harnessTlsServerReply(testS3ServerResponse(200, "OK", NULL, NULL)); + + // File is written in chunks with nothing left over on close + harnessTlsServerExpect(testS3ServerRequest(HTTP_VERB_POST, "/file.txt?uploads=", NULL, storageS3UriStyleHost)); + harnessTlsServerReply(testS3ServerResponse( + 200, "OK", NULL, + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<InitiateMultipartUploadResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">" + "<Bucket>bucket</Bucket>" + "<Key>file.txt</Key>" + "<UploadId>WxRt</UploadId>" + "</InitiateMultipartUploadResult>")); + + harnessTlsServerExpect( + testS3ServerRequest(HTTP_VERB_PUT, "/file.txt?partNumber=1&uploadId=WxRt", "1234567890123456", storageS3UriStyleHost)); + harnessTlsServerReply(testS3ServerResponse(200, "OK", "etag:WxRt1", NULL)); + harnessTlsServerExpect( + testS3ServerRequest(HTTP_VERB_PUT, "/file.txt?partNumber=2&uploadId=WxRt", "7890123456789012", storageS3UriStyleHost)); + harnessTlsServerReply(testS3ServerResponse(200, "OK", "eTag:WxRt2", NULL)); + + harnessTlsServerExpect(testS3ServerRequest( + HTTP_VERB_POST, "/file.txt?uploadId=WxRt", + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<CompleteMultipartUpload>" + "<Part><PartNumber>1</PartNumber><ETag>WxRt1</ETag></Part>" + "<Part><PartNumber>2</PartNumber><ETag>WxRt2</ETag></Part>" + "</CompleteMultipartUpload>\n", + storageS3UriStyleHost)); + harnessTlsServerReply(testS3ServerResponse(200, "OK", NULL, NULL)); + + // File is written in chunks with something left over on close + harnessTlsServerExpect(testS3ServerRequest(HTTP_VERB_POST, "/file.txt?uploads=", NULL, storageS3UriStyleHost)); + harnessTlsServerReply(testS3ServerResponse( + 200, "OK", NULL, + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<InitiateMultipartUploadResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">" + "<Bucket>bucket</Bucket>" + "<Key>file.txt</Key>" + "<UploadId>RR55</UploadId>" + "</InitiateMultipartUploadResult>")); + + harnessTlsServerExpect( + testS3ServerRequest(HTTP_VERB_PUT, "/file.txt?partNumber=1&uploadId=RR55", "1234567890123456", storageS3UriStyleHost)); + harnessTlsServerReply(testS3ServerResponse(200, "OK", "etag:RR551", NULL)); + harnessTlsServerExpect( + testS3ServerRequest(HTTP_VERB_PUT, "/file.txt?partNumber=2&uploadId=RR55", "7890", storageS3UriStyleHost)); + harnessTlsServerReply(testS3ServerResponse(200, "OK", "eTag:RR552", NULL)); + + harnessTlsServerExpect(testS3ServerRequest( + HTTP_VERB_POST, "/file.txt?uploadId=RR55", + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<CompleteMultipartUpload>" + "<Part><PartNumber>1</PartNumber><ETag>RR551</ETag></Part>" + "<Part><PartNumber>2</PartNumber><ETag>RR552</ETag></Part>" + "</CompleteMultipartUpload>\n", + storageS3UriStyleHost)); + harnessTlsServerReply(testS3ServerResponse(200, "OK", NULL, NULL)); + + // storageDriverExists() + // ------------------------------------------------------------------------------------------------------------------------- + // File missing + harnessTlsServerExpect(testS3ServerRequest(HTTP_VERB_HEAD, "/BOGUS", NULL, storageS3UriStyleHost)); + harnessTlsServerReply(testS3ServerResponse(404, "Not Found", NULL, NULL)); + + // File exists + harnessTlsServerExpect(testS3ServerRequest(HTTP_VERB_HEAD, "/subdir/file1.txt", NULL, storageS3UriStyleHost)); + harnessTlsServerReply(testS3ServerResponse( + 200, "OK", + "content-length:999\r\n" + "Last-Modified: Wed, 21 Oct 2015 07:28:00 GMT", + NULL)); + + // Info() + // ------------------------------------------------------------------------------------------------------------------------- + // File missing + harnessTlsServerExpect(testS3ServerRequest(HTTP_VERB_HEAD, "/BOGUS", NULL, storageS3UriStyleHost)); + harnessTlsServerReply(testS3ServerResponse(404, "Not Found", NULL, NULL)); + + // File exists + harnessTlsServerExpect(testS3ServerRequest(HTTP_VERB_HEAD, "/subdir/file1.txt", NULL, storageS3UriStyleHost)); + harnessTlsServerReply(testS3ServerResponse( + 200, "OK", + "content-length:9999\r\n" + "Last-Modified: Wed, 21 Oct 2015 07:28:00 GMT", + NULL)); + + // File exists and only checking existence + harnessTlsServerExpect(testS3ServerRequest(HTTP_VERB_HEAD, "/subdir/file2.txt", NULL, storageS3UriStyleHost)); + harnessTlsServerReply(testS3ServerResponse( + 200, "OK", + "content-length:777\r\n" + "Last-Modified: Wed, 22 Oct 2015 07:28:00 GMT", + NULL)); + + // InfoList() + // ------------------------------------------------------------------------------------------------------------------------- + harnessTlsServerExpect( + testS3ServerRequest(HTTP_VERB_GET, "/?delimiter=%2F&list-type=2&prefix=path%2Fto%2F", NULL, storageS3UriStyleHost)); + harnessTlsServerReply( + testS3ServerResponse( + 200, "OK", NULL, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" - "<Error>" - "<Code>RequestTimeTooSkewed</Code>" - "<Message>The difference between the request time and the current time is too large.</Message>" - "<RequestTime>20190726T221748Z</RequestTime>" - "<ServerTime>2019-07-26T22:33:27Z</ServerTime>" - "<MaxAllowedSkewMilliseconds>900000</MaxAllowedSkewMilliseconds>" - "<RequestId>601AA1A7F7E37AE9</RequestId>" - "<HostId>KYMys77PoloZrGCkiQRyOIl0biqdHsk4T2EdTkhzkH1l8x00D4lvv/py5uUuHwQXG9qz6NRuldQ=</HostId>" - "</Error>")); - - harnessTlsServerAccept(); - harnessTlsServerExpect(testS3ServerRequest(HTTP_VERB_PUT, "/file.txt", "ABCD", storageS3UriStyleHost)); - harnessTlsServerReply(testS3ServerResponse(200, "OK", NULL, NULL)); - - // Zero-length file - harnessTlsServerExpect(testS3ServerRequest(HTTP_VERB_PUT, "/file.txt", "", storageS3UriStyleHost)); - harnessTlsServerReply(testS3ServerResponse(200, "OK", NULL, NULL)); - - // File is written in chunks with nothing left over on close - harnessTlsServerExpect(testS3ServerRequest(HTTP_VERB_POST, "/file.txt?uploads=", NULL, storageS3UriStyleHost)); - harnessTlsServerReply(testS3ServerResponse( + "<ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">" + " <Contents>" + " <Key>path/to/test_file</Key>" + " <LastModified>2009-10-12T17:50:30.000Z</LastModified>" + " <Size>787</Size>" + " </Contents>" + " <CommonPrefixes>" + " <Prefix>path/to/test_path/</Prefix>" + " </CommonPrefixes>" + "</ListBucketResult>")); + + // storageDriverList() + // ------------------------------------------------------------------------------------------------------------------------- + // Throw errors + harnessTlsServerExpect(testS3ServerRequest(HTTP_VERB_GET, "/?delimiter=%2F&list-type=2", NULL, storageS3UriStyleHost)); + harnessTlsServerReply(testS3ServerResponse( 344, "Another bad status", NULL, NULL)); + + harnessTlsServerExpect(testS3ServerRequest(HTTP_VERB_GET, "/?delimiter=%2F&list-type=2", NULL, storageS3UriStyleHost)); + harnessTlsServerReply(testS3ServerResponse( + 344, "Another bad status with xml", NULL, + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<Error>" + "<Code>SomeOtherCode</Code>" + "</Error>")); + + harnessTlsServerExpect(testS3ServerRequest(HTTP_VERB_GET, "/?delimiter=%2F&list-type=2", NULL, storageS3UriStyleHost)); + harnessTlsServerReply(testS3ServerResponse( + 403, "Forbidden", NULL, + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<Error>" + "<Code>RequestTimeTooSkewed</Code>" + "<Message>The difference between the request time and the current time is too large.</Message>" + "</Error>")); + harnessTlsServerExpect(testS3ServerRequest(HTTP_VERB_GET, "/?delimiter=%2F&list-type=2", NULL, storageS3UriStyleHost)); + harnessTlsServerReply(testS3ServerResponse( + 403, "Forbidden", NULL, + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<Error>" + "<Code>RequestTimeTooSkewed</Code>" + "<Message>The difference between the request time and the current time is too large.</Message>" + "</Error>")); + harnessTlsServerExpect(testS3ServerRequest(HTTP_VERB_GET, "/?delimiter=%2F&list-type=2", NULL, storageS3UriStyleHost)); + harnessTlsServerReply(testS3ServerResponse( + 403, "Forbidden", NULL, + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<Error>" + "<Code>RequestTimeTooSkewed</Code>" + "<Message>The difference between the request time and the current time is too large.</Message>" + "</Error>")); + + // list a file/path in root + harnessTlsServerExpect(testS3ServerRequest(HTTP_VERB_GET, "/?delimiter=%2F&list-type=2", NULL, storageS3UriStyleHost)); + harnessTlsServerReply( + testS3ServerResponse( 200, "OK", NULL, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" - "<InitiateMultipartUploadResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">" - "<Bucket>bucket</Bucket>" - "<Key>file.txt</Key>" - "<UploadId>WxRt</UploadId>" - "</InitiateMultipartUploadResult>")); - - harnessTlsServerExpect( - testS3ServerRequest(HTTP_VERB_PUT, "/file.txt?partNumber=1&uploadId=WxRt", "1234567890123456", storageS3UriStyleHost)); - harnessTlsServerReply(testS3ServerResponse(200, "OK", "etag:WxRt1", NULL)); - harnessTlsServerExpect( - testS3ServerRequest(HTTP_VERB_PUT, "/file.txt?partNumber=2&uploadId=WxRt", "7890123456789012", storageS3UriStyleHost)); - harnessTlsServerReply(testS3ServerResponse(200, "OK", "eTag:WxRt2", NULL)); - - harnessTlsServerExpect(testS3ServerRequest( - HTTP_VERB_POST, "/file.txt?uploadId=WxRt", - "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" - "<CompleteMultipartUpload>" - "<Part><PartNumber>1</PartNumber><ETag>WxRt1</ETag></Part>" - "<Part><PartNumber>2</PartNumber><ETag>WxRt2</ETag></Part>" - "</CompleteMultipartUpload>\n", - storageS3UriStyleHost)); - harnessTlsServerReply(testS3ServerResponse(200, "OK", NULL, NULL)); - - // File is written in chunks with something left over on close - harnessTlsServerExpect(testS3ServerRequest(HTTP_VERB_POST, "/file.txt?uploads=", NULL, storageS3UriStyleHost)); - harnessTlsServerReply(testS3ServerResponse( + "<ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">" + " <Contents>" + " <Key>test1.txt</Key>" + " </Contents>" + " <CommonPrefixes>" + " <Prefix>path1/</Prefix>" + " </CommonPrefixes>" + "</ListBucketResult>")); + + // list a file in root with expression + harnessTlsServerExpect( + testS3ServerRequest(HTTP_VERB_GET, "/?delimiter=%2F&list-type=2&prefix=test", NULL, storageS3UriStyleHost)); + harnessTlsServerReply( + testS3ServerResponse( 200, "OK", NULL, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" - "<InitiateMultipartUploadResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">" - "<Bucket>bucket</Bucket>" - "<Key>file.txt</Key>" - "<UploadId>RR55</UploadId>" - "</InitiateMultipartUploadResult>")); - - harnessTlsServerExpect( - testS3ServerRequest(HTTP_VERB_PUT, "/file.txt?partNumber=1&uploadId=RR55", "1234567890123456", storageS3UriStyleHost)); - harnessTlsServerReply(testS3ServerResponse(200, "OK", "etag:RR551", NULL)); - harnessTlsServerExpect( - testS3ServerRequest(HTTP_VERB_PUT, "/file.txt?partNumber=2&uploadId=RR55", "7890", storageS3UriStyleHost)); - harnessTlsServerReply(testS3ServerResponse(200, "OK", "eTag:RR552", NULL)); - - harnessTlsServerExpect(testS3ServerRequest( - HTTP_VERB_POST, "/file.txt?uploadId=RR55", - "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" - "<CompleteMultipartUpload>" - "<Part><PartNumber>1</PartNumber><ETag>RR551</ETag></Part>" - "<Part><PartNumber>2</PartNumber><ETag>RR552</ETag></Part>" - "</CompleteMultipartUpload>\n", - storageS3UriStyleHost)); - harnessTlsServerReply(testS3ServerResponse(200, "OK", NULL, NULL)); - - // storageDriverExists() - // ------------------------------------------------------------------------------------------------------------------------- - // File missing - harnessTlsServerExpect(testS3ServerRequest(HTTP_VERB_HEAD, "/BOGUS", NULL, storageS3UriStyleHost)); - harnessTlsServerReply(testS3ServerResponse(404, "Not Found", NULL, NULL)); - - // File exists - harnessTlsServerExpect(testS3ServerRequest(HTTP_VERB_HEAD, "/subdir/file1.txt", NULL, storageS3UriStyleHost)); - harnessTlsServerReply(testS3ServerResponse( - 200, "OK", - "content-length:999\r\n" - "Last-Modified: Wed, 21 Oct 2015 07:28:00 GMT", - NULL)); - - // Info() - // ------------------------------------------------------------------------------------------------------------------------- - // File missing - harnessTlsServerExpect(testS3ServerRequest(HTTP_VERB_HEAD, "/BOGUS", NULL, storageS3UriStyleHost)); - harnessTlsServerReply(testS3ServerResponse(404, "Not Found", NULL, NULL)); - - // File exists - harnessTlsServerExpect(testS3ServerRequest(HTTP_VERB_HEAD, "/subdir/file1.txt", NULL, storageS3UriStyleHost)); - harnessTlsServerReply(testS3ServerResponse( - 200, "OK", - "content-length:9999\r\n" - "Last-Modified: Wed, 21 Oct 2015 07:28:00 GMT", - NULL)); - - // File exists and only checking existence - harnessTlsServerExpect(testS3ServerRequest(HTTP_VERB_HEAD, "/subdir/file2.txt", NULL, storageS3UriStyleHost)); - harnessTlsServerReply(testS3ServerResponse( - 200, "OK", - "content-length:777\r\n" - "Last-Modified: Wed, 22 Oct 2015 07:28:00 GMT", - NULL)); - - // InfoList() - // ------------------------------------------------------------------------------------------------------------------------- - harnessTlsServerExpect( - testS3ServerRequest(HTTP_VERB_GET, "/?delimiter=%2F&list-type=2&prefix=path%2Fto%2F", NULL, storageS3UriStyleHost)); - harnessTlsServerReply( - testS3ServerResponse( - 200, "OK", NULL, - "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" - "<ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">" - " <Contents>" - " <Key>path/to/test_file</Key>" - " <LastModified>2009-10-12T17:50:30.000Z</LastModified>" - " <Size>787</Size>" - " </Contents>" - " <CommonPrefixes>" - " <Prefix>path/to/test_path/</Prefix>" - " </CommonPrefixes>" - "</ListBucketResult>")); - - // storageDriverList() - // ------------------------------------------------------------------------------------------------------------------------- - // Throw errors - harnessTlsServerExpect(testS3ServerRequest(HTTP_VERB_GET, "/?delimiter=%2F&list-type=2", NULL, storageS3UriStyleHost)); - harnessTlsServerReply(testS3ServerResponse( 344, "Another bad status", NULL, NULL)); - - harnessTlsServerExpect(testS3ServerRequest(HTTP_VERB_GET, "/?delimiter=%2F&list-type=2", NULL, storageS3UriStyleHost)); - harnessTlsServerReply(testS3ServerResponse( - 344, "Another bad status with xml", NULL, + "<ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">" + " <Contents>" + " <Key>test1.txt</Key>" + " </Contents>" + "</ListBucketResult>")); + + // list files with continuation + harnessTlsServerExpect( + testS3ServerRequest(HTTP_VERB_GET, "/?delimiter=%2F&list-type=2&prefix=path%2Fto%2F", NULL, storageS3UriStyleHost)); + harnessTlsServerReply( + testS3ServerResponse( + 200, "OK", NULL, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" - "<Error>" - "<Code>SomeOtherCode</Code>" - "</Error>")); - - harnessTlsServerExpect(testS3ServerRequest(HTTP_VERB_GET, "/?delimiter=%2F&list-type=2", NULL, storageS3UriStyleHost)); - harnessTlsServerReply(testS3ServerResponse( - 403, "Forbidden", NULL, + "<ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">" + " <NextContinuationToken>1ueGcxLPRx1Tr/XYExHnhbYLgveDs2J/wm36Hy4vbOwM=</NextContinuationToken>" + " <Contents>" + " <Key>path/to/test1.txt</Key>" + " </Contents>" + " <Contents>" + " <Key>path/to/test2.txt</Key>" + " </Contents>" + " <CommonPrefixes>" + " <Prefix>path/to/path1/</Prefix>" + " </CommonPrefixes>" + "</ListBucketResult>")); + + harnessTlsServerExpect( + testS3ServerRequest( + HTTP_VERB_GET, + "/?continuation-token=1ueGcxLPRx1Tr%2FXYExHnhbYLgveDs2J%2Fwm36Hy4vbOwM%3D&delimiter=%2F&list-type=2" + "&prefix=path%2Fto%2F", + NULL, storageS3UriStyleHost)); + harnessTlsServerReply( + testS3ServerResponse( + 200, "OK", NULL, + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">" + " <Contents>" + " <Key>path/to/test3.txt</Key>" + " </Contents>" + " <CommonPrefixes>" + " <Prefix>path/to/path2/</Prefix>" + " </CommonPrefixes>" + "</ListBucketResult>")); + + // list files with expression + harnessTlsServerExpect( + testS3ServerRequest(HTTP_VERB_GET, "/?delimiter=%2F&list-type=2&prefix=path%2Fto%2Ftest", NULL, storageS3UriStyleHost)); + harnessTlsServerReply( + testS3ServerResponse( + 200, "OK", NULL, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" - "<Error>" - "<Code>RequestTimeTooSkewed</Code>" - "<Message>The difference between the request time and the current time is too large.</Message>" - "</Error>")); - harnessTlsServerExpect(testS3ServerRequest(HTTP_VERB_GET, "/?delimiter=%2F&list-type=2", NULL, storageS3UriStyleHost)); - harnessTlsServerReply(testS3ServerResponse( - 403, "Forbidden", NULL, + "<ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">" + " <Contents>" + " <Key>path/to/test1.txt</Key>" + " </Contents>" + " <Contents>" + " <Key>path/to/test2.txt</Key>" + " </Contents>" + " <Contents>" + " <Key>path/to/test3.txt</Key>" + " </Contents>" + " <CommonPrefixes>" + " <Prefix>path/to/test1.path/</Prefix>" + " </CommonPrefixes>" + " <CommonPrefixes>" + " <Prefix>path/to/test2.path/</Prefix>" + " </CommonPrefixes>" + "</ListBucketResult>")); + + // storageDriverPathRemove() + // ------------------------------------------------------------------------------------------------------------------------- + // Switch to path-style URIs + harnessTlsServerClose(); + harnessTlsServerAccept(); + + // delete files from root + harnessTlsServerExpect( + testS3ServerRequest(HTTP_VERB_GET, "/bucket/?list-type=2", NULL, storageS3UriStylePath)); + harnessTlsServerReply( + testS3ServerResponse( + 200, "OK", NULL, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" - "<Error>" - "<Code>RequestTimeTooSkewed</Code>" - "<Message>The difference between the request time and the current time is too large.</Message>" - "</Error>")); - harnessTlsServerExpect(testS3ServerRequest(HTTP_VERB_GET, "/?delimiter=%2F&list-type=2", NULL, storageS3UriStyleHost)); - harnessTlsServerReply(testS3ServerResponse( - 403, "Forbidden", NULL, + "<ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">" + " <Contents>" + " <Key>test1.txt</Key>" + " </Contents>" + " <Contents>" + " <Key>path1/xxx.zzz</Key>" + " </Contents>" + "</ListBucketResult>")); + + harnessTlsServerExpect( + testS3ServerRequest(HTTP_VERB_POST, "/bucket/?delete=", + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<Delete><Quiet>true</Quiet>" + "<Object><Key>test1.txt</Key></Object>" + "<Object><Key>path1/xxx.zzz</Key></Object>" + "</Delete>\n", + storageS3UriStylePath)); + harnessTlsServerReply( + testS3ServerResponse( + 200, "OK", NULL, "<DeleteResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"></DeleteResult>")); + + // nothing to do in empty subpath + harnessTlsServerExpect( + testS3ServerRequest(HTTP_VERB_GET, "/bucket/?list-type=2&prefix=path%2F", NULL, storageS3UriStylePath)); + harnessTlsServerReply( + testS3ServerResponse( + 200, "OK", NULL, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" - "<Error>" - "<Code>RequestTimeTooSkewed</Code>" - "<Message>The difference between the request time and the current time is too large.</Message>" - "</Error>")); - - // list a file/path in root - harnessTlsServerExpect(testS3ServerRequest(HTTP_VERB_GET, "/?delimiter=%2F&list-type=2", NULL, storageS3UriStyleHost)); - harnessTlsServerReply( - testS3ServerResponse( - 200, "OK", NULL, - "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" - "<ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">" - " <Contents>" - " <Key>test1.txt</Key>" - " </Contents>" - " <CommonPrefixes>" - " <Prefix>path1/</Prefix>" - " </CommonPrefixes>" - "</ListBucketResult>")); - - // list a file in root with expression - harnessTlsServerExpect( - testS3ServerRequest(HTTP_VERB_GET, "/?delimiter=%2F&list-type=2&prefix=test", NULL, storageS3UriStyleHost)); - harnessTlsServerReply( - testS3ServerResponse( - 200, "OK", NULL, - "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" - "<ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">" - " <Contents>" - " <Key>test1.txt</Key>" - " </Contents>" - "</ListBucketResult>")); - - // list files with continuation - harnessTlsServerExpect( - testS3ServerRequest(HTTP_VERB_GET, "/?delimiter=%2F&list-type=2&prefix=path%2Fto%2F", NULL, storageS3UriStyleHost)); - harnessTlsServerReply( - testS3ServerResponse( - 200, "OK", NULL, - "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" - "<ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">" - " <NextContinuationToken>1ueGcxLPRx1Tr/XYExHnhbYLgveDs2J/wm36Hy4vbOwM=</NextContinuationToken>" - " <Contents>" - " <Key>path/to/test1.txt</Key>" - " </Contents>" - " <Contents>" - " <Key>path/to/test2.txt</Key>" - " </Contents>" - " <CommonPrefixes>" - " <Prefix>path/to/path1/</Prefix>" - " </CommonPrefixes>" - "</ListBucketResult>")); - - harnessTlsServerExpect( - testS3ServerRequest( - HTTP_VERB_GET, - "/?continuation-token=1ueGcxLPRx1Tr%2FXYExHnhbYLgveDs2J%2Fwm36Hy4vbOwM%3D&delimiter=%2F&list-type=2" - "&prefix=path%2Fto%2F", - NULL, storageS3UriStyleHost)); - harnessTlsServerReply( - testS3ServerResponse( - 200, "OK", NULL, - "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" - "<ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">" - " <Contents>" - " <Key>path/to/test3.txt</Key>" - " </Contents>" - " <CommonPrefixes>" - " <Prefix>path/to/path2/</Prefix>" - " </CommonPrefixes>" - "</ListBucketResult>")); - - // list files with expression - harnessTlsServerExpect( - testS3ServerRequest(HTTP_VERB_GET, "/?delimiter=%2F&list-type=2&prefix=path%2Fto%2Ftest", NULL, storageS3UriStyleHost)); - harnessTlsServerReply( - testS3ServerResponse( - 200, "OK", NULL, - "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" - "<ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">" - " <Contents>" - " <Key>path/to/test1.txt</Key>" - " </Contents>" - " <Contents>" - " <Key>path/to/test2.txt</Key>" - " </Contents>" - " <Contents>" - " <Key>path/to/test3.txt</Key>" - " </Contents>" - " <CommonPrefixes>" - " <Prefix>path/to/test1.path/</Prefix>" - " </CommonPrefixes>" - " <CommonPrefixes>" - " <Prefix>path/to/test2.path/</Prefix>" - " </CommonPrefixes>" - "</ListBucketResult>")); - - // storageDriverPathRemove() - // ------------------------------------------------------------------------------------------------------------------------- - // Switch to path-style URIs - harnessTlsServerClose(); - harnessTlsServerAccept(); - - // delete files from root - harnessTlsServerExpect( - testS3ServerRequest(HTTP_VERB_GET, "/bucket/?list-type=2", NULL, storageS3UriStylePath)); - harnessTlsServerReply( - testS3ServerResponse( - 200, "OK", NULL, - "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" - "<ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">" - " <Contents>" - " <Key>test1.txt</Key>" - " </Contents>" - " <Contents>" - " <Key>path1/xxx.zzz</Key>" - " </Contents>" - "</ListBucketResult>")); - - harnessTlsServerExpect( - testS3ServerRequest(HTTP_VERB_POST, "/bucket/?delete=", - "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" - "<Delete><Quiet>true</Quiet>" - "<Object><Key>test1.txt</Key></Object>" - "<Object><Key>path1/xxx.zzz</Key></Object>" - "</Delete>\n", - storageS3UriStylePath)); - harnessTlsServerReply( - testS3ServerResponse( - 200, "OK", NULL, "<DeleteResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"></DeleteResult>")); - - // nothing to do in empty subpath - harnessTlsServerExpect( - testS3ServerRequest(HTTP_VERB_GET, "/bucket/?list-type=2&prefix=path%2F", NULL, storageS3UriStylePath)); - harnessTlsServerReply( - testS3ServerResponse( - 200, "OK", NULL, - "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" - "<ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">" - "</ListBucketResult>")); - - // delete with continuation - harnessTlsServerExpect( - testS3ServerRequest(HTTP_VERB_GET, "/bucket/?list-type=2&prefix=path%2Fto%2F", NULL, storageS3UriStylePath)); - harnessTlsServerReply( - testS3ServerResponse( - 200, "OK", NULL, - "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" - "<ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">" - " <NextContinuationToken>continue</NextContinuationToken>" - " <CommonPrefixes>" - " <Prefix>path/to/test3/</Prefix>" - " </CommonPrefixes>" - " <Contents>" - " <Key>path/to/test1.txt</Key>" - " </Contents>" - "</ListBucketResult>")); - - harnessTlsServerExpect( - testS3ServerRequest( - HTTP_VERB_GET, "/bucket/?continuation-token=continue&list-type=2&prefix=path%2Fto%2F", NULL, - storageS3UriStylePath)); - harnessTlsServerReply( - testS3ServerResponse( - 200, "OK", NULL, - "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" - "<ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">" - " <Contents>" - " <Key>path/to/test3.txt</Key>" - " </Contents>" - " <Contents>" - " <Key>path/to/test2.txt</Key>" - " </Contents>" - "</ListBucketResult>")); - - harnessTlsServerExpect( - testS3ServerRequest(HTTP_VERB_POST, "/bucket/?delete=", - "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" - "<Delete><Quiet>true</Quiet>" - "<Object><Key>path/to/test1.txt</Key></Object>" - "<Object><Key>path/to/test3.txt</Key></Object>" - "</Delete>\n", - storageS3UriStylePath)); - harnessTlsServerReply(testS3ServerResponse(200, "OK", NULL, NULL)); - - harnessTlsServerExpect( - testS3ServerRequest(HTTP_VERB_POST, "/bucket/?delete=", - "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" - "<Delete><Quiet>true</Quiet>" - "<Object><Key>path/to/test2.txt</Key></Object>" - "</Delete>\n", - storageS3UriStylePath)); - harnessTlsServerReply(testS3ServerResponse(200, "OK", NULL, NULL)); - - // delete error - harnessTlsServerExpect( - testS3ServerRequest(HTTP_VERB_GET, "/bucket/?list-type=2&prefix=path%2F", NULL, storageS3UriStylePath)); - harnessTlsServerReply( - testS3ServerResponse( - 200, "OK", NULL, - "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" - "<ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">" - " <Contents>" - " <Key>path/sample.txt</Key>" - " </Contents>" - " <Contents>" - " <Key>path/sample2.txt</Key>" - " </Contents>" - "</ListBucketResult>")); - - harnessTlsServerExpect( - testS3ServerRequest(HTTP_VERB_POST, "/bucket/?delete=", - "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" - "<Delete><Quiet>true</Quiet>" - "<Object><Key>path/sample.txt</Key></Object>" - "<Object><Key>path/sample2.txt</Key></Object>" - "</Delete>\n", + "<ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">" + "</ListBucketResult>")); + + // delete with continuation + harnessTlsServerExpect( + testS3ServerRequest(HTTP_VERB_GET, "/bucket/?list-type=2&prefix=path%2Fto%2F", NULL, storageS3UriStylePath)); + harnessTlsServerReply( + testS3ServerResponse( + 200, "OK", NULL, + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">" + " <NextContinuationToken>continue</NextContinuationToken>" + " <CommonPrefixes>" + " <Prefix>path/to/test3/</Prefix>" + " </CommonPrefixes>" + " <Contents>" + " <Key>path/to/test1.txt</Key>" + " </Contents>" + "</ListBucketResult>")); + + harnessTlsServerExpect( + testS3ServerRequest( + HTTP_VERB_GET, "/bucket/?continuation-token=continue&list-type=2&prefix=path%2Fto%2F", NULL, storageS3UriStylePath)); - harnessTlsServerReply( - testS3ServerResponse( - 200, "OK", NULL, - "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" - "<DeleteResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">" - "<Error><Key>sample2.txt</Key><Code>AccessDenied</Code><Message>Access Denied</Message></Error>" - "</DeleteResult>")); - - // storageDriverRemove() - // ------------------------------------------------------------------------------------------------------------------------- - // remove file - harnessTlsServerExpect(testS3ServerRequest(HTTP_VERB_DELETE, "/bucket/path/to/test.txt", NULL, storageS3UriStylePath)); - harnessTlsServerReply(testS3ServerResponse(204, "No Content", NULL, NULL)); + harnessTlsServerReply( + testS3ServerResponse( + 200, "OK", NULL, + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">" + " <Contents>" + " <Key>path/to/test3.txt</Key>" + " </Contents>" + " <Contents>" + " <Key>path/to/test2.txt</Key>" + " </Contents>" + "</ListBucketResult>")); + + harnessTlsServerExpect( + testS3ServerRequest(HTTP_VERB_POST, "/bucket/?delete=", + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<Delete><Quiet>true</Quiet>" + "<Object><Key>path/to/test1.txt</Key></Object>" + "<Object><Key>path/to/test3.txt</Key></Object>" + "</Delete>\n", + storageS3UriStylePath)); + harnessTlsServerReply(testS3ServerResponse(200, "OK", NULL, NULL)); + + harnessTlsServerExpect( + testS3ServerRequest(HTTP_VERB_POST, "/bucket/?delete=", + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<Delete><Quiet>true</Quiet>" + "<Object><Key>path/to/test2.txt</Key></Object>" + "</Delete>\n", + storageS3UriStylePath)); + harnessTlsServerReply(testS3ServerResponse(200, "OK", NULL, NULL)); + + // delete error + harnessTlsServerExpect( + testS3ServerRequest(HTTP_VERB_GET, "/bucket/?list-type=2&prefix=path%2F", NULL, storageS3UriStylePath)); + harnessTlsServerReply( + testS3ServerResponse( + 200, "OK", NULL, + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">" + " <Contents>" + " <Key>path/sample.txt</Key>" + " </Contents>" + " <Contents>" + " <Key>path/sample2.txt</Key>" + " </Contents>" + "</ListBucketResult>")); + + harnessTlsServerExpect( + testS3ServerRequest(HTTP_VERB_POST, "/bucket/?delete=", + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<Delete><Quiet>true</Quiet>" + "<Object><Key>path/sample.txt</Key></Object>" + "<Object><Key>path/sample2.txt</Key></Object>" + "</Delete>\n", + storageS3UriStylePath)); + harnessTlsServerReply( + testS3ServerResponse( + 200, "OK", NULL, + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<DeleteResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">" + "<Error><Key>sample2.txt</Key><Code>AccessDenied</Code><Message>Access Denied</Message></Error>" + "</DeleteResult>")); - harnessTlsServerClose(); - exit(0); - } + // storageDriverRemove() + // ------------------------------------------------------------------------------------------------------------------------- + // remove file + harnessTlsServerExpect(testS3ServerRequest(HTTP_VERB_DELETE, "/bucket/path/to/test.txt", NULL, storageS3UriStylePath)); + harnessTlsServerReply(testS3ServerResponse(204, "No Content", NULL, NULL)); + + harnessTlsServerClose(); FUNCTION_HARNESS_RESULT_VOID(); } @@ -759,240 +753,254 @@ testRun(void) // ***************************************************************************************************************************** if (testBegin("storageS3*(), StorageReadS3, and StorageWriteS3")) { - TEST_RESULT_VOID(testS3Server(), "s3 server begin"); - - Storage *s3 = storageS3New( - path, true, NULL, bucket, endPoint, storageS3UriStyleHost, region, accessKey, secretAccessKey, NULL, 16, 2, host, port, - 1000, testContainer(), NULL, NULL); - - // Coverage for noop functions - // ------------------------------------------------------------------------------------------------------------------------- - TEST_RESULT_VOID(storagePathSyncP(s3, strNew("path")), "path sync is a noop"); - - // storageS3NewRead() and StorageS3FileRead - // ------------------------------------------------------------------------------------------------------------------------- - TEST_RESULT_PTR( - storageGetP(storageNewReadP(s3, strNew("fi&le.txt"), .ignoreMissing = true)), NULL, "ignore missing file"); - TEST_ERROR( - storageGetP(storageNewReadP(s3, strNew("file.txt"))), FileMissingError, - "unable to open '/file.txt': No such file or directory"); - TEST_RESULT_STR_Z(strNewBuf(storageGetP(storageNewReadP(s3, strNew("file.txt")))), "this is a sample file", "get file"); - TEST_RESULT_STR_Z(strNewBuf(storageGetP(storageNewReadP(s3, strNew("file0.txt")))), "", "get zero-length file"); - - StorageRead *read = NULL; - TEST_ASSIGN(read, storageNewReadP(s3, strNew("file.txt"), .ignoreMissing = true), "new read file"); - TEST_RESULT_BOOL(storageReadIgnoreMissing(read), true, " check ignore missing"); - TEST_RESULT_STR_Z(storageReadName(read), "/file.txt", " check name"); - - TEST_ERROR( - ioReadOpen(storageReadIo(read)), ProtocolError, - "S3 request failed with 303: Some bad status\n" - "*** URI/Query ***:\n" - "/file.txt\n" - "*** Request Headers ***:\n" - "authorization: <redacted>\n" - "content-length: 0\n" - "host: bucket." S3_TEST_HOST "\n" - "x-amz-content-sha256: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n" - "x-amz-date: <redacted>\n" - "*** Response Headers ***:\n" - "content-length: 7\n" - "*** Response Content ***:\n" - "CONTENT") - - // storageS3NewWrite() and StorageWriteS3 - // ------------------------------------------------------------------------------------------------------------------------- - // File is written all at once - StorageWrite *write = NULL; - TEST_ASSIGN(write, storageNewWriteP(s3, strNew("file.txt")), "new write file"); - TEST_RESULT_VOID(storagePutP(write, BUFSTRDEF("ABCD")), "put file all at once"); - - TEST_RESULT_BOOL(storageWriteAtomic(write), true, "write is atomic"); - TEST_RESULT_BOOL(storageWriteCreatePath(write), true, "path will be created"); - TEST_RESULT_UINT(storageWriteModeFile(write), 0, "file mode is 0"); - TEST_RESULT_UINT(storageWriteModePath(write), 0, "path mode is 0"); - TEST_RESULT_STR_Z(storageWriteName(write), "/file.txt", "check file name"); - TEST_RESULT_BOOL(storageWriteSyncFile(write), true, "file is synced"); - TEST_RESULT_BOOL(storageWriteSyncPath(write), true, "path is synced"); - - TEST_RESULT_VOID(storageWriteS3Close(write->driver), "close file again"); - - // Zero-length file - TEST_ASSIGN(write, storageNewWriteP(s3, strNew("file.txt")), "new write file"); - TEST_RESULT_VOID(storagePutP(write, NULL), "write zero-length file"); - - // File is written in chunks with nothing left over on close - TEST_ASSIGN(write, storageNewWriteP(s3, strNew("file.txt")), "new write file"); - TEST_RESULT_VOID( - storagePutP(write, BUFSTRDEF("12345678901234567890123456789012")), - "write file in chunks -- nothing left on close"); - - // File is written in chunks with something left over on close - TEST_ASSIGN(write, storageNewWriteP(s3, strNew("file.txt")), "new write file"); - TEST_RESULT_VOID( - storagePutP(write, BUFSTRDEF("12345678901234567890")), - "write file in chunks -- something left on close"); - - // storageDriverExists() - // ------------------------------------------------------------------------------------------------------------------------- - TEST_RESULT_BOOL(storageExistsP(s3, strNew("BOGUS")), false, "file does not exist"); - TEST_RESULT_BOOL(storageExistsP(s3, strNew("subdir/file1.txt")), true, "file exists"); - - // Info() - // ------------------------------------------------------------------------------------------------------------------------- - TEST_RESULT_BOOL(storageInfoP(s3, strNew("BOGUS"), .ignoreMissing = true).exists, false, "file does not exist"); - - StorageInfo info; - TEST_ASSIGN(info, storageInfoP(s3, strNew("subdir/file1.txt")), "file exists"); - TEST_RESULT_BOOL(info.exists, true, " check exists"); - TEST_RESULT_UINT(info.type, storageTypeFile, " check type"); - TEST_RESULT_UINT(info.size, 9999, " check exists"); - TEST_RESULT_INT(info.timeModified, 1445412480, " check time"); - - TEST_TITLE("file exists and only checking existence"); - - TEST_ASSIGN(info, storageInfoP(s3, strNew("subdir/file2.txt"), .level = storageInfoLevelExists), "file exists"); - TEST_RESULT_BOOL(info.exists, true, " check exists"); - TEST_RESULT_UINT(info.type, storageTypeFile, " check type"); - TEST_RESULT_UINT(info.size, 0, " check exists"); - TEST_RESULT_INT(info.timeModified, 0, " check time"); - - // ------------------------------------------------------------------------------------------------------------------------- - TEST_TITLE("list basic level"); - - HarnessStorageInfoListCallbackData callbackData = + HARNESS_FORK_BEGIN() { - .content = strNew(""), - }; - - TEST_ERROR( - storageInfoListP(s3, strNew("/"), hrnStorageInfoListCallback, NULL, .errorOnMissing = true), - AssertError, "assertion '!param.errorOnMissing || storageFeature(this, storageFeaturePath)' failed"); - - TEST_RESULT_VOID( - storageInfoListP(s3, strNew("/path/to"), hrnStorageInfoListCallback, &callbackData), "info list files"); - TEST_RESULT_STR_Z( - callbackData.content, - "test_path {path}\n" - "test_file {file, s=787, t=1255369830}\n", - " check content"); - - // ------------------------------------------------------------------------------------------------------------------------- - TEST_TITLE("various errors"); - - TEST_ERROR( - storageListP(s3, strNew("/"), .errorOnMissing = true), AssertError, - "assertion '!param.errorOnMissing || storageFeature(this, storageFeaturePath)' failed"); - TEST_ERROR(storageListP(s3, strNew("/")), ProtocolError, - "S3 request failed with 344: Another bad status\n" - "*** URI/Query ***:\n" - "/?delimiter=%2F&list-type=2\n" - "*** Request Headers ***:\n" - "authorization: <redacted>\n" - "content-length: 0\n" - "host: bucket." S3_TEST_HOST "\n" - "x-amz-content-sha256: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n" - "x-amz-date: <redacted>"); - TEST_ERROR(storageListP(s3, strNew("/")), ProtocolError, - "S3 request failed with 344: Another bad status with xml\n" - "*** URI/Query ***:\n" - "/?delimiter=%2F&list-type=2\n" - "*** Request Headers ***:\n" - "authorization: <redacted>\n" - "content-length: 0\n" - "host: bucket." S3_TEST_HOST "\n" - "x-amz-content-sha256: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n" - "x-amz-date: <redacted>\n" - "*** Response Headers ***:\n" - "content-length: 79\n" - "*** Response Content ***:\n" - "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Error><Code>SomeOtherCode</Code></Error>"); - TEST_ERROR(storageListP(s3, strNew("/")), ProtocolError, - "S3 request failed with 403: Forbidden\n" - "*** URI/Query ***:\n" - "/?delimiter=%2F&list-type=2\n" - "*** Request Headers ***:\n" - "authorization: <redacted>\n" - "content-length: 0\n" - "host: bucket." S3_TEST_HOST "\n" - "x-amz-content-sha256: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n" - "x-amz-date: <redacted>\n" - "*** Response Headers ***:\n" - "content-length: 179\n" - "*** Response Content ***:\n" - "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Error><Code>RequestTimeTooSkewed</Code>" - "<Message>The difference between the request time and the current time is too large.</Message></Error>"); - - // ------------------------------------------------------------------------------------------------------------------------- - TEST_TITLE("list exists level"); - - callbackData.content = strNew(""); - TEST_RESULT_VOID( - storageInfoListP(s3, strNew("/"), hrnStorageInfoListCallback, &callbackData, .level = storageInfoLevelExists), - "list a file/path in root"); - TEST_RESULT_STR_Z( - callbackData.content, - "path1 {}\n" - "test1.txt {}\n", - " check content"); - - callbackData.content = strNew(""); - TEST_RESULT_VOID( - storageInfoListP( - s3, strNew("/"), hrnStorageInfoListCallback, &callbackData, .expression = strNew("^test.*$"), - .level = storageInfoLevelExists), - "list a file in root with expression"); - TEST_RESULT_STR_Z( - callbackData.content, - "test1.txt {}\n", - " check content"); - - callbackData.content = strNew(""); - TEST_RESULT_VOID( - storageInfoListP(s3, strNew("/path/to"), hrnStorageInfoListCallback, &callbackData, .level = storageInfoLevelExists), - "list files with continuation"); - TEST_RESULT_STR_Z( - callbackData.content, - "path1 {}\n" - "test1.txt {}\n" - "test2.txt {}\n" - "path2 {}\n" - "test3.txt {}\n", - " check content"); - - callbackData.content = strNew(""); - TEST_RESULT_VOID( - storageInfoListP( - s3, strNew("/path/to"), hrnStorageInfoListCallback, &callbackData, .expression = strNew("^test(1|3)"), - .level = storageInfoLevelExists), - "list files with expression"); - TEST_RESULT_STR_Z( - callbackData.content, - "test1.path {}\n" - "test1.txt {}\n" - "test3.txt {}\n", - " check content"); - - // storageDriverPathRemove() - // ------------------------------------------------------------------------------------------------------------------------- - // Switch to path-style URIs - s3 = storageS3New( - path, true, NULL, bucket, endPoint, storageS3UriStylePath, region, accessKey, secretAccessKey, NULL, 16, 2, host, port, - 1000, testContainer(), NULL, NULL); - - TEST_ERROR( - storagePathRemoveP(s3, strNew("/")), AssertError, - "assertion 'param.recurse || storageFeature(this, storageFeaturePath)' failed"); - TEST_RESULT_VOID(storagePathRemoveP(s3, strNew("/"), .recurse = true), "remove root path"); - TEST_RESULT_VOID(storagePathRemoveP(s3, strNew("/path"), .recurse = true), "nothing to do in empty subpath"); - TEST_RESULT_VOID(storagePathRemoveP(s3, strNew("/path/to"), .recurse = true), "delete with continuation"); - TEST_ERROR( - storagePathRemoveP(s3, strNew("/path"), .recurse = true), FileRemoveError, - "unable to remove file 'sample2.txt': [AccessDenied] Access Denied"); - - // storageDriverRemove() - // ------------------------------------------------------------------------------------------------------------------------- - TEST_RESULT_VOID(storageRemoveP(s3, strNew("/path/to/test.txt")), "remove file"); + HARNESS_FORK_CHILD_BEGIN(0, false) + { + TEST_RESULT_VOID(testS3Server(), "s3 server begin"); + } + HARNESS_FORK_CHILD_END(); + + HARNESS_FORK_PARENT_BEGIN() + { + Storage *s3 = storageS3New( + path, true, NULL, bucket, endPoint, storageS3UriStyleHost, region, accessKey, secretAccessKey, NULL, 16, 2, + host, port, 1000, testContainer(), NULL, NULL); + + // Coverage for noop functions + // ----------------------------------------------------------------------------------------------------------------- + TEST_RESULT_VOID(storagePathSyncP(s3, strNew("path")), "path sync is a noop"); + + // storageS3NewRead() and StorageS3FileRead + // ----------------------------------------------------------------------------------------------------------------- + TEST_RESULT_PTR( + storageGetP(storageNewReadP(s3, strNew("fi&le.txt"), .ignoreMissing = true)), NULL, "ignore missing file"); + TEST_ERROR( + storageGetP(storageNewReadP(s3, strNew("file.txt"))), FileMissingError, + "unable to open '/file.txt': No such file or directory"); + TEST_RESULT_STR_Z( + strNewBuf(storageGetP(storageNewReadP(s3, strNew("file.txt")))), "this is a sample file", "get file"); + TEST_RESULT_STR_Z(strNewBuf(storageGetP(storageNewReadP(s3, strNew("file0.txt")))), "", "get zero-length file"); + + StorageRead *read = NULL; + TEST_ASSIGN(read, storageNewReadP(s3, strNew("file.txt"), .ignoreMissing = true), "new read file"); + TEST_RESULT_BOOL(storageReadIgnoreMissing(read), true, " check ignore missing"); + TEST_RESULT_STR_Z(storageReadName(read), "/file.txt", " check name"); + + TEST_ERROR( + ioReadOpen(storageReadIo(read)), ProtocolError, + "S3 request failed with 303: Some bad status\n" + "*** URI/Query ***:\n" + "/file.txt\n" + "*** Request Headers ***:\n" + "authorization: <redacted>\n" + "content-length: 0\n" + "host: bucket." S3_TEST_HOST "\n" + "x-amz-content-sha256: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n" + "x-amz-date: <redacted>\n" + "*** Response Headers ***:\n" + "content-length: 7\n" + "*** Response Content ***:\n" + "CONTENT") + + // storageS3NewWrite() and StorageWriteS3 + // ----------------------------------------------------------------------------------------------------------------- + // File is written all at once + StorageWrite *write = NULL; + TEST_ASSIGN(write, storageNewWriteP(s3, strNew("file.txt")), "new write file"); + TEST_RESULT_VOID(storagePutP(write, BUFSTRDEF("ABCD")), "put file all at once"); + + TEST_RESULT_BOOL(storageWriteAtomic(write), true, "write is atomic"); + TEST_RESULT_BOOL(storageWriteCreatePath(write), true, "path will be created"); + TEST_RESULT_UINT(storageWriteModeFile(write), 0, "file mode is 0"); + TEST_RESULT_UINT(storageWriteModePath(write), 0, "path mode is 0"); + TEST_RESULT_STR_Z(storageWriteName(write), "/file.txt", "check file name"); + TEST_RESULT_BOOL(storageWriteSyncFile(write), true, "file is synced"); + TEST_RESULT_BOOL(storageWriteSyncPath(write), true, "path is synced"); + + TEST_RESULT_VOID(storageWriteS3Close(write->driver), "close file again"); + + // Zero-length file + TEST_ASSIGN(write, storageNewWriteP(s3, strNew("file.txt")), "new write file"); + TEST_RESULT_VOID(storagePutP(write, NULL), "write zero-length file"); + + // File is written in chunks with nothing left over on close + TEST_ASSIGN(write, storageNewWriteP(s3, strNew("file.txt")), "new write file"); + TEST_RESULT_VOID( + storagePutP(write, BUFSTRDEF("12345678901234567890123456789012")), + "write file in chunks -- nothing left on close"); + + // File is written in chunks with something left over on close + TEST_ASSIGN(write, storageNewWriteP(s3, strNew("file.txt")), "new write file"); + TEST_RESULT_VOID( + storagePutP(write, BUFSTRDEF("12345678901234567890")), + "write file in chunks -- something left on close"); + + // storageDriverExists() + // ----------------------------------------------------------------------------------------------------------------- + TEST_RESULT_BOOL(storageExistsP(s3, strNew("BOGUS")), false, "file does not exist"); + TEST_RESULT_BOOL(storageExistsP(s3, strNew("subdir/file1.txt")), true, "file exists"); + + // Info() + // ----------------------------------------------------------------------------------------------------------------- + TEST_RESULT_BOOL(storageInfoP(s3, strNew("BOGUS"), .ignoreMissing = true).exists, false, "file does not exist"); + + StorageInfo info; + TEST_ASSIGN(info, storageInfoP(s3, strNew("subdir/file1.txt")), "file exists"); + TEST_RESULT_BOOL(info.exists, true, " check exists"); + TEST_RESULT_UINT(info.type, storageTypeFile, " check type"); + TEST_RESULT_UINT(info.size, 9999, " check exists"); + TEST_RESULT_INT(info.timeModified, 1445412480, " check time"); + + TEST_TITLE("file exists and only checking existence"); + + TEST_ASSIGN(info, storageInfoP(s3, strNew("subdir/file2.txt"), .level = storageInfoLevelExists), "file exists"); + TEST_RESULT_BOOL(info.exists, true, " check exists"); + TEST_RESULT_UINT(info.type, storageTypeFile, " check type"); + TEST_RESULT_UINT(info.size, 0, " check exists"); + TEST_RESULT_INT(info.timeModified, 0, " check time"); + + // ----------------------------------------------------------------------------------------------------------------- + TEST_TITLE("list basic level"); + + HarnessStorageInfoListCallbackData callbackData = + { + .content = strNew(""), + }; + + TEST_ERROR( + storageInfoListP(s3, strNew("/"), hrnStorageInfoListCallback, NULL, .errorOnMissing = true), + AssertError, "assertion '!param.errorOnMissing || storageFeature(this, storageFeaturePath)' failed"); + + TEST_RESULT_VOID( + storageInfoListP(s3, strNew("/path/to"), hrnStorageInfoListCallback, &callbackData), "info list files"); + TEST_RESULT_STR_Z( + callbackData.content, + "test_path {path}\n" + "test_file {file, s=787, t=1255369830}\n", + " check content"); + + // ----------------------------------------------------------------------------------------------------------------- + TEST_TITLE("various errors"); + + TEST_ERROR( + storageListP(s3, strNew("/"), .errorOnMissing = true), AssertError, + "assertion '!param.errorOnMissing || storageFeature(this, storageFeaturePath)' failed"); + TEST_ERROR(storageListP(s3, strNew("/")), ProtocolError, + "S3 request failed with 344: Another bad status\n" + "*** URI/Query ***:\n" + "/?delimiter=%2F&list-type=2\n" + "*** Request Headers ***:\n" + "authorization: <redacted>\n" + "content-length: 0\n" + "host: bucket." S3_TEST_HOST "\n" + "x-amz-content-sha256: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n" + "x-amz-date: <redacted>"); + TEST_ERROR(storageListP(s3, strNew("/")), ProtocolError, + "S3 request failed with 344: Another bad status with xml\n" + "*** URI/Query ***:\n" + "/?delimiter=%2F&list-type=2\n" + "*** Request Headers ***:\n" + "authorization: <redacted>\n" + "content-length: 0\n" + "host: bucket." S3_TEST_HOST "\n" + "x-amz-content-sha256: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n" + "x-amz-date: <redacted>\n" + "*** Response Headers ***:\n" + "content-length: 79\n" + "*** Response Content ***:\n" + "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Error><Code>SomeOtherCode</Code></Error>"); + TEST_ERROR(storageListP(s3, strNew("/")), ProtocolError, + "S3 request failed with 403: Forbidden\n" + "*** URI/Query ***:\n" + "/?delimiter=%2F&list-type=2\n" + "*** Request Headers ***:\n" + "authorization: <redacted>\n" + "content-length: 0\n" + "host: bucket." S3_TEST_HOST "\n" + "x-amz-content-sha256: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n" + "x-amz-date: <redacted>\n" + "*** Response Headers ***:\n" + "content-length: 179\n" + "*** Response Content ***:\n" + "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Error><Code>RequestTimeTooSkewed</Code>" + "<Message>The difference between the request time and the current time is too large.</Message></Error>"); + + // ----------------------------------------------------------------------------------------------------------------- + TEST_TITLE("list exists level"); + + callbackData.content = strNew(""); + TEST_RESULT_VOID( + storageInfoListP(s3, strNew("/"), hrnStorageInfoListCallback, &callbackData, .level = storageInfoLevelExists), + "list a file/path in root"); + TEST_RESULT_STR_Z( + callbackData.content, + "path1 {}\n" + "test1.txt {}\n", + " check content"); + + callbackData.content = strNew(""); + TEST_RESULT_VOID( + storageInfoListP( + s3, strNew("/"), hrnStorageInfoListCallback, &callbackData, .expression = strNew("^test.*$"), + .level = storageInfoLevelExists), + "list a file in root with expression"); + TEST_RESULT_STR_Z( + callbackData.content, + "test1.txt {}\n", + " check content"); + + callbackData.content = strNew(""); + TEST_RESULT_VOID( + storageInfoListP( + s3, strNew("/path/to"), hrnStorageInfoListCallback, &callbackData, .level = storageInfoLevelExists), + "list files with continuation"); + TEST_RESULT_STR_Z( + callbackData.content, + "path1 {}\n" + "test1.txt {}\n" + "test2.txt {}\n" + "path2 {}\n" + "test3.txt {}\n", + " check content"); + + callbackData.content = strNew(""); + TEST_RESULT_VOID( + storageInfoListP( + s3, strNew("/path/to"), hrnStorageInfoListCallback, &callbackData, .expression = strNew("^test(1|3)"), + .level = storageInfoLevelExists), + "list files with expression"); + TEST_RESULT_STR_Z( + callbackData.content, + "test1.path {}\n" + "test1.txt {}\n" + "test3.txt {}\n", + " check content"); + + // storageDriverPathRemove() + // ----------------------------------------------------------------------------------------------------------------- + // Switch to path-style URIs + s3 = storageS3New( + path, true, NULL, bucket, endPoint, storageS3UriStylePath, region, accessKey, secretAccessKey, NULL, 16, 2, + host, port, 1000, testContainer(), NULL, NULL); + + TEST_ERROR( + storagePathRemoveP(s3, strNew("/")), AssertError, + "assertion 'param.recurse || storageFeature(this, storageFeaturePath)' failed"); + TEST_RESULT_VOID(storagePathRemoveP(s3, strNew("/"), .recurse = true), "remove root path"); + TEST_RESULT_VOID(storagePathRemoveP(s3, strNew("/path"), .recurse = true), "nothing to do in empty subpath"); + TEST_RESULT_VOID(storagePathRemoveP(s3, strNew("/path/to"), .recurse = true), "delete with continuation"); + TEST_ERROR( + storagePathRemoveP(s3, strNew("/path"), .recurse = true), FileRemoveError, + "unable to remove file 'sample2.txt': [AccessDenied] Access Denied"); + + // storageDriverRemove() + // ----------------------------------------------------------------------------------------------------------------- + TEST_RESULT_VOID(storageRemoveP(s3, strNew("/path/to/test.txt")), "remove file"); + } + HARNESS_FORK_PARENT_END(); + } + HARNESS_FORK_END(); } FUNCTION_HARNESS_RESULT_VOID(); [parent: 42246401b880]