/src/libzmq/tests/test_bind_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 | | #ifndef PATH_MAX |
13 | | #define PATH_MAX 1024 |
14 | | #endif |
15 | | |
16 | | // Test that zmq_bind can handle malformed strings |
17 | | extern "C" int LLVMFuzzerTestOneInput (const uint8_t *data, size_t size) |
18 | 1.58k | { |
19 | | // This test might create socket files, so move to /tmp to avoid clobbering |
20 | | // the working directory with random filenames |
21 | 1.58k | char *pwd = (char *) malloc (PATH_MAX + 1); |
22 | 1.58k | TEST_ASSERT_NOT_NULL (pwd); |
23 | 1.58k | TEST_ASSERT_NOT_NULL (getcwd (pwd, PATH_MAX + 1)); |
24 | 1.58k | TEST_ASSERT_SUCCESS_ERRNO (chdir ("/tmp")); |
25 | | |
26 | 1.58k | setup_test_context (); |
27 | 1.58k | std::string my_endpoint (reinterpret_cast<const char *> (data), size); |
28 | 1.58k | void *socket = test_context_socket (ZMQ_PUB); |
29 | 1.58k | zmq_bind (socket, my_endpoint.c_str ()); |
30 | | |
31 | 1.58k | test_context_socket_close_zero_linger (socket); |
32 | 1.58k | teardown_test_context (); |
33 | 1.58k | TEST_ASSERT_SUCCESS_ERRNO (chdir (pwd)); |
34 | 1.58k | free (pwd); |
35 | | |
36 | 1.58k | return 0; |
37 | 1.58k | } |
38 | | |
39 | | #ifndef ZMQ_USE_FUZZING_ENGINE |
40 | | void test_bind_fuzzer () |
41 | | { |
42 | | uint8_t **data; |
43 | | size_t *len, num_cases = 0; |
44 | | if (fuzzer_corpus_encode ( |
45 | | "tests/libzmq-fuzz-corpora/test_bind_fuzzer_seed_corpus", &data, &len, |
46 | | &num_cases) |
47 | | != 0) |
48 | | exit (77); |
49 | | |
50 | | while (num_cases-- > 0) { |
51 | | TEST_ASSERT_SUCCESS_ERRNO ( |
52 | | LLVMFuzzerTestOneInput (data[num_cases], len[num_cases])); |
53 | | free (data[num_cases]); |
54 | | } |
55 | | |
56 | | free (data); |
57 | | free (len); |
58 | | } |
59 | | |
60 | | int main (int argc, char **argv) |
61 | | { |
62 | | LIBZMQ_UNUSED (argc); |
63 | | LIBZMQ_UNUSED (argv); |
64 | | |
65 | | setup_test_environment (); |
66 | | |
67 | | UNITY_BEGIN (); |
68 | | RUN_TEST (test_bind_fuzzer); |
69 | | |
70 | | return UNITY_END (); |
71 | | } |
72 | | #endif |