| 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 <netdb.h> |
| 34 | #include <string.h> |
| 35 | |
| 36 | #ifndef _WIN32 |
| 37 | #include <unistd.h> |
| 38 | #endif |
| 39 | |
| 40 | SETUP_TEARDOWN_TESTCONTEXT |
| 41 | |
| 42 | void test_poll_fd () |
| 43 | { |
| 44 | int recv_socket = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
| 45 | TEST_ASSERT_NOT_EQUAL (-1, recv_socket); |
| 46 | |
| 47 | int flag = 1; |
| 48 | TEST_ASSERT_SUCCESS_ERRNO ( |
| 49 | setsockopt (recv_socket, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof (int))); |
| 50 | |
| 51 | struct sockaddr_in saddr = bind_bsd_socket (recv_socket); |
| 52 | |
| 53 | void *sb = test_context_socket (ZMQ_REP); |
| 54 | |
| 55 | TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (sb, "tcp://127.0.0.1:*" )); |
| 56 | |
| 57 | zmq_pollitem_t pollitems[] = { |
| 58 | {sb, 0, ZMQ_POLLIN, 0}, |
| 59 | {NULL, recv_socket, ZMQ_POLLIN, 0}, |
| 60 | }; |
| 61 | |
| 62 | int send_socket = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
| 63 | TEST_ASSERT_NOT_EQUAL (-1, send_socket); |
| 64 | |
| 65 | char buf[10]; |
| 66 | memset (buf, 1, 10); |
| 67 | |
| 68 | TEST_ASSERT_SUCCESS_ERRNO (sendto ( |
| 69 | send_socket, buf, 10, 0, (struct sockaddr *) &saddr, sizeof (saddr))); |
| 70 | |
| 71 | TEST_ASSERT_EQUAL (1, zmq_poll (pollitems, 2, 1)); |
| 72 | TEST_ASSERT_BITS_LOW (ZMQ_POLLIN, pollitems[0].revents); |
| 73 | TEST_ASSERT_BITS_HIGH (ZMQ_POLLIN, pollitems[1].revents); |
| 74 | |
| 75 | test_context_socket_close (sb); |
| 76 | |
| 77 | close (send_socket); |
| 78 | close (recv_socket); |
| 79 | } |
| 80 | |
| 81 | int main () |
| 82 | { |
| 83 | UNITY_BEGIN (); |
| 84 | RUN_TEST (test_poll_fd); |
| 85 | return UNITY_END (); |
| 86 | } |
| 87 | |