1 /*
2 Copyright (c) 2012 Martin Sustrik All rights reserved.
3 Copyright (c) 2013 GoPivotal, Inc. All rights reserved.
4
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"),
7 to deal in the Software without restriction, including without limitation
8 the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 and/or sell copies of the Software, and to permit persons to whom
10 the Software is furnished to do so, subject to the following conditions:
11
12 The above copyright notice and this permission notice shall be included
13 in all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 IN THE SOFTWARE.
22 */
23
24 #include "../src/nn.h"
25 #include "../src/bus.h"
26 #include "../src/pair.h"
27 #include "../src/pipeline.h"
28 #include "../src/inproc.h"
29
30 #include "testutil.h"
31 #include "../src/utils/attr.h"
32 #include "../src/utils/thread.c"
33
34 #define SOCKET_ADDRESS_A "inproc://a"
35 #define SOCKET_ADDRESS_B "inproc://b"
36 #define SOCKET_ADDRESS_C "inproc://c"
37 #define SOCKET_ADDRESS_D "inproc://d"
38 #define SOCKET_ADDRESS_E "inproc://e"
39
device1(NN_UNUSED void * arg)40 void device1 (NN_UNUSED void *arg)
41 {
42 int rc;
43 int deva;
44 int devb;
45
46 /* Intialise the device sockets. */
47 deva = test_socket (AF_SP_RAW, NN_PAIR);
48 test_bind (deva, SOCKET_ADDRESS_A);
49 devb = test_socket (AF_SP_RAW, NN_PAIR);
50 test_bind (devb, SOCKET_ADDRESS_B);
51
52 /* Run the device. */
53 rc = nn_device (deva, devb);
54 nn_assert (rc < 0 && (nn_errno () == EBADF));
55
56 /* Clean up. */
57 test_close (devb);
58 test_close (deva);
59 }
60
device2(NN_UNUSED void * arg)61 void device2 (NN_UNUSED void *arg)
62 {
63 int rc;
64 int devc;
65 int devd;
66
67 /* Intialise the device sockets. */
68 devc = test_socket (AF_SP_RAW, NN_PULL);
69 test_bind (devc, SOCKET_ADDRESS_C);
70 devd = test_socket (AF_SP_RAW, NN_PUSH);
71 test_bind (devd, SOCKET_ADDRESS_D);
72
73 /* Run the device. */
74 rc = nn_device (devc, devd);
75 nn_assert (rc < 0 && nn_errno () == EBADF);
76
77 /* Clean up. */
78 test_close (devd);
79 test_close (devc);
80 }
81
device3(NN_UNUSED void * arg)82 void device3 (NN_UNUSED void *arg)
83 {
84 int rc;
85 int deve;
86
87 /* Intialise the device socket. */
88 deve = test_socket (AF_SP_RAW, NN_BUS);
89 test_bind (deve, SOCKET_ADDRESS_E);
90
91 /* Run the device. */
92 rc = nn_device (deve, -1);
93 nn_assert (rc < 0 && nn_errno () == EBADF);
94
95 /* Clean up. */
96 test_close (deve);
97 }
98
main()99 int main ()
100 {
101 int enda;
102 int endb;
103 int endc;
104 int endd;
105 int ende1;
106 int ende2;
107 struct nn_thread thread1;
108 struct nn_thread thread2;
109 struct nn_thread thread3;
110 int timeo;
111
112 /* Test the bi-directional device. */
113
114 /* Start the device. */
115 nn_thread_init (&thread1, device1, NULL);
116
117 /* Create two sockets to connect to the device. */
118 enda = test_socket (AF_SP, NN_PAIR);
119 test_connect (enda, SOCKET_ADDRESS_A);
120 endb = test_socket (AF_SP, NN_PAIR);
121 test_connect (endb, SOCKET_ADDRESS_B);
122
123 /* Pass a pair of messages between endpoints. */
124 test_send (enda, "ABC");
125 test_recv (endb, "ABC");
126 test_send (endb, "ABC");
127 test_recv (enda, "ABC");
128
129 /* Clean up. */
130 test_close (endb);
131 test_close (enda);
132
133 /* Test the uni-directional device. */
134
135 /* Start the device. */
136 nn_thread_init (&thread2, device2, NULL);
137
138 /* Create two sockets to connect to the device. */
139 endc = test_socket (AF_SP, NN_PUSH);
140 test_connect (endc, SOCKET_ADDRESS_C);
141 endd = test_socket (AF_SP, NN_PULL);
142 test_connect (endd, SOCKET_ADDRESS_D);
143
144 /* Pass a message between endpoints. */
145 test_send (endc, "XYZ");
146 test_recv (endd, "XYZ");
147
148 /* Clean up. */
149 test_close (endd);
150 test_close (endc);
151
152 /* Test the loopback device. */
153
154 /* Start the device. */
155 nn_thread_init (&thread3, device3, NULL);
156
157 /* Create two sockets to connect to the device. */
158 ende1 = test_socket (AF_SP, NN_BUS);
159 test_connect (ende1, SOCKET_ADDRESS_E);
160 ende2 = test_socket (AF_SP, NN_BUS);
161 test_connect (ende2, SOCKET_ADDRESS_E);
162
163 /* BUS is unreliable so wait a bit for connections to be established. */
164 nn_sleep (100);
165
166 /* Pass a message to the bus. */
167 test_send (ende1, "KLM");
168 test_recv (ende2, "KLM");
169
170 /* Make sure that the message doesn't arrive at the socket it was
171 originally sent to. */
172 timeo = 100;
173 test_setsockopt (ende1, NN_SOL_SOCKET, NN_RCVTIMEO,
174 &timeo, sizeof (timeo));
175 test_drop (ende1, ETIMEDOUT);
176
177 /* Clean up. */
178 test_close (ende2);
179 test_close (ende1);
180
181 /* Shut down the devices. */
182 nn_term ();
183 nn_thread_term (&thread1);
184 nn_thread_term (&thread2);
185 nn_thread_term (&thread3);
186
187 return 0;
188 }
189
190