/src/ndpi/fuzz/fuzz_ds_domain_classify.cpp
Line | Count | Source |
1 | | #include "ndpi_api.h" |
2 | | #include "fuzz_common_code.h" |
3 | | |
4 | | #include <stdint.h> |
5 | | #include <stdio.h> |
6 | | #include <assert.h> |
7 | | #include "fuzzer/FuzzedDataProvider.h" |
8 | | |
9 | 1.37k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
10 | 1.37k | FuzzedDataProvider fuzzed_data(data, size); |
11 | 1.37k | u_int16_t i, num_iteration, is_added = 0; |
12 | 1.37k | bool rc; |
13 | 1.37k | ndpi_domain_classify *d; |
14 | 1.37k | u_int8_t class_id; |
15 | 1.37k | std::string value, value_added; |
16 | | |
17 | | /* To allow memory allocation failures */ |
18 | 1.37k | fuzz_set_alloc_callbacks_and_seed(size); |
19 | | |
20 | 1.37k | d = ndpi_domain_classify_alloc(); |
21 | | |
22 | 1.37k | num_iteration = fuzzed_data.ConsumeIntegral<u_int8_t>(); |
23 | 96.7k | for (i = 0; i < num_iteration; i++) { |
24 | | |
25 | 95.3k | value = fuzzed_data.ConsumeBytesAsString(fuzzed_data.ConsumeIntegral<u_int8_t>()); |
26 | 95.3k | class_id = fuzzed_data.ConsumeIntegral<u_int8_t>(); |
27 | | |
28 | 95.3k | rc = ndpi_domain_classify_add(d, class_id, value.c_str()); |
29 | | /* Keep one random entry really added */ |
30 | 95.3k | if (rc == true && is_added == 0 && fuzzed_data.ConsumeBool()) { |
31 | 885 | value_added = value; |
32 | 885 | is_added = 1; |
33 | 885 | } |
34 | 95.3k | } |
35 | | |
36 | 1.37k | ndpi_domain_classify_add_domains(d, |
37 | 1.37k | fuzzed_data.ConsumeIntegralInRange(0, NDPI_LAST_IMPLEMENTED_PROTOCOL - 1), |
38 | 1.37k | fuzzed_data.ConsumeBool() ? (char *)"random_list.list" : (char *)"wrong_path"); |
39 | | |
40 | 1.37k | if (fuzzed_data.ConsumeBool()) |
41 | 183 | ndpi_domain_classify_finalize(d); |
42 | | |
43 | | /* "Random" search */ |
44 | 1.37k | num_iteration = fuzzed_data.ConsumeIntegral<u_int8_t>(); |
45 | 34.0k | for (i = 0; i < num_iteration; i++) { |
46 | 32.7k | value = fuzzed_data.ConsumeBytesAsString(fuzzed_data.ConsumeIntegral<u_int8_t>()); |
47 | | |
48 | 32.7k | ndpi_domain_classify_contains(d, &class_id, value.c_str()); |
49 | 32.7k | } |
50 | | /* Search of an added entry */ |
51 | 1.37k | if (is_added) { |
52 | 885 | ndpi_domain_classify_contains(d, &class_id, value_added.c_str()); |
53 | 885 | } |
54 | | |
55 | 1.37k | ndpi_domain_classify_size(d); |
56 | | |
57 | 1.37k | ndpi_domain_classify_free(d); |
58 | | |
59 | 1.37k | return 0; |
60 | 1.37k | } |