aboutsummaryrefslogtreecommitdiff
path: root/tests/sock.c
blob: 4ac2fe359224a7c5a5d02810a4f6a9abc9ecb9d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
//
// Copyright 2017 Garrett D'Amore <garrett@damore.org>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
// file was obtained (LICENSE.txt).  A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//

#include "convey.h"
#include "core/nng_impl.h"
#include "nng.h"

#include <string.h>

Main({
	Test("Socket Operations", {

		Convey("We are able to open a PAIR socket", {
			int        rv;
			nng_socket sock;

			So(nng_pair_open(&sock) == 0);

			Reset({ nng_close(sock); });

			Convey("And we can shut it down", {
				rv = nng_shutdown(sock);
				So(rv == 0);
				rv = nng_shutdown(sock);
				So(rv == NNG_ECLOSED);
			});

			Convey("It's type is still proto",
			    { So(nng_protocol(sock) == NNG_PROTO_PAIR); });

			Convey("Recv with no pipes times out correctly", {
				nng_msg *msg  = NULL;
				int64_t  when = 100000;
				uint64_t now;

				now = nni_clock();

				rv = nng_setopt(sock, NNG_OPT_RCVTIMEO, &when,
				    sizeof(when));
				So(rv == 0);
				rv = nng_recvmsg(sock, &msg, 0);
				So(rv == NNG_ETIMEDOUT);
				So(msg == NULL);
				So(nni_clock() >= (now + when));
				So(nni_clock() < (now + (when * 2)));
			});

			Convey("Recv nonblock with no pipes gives EAGAIN", {
				nng_msg *msg = NULL;
				rv =
				    nng_recvmsg(sock, &msg, NNG_FLAG_NONBLOCK);
				So(rv == NNG_EAGAIN);
				So(msg == NULL);
			});

			Convey("Send with no pipes times out correctly", {
				nng_msg *msg  = NULL;
				int64_t  when = 100000;
				uint64_t now;

				// We cheat to get access to the core's clock.
				So(nng_msg_alloc(&msg, 0) == 0);
				So(msg != NULL);
				now = nni_clock();

				rv = nng_setopt(sock, NNG_OPT_SNDTIMEO, &when,
				    sizeof(when));
				So(rv == 0);
				rv = nng_sendmsg(sock, msg, 0);
				So(rv == NNG_ETIMEDOUT);
				So(nni_clock() >= (now + when));
				So(nni_clock() < (now + (when * 2)));
				nng_msg_free(msg);
			});

			Convey("We can set and get options", {
				int64_t when  = 1234;
				int64_t check = 0;
				size_t  sz;
				rv = nng_setopt(sock, NNG_OPT_SNDTIMEO, &when,
				    sizeof(when));
				So(rv == 0);
				sz = sizeof(check);
				Convey("Short size is not copied", {
					sz = 0;
					rv = nng_getopt(sock, NNG_OPT_SNDTIMEO,
					    &check, &sz);
					So(rv == 0);
					So(sz == sizeof(check));
					So(check == 0);
				});
				Convey("Correct size is copied", {
					sz = sizeof(check);
					rv = nng_getopt(sock, NNG_OPT_SNDTIMEO,
					    &check, &sz);
					So(rv == 0);
					So(sz == sizeof(check));
					So(check == 1234);
				});

				Convey("Short size buf is not copied", {
					int len = 5;
					sz      = 0;
					So(nng_getopt(sock, NNG_OPT_RCVBUF,
					       &len, &sz) == 0);
					So(len == 5);
				});

				Convey("Insane buffer size fails", {
					int len = 1024 * 1024;
					sz      = sizeof(len);
					So(nng_setopt(sock, NNG_OPT_RCVBUF,
					       &len, sz) == NNG_EINVAL);
				});

				Convey("Negative timeout fails", {
					when = -5;
					sz   = sizeof(when);
					So(nng_setopt(sock, NNG_OPT_RCVTIMEO,
					       &when, sz) == NNG_EINVAL);
				});

				Convey("Short timeout fails", {
					when = 0;
					sz   = sizeof(when) - 1;
					So(nng_setopt(sock, NNG_OPT_RCVTIMEO,
					       &when, sz) == NNG_EINVAL);
				});

				Convey("Bogus raw fails", {
					int raw = 42;
					sz      = sizeof(raw);

					raw = 42;
					So(nng_setopt(sock, NNG_OPT_RAW, &raw,
					       sz) == NNG_EINVAL);
					raw = -42;
					So(nng_setopt(sock, NNG_OPT_RAW, &raw,
					       sz) == NNG_EINVAL);

					raw = 0;
					So(nng_setopt(sock, NNG_OPT_RAW, &raw,
					       sz) == 0);
				});

				Convey("Unsupported options fail", {
					char *crap = "crap";
					So(nng_setopt(sock, NNG_OPT_SUBSCRIBE,
					       crap,
					       strlen(crap)) == NNG_ENOTSUP);
				});

				Convey("Bogus sizes fail", {
					size_t rmax;
					rmax = 6550;
					So(nng_setopt(sock, NNG_OPT_RCVMAXSZ,
					       &rmax, sizeof(rmax)) == 0);
					rmax = 0;
					sz   = sizeof(rmax);
					So(nng_getopt(sock, NNG_OPT_RCVMAXSZ,
					       &rmax, &sz) == 0);
					So(rmax == 6550);

					rmax = 102400;
					So(nng_setopt(sock, NNG_OPT_RCVMAXSZ,
					       &rmax, 1) == NNG_EINVAL);
					So(nng_getopt(sock, NNG_OPT_RCVMAXSZ,
					       &rmax, &sz) == 0);
					So(rmax == 6550);

					if (sizeof(size_t) == 8) {
						rmax = 0x10000;
						rmax <<= 30;
						So(nng_setopt(sock,
						       NNG_OPT_RCVMAXSZ, &rmax,
						       sizeof(rmax)) ==
						    NNG_EINVAL);
						So(nng_getopt(sock,
						       NNG_OPT_RCVMAXSZ, &rmax,
						       &sz) == 0);
						So(rmax == 6550);
					}
				});
			});

			Convey("Bogus URLs not supported", {
				Convey("Dialing fails properly", {
					rv = nng_dial(sock,
					    "bogus://somewhere", NULL, 0);
					So(rv == NNG_ENOTSUP);
				});
				Convey("Listening fails properly", {
					rv = nng_listen(sock,
					    "bogus://elsewhere", NULL, 0);
					So(rv == NNG_ENOTSUP);
				});
			});

			Convey("Dialing synch can get refused", {
				rv = nng_dial(sock, "inproc://notthere", NULL,
				    NNG_FLAG_SYNCH);
				So(rv == NNG_ECONNREFUSED);
			});

			Convey("Listening works", {
				rv = nng_listen(sock, "inproc://here", NULL,
				    NNG_FLAG_SYNCH);
				So(rv == 0);

				Convey("Second listen fails ADDRINUSE", {
					rv = nng_listen(sock, "inproc://here",
					    NULL, NNG_FLAG_SYNCH);
					So(rv == NNG_EADDRINUSE);
				});

				Convey("We can connect to it", {
					nng_socket sock2;
					So(nng_pair_open(&sock2) == 0);
					Reset({ nng_close(sock2); });
					rv = nng_dial(sock2, "inproc://here",
					    NULL, NNG_FLAG_SYNCH);
					So(rv == 0);
					nng_close(sock2);
				});
			});

			Convey("We can send and receive messages", {
				nng_socket sock2;
				int        len = 1;
				size_t     sz;
				uint64_t   second = 3000000;
				char *     buf;

				So(nng_pair_open(&sock2) == 0);
				Reset({ nng_close(sock2); });

				So(nng_setopt(sock, NNG_OPT_RCVBUF, &len,
				       sizeof(len)) == 0);
				sz = sizeof(len);
				So(nng_getopt(
				       sock, NNG_OPT_RCVBUF, &len, &sz) == 0);
				So(len == 1);
				sz = 0;

				So(nng_setopt(sock, NNG_OPT_SNDBUF, &len,
				       sizeof(len)) == 0);
				So(nng_setopt(sock2, NNG_OPT_SNDBUF, &len,
				       sizeof(len)) == 0);

				So(nng_setopt(sock, NNG_OPT_SNDTIMEO, &second,
				       sizeof(second)) == 0);
				So(nng_setopt(sock, NNG_OPT_RCVTIMEO, &second,
				       sizeof(second)) == 0);
				So(nng_setopt(sock2, NNG_OPT_SNDTIMEO, &second,
				       sizeof(second)) == 0);
				So(nng_setopt(sock2, NNG_OPT_RCVTIMEO, &second,
				       sizeof(second)) == 0);

				So(nng_listen(sock, "inproc://test1", NULL,
				       NNG_FLAG_SYNCH) == 0);
				So(nng_dial(sock2, "inproc://test1", NULL,
				       NNG_FLAG_SYNCH) == 0);

				So(nng_send(sock, "abc", 4, 0) == 0);
				So(nng_recv(
				       sock2, &buf, &sz, NNG_FLAG_ALLOC) == 0);
				So(buf != NULL);
				So(sz == 4);
				So(memcmp(buf, "abc", 4) == 0);
				nng_free(buf, sz);
			});
		});
	});
})