Greenbone Vulnerability Management Libraries 22.32.0
compressutils_tests.c File Reference
#include "compressutils.c"
#include <cgreen/cgreen.h>
#include <cgreen/mocks.h>
#include <fcntl.h>
Include dependency graph for compressutils_tests.c:

Go to the source code of this file.

Functions

 Describe (compressutils)
 BeforeEach (compressutils)
 AfterEach (compressutils)
 Ensure (compressutils, can_compress_and_uncompress_without_header)
 Ensure (compressutils, can_compress_and_uncompress_with_header)
 Ensure (compressutils, can_uncompress_using_reader)
 Ensure (compressutils, can_uncompress_using_fd_reader)
int main (int argc, char **argv)

Function Documentation

◆ AfterEach()

AfterEach ( compressutils )

Definition at line 17 of file compressutils_tests.c.

18{
19}

◆ BeforeEach()

BeforeEach ( compressutils )

Definition at line 13 of file compressutils_tests.c.

14{
15}

◆ Describe()

Describe ( compressutils )

◆ Ensure() [1/4]

Ensure ( compressutils ,
can_compress_and_uncompress_with_header  )

Definition at line 41 of file compressutils_tests.c.

42{
43 const char *testdata = "TEST-12345-12345-TEST";
44
45 unsigned long compressed_len;
46 char *compressed =
47 gvm_compress_gzipheader (testdata, strlen (testdata) + 1, &compressed_len);
48 assert_that (compressed_len, is_greater_than (0));
49 assert_that (compressed, is_not_null);
50 assert_that (compressed, is_not_equal_to_string (testdata));
51 // Check for gzip magic number and deflate compression mode byte
52 assert_that (compressed[0], is_equal_to ((char) 0x1f));
53 assert_that (compressed[1], is_equal_to ((char) 0x8b));
54 assert_that (compressed[2], is_equal_to (8));
55
56 unsigned long uncompressed_len;
57 char *uncompressed =
58 gvm_uncompress (compressed, compressed_len, &uncompressed_len);
59 assert_that (uncompressed_len, is_equal_to (strlen (testdata) + 1));
60 assert_that (uncompressed, is_equal_to_string (testdata));
61 g_free (compressed);
62 g_free (uncompressed);
63}
void * gvm_uncompress(const void *src, unsigned long srclen, unsigned long *dstlen)
Uncompresses data in src buffer.
void * gvm_compress_gzipheader(const void *src, unsigned long srclen, unsigned long *dstlen)
Compresses data in src buffer, gzip format compatible.

References gvm_compress_gzipheader(), and gvm_uncompress().

Here is the call graph for this function:

◆ Ensure() [2/4]

Ensure ( compressutils ,
can_compress_and_uncompress_without_header  )

Definition at line 21 of file compressutils_tests.c.

22{
23 const char *testdata = "TEST-12345-12345-TEST";
24
25 unsigned long compressed_len = 0;
26 char *compressed =
27 gvm_compress (testdata, strlen (testdata) + 1, &compressed_len);
28 assert_that (compressed_len, is_greater_than (0));
29 assert_that (compressed, is_not_null);
30 assert_that (compressed, is_not_equal_to_string (testdata));
31
32 unsigned long uncompressed_len;
33 char *uncompressed =
34 gvm_uncompress (compressed, compressed_len, &uncompressed_len);
35 assert_that (uncompressed_len, is_equal_to (strlen (testdata) + 1));
36 assert_that (uncompressed, is_equal_to_string (testdata));
37 g_free (compressed);
38 g_free (uncompressed);
39}
void * gvm_compress(const void *src, unsigned long srclen, unsigned long *dstlen)
Compresses data in src buffer.

References gvm_compress(), and gvm_uncompress().

Here is the call graph for this function:

◆ Ensure() [3/4]

Ensure ( compressutils ,
can_uncompress_using_fd_reader  )

Definition at line 89 of file compressutils_tests.c.

90{
91 const char *testdata = "TEST-12345-12345-TEST";
92 unsigned long compressed_len;
93 char *compressed =
94 gvm_compress_gzipheader (testdata, strlen (testdata) + 1, &compressed_len);
95
96 char compressed_filename[35] = "/tmp/gvm_gzip_test_XXXXXX";
97 int compressed_fd = mkstemp (compressed_filename);
98 (void) !write (compressed_fd, compressed, compressed_len);
99 close (compressed_fd);
100 g_free (compressed);
101
102 compressed_fd = open (compressed_filename, O_RDONLY);
103
104 FILE *stream = gvm_gzip_open_file_reader_fd (compressed_fd);
105 assert_that (stream, is_not_null);
106
107 gchar *uncompressed = g_malloc0 (30);
108 (void) !fread (uncompressed, 1, 30, stream);
109 assert_that (uncompressed, is_equal_to_string (testdata));
110 g_free (uncompressed);
111
112 assert_that (fclose (stream), is_equal_to (0));
113}
FILE * gvm_gzip_open_file_reader_fd(int fd)
Opens a gzip file as a FILE* stream for reading and decompression.

References gvm_compress_gzipheader(), and gvm_gzip_open_file_reader_fd().

Here is the call graph for this function:

◆ Ensure() [4/4]

Ensure ( compressutils ,
can_uncompress_using_reader  )

Definition at line 65 of file compressutils_tests.c.

66{
67 const char *testdata = "TEST-12345-12345-TEST";
68 unsigned long compressed_len;
69 char *compressed =
70 gvm_compress_gzipheader (testdata, strlen (testdata) + 1, &compressed_len);
71
72 char compressed_filename[35] = "/tmp/gvm_gzip_test_XXXXXX";
73 int compressed_fd = mkstemp (compressed_filename);
74 (void) !write (compressed_fd, compressed, compressed_len);
75 close (compressed_fd);
76 g_free (compressed);
77
78 FILE *stream = gvm_gzip_open_file_reader (compressed_filename);
79 assert_that (stream, is_not_null);
80
81 gchar *uncompressed = g_malloc0 (30);
82 (void) !fread (uncompressed, 1, 30, stream);
83 assert_that (uncompressed, is_equal_to_string (testdata));
84 g_free (uncompressed);
85
86 assert_that (fclose (stream), is_equal_to (0));
87}
FILE * gvm_gzip_open_file_reader(const char *path)
Opens a gzip file as a FILE* stream for reading and decompression.

References gvm_compress_gzipheader(), and gvm_gzip_open_file_reader().

Here is the call graph for this function:

◆ main()

int main ( int argc,
char ** argv )

Definition at line 117 of file compressutils_tests.c.

118{
119 int ret;
120 TestSuite *suite;
121
122 suite = create_test_suite ();
123
124 add_test_with_context (suite, compressutils,
125 can_compress_and_uncompress_without_header);
126 add_test_with_context (suite, compressutils,
127 can_compress_and_uncompress_with_header);
128 add_test_with_context (suite, compressutils, can_uncompress_using_reader);
129 add_test_with_context (suite, compressutils, can_uncompress_using_fd_reader);
130
131 if (argc > 1)
132 ret = run_single_test (suite, argv[1], create_text_reporter ());
133 else
134 ret = run_test_suite (suite, create_text_reporter ());
135
136 destroy_test_suite (suite);
137
138 return ret;
139}