1 | /* |
2 | Copyright (c) 2016 Contributors as noted in the AUTHORS file |
3 | |
4 | This file is part of libzmq, the ZeroMQ core engine in C++. |
5 | |
6 | libzmq is free software; you can redistribute it and/or modify it under |
7 | the terms of the GNU Lesser General Public License (LGPL) as published |
8 | by the Free Software Foundation; either version 3 of the License, or |
9 | (at your option) any later version. |
10 | |
11 | As a special exception, the Contributors give you permission to link |
12 | this library with independent modules to produce an executable, |
13 | regardless of the license terms of these independent modules, and to |
14 | copy and distribute the resulting executable under terms of your choice, |
15 | provided that you also meet, for each linked independent module, the |
16 | terms and conditions of the license of that module. An independent |
17 | module is a module which is not derived from or based on this library. |
18 | If you modify this library, you must extend this exception to your |
19 | version of the library. |
20 | |
21 | libzmq is distributed in the hope that it will be useful, but WITHOUT |
22 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
23 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
24 | License for more details. |
25 | |
26 | You should have received a copy of the GNU Lesser General Public License |
27 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
28 | */ |
29 | |
30 | #include "testutil.hpp" |
31 | #include "testutil_unity.hpp" |
32 | |
33 | #include <stdlib.h> |
34 | #include <string.h> |
35 | |
36 | SETUP_TEARDOWN_TESTCONTEXT |
37 | |
38 | void str_send_to (void *s_, const char *content_, const char *address_) |
39 | { |
40 | send_string_expect_success (s_, address_, ZMQ_SNDMORE); |
41 | send_string_expect_success (s_, content_, 0); |
42 | } |
43 | |
44 | void str_recv_from (void *s_, char **ptr_content_, char **ptr_address_) |
45 | { |
46 | *ptr_address_ = s_recv (s_); |
47 | TEST_ASSERT_NOT_NULL (ptr_address_); |
48 | |
49 | *ptr_content_ = s_recv (s_); |
50 | TEST_ASSERT_NOT_NULL (ptr_content_); |
51 | } |
52 | |
53 | static const char test_question[] = "Is someone there ?" ; |
54 | static const char test_answer[] = "Yes, there is !" ; |
55 | |
56 | void test_connect_fails () |
57 | { |
58 | void *socket = test_context_socket (ZMQ_DGRAM); |
59 | |
60 | // Connecting dgram should fail |
61 | TEST_ASSERT_FAILURE_ERRNO (ENOCOMPATPROTO, |
62 | zmq_connect (socket, ENDPOINT_4)); |
63 | |
64 | test_context_socket_close (socket); |
65 | } |
66 | |
67 | void test_roundtrip () |
68 | { |
69 | char *message_string; |
70 | char *address; |
71 | |
72 | void *sender = test_context_socket (ZMQ_DGRAM); |
73 | void *listener = test_context_socket (ZMQ_DGRAM); |
74 | |
75 | TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (listener, ENDPOINT_4)); |
76 | |
77 | TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (sender, ENDPOINT_5)); |
78 | |
79 | str_send_to (sender, test_question, strrchr (ENDPOINT_4, '/') + 1); |
80 | |
81 | str_recv_from (listener, &message_string, &address); |
82 | TEST_ASSERT_EQUAL_STRING (test_question, message_string); |
83 | TEST_ASSERT_EQUAL_STRING (strrchr (ENDPOINT_5, '/') + 1, address); |
84 | free (message_string); |
85 | |
86 | str_send_to (listener, test_answer, address); |
87 | free (address); |
88 | |
89 | str_recv_from (sender, &message_string, &address); |
90 | TEST_ASSERT_EQUAL_STRING (test_answer, message_string); |
91 | TEST_ASSERT_EQUAL_STRING (strrchr (ENDPOINT_4, '/') + 1, address); |
92 | free (message_string); |
93 | free (address); |
94 | |
95 | test_context_socket_close (sender); |
96 | test_context_socket_close (listener); |
97 | } |
98 | |
99 | int main (void) |
100 | { |
101 | setup_test_environment (); |
102 | |
103 | UNITY_BEGIN (); |
104 | RUN_TEST (test_connect_fails); |
105 | RUN_TEST (test_roundtrip); |
106 | return UNITY_END (); |
107 | } |
108 | |