test_connect_curve_fuzzer.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MPL-2.0 */
2 
3 #ifdef ZMQ_USE_FUZZING_ENGINE
4 #include <fuzzer/FuzzedDataProvider.h>
5 #endif
6 
7 #include "testutil.hpp"
8 #include "testutil_security.hpp"
9 
10 // Test that the ZMTP engine handles invalid handshake when connecting
11 // https://rfc.zeromq.org/spec/37/
12 // https://rfc.zeromq.org/spec/26/
13 extern "C" int LLVMFuzzerTestOneInput (const uint8_t *data, size_t size)
14 {
15  const char *fixed_client_public =
16  "{{k*81)yMWEF{/BxdMd[5RL^qRFxBgoL<8m.D^KD";
17  const char *fixed_client_secret =
18  "N?Gmik8R[2ACw{b7*[-$S6[4}aO#?DB?#=<OQPc7";
19  const char *fixed_server_public =
20  "3.9-xXwy{g*w72TP*3iB9IJJRxlBH<ufTAvPd2>C";
21 
24  fd_t server = bind_socket_resolve_port ("127.0.0.1", "0", my_endpoint);
25 
27  fixed_server_public, fixed_client_public, fixed_client_secret};
28  void *client_mon;
31 
32  fd_t server_accept =
34 
35  // If there is not enough data for a full greeting, just send what we can
36  // Otherwise send greeting first, as expected by the protocol
37  uint8_t buf[512];
38  if (size >= 64) {
39  send (server_accept, (void *) data, 64, MSG_NOSIGNAL);
40  data += 64;
41  size -= 64;
42  }
43  recv (server_accept, buf, 64, 0);
44  // Then expect HELLO and send WELCOME if there's enough data
45  if (size >= 170) {
46  recv (server_accept, buf, 202, 0);
47  send (server_accept, (void *) data, 170, MSG_NOSIGNAL);
48  data += 170;
49  size -= 170;
50  }
51  // Then expect INITIATE and send READY if there's enough data
52  if (size >= 72) {
53  recv (server_accept, buf, 512, 0);
54  send (server_accept, (void *) data, 72, MSG_NOSIGNAL);
55  data += 72;
56  size -= 72;
57  }
58  msleep (250);
59  for (ssize_t sent = 0; size > 0 && (sent != -1 || errno == EINTR);
60  size -= sent > 0 ? sent : 0, data += sent > 0 ? sent : 0)
61  sent = send (server_accept, (const char *) data, size, MSG_NOSIGNAL);
62  recv (server_accept, buf, 512, MSG_DONTWAIT);
63  msleep (250);
64 
65  zmq_msg_t msg;
66  zmq_msg_init (&msg);
67  while (-1 != zmq_msg_recv (&msg, client, ZMQ_DONTWAIT)) {
68  zmq_msg_close (&msg);
69  zmq_msg_init (&msg);
70  }
71 
72  close (server_accept);
73  close (server);
74 
78 
79  return 0;
80 }
81 
82 #ifndef ZMQ_USE_FUZZING_ENGINE
84 {
85  uint8_t **data;
86  size_t *len, num_cases = 0;
88  "tests/libzmq-fuzz-corpora/test_connect_curve_fuzzer_seed_corpus",
89  &data, &len, &num_cases)
90  != 0)
91  exit (77);
92 
93  while (num_cases-- > 0) {
95  LLVMFuzzerTestOneInput (data[num_cases], len[num_cases]));
96  free (data[num_cases]);
97  }
98 
99  free (data);
100  free (len);
101 }
102 
103 int main (int argc, char **argv)
104 {
106 
107  UNITY_BEGIN ();
109 
110  return UNITY_END ();
111 }
112 #endif
curve_client_data_t
Definition: testutil_security.hpp:35
NULL
NULL
Definition: test_security_zap.cpp:405
UNITY_END
return UNITY_END()
EINTR
#define EINTR
Definition: errno.hpp:7
test_connect_curve_fuzzer
void test_connect_curve_fuzzer()
Definition: test_connect_curve_fuzzer.cpp:83
msleep
void msleep(int milliseconds_)
Definition: testutil.cpp:227
main
int main(int argc, char **argv)
Definition: test_connect_curve_fuzzer.cpp:103
RUN_TEST
#define RUN_TEST(func)
Definition: unity_internals.h:615
testutil_security.hpp
setup_test_context
void setup_test_context()
Definition: testutil_unity.cpp:179
errno
int errno
teardown_test_context
void teardown_test_context()
Definition: testutil_unity.cpp:189
client
void client(int num)
Definition: test_multithread.cpp:134
send
void send(fd_t fd_, const char(&data_)[N])
Definition: test_security_curve.cpp:209
curve_client_data
static NULL curve_client_data_t curve_client_data
Definition: test_security_zap.cpp:407
test_context_socket_close_zero_linger
void * test_context_socket_close_zero_linger(void *socket_)
Definition: testutil_unity.cpp:215
fuzzer_corpus_encode
int fuzzer_corpus_encode(const char *dirname, uint8_t ***data, size_t **len, size_t *num_cases)
Definition: testutil.cpp:534
LLVMFuzzerTestOneInput
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
Definition: test_connect_curve_fuzzer.cpp:13
TEST_ASSERT_SUCCESS_RAW_ERRNO
#define TEST_ASSERT_SUCCESS_RAW_ERRNO(expr)
Definition: testutil_unity.hpp:69
testutil.hpp
zmq_msg_t
Definition: zmq.h:218
my_endpoint
char my_endpoint[MAX_SOCKET_STRING]
Definition: test_security_curve.cpp:31
MAX_SOCKET_STRING
#define MAX_SOCKET_STRING
Definition: libzmq/tests/testutil.hpp:35
size
#define size
Definition: glcorearb.h:2944
zmq_msg_recv
ZMQ_EXPORT int zmq_msg_recv(zmq_msg_t *msg_, void *s_, int flags_)
Definition: zmq.cpp:617
ZMQ_DONTWAIT
#define ZMQ_DONTWAIT
Definition: zmq.h:358
server
void * server
Definition: test_security_curve.cpp:29
buf
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:4175
zmq_msg_init
ZMQ_EXPORT int zmq_msg_init(zmq_msg_t *msg_)
Definition: zmq.cpp:587
len
int len
Definition: php/ext/google/protobuf/map.c:206
create_and_connect_client
void * create_and_connect_client(char *my_endpoint_, socket_config_fn socket_config_, void *socket_config_data_, void **client_mon_)
Definition: testutil_security.cpp:338
size
GLsizeiptr size
Definition: glcorearb.h:2943
setup_test_environment
void setup_test_environment(int timeout_seconds_)
Definition: testutil.cpp:201
UNITY_BEGIN
UNITY_BEGIN()
fd_t
zmq_fd_t fd_t
Definition: libzmq/tests/testutil.hpp:98
socket_config_curve_client
void socket_config_curve_client(void *client_, void *data_)
Definition: testutil_security.cpp:83
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: glcorearb.h:2879
zmq_msg_close
ZMQ_EXPORT int zmq_msg_close(zmq_msg_t *msg_)
Definition: zmq.cpp:625
TEST_ASSERT_SUCCESS_ERRNO
#define TEST_ASSERT_SUCCESS_ERRNO(expr)
Definition: proxy_thr.cpp:47
MSG_NOSIGNAL
#define MSG_NOSIGNAL
Definition: libzmq/tests/testutil.hpp:71
bind_socket_resolve_port
fd_t bind_socket_resolve_port(const char *address_, const char *port_, char *my_endpoint_, const int af_, const int protocol_)
Definition: testutil.cpp:410


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:59