1 /*
2     Copyright (c) 2012 Martin Sustrik  All rights reserved.
3     Copyright (c) 2013 GoPivotal, Inc.  All rights reserved.
4     Copyright 2015 Garrett D'Amore <garrett@damore.org>
5     Copyright 2016 Franklin "Snaipe" Mathieu <franklinmathieu@gmail.com>
6 
7     Permission is hereby granted, free of charge, to any person obtaining a copy
8     of this software and associated documentation files (the "Software"),
9     to deal in the Software without restriction, including without limitation
10     the rights to use, copy, modify, merge, publish, distribute, sublicense,
11     and/or sell copies of the Software, and to permit persons to whom
12     the Software is furnished to do so, subject to the following conditions:
13 
14     The above copyright notice and this permission notice shall be included
15     in all copies or substantial portions of the Software.
16 
17     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20     THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22     FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23     IN THE SOFTWARE.
24 */
25 
26 #include "../src/nn.h"
27 #include "../src/survey.h"
28 #include "../src/tcp.h"
29 
30 #include "testutil.h"
31 #include "../src/utils/attr.h"
32 #include "../src/utils/thread.c"
33 
34 static char socket_address_h[128], socket_address_i[128], socket_address_j[128];
35 
device5(NN_UNUSED void * arg)36 void device5 (NN_UNUSED void *arg)
37 {
38     int rc;
39     int dev0;
40     int dev1;
41 
42     /*  Intialise the device sockets. */
43     dev0 = test_socket (AF_SP_RAW, NN_RESPONDENT);
44     test_bind (dev0, socket_address_h);
45     dev1 = test_socket (AF_SP_RAW, NN_SURVEYOR);
46     test_bind (dev1, socket_address_i);
47 
48     /*  Run the device. */
49     rc = nn_device (dev0, dev1);
50     nn_assert (rc < 0 && nn_errno () == EBADF);
51 
52     /*  Clean up. */
53     test_close (dev0);
54     test_close (dev1);
55 }
56 
device6(NN_UNUSED void * arg)57 void device6 (NN_UNUSED void *arg)
58 {
59     int rc;
60     int dev2;
61     int dev3;
62 
63     dev2 = test_socket (AF_SP_RAW, NN_RESPONDENT);
64     test_connect (dev2, socket_address_i);
65     dev3 = test_socket (AF_SP_RAW, NN_SURVEYOR);
66     test_bind (dev3, socket_address_j);
67 
68     /*  Run the device. */
69     rc = nn_device (dev2, dev3);
70     nn_assert (rc < 0 && nn_errno () == EBADF);
71 
72     /*  Clean up. */
73     test_close (dev2);
74     test_close (dev3);
75 }
76 
main(int argc,const char * argv[])77 int main (int argc, const char *argv[])
78 {
79     int end0;
80     int end1;
81     struct nn_thread thread5;
82     struct nn_thread thread6;
83 
84     int port = get_test_port(argc, argv);
85 
86     test_addr_from(socket_address_h, "tcp", "127.0.0.1", port);
87     test_addr_from(socket_address_i, "tcp", "127.0.0.1", port + 1);
88     test_addr_from(socket_address_j, "tcp", "127.0.0.1", port + 2);
89 
90     /*  Test the bi-directional device with SURVEYOR(headers). */
91 
92     /*  Start the devices. */
93     nn_thread_init (&thread5, device5, NULL);
94     nn_thread_init (&thread6, device6, NULL);
95 
96     /*  Create two sockets to connect to the device. */
97     end0 = test_socket (AF_SP, NN_SURVEYOR);
98     test_connect (end0, socket_address_h);
99     end1 = test_socket (AF_SP, NN_RESPONDENT);
100     test_connect (end1, socket_address_j);
101 
102     /*  Wait up to a second for TCP to establish. */
103     nn_sleep (1000);
104 
105     /*  Pass a message between endpoints. */
106     test_send (end0, "XYZ");
107     test_recv (end1, "XYZ");
108 
109     /*  Now send a reply. */
110     test_send (end1, "REPLYXYZ");
111     test_recv (end0, "REPLYXYZ");
112 
113     /*  Clean up. */
114     test_close (end0);
115     test_close (end1);
116 
117     /*  Shut down the devices. */
118     nn_term ();
119     nn_thread_term (&thread5);
120     nn_thread_term (&thread6);
121 
122     return 0;
123 }
124 
125