1 /*
2     Copyright (c) 2012 Martin Sustrik  All rights reserved.
3     Copyright (c) 2013 GoPivotal, Inc.  All rights reserved.
4     Copyright 2016 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/reqrep.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_a[128];
35 static char socket_address_b[128];
36 int dev0;
37 int dev1;
38 
device(NN_UNUSED void * arg)39 void device (NN_UNUSED void *arg)
40 {
41     int rc;
42 
43     /*  Run the device. */
44     rc = nn_device (dev0, dev1);
45     nn_assert (rc < 0 && nn_errno () == EBADF);
46 
47     /*  Clean up. */
48     test_close (dev0);
49     test_close (dev1);
50 }
51 
main(int argc,const char * argv[])52 int main (int argc, const char *argv[])
53 {
54     int end0;
55     int end1;
56     struct nn_thread thread1;
57     int timeo;
58     int maxttl;
59     size_t sz;
60     int rc;
61 
62     int port = get_test_port(argc, argv);
63 
64     test_addr_from(socket_address_a, "tcp", "127.0.0.1", port);
65     test_addr_from(socket_address_b, "tcp", "127.0.0.1", port + 1);
66 
67     /*  Intialise the device sockets. */
68     dev0 = test_socket (AF_SP_RAW, NN_REP);
69     dev1 = test_socket (AF_SP_RAW, NN_REQ);
70 
71     test_bind (dev0, socket_address_a);
72     test_bind (dev1, socket_address_b);
73 
74     /*  Start the device. */
75     nn_thread_init (&thread1, device, NULL);
76 
77     end0 = test_socket (AF_SP, NN_REQ);
78     end1 = test_socket (AF_SP, NN_REP);
79 
80     /*  Test the bi-directional device TTL */
81     test_connect (end0, socket_address_a);
82     test_connect (end1, socket_address_b);
83 
84     /*  Wait for TCP to establish. */
85     nn_sleep (100);
86 
87     /*  Pass a message between endpoints. */
88     /*  Set up max receive timeout. */
89     timeo = 100;
90     test_setsockopt (end0, NN_SOL_SOCKET, NN_RCVTIMEO, &timeo, sizeof (timeo));
91     timeo = 100;
92     test_setsockopt (end1, NN_SOL_SOCKET, NN_RCVTIMEO, &timeo, sizeof (timeo));
93 
94     /*  Test default TTL is 8. */
95     sz = sizeof (maxttl);
96     maxttl = -1;
97     rc = nn_getsockopt(end1, NN_SOL_SOCKET, NN_MAXTTL, &maxttl, &sz);
98     nn_assert (rc == 0);
99     nn_assert (sz == sizeof (maxttl));
100     nn_assert (maxttl == 8);
101 
102     /*  Test to make sure option TTL cannot be set below 1. */
103     maxttl = -1;
104     rc = nn_setsockopt(end1, NN_SOL_SOCKET, NN_MAXTTL, &maxttl, sizeof (maxttl));
105     nn_assert (rc < 0 && nn_errno () == EINVAL);
106     nn_assert (maxttl == -1);
107     maxttl = 0;
108     rc = nn_setsockopt(end1, NN_SOL_SOCKET, NN_MAXTTL, &maxttl, sizeof (maxttl));
109     nn_assert (rc < 0 && nn_errno () == EINVAL);
110     nn_assert (maxttl == 0);
111 
112     /*  Test to set non-integer size */
113     maxttl = 8;
114     rc = nn_setsockopt(end1, NN_SOL_SOCKET, NN_MAXTTL, &maxttl, 1);
115     nn_assert (rc < 0 && nn_errno () == EINVAL);
116     nn_assert (maxttl == 8);
117 
118     test_send (end0, "XYZ");
119 
120     test_recv (end1, "XYZ");
121 
122     /*  Now send a reply. */
123     test_send (end1, "REPLYXYZ\n");
124     test_recv (end0, "REPLYXYZ\n");
125 
126     /*  Now set the max TTL. */
127     maxttl = 1;
128     test_setsockopt (end0, NN_SOL_SOCKET, NN_MAXTTL, &maxttl, sizeof (maxttl));
129     test_setsockopt (end1, NN_SOL_SOCKET, NN_MAXTTL, &maxttl, sizeof (maxttl));
130 
131     test_send (end0, "DROPTHIS");
132     test_drop (end1, ETIMEDOUT);
133 
134     /*  Now set the max TTL up. */
135     maxttl = 2;
136     test_setsockopt (end0, NN_SOL_SOCKET, NN_MAXTTL, &maxttl, sizeof (maxttl));
137     test_setsockopt (end1, NN_SOL_SOCKET, NN_MAXTTL, &maxttl, sizeof (maxttl));
138 
139     test_send (end0, "DONTDROP");
140     test_recv (end1, "DONTDROP");
141 
142    test_send (end1, "GOTIT");
143    test_recv (end0, "GOTIT");
144 
145     /*  Clean up. */
146     test_close (end0);
147     test_close (end1);
148 
149     /*  Shut down the devices. */
150     nn_term ();
151 
152     nn_thread_term (&thread1);
153 
154     return 0;
155 }
156