Greenbone Vulnerability Management Libraries 22.32.0
streamvalidator_tests.c File Reference
#include "streamvalidator.h"
#include <assert.h>
#include <cgreen/cgreen.h>
#include <cgreen/mocks.h>
Include dependency graph for streamvalidator_tests.c:

Go to the source code of this file.

Macros

#define VALID_DATA   "This should be valid...."
#define TOO_SHORT_DATA   "This is too short!"
#define TOO_LONG_DATA   "This text is longer than expected!"
#define INVALID_DATA   "This shouldn't be valid!"
#define VALID_DATA_HASH    "sha256:4ae8f10c9e9551173520b7a675e9caba163007edf04dbbd06022bf61ad3fe4fb"

Functions

 Describe (streamvalidator)
 BeforeEach (streamvalidator)
 AfterEach (streamvalidator)
 Ensure (streamvalidator, accepts_valid_data)
 Ensure (streamvalidator, accepts_valid_data_after_multiple_writes)
 Ensure (streamvalidator, accepts_valid_data_after_rewind)
 Ensure (streamvalidator, rejects_too_long_data)
 Ensure (streamvalidator, rejects_too_short_data)
 Ensure (streamvalidator, rejects_hash_mismatch)
 Ensure (streamvalidator, init_rejects_empty_hash)
 Ensure (streamvalidator, init_rejects_invalid_syntax_hashes)
 Ensure (streamvalidator, init_rejects_invalid_algo_hashes)
 Ensure (streamvalidator, init_rejects_invalid_value_hashes)
 Ensure (streamvalidator, accepts_valid_checksum_only_without_size)
 Ensure (streamvalidator, rejects_checksum_with_extra_colon_suffix_as_invalid_hex)
 Ensure (streamvalidator, enforces_size_from_arg_when_no_suffix)
 Ensure (streamvalidator, checksum_only_extra_bytes_yield_hash_mismatch)
 Ensure (streamvalidator, init_rejects_invalid_syntax_algo_and_value)
 Ensure (streamvalidator, return_str_ok_is_null)
 Ensure (streamvalidator, return_str_maps_known_codes)
 Ensure (streamvalidator, return_str_unknown_code_yields_unknown_error)
 Ensure (streamvalidator, wrapper_checksum_only_accepts_valid_data)
 Ensure (streamvalidator, wrapper_checksum_only_does_not_enforce_size)
 Ensure (streamvalidator, wrapper_rejects_invalid_hash_syntax)
int main (int argc, char **argv)

Macro Definition Documentation

◆ INVALID_DATA

#define INVALID_DATA   "This shouldn't be valid!"

Definition at line 15 of file streamvalidator_tests.c.

Referenced by Ensure().

◆ TOO_LONG_DATA

#define TOO_LONG_DATA   "This text is longer than expected!"

Definition at line 14 of file streamvalidator_tests.c.

Referenced by Ensure(), Ensure(), Ensure(), and Ensure().

◆ TOO_SHORT_DATA

#define TOO_SHORT_DATA   "This is too short!"

Definition at line 13 of file streamvalidator_tests.c.

Referenced by Ensure(), and Ensure().

◆ VALID_DATA

#define VALID_DATA   "This should be valid...."

Definition at line 12 of file streamvalidator_tests.c.

Referenced by Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), and Ensure().

◆ VALID_DATA_HASH

#define VALID_DATA_HASH    "sha256:4ae8f10c9e9551173520b7a675e9caba163007edf04dbbd06022bf61ad3fe4fb"

Definition at line 16 of file streamvalidator_tests.c.

16#define VALID_DATA_HASH \
17 "sha256:4ae8f10c9e9551173520b7a675e9caba163007edf04dbbd06022bf61ad3fe4fb"

Referenced by Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), and Ensure().

Function Documentation

◆ AfterEach()

AfterEach ( streamvalidator )

Definition at line 23 of file streamvalidator_tests.c.

24{
25}

◆ BeforeEach()

BeforeEach ( streamvalidator )

Definition at line 20 of file streamvalidator_tests.c.

21{
22}

◆ Describe()

Describe ( streamvalidator )

◆ Ensure() [1/21]

Ensure ( streamvalidator ,
accepts_valid_checksum_only_without_size  )

Definition at line 188 of file streamvalidator_tests.c.

189{
190 gvm_stream_validator_t validator = NULL;
191
195
196 assert_equal (
197 gvm_stream_validator_write (validator, VALID_DATA, strlen (VALID_DATA)),
199
200 assert_equal (gvm_stream_validator_end (validator), GVM_STREAM_VALIDATOR_OK);
201
202 gvm_stream_validator_free (validator);
203}
void gvm_stream_validator_free(gvm_stream_validator_t validator)
Free a stream validator and all of its fields.
gvm_stream_validator_return_t gvm_stream_validator_with_size_new(const char *expected_hash_str, size_t expected_size, gvm_stream_validator_t *validator_out)
Allocate and initialize a new data stream validator.
gvm_stream_validator_return_t gvm_stream_validator_write(gvm_stream_validator_t validator, const char *data, size_t length)
Write data to a validator, updating the hash state and current size.
gvm_stream_validator_return_t gvm_stream_validator_end(gvm_stream_validator_t validator)
Signal the end of data input into a validator and produce the result of the validation.
#define GVM_STREAM_VALIDATOR_NO_SIZE
struct gvm_stream_validator * gvm_stream_validator_t
Pointer to an opaque stream validator data structure.
@ GVM_STREAM_VALIDATOR_OK
#define VALID_DATA
#define VALID_DATA_HASH

References gvm_stream_validator_end(), gvm_stream_validator_free(), GVM_STREAM_VALIDATOR_NO_SIZE, GVM_STREAM_VALIDATOR_OK, gvm_stream_validator_with_size_new(), gvm_stream_validator_write(), VALID_DATA, and VALID_DATA_HASH.

Here is the call graph for this function:

◆ Ensure() [2/21]

Ensure ( streamvalidator ,
accepts_valid_data  )

Definition at line 27 of file streamvalidator_tests.c.

28{
29 gvm_stream_validator_t validator = NULL;
30
32 VALID_DATA_HASH, strlen (VALID_DATA), &validator),
34 assert_equal (
37 assert_equal (gvm_stream_validator_end (validator), GVM_STREAM_VALIDATOR_OK);
38
39 gvm_stream_validator_free (validator);
40}

References gvm_stream_validator_end(), gvm_stream_validator_free(), GVM_STREAM_VALIDATOR_OK, gvm_stream_validator_with_size_new(), gvm_stream_validator_write(), VALID_DATA, and VALID_DATA_HASH.

Here is the call graph for this function:

◆ Ensure() [3/21]

Ensure ( streamvalidator ,
accepts_valid_data_after_multiple_writes  )

Definition at line 42 of file streamvalidator_tests.c.

43{
44 gvm_stream_validator_t validator = NULL;
45
47 VALID_DATA_HASH, strlen (VALID_DATA), &validator),
49 assert_equal (gvm_stream_validator_write (validator, VALID_DATA, 5),
51 assert_equal (gvm_stream_validator_write (validator, VALID_DATA + 5, 5),
53 assert_equal (gvm_stream_validator_write (validator, VALID_DATA + 10,
54 strlen (VALID_DATA) - 10),
56 assert_equal (gvm_stream_validator_end (validator), GVM_STREAM_VALIDATOR_OK);
57
58 gvm_stream_validator_free (validator);
59}

References gvm_stream_validator_end(), gvm_stream_validator_free(), GVM_STREAM_VALIDATOR_OK, gvm_stream_validator_with_size_new(), gvm_stream_validator_write(), VALID_DATA, and VALID_DATA_HASH.

Here is the call graph for this function:

◆ Ensure() [4/21]

Ensure ( streamvalidator ,
accepts_valid_data_after_rewind  )

Definition at line 61 of file streamvalidator_tests.c.

62{
63 gvm_stream_validator_t validator = NULL;
64
66 VALID_DATA_HASH, strlen (VALID_DATA), &validator),
68 assert_equal (gvm_stream_validator_write (validator, TOO_SHORT_DATA,
69 strlen (TOO_SHORT_DATA)),
72 assert_equal (
75 assert_equal (gvm_stream_validator_end (validator), GVM_STREAM_VALIDATOR_OK);
76
77 gvm_stream_validator_free (validator);
78}
void gvm_stream_validator_rewind(gvm_stream_validator_t validator)
Rewind the validation state of a stream validator while keeping the expected hash and data size.
#define TOO_SHORT_DATA

References gvm_stream_validator_end(), gvm_stream_validator_free(), GVM_STREAM_VALIDATOR_OK, gvm_stream_validator_rewind(), gvm_stream_validator_with_size_new(), gvm_stream_validator_write(), TOO_SHORT_DATA, VALID_DATA, and VALID_DATA_HASH.

Here is the call graph for this function:

◆ Ensure() [5/21]

Ensure ( streamvalidator ,
checksum_only_extra_bytes_yield_hash_mismatch  )

◆ Ensure() [6/21]

Ensure ( streamvalidator ,
enforces_size_from_arg_when_no_suffix  )

Definition at line 223 of file streamvalidator_tests.c.

224{
225 gvm_stream_validator_t validator = NULL;
226
228 VALID_DATA_HASH, strlen (VALID_DATA), &validator),
230 assert_equal (
231 gvm_stream_validator_write (validator, VALID_DATA, strlen (VALID_DATA) - 1),
233 assert_equal (gvm_stream_validator_end (validator),
235 gvm_stream_validator_free (validator);
236
238 VALID_DATA_HASH, strlen (VALID_DATA), &validator),
240 assert_equal (gvm_stream_validator_write (validator, TOO_LONG_DATA,
241 strlen (TOO_LONG_DATA)),
243 assert_not_equal (gvm_stream_validator_end (validator),
245 gvm_stream_validator_free (validator);
246
248 VALID_DATA_HASH, strlen (VALID_DATA), &validator),
250 assert_equal (
251 gvm_stream_validator_write (validator, VALID_DATA, strlen (VALID_DATA)),
253 assert_equal (gvm_stream_validator_end (validator), GVM_STREAM_VALIDATOR_OK);
254 gvm_stream_validator_free (validator);
255}
@ GVM_STREAM_VALIDATOR_DATA_TOO_SHORT
@ GVM_STREAM_VALIDATOR_DATA_TOO_LONG

References GVM_STREAM_VALIDATOR_DATA_TOO_LONG, GVM_STREAM_VALIDATOR_DATA_TOO_SHORT, gvm_stream_validator_end(), gvm_stream_validator_free(), GVM_STREAM_VALIDATOR_OK, gvm_stream_validator_with_size_new(), gvm_stream_validator_write(), TOO_LONG_DATA, VALID_DATA, and VALID_DATA_HASH.

Here is the call graph for this function:

◆ Ensure() [7/21]

Ensure ( streamvalidator ,
init_rejects_empty_hash  )

Definition at line 128 of file streamvalidator_tests.c.

129{
130 gvm_stream_validator_t validator = NULL;
131
132 assert_equal (gvm_stream_validator_with_size_new ("", 123, &validator),
134 assert_equal (validator, NULL);
135}
@ GVM_STREAM_VALIDATOR_INVALID_HASH_SYNTAX

References GVM_STREAM_VALIDATOR_INVALID_HASH_SYNTAX, and gvm_stream_validator_with_size_new().

Here is the call graph for this function:

◆ Ensure() [8/21]

Ensure ( streamvalidator ,
init_rejects_invalid_algo_hashes  )

Definition at line 150 of file streamvalidator_tests.c.

151{
152 gvm_stream_validator_t validator = NULL;
153
154 assert_equal (gvm_stream_validator_with_size_new (":0123", 123, &validator),
156 assert_equal (validator, NULL);
157
158 assert_equal (
159 gvm_stream_validator_with_size_new ("xyz:0123", 123, &validator),
161 assert_equal (validator, NULL);
162}
@ GVM_STREAM_VALIDATOR_INVALID_HASH_ALGORITHM

References GVM_STREAM_VALIDATOR_INVALID_HASH_ALGORITHM, and gvm_stream_validator_with_size_new().

Here is the call graph for this function:

◆ Ensure() [9/21]

Ensure ( streamvalidator ,
init_rejects_invalid_syntax_algo_and_value  )

Definition at line 275 of file streamvalidator_tests.c.

276{
277 gvm_stream_validator_t validator = NULL;
278
279 assert_equal (gvm_stream_validator_with_size_new ("0123", 123, &validator),
281 assert_equal (validator, NULL);
282 assert_equal (gvm_stream_validator_with_size_new ("sha256", 123, &validator),
284 assert_equal (validator, NULL);
285
286 assert_equal (gvm_stream_validator_with_size_new (":0123", 123, &validator),
288 assert_equal (validator, NULL);
289 assert_equal (
290 gvm_stream_validator_with_size_new ("xyz:0123", 123, &validator),
292 assert_equal (validator, NULL);
293
294 assert_equal (gvm_stream_validator_with_size_new ("sha256:", 123, &validator),
296 assert_equal (validator, NULL);
297 assert_equal (
298 gvm_stream_validator_with_size_new ("sha256:xyz", 123, &validator),
300 assert_equal (validator, NULL);
301 assert_equal (
302 gvm_stream_validator_with_size_new ("sha256:123", 123, &validator),
304 assert_equal (validator, NULL);
305 assert_equal (
306 gvm_stream_validator_with_size_new ("sha256:0123ab", 123, &validator),
308 assert_equal (validator, NULL);
309
310 assert_equal (
312 VALID_DATA_HASH ":12abc", GVM_STREAM_VALIDATOR_NO_SIZE, &validator),
314 assert_equal (validator, NULL);
315 assert_equal (
319 assert_equal (validator, NULL);
320}
@ GVM_STREAM_VALIDATOR_INVALID_HASH_VALUE

References GVM_STREAM_VALIDATOR_INVALID_HASH_ALGORITHM, GVM_STREAM_VALIDATOR_INVALID_HASH_SYNTAX, GVM_STREAM_VALIDATOR_INVALID_HASH_VALUE, GVM_STREAM_VALIDATOR_NO_SIZE, gvm_stream_validator_with_size_new(), and VALID_DATA_HASH.

Here is the call graph for this function:

◆ Ensure() [10/21]

Ensure ( streamvalidator ,
init_rejects_invalid_syntax_hashes  )

Definition at line 137 of file streamvalidator_tests.c.

138{
139 gvm_stream_validator_t validator = NULL;
140
141 assert_equal (gvm_stream_validator_with_size_new ("0123", 123, &validator),
143 assert_equal (validator, NULL);
144
145 assert_equal (gvm_stream_validator_with_size_new ("sha256", 123, &validator),
147 assert_equal (validator, NULL);
148}

References GVM_STREAM_VALIDATOR_INVALID_HASH_SYNTAX, and gvm_stream_validator_with_size_new().

Here is the call graph for this function:

◆ Ensure() [11/21]

Ensure ( streamvalidator ,
init_rejects_invalid_value_hashes  )

Definition at line 164 of file streamvalidator_tests.c.

165{
166 gvm_stream_validator_t validator = NULL;
167
168 assert_equal (gvm_stream_validator_with_size_new ("sha256:", 123, &validator),
170 assert_equal (validator, NULL);
171
172 assert_equal (
173 gvm_stream_validator_with_size_new ("sha256:xyz", 123, &validator),
175 assert_equal (validator, NULL);
176
177 assert_equal (
178 gvm_stream_validator_with_size_new ("sha256:123", 123, &validator),
180 assert_equal (validator, NULL);
181
182 assert_equal (
183 gvm_stream_validator_with_size_new ("sha256:0123ab", 123, &validator),
185 assert_equal (validator, NULL);
186}

References GVM_STREAM_VALIDATOR_INVALID_HASH_VALUE, and gvm_stream_validator_with_size_new().

Here is the call graph for this function:

◆ Ensure() [12/21]

Ensure ( streamvalidator ,
rejects_checksum_with_extra_colon_suffix_as_invalid_hex  )

Definition at line 205 of file streamvalidator_tests.c.

207{
208 gvm_stream_validator_t validator = NULL;
209
210 assert_equal (
214 assert_equal (validator, NULL);
215
216 assert_equal (
220 assert_equal (validator, NULL);
221}

References GVM_STREAM_VALIDATOR_INVALID_HASH_VALUE, GVM_STREAM_VALIDATOR_NO_SIZE, gvm_stream_validator_with_size_new(), and VALID_DATA_HASH.

Here is the call graph for this function:

◆ Ensure() [13/21]

Ensure ( streamvalidator ,
rejects_hash_mismatch  )

◆ Ensure() [14/21]

Ensure ( streamvalidator ,
rejects_too_long_data  )

◆ Ensure() [15/21]

Ensure ( streamvalidator ,
rejects_too_short_data  )

◆ Ensure() [16/21]

Ensure ( streamvalidator ,
return_str_maps_known_codes  )

Definition at line 328 of file streamvalidator_tests.c.

329{
330 assert_string_equal (
332 "internal error");
333
334 assert_string_equal (
336 "too short");
337
338 assert_string_equal (
340 "too long");
341
342 assert_string_equal (
344 "invalid hash syntax");
345
346 assert_string_equal (gvm_stream_validator_return_str (
348 "invalid or unsupported hash algorithm");
349
350 assert_string_equal (
352 "invalid hash value");
353
354 assert_string_equal (
356 "hash does not match");
357}
const char * gvm_stream_validator_return_str(gvm_stream_validator_return_t value)
Gets a string representation of a gvm_stream_validator_return_t.
@ GVM_STREAM_VALIDATOR_INTERNAL_ERROR

References GVM_STREAM_VALIDATOR_DATA_TOO_LONG, GVM_STREAM_VALIDATOR_DATA_TOO_SHORT, GVM_STREAM_VALIDATOR_HASH_MISMATCH, GVM_STREAM_VALIDATOR_INTERNAL_ERROR, GVM_STREAM_VALIDATOR_INVALID_HASH_ALGORITHM, GVM_STREAM_VALIDATOR_INVALID_HASH_SYNTAX, GVM_STREAM_VALIDATOR_INVALID_HASH_VALUE, and gvm_stream_validator_return_str().

Here is the call graph for this function:

◆ Ensure() [17/21]

Ensure ( streamvalidator ,
return_str_ok_is_null  )

Definition at line 322 of file streamvalidator_tests.c.

323{
325 assert_equal (s, NULL);
326}

References GVM_STREAM_VALIDATOR_OK, and gvm_stream_validator_return_str().

Here is the call graph for this function:

◆ Ensure() [18/21]

Ensure ( streamvalidator ,
return_str_unknown_code_yields_unknown_error  )

Definition at line 359 of file streamvalidator_tests.c.

360{
361 const char *s =
363 assert_string_equal (s, "unknown error");
364}
gvm_stream_validator_return_t

References gvm_stream_validator_return_str().

Here is the call graph for this function:

◆ Ensure() [19/21]

Ensure ( streamvalidator ,
wrapper_checksum_only_accepts_valid_data  )

Definition at line 366 of file streamvalidator_tests.c.

367{
368 gvm_stream_validator_t v = NULL;
369
370 assert_equal (gvm_stream_validator_new (VALID_DATA_HASH, &v),
372
373 assert_equal (gvm_stream_validator_write (v, VALID_DATA, strlen (VALID_DATA)),
375
377
379}
gvm_stream_validator_return_t gvm_stream_validator_new(const char *expected_hash_str, gvm_stream_validator_t *validator_out)
Allocate and initialize a checksum-only stream validator.

References gvm_stream_validator_end(), gvm_stream_validator_free(), gvm_stream_validator_new(), GVM_STREAM_VALIDATOR_OK, gvm_stream_validator_write(), VALID_DATA, and VALID_DATA_HASH.

Here is the call graph for this function:

◆ Ensure() [20/21]

Ensure ( streamvalidator ,
wrapper_checksum_only_does_not_enforce_size  )

◆ Ensure() [21/21]

Ensure ( streamvalidator ,
wrapper_rejects_invalid_hash_syntax  )

Definition at line 398 of file streamvalidator_tests.c.

399{
400 gvm_stream_validator_t v = NULL;
401
402 assert_equal (gvm_stream_validator_new ("sha256", &v),
404 assert_equal (v, NULL);
405
406 assert_equal (gvm_stream_validator_new ("", &v),
408 assert_equal (v, NULL);
409}

References GVM_STREAM_VALIDATOR_INVALID_HASH_SYNTAX, and gvm_stream_validator_new().

Here is the call graph for this function:

◆ main()

int main ( int argc,
char ** argv )

Definition at line 412 of file streamvalidator_tests.c.

413{
414 int ret;
415 TestSuite *suite;
416
417 suite = create_test_suite ();
418
419 add_test_with_context (suite, streamvalidator, accepts_valid_data);
420 add_test_with_context (suite, streamvalidator,
421 accepts_valid_data_after_multiple_writes);
422 add_test_with_context (suite, streamvalidator,
423 accepts_valid_data_after_rewind);
424
425 add_test_with_context (suite, streamvalidator, rejects_too_long_data);
426 add_test_with_context (suite, streamvalidator, rejects_too_short_data);
427 add_test_with_context (suite, streamvalidator, rejects_hash_mismatch);
428
429 add_test_with_context (suite, streamvalidator, init_rejects_empty_hash);
430 add_test_with_context (suite, streamvalidator,
431 init_rejects_invalid_syntax_hashes);
432 add_test_with_context (suite, streamvalidator,
433 init_rejects_invalid_algo_hashes);
434 add_test_with_context (suite, streamvalidator,
435 init_rejects_invalid_value_hashes);
436 add_test_with_context (suite, streamvalidator,
437 accepts_valid_checksum_only_without_size);
438 add_test_with_context (suite, streamvalidator,
439 accepts_valid_checksum_only_without_size);
440 add_test_with_context (
441 suite, streamvalidator,
442 rejects_checksum_with_extra_colon_suffix_as_invalid_hex);
443 add_test_with_context (suite, streamvalidator,
444 enforces_size_from_arg_when_no_suffix);
445 add_test_with_context (suite, streamvalidator,
446 checksum_only_extra_bytes_yield_hash_mismatch);
447 add_test_with_context (suite, streamvalidator,
448 init_rejects_invalid_syntax_algo_and_value);
449 add_test_with_context (suite, streamvalidator, return_str_ok_is_null);
450 add_test_with_context (suite, streamvalidator, return_str_maps_known_codes);
451 add_test_with_context (suite, streamvalidator,
452 return_str_unknown_code_yields_unknown_error);
453 add_test_with_context (suite, streamvalidator,
454 wrapper_checksum_only_accepts_valid_data);
455 add_test_with_context (suite, streamvalidator,
456 wrapper_checksum_only_does_not_enforce_size);
457 add_test_with_context (suite, streamvalidator,
458 wrapper_rejects_invalid_hash_syntax);
459
460 if (argc > 1)
461 ret = run_single_test (suite, argv[1], create_text_reporter ());
462 else
463 ret = run_test_suite (suite, create_text_reporter ());
464
465 destroy_test_suite (suite);
466
467 return ret;
468}