/src/libzmq/tests/test_connect_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 | | |
9 | | #include "testutil.hpp" |
10 | | #include "testutil_unity.hpp" |
11 | | |
12 | | // Test that zmq_connect can handle malformed strings |
13 | | extern "C" int LLVMFuzzerTestOneInput (const uint8_t *data, size_t size) |
14 | 1.65k | { |
15 | 1.65k | setup_test_context (); |
16 | 1.65k | std::string my_endpoint (reinterpret_cast<const char *> (data), size); |
17 | 1.65k | void *socket = test_context_socket (ZMQ_PUB); |
18 | 1.65k | zmq_connect (socket, my_endpoint.c_str ()); |
19 | | |
20 | 1.65k | test_context_socket_close_zero_linger (socket); |
21 | 1.65k | teardown_test_context (); |
22 | | |
23 | 1.65k | return 0; |
24 | 1.65k | } |
25 | | |
26 | | #ifndef ZMQ_USE_FUZZING_ENGINE |
27 | | void test_connect_fuzzer () |
28 | | { |
29 | | uint8_t **data; |
30 | | size_t *len, num_cases = 0; |
31 | | if (fuzzer_corpus_encode ( |
32 | | "tests/libzmq-fuzz-corpora/test_connect_fuzzer_seed_corpus", &data, |
33 | | &len, &num_cases) |
34 | | != 0) |
35 | | exit (77); |
36 | | |
37 | | while (num_cases-- > 0) { |
38 | | TEST_ASSERT_SUCCESS_ERRNO ( |
39 | | LLVMFuzzerTestOneInput (data[num_cases], len[num_cases])); |
40 | | free (data[num_cases]); |
41 | | } |
42 | | |
43 | | free (data); |
44 | | free (len); |
45 | | } |
46 | | |
47 | | int main (int argc, char **argv) |
48 | | { |
49 | | LIBZMQ_UNUSED (argc); |
50 | | LIBZMQ_UNUSED (argv); |
51 | | |
52 | | setup_test_environment (); |
53 | | |
54 | | UNITY_BEGIN (); |
55 | | RUN_TEST (test_connect_fuzzer); |
56 | | |
57 | | return UNITY_END (); |
58 | | } |
59 | | #endif |