1 /*
2 Copyright (c) 2013 Insollo Entertainment, LLC. All rights reserved.
3 Copyright 2015 Garrett D'Amore <garrett@damore.org>
4 Copyright 2016 Franklin "Snaipe" Mathieu <franklinmathieu@gmail.com>
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"),
8 to deal in the Software without restriction, including without limitation
9 the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 and/or sell copies of the Software, and to permit persons to whom
11 the Software is furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included
14 in all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 IN THE SOFTWARE.
23 */
24
25 #ifndef TESTUTIL_H_INCLUDED
26 #define TESTUTIL_H_INCLUDED
27
28 #include "../src/utils/attr.h"
29 #include "../src/utils/err.c"
30 #include "../src/utils/sleep.c"
31
32 static int test_socket_impl (char *file, int line, int family, int protocol);
33 static int test_connect_impl (char *file, int line, int sock, char *address);
34 static int test_bind_impl (char *file, int line, int sock, char *address);
35 static void test_close_impl (char *file, int line, int sock);
36 static void test_send_impl (char *file, int line, int sock, char *data);
37 static void test_recv_impl (char *file, int line, int sock, char *data);
38 static void test_drop_impl (char *file, int line, int sock, int err);
39 static int test_setsockopt_impl (char *file, int line, int sock, int level,
40 int option, const void *optval, size_t optlen);
41
42 #define test_socket(f, p) test_socket_impl (__FILE__, __LINE__, (f), (p))
43 #define test_connect(s, a) test_connect_impl (__FILE__, __LINE__, (s), (a))
44 #define test_bind(s, a) test_bind_impl (__FILE__, __LINE__, (s), (a))
45 #define test_send(s, d) test_send_impl (__FILE__, __LINE__, (s), (d))
46 #define test_recv(s, d) test_recv_impl (__FILE__, __LINE__, (s), (d))
47 #define test_drop(s, e) test_drop_impl (__FILE__, __LINE__, (s), (e))
48 #define test_close(s) test_close_impl (__FILE__, __LINE__, (s))
49 #define test_setsockopt(s, l, o, v, z) test_setsockopt_impl (__FILE__, \
50 __LINE__, (s), (l), (o), (v), (z))
51
test_socket_impl(char * file,int line,int family,int protocol)52 static int NN_UNUSED test_socket_impl (char *file, int line, int family,
53 int protocol)
54 {
55 int sock;
56
57 sock = nn_socket (family, protocol);
58 if (sock == -1) {
59 fprintf (stderr, "Failed create socket: %s [%d] (%s:%d)\n",
60 nn_err_strerror (errno),
61 (int) errno, file, line);
62 nn_err_abort ();
63 }
64
65 return sock;
66 }
67
test_connect_impl(char * file,int line,int sock,char * address)68 static int NN_UNUSED test_connect_impl (char *file, int line,
69 int sock, char *address)
70 {
71 int rc;
72
73 rc = nn_connect (sock, address);
74 if(rc < 0) {
75 fprintf (stderr, "Failed connect to \"%s\": %s [%d] (%s:%d)\n",
76 address,
77 nn_err_strerror (errno),
78 (int) errno, file, line);
79 nn_err_abort ();
80 }
81 return rc;
82 }
83
test_bind_impl(char * file,int line,int sock,char * address)84 static int NN_UNUSED test_bind_impl (char *file, int line,
85 int sock, char *address)
86 {
87 int rc;
88
89 rc = nn_bind (sock, address);
90 if(rc < 0) {
91 fprintf (stderr, "Failed bind to \"%s\": %s [%d] (%s:%d)\n",
92 address,
93 nn_err_strerror (errno),
94 (int) errno, file, line);
95 nn_err_abort ();
96 }
97 return rc;
98 }
99
test_setsockopt_impl(char * file,int line,int sock,int level,int option,const void * optval,size_t optlen)100 static int NN_UNUSED test_setsockopt_impl (char *file, int line,
101 int sock, int level, int option, const void *optval, size_t optlen)
102 {
103 int rc;
104
105 rc = nn_setsockopt (sock, level, option, optval, optlen);
106 if(rc < 0) {
107 fprintf (stderr, "Failed set option \"%d\": %s [%d] (%s:%d)\n",
108 option,
109 nn_err_strerror (errno),
110 (int) errno, file, line);
111 nn_err_abort ();
112 }
113 return rc;
114 }
115
test_close_impl(char * file,int line,int sock)116 static void NN_UNUSED test_close_impl (char *file, int line, int sock)
117 {
118 int rc;
119
120 rc = nn_close (sock);
121 if ((rc != 0) && (errno != EBADF && errno != ETERM)) {
122 fprintf (stderr, "Failed to close socket: %s [%d] (%s:%d)\n",
123 nn_err_strerror (errno),
124 (int) errno, file, line);
125 nn_err_abort ();
126 }
127 }
128
test_send_impl(char * file,int line,int sock,char * data)129 static void NN_UNUSED test_send_impl (char *file, int line,
130 int sock, char *data)
131 {
132 size_t data_len;
133 int rc;
134
135 data_len = strlen (data);
136
137 rc = nn_send (sock, data, data_len, 0);
138 if (rc < 0) {
139 fprintf (stderr, "Failed to send: %s [%d] (%s:%d)\n",
140 nn_err_strerror (errno),
141 (int) errno, file, line);
142 nn_err_abort ();
143 }
144 if (rc != (int)data_len) {
145 fprintf (stderr, "Data to send is truncated: %d != %d (%s:%d)\n",
146 rc, (int) data_len,
147 file, line);
148 nn_err_abort ();
149 }
150 }
151
test_recv_impl(char * file,int line,int sock,char * data)152 static void NN_UNUSED test_recv_impl (char *file, int line, int sock, char *data)
153 {
154 size_t data_len;
155 int rc;
156 char *buf;
157
158 data_len = strlen (data);
159 /* We allocate plus one byte so that we are sure that message received
160 has correct length and not truncated */
161 buf = malloc (data_len+1);
162 alloc_assert (buf);
163
164 rc = nn_recv (sock, buf, data_len+1, 0);
165 if (rc < 0) {
166 fprintf (stderr, "Failed to recv: %s [%d] (%s:%d)\n",
167 nn_err_strerror (errno),
168 (int) errno, file, line);
169 nn_err_abort ();
170 }
171 if (rc != (int)data_len) {
172 fprintf (stderr, "Received data has wrong length: %d != %d (%s:%d)\n",
173 rc, (int) data_len,
174 file, line);
175 nn_err_abort ();
176 }
177 if (memcmp (data, buf, data_len) != 0) {
178 /* We don't print the data as it may have binary garbage */
179 fprintf (stderr, "Received data is wrong (%s:%d)\n", file, line);
180 nn_err_abort ();
181 }
182
183 free (buf);
184 }
185
test_drop_impl(char * file,int line,int sock,int err)186 static void NN_UNUSED test_drop_impl (char *file, int line, int sock, int err)
187 {
188 int rc;
189 char buf[1024];
190
191 rc = nn_recv (sock, buf, sizeof (buf), 0);
192 if (rc < 0 && err != errno) {
193 fprintf (stderr, "Got wrong err to recv: %s [%d != %d] (%s:%d)\n",
194 nn_err_strerror (errno),
195 (int) errno, err, file, line);
196 nn_err_abort ();
197 } else if (rc >= 0) {
198 fprintf (stderr, "Did not drop message: [%d bytes] (%s:%d)\n",
199 rc, file, line);
200 nn_err_abort ();
201 }
202 }
203
get_test_port(int argc,const char * argv[])204 static int NN_UNUSED get_test_port (int argc, const char *argv[])
205 {
206 return atoi(argc < 2 ? "5555" : argv[1]);
207 }
208
test_addr_from(char * out,const char * proto,const char * ip,int port)209 static void NN_UNUSED test_addr_from (char *out, const char *proto,
210 const char *ip, int port)
211 {
212 sprintf(out, "%s://%s:%d", proto, ip, port);
213 }
214
215 #endif
216