/src/libzmq/tests/test_z85_decode_fuzzer.cpp
Line | Count | Source |
1 | | /* SPDX-License-Identifier: MPL-2.0 */ |
2 | | |
3 | | #ifdef ZMQ_USE_FUZZING_ENGINE |
4 | | #include <fuzzer/FuzzedDataProvider.h> |
5 | | #endif |
6 | | |
7 | | #include <string> |
8 | | #include <stdlib.h> |
9 | | |
10 | | #include "testutil.hpp" |
11 | | #include "testutil_unity.hpp" |
12 | | |
13 | | extern "C" int LLVMFuzzerTestOneInput (const uint8_t *data, size_t size) |
14 | 161 | { |
15 | 161 | uint8_t *secret_key; |
16 | | |
17 | 161 | if (size < 5) |
18 | 4 | return 0; |
19 | | |
20 | | // As per API definition, input must be divisible by 5, so truncate it if it's not |
21 | 157 | size -= size % 5; |
22 | | // As per API definition, the destination must be at least 0.8 times the input data |
23 | 157 | TEST_ASSERT_NOT_NULL (secret_key = (uint8_t *) malloc (size * 4 / 5)); |
24 | | |
25 | 157 | std::string z85_secret_key (reinterpret_cast<const char *> (data), size); |
26 | 157 | zmq_z85_decode (secret_key, z85_secret_key.c_str ()); |
27 | | |
28 | 157 | free (secret_key); |
29 | | |
30 | 157 | return 0; |
31 | 161 | } |
32 | | |
33 | | #ifndef ZMQ_USE_FUZZING_ENGINE |
34 | | void test_z85_decode_fuzzer () |
35 | | { |
36 | | uint8_t **data; |
37 | | size_t *len, num_cases = 0; |
38 | | if (fuzzer_corpus_encode ( |
39 | | "tests/libzmq-fuzz-corpora/test_z85_decode_fuzzer_seed_corpus", &data, |
40 | | &len, &num_cases) |
41 | | != 0) |
42 | | exit (77); |
43 | | |
44 | | while (num_cases-- > 0) { |
45 | | TEST_ASSERT_SUCCESS_ERRNO ( |
46 | | LLVMFuzzerTestOneInput (data[num_cases], len[num_cases])); |
47 | | free (data[num_cases]); |
48 | | } |
49 | | |
50 | | free (data); |
51 | | free (len); |
52 | | } |
53 | | |
54 | | int main (int argc, char **argv) |
55 | | { |
56 | | setup_test_environment (); |
57 | | |
58 | | UNITY_BEGIN (); |
59 | | RUN_TEST (test_z85_decode_fuzzer); |
60 | | |
61 | | return UNITY_END (); |
62 | | } |
63 | | #endif |