1/*
2 Copyright (c) 2007-2017 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 <string.h>
34
35SETUP_TEARDOWN_TESTCONTEXT
36
37void ffn (void *data_, void *hint_)
38{
39 // Signal that ffn has been called by writing "freed" to hint
40 (void) data_; // Suppress 'unused' warnings at compile time
41 memcpy (hint_, (void *) "freed", 5);
42}
43
44void test_msg_ffn ()
45{
46 // Create the infrastructure
47 char my_endpoint[MAX_SOCKET_STRING];
48
49 void *router = test_context_socket (ZMQ_ROUTER);
50 bind_loopback_ipv4 (router, my_endpoint, sizeof my_endpoint);
51
52 void *dealer = test_context_socket (ZMQ_DEALER);
53 TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (dealer, my_endpoint));
54
55 // Test that creating and closing a message triggers ffn
56 zmq_msg_t msg;
57 char hint[5];
58 char data[255];
59 memset (data, 0, 255);
60 memcpy (data, (void *) "data", 4);
61 memcpy (hint, (void *) "hint", 4);
62 TEST_ASSERT_SUCCESS_ERRNO (
63 zmq_msg_init_data (&msg, (void *) data, 255, ffn, (void *) hint));
64 TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
65
66 msleep (SETTLE_TIME);
67 TEST_ASSERT_EQUAL_STRING_LEN ("freed", hint, 5);
68 memcpy (hint, (void *) "hint", 4);
69
70 // Making and closing a copy triggers ffn
71 zmq_msg_t msg2;
72 zmq_msg_init (&msg2);
73 TEST_ASSERT_SUCCESS_ERRNO (
74 zmq_msg_init_data (&msg, (void *) data, 255, ffn, (void *) hint));
75 TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_copy (&msg2, &msg));
76 TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg2));
77 TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
78
79 msleep (SETTLE_TIME);
80 TEST_ASSERT_EQUAL_STRING_LEN ("freed", hint, 5);
81 memcpy (hint, (void *) "hint", 4);
82
83 // Test that sending a message triggers ffn
84 TEST_ASSERT_SUCCESS_ERRNO (
85 zmq_msg_init_data (&msg, (void *) data, 255, ffn, (void *) hint));
86
87 zmq_msg_send (&msg, dealer, 0);
88 char buf[255];
89 TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (router, buf, 255, 0));
90 TEST_ASSERT_EQUAL_INT (255, zmq_recv (router, buf, 255, 0));
91 TEST_ASSERT_EQUAL_STRING_LEN (data, buf, 4);
92
93 msleep (SETTLE_TIME);
94 TEST_ASSERT_EQUAL_STRING_LEN ("freed", hint, 5);
95 memcpy (hint, (void *) "hint", 4);
96 TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
97
98 // Sending a copy of a message triggers ffn
99 TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&msg2));
100 TEST_ASSERT_SUCCESS_ERRNO (
101 zmq_msg_init_data (&msg, (void *) data, 255, ffn, (void *) hint));
102 TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_copy (&msg2, &msg));
103
104 zmq_msg_send (&msg, dealer, 0);
105 TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (router, buf, 255, 0));
106 TEST_ASSERT_EQUAL_INT (255, zmq_recv (router, buf, 255, 0));
107 TEST_ASSERT_EQUAL_STRING_LEN (data, buf, 4);
108 TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg2));
109 TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
110
111 msleep (SETTLE_TIME);
112 TEST_ASSERT_EQUAL_STRING_LEN ("freed", hint, 5);
113
114 // Deallocate the infrastructure.
115 test_context_socket_close (router);
116 test_context_socket_close (dealer);
117}
118
119int main (void)
120{
121 setup_test_environment ();
122
123 UNITY_BEGIN ();
124 RUN_TEST (test_msg_ffn);
125 return UNITY_END ();
126}
127