Skip to content

Commit

Permalink
Added unit test for gzip files via file descriptor.
Browse files Browse the repository at this point in the history
  • Loading branch information
jhelmold committed Oct 21, 2024
1 parent 85c071c commit b56e871
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion util/compressutils_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <cgreen/cgreen.h>
#include <cgreen/mocks.h>
#include <fcntl.h>

Describe (compressutils);
BeforeEach (compressutils)
Expand Down Expand Up @@ -79,6 +80,30 @@ Ensure (compressutils, can_uncompress_using_reader)
assert_that (fclose (stream), is_equal_to (0));
}

Ensure (compressutils, can_uncompress_using_fd_reader)
{
const char *testdata = "TEST-12345-12345-TEST";
size_t compressed_len;
char *compressed =
gvm_compress_gzipheader (testdata, strlen (testdata) + 1, &compressed_len);

char compressed_filename[35] = "/tmp/gvm_gzip_test_XXXXXX";
int compressed_fd = mkstemp (compressed_filename);
write (compressed_fd, compressed, compressed_len);
close (compressed_fd);

compressed_fd = open (compressed_filename, O_RDONLY);

FILE *stream = gvm_gzip_open_file_reader_fd (compressed_fd);
assert_that (stream, is_not_null);

gchar *uncompressed = g_malloc0 (30);
fread (uncompressed, 1, 30, stream);
assert_that (uncompressed, is_equal_to_string (testdata));

assert_that (fclose (stream), is_equal_to (0));
}

/* Test suite. */
int
main (int argc, char **argv)
Expand All @@ -92,9 +117,10 @@ main (int argc, char **argv)
add_test_with_context (suite, compressutils,
can_compress_and_uncompress_with_header);
add_test_with_context (suite, compressutils, can_uncompress_using_reader);
add_test_with_context (suite, compressutils, can_uncompress_using_fd_reader);

if (argc > 1)
return run_single_test (suite, argv[1], create_text_reporter ());

return run_test_suite (suite, create_text_reporter ());
}
}

0 comments on commit b56e871

Please sign in to comment.