#include #include #include #include #include #define NO_MAIN 1 #include "gzip_nop.c" #define READ_PIPE 0 #define WRITE_PIPE 1 // Tests that passing a randomly-generated byte stream through the "gzip" // function, then passing the gzipped output back through the "gunzip" program, // results in the same randomly-generated byte stream. // // This test uses Windows-specific API calls, and is not portable to other // systems. int main(void) { // Generate original random byte stream uint8_t input_buffer[UINT16_MAX + 256] = {0}; for (size_t i = 0; i < sizeof(input_buffer); i++) { input_buffer[i] = rand(); } int input_pipe[2]; int gzipped_pipe[2]; int output_pipe[2]; assert(_pipe(input_pipe, sizeof(input_buffer) + 256, _O_BINARY) != -1); assert(_pipe(gzipped_pipe, sizeof(input_buffer) + 256, _O_BINARY) != -1); assert(_pipe(output_pipe, sizeof(input_buffer) + 256, _O_BINARY) != -1); assert(write(input_pipe[WRITE_PIPE], input_buffer, sizeof(input_buffer)) == sizeof(input_buffer)); assert(close(input_pipe[WRITE_PIPE]) != -1); FILE *input_stream = fdopen(input_pipe[READ_PIPE], "rb"); assert(input_stream != NULL); FILE *gzipped_stream = fdopen(gzipped_pipe[WRITE_PIPE], "wb"); assert(gzipped_stream != NULL); // Apply GZIP to original randomly-generated byte stream gzip(input_stream, gzipped_stream); assert(fclose(gzipped_stream) != EOF); // Run external gunzip program to decompress the gzipped byte stream char command_buffer[256]; int len = snprintf(command_buffer, sizeof(command_buffer), "gzip --decompress --stdout <&%d >&%d", gzipped_pipe[READ_PIPE], output_pipe[WRITE_PIPE]); assert(len > 0 && (size_t)len < sizeof(command_buffer)); assert(system(command_buffer) == EXIT_SUCCESS); uint8_t output_buffer[sizeof(input_buffer) + 256] = {0}; len = read(output_pipe[READ_PIPE], output_buffer, sizeof(output_buffer)); // Assert that the decompressed byte stream is identical to the original // randomly-generated byte stream int got = len, want = sizeof(input_buffer); if (got != want) { fprintf(stderr, "Output length = %d, want %d", got, want); exit(EXIT_FAILURE); } for (int i = 0; i < len; i++) { uint8_t got = input_buffer[i], want = output_buffer[i]; if (got != want) { fprintf(stderr, "Output[%d] = %x, want %x", i, got, want); exit(EXIT_FAILURE); } } return EXIT_SUCCESS; }