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
|
//
// 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 "nng.h"
#include <string.h>
TestMain("Socket Operations", {
Convey("We are able to open a PAIR socket", {
int rv;
nng_socket s1;
So(nng_pair_open(&s1) == 0);
Reset({ nng_close(s1); });
Convey("And we can shut it down", {
So(nng_shutdown(s1) == 0);
So(nng_shutdown(s1) == NNG_ECLOSED);
});
Convey("It's type & peer are still PAIR", {
So(nng_protocol(s1) == NNG_PROTO_PAIR);
So(nng_peer(s1) == NNG_PROTO_PAIR);
});
Convey("Recv with no pipes times out correctly", {
nng_msg *msg = NULL;
int64_t to = 100000;
uint64_t now;
now = nng_clock();
So(now > 0);
So(nng_setopt_duration(s1, NNG_OPT_RCVTIMEO, to) == 0);
So(nng_recvmsg(s1, &msg, 0) == NNG_ETIMEDOUT);
So(msg == NULL);
So(nng_clock() >= (now + to));
So(nng_clock() < (now + (to * 2)));
});
Convey("Recv nonblock with no pipes gives EAGAIN", {
nng_msg *msg = NULL;
So(nng_recvmsg(s1, &msg, NNG_FLAG_NONBLOCK) ==
NNG_EAGAIN);
So(msg == NULL);
});
Convey("Send with no pipes times out correctly", {
nng_msg *msg = NULL;
int64_t to = 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 = nng_clock();
So(nng_setopt_duration(s1, NNG_OPT_SNDTIMEO, to) == 0);
So(nng_sendmsg(s1, msg, 0) == NNG_ETIMEDOUT);
So(nng_clock() >= (now + to));
So(nng_clock() < (now + (to * 2)));
nng_msg_free(msg);
});
Convey("We can set and get options", {
int64_t to = 1234;
int64_t v = 0;
size_t sz;
So(nng_setopt_duration(s1, NNG_OPT_SNDTIMEO, to) == 0);
Convey("Short size is not copied", {
sz = 0;
So(nng_getopt(s1, NNG_OPT_SNDTIMEO, &v, &sz) ==
0);
So(sz == sizeof(v));
So(v == 0);
});
Convey("Correct size is copied", {
sz = sizeof(v);
So(nng_getopt(s1, NNG_OPT_SNDTIMEO, &v, &sz) ==
0);
So(sz == sizeof(v));
So(v == 1234);
});
Convey("Short size buf is not copied", {
int l = 5;
sz = 0;
So(nng_getopt(s1, NNG_OPT_RCVBUF, &l, &sz) ==
0);
So(sz == sizeof(l));
So(l == 5);
});
Convey("Insane buffer size fails", {
So(nng_setopt_int(s1, NNG_OPT_RCVBUF,
0x100000) == NNG_EINVAL);
});
Convey("Negative timeout fails", {
So(nng_setopt_duration(s1, NNG_OPT_RCVTIMEO,
-5) == NNG_EINVAL);
});
Convey("Short timeout fails", {
to = 0;
sz = sizeof(to) - 1;
So(nng_setopt(s1, NNG_OPT_RCVTIMEO, &to, sz) ==
NNG_EINVAL);
});
Convey("Bogus raw fails", {
So(nng_setopt_int(s1, NNG_OPT_RAW, 42) ==
NNG_EINVAL);
So(nng_setopt_int(s1, NNG_OPT_RAW, -42) ==
NNG_EINVAL);
So(nng_setopt_int(s1, NNG_OPT_RAW, 0) == 0);
});
Convey("Unsupported options fail", {
char *crap = "crap";
So(nng_setopt(s1, NNG_OPT_SUBSCRIBE, crap,
strlen(crap)) == NNG_ENOTSUP);
});
Convey("Bogus sizes fail", {
size_t v;
So(nng_setopt_size(
s1, NNG_OPT_RCVMAXSZ, 6550) == 0);
So(nng_getopt_size(s1, NNG_OPT_RCVMAXSZ, &v) ==
0);
So(v == 6550);
v = 102400;
So(nng_setopt(s1, NNG_OPT_RCVMAXSZ, &v, 1) ==
NNG_EINVAL);
So(nng_getopt_size(s1, NNG_OPT_RCVMAXSZ, &v) ==
0);
So(v == 6550);
if (sizeof(size_t) == 8) {
v = 0x10000;
v <<= 30;
So(nng_setopt_size(s1,
NNG_OPT_RCVMAXSZ,
v) == NNG_EINVAL);
So(nng_getopt_size(
s1, NNG_OPT_RCVMAXSZ, &v) == 0);
So(v == 6550);
}
});
});
Convey("Bogus URLs not supported", {
Convey("Dialing fails properly", {
rv = nng_dial(s1, "bogus://1", NULL, 0);
So(rv == NNG_ENOTSUP);
});
Convey("Listening fails properly", {
rv = nng_listen(s1, "bogus://2", NULL, 0);
So(rv == NNG_ENOTSUP);
});
});
Convey("Dialing synch can get refused", {
rv = nng_dial(s1, "inproc://no", NULL, NNG_FLAG_SYNCH);
So(rv == NNG_ECONNREFUSED);
});
Convey("Listening works", {
char *a = "inproc://here";
rv = nng_listen(s1, a, NULL, NNG_FLAG_SYNCH);
So(rv == 0);
Convey("Second listen fails ADDRINUSE", {
rv = nng_listen(s1, a, NULL, NNG_FLAG_SYNCH);
So(rv == NNG_EADDRINUSE);
});
Convey("We can connect to it", {
nng_socket s2;
So(nng_pair_open(&s2) == 0);
Reset({ nng_close(s2); });
So(nng_dial(s2, a, NULL, NNG_FLAG_SYNCH) == 0);
nng_close(s2);
});
});
Convey("We can send and receive messages", {
nng_socket s2;
int len;
size_t sz;
uint64_t to = 3000000;
char * buf;
char * a = "inproc://t1";
So(nng_pair_open(&s2) == 0);
Reset({ nng_close(s2); });
So(nng_setopt_int(s1, NNG_OPT_RCVBUF, 1) == 0);
So(nng_getopt_int(s1, NNG_OPT_RCVBUF, &len) == 0);
So(len == 1);
So(nng_setopt_int(s1, NNG_OPT_SNDBUF, 1) == 0);
So(nng_setopt_int(s2, NNG_OPT_SNDBUF, 1) == 0);
So(nng_setopt_duration(s1, NNG_OPT_SNDTIMEO, to) == 0);
So(nng_setopt_duration(s1, NNG_OPT_RCVTIMEO, to) == 0);
So(nng_setopt_duration(s2, NNG_OPT_SNDTIMEO, to) == 0);
So(nng_setopt_duration(s2, NNG_OPT_RCVTIMEO, to) == 0);
So(nng_listen(s1, a, NULL, NNG_FLAG_SYNCH) == 0);
So(nng_dial(s2, a, NULL, NNG_FLAG_SYNCH) == 0);
So(nng_send(s1, "abc", 4, 0) == 0);
So(nng_recv(s2, &buf, &sz, NNG_FLAG_ALLOC) == 0);
So(buf != NULL);
So(sz == 4);
So(memcmp(buf, "abc", 4) == 0);
nng_free(buf, sz);
});
});
})
|