libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
test_filter_ipc.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2007-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 
32 static void bounce_fail (void *server, void *client)
33 {
34  const char *content = "12345678ABCDEFGH12345678abcdefgh";
35  char buffer [32];
36 
37  // Send message from client to server
38  int rc = zmq_send (client, content, 32, ZMQ_SNDMORE);
39  assert (rc == 32);
40  rc = zmq_send (client, content, 32, 0);
41  assert (rc == 32);
42 
43  // Receive message at server side (should not succeed)
44  int timeout = 250;
45  rc = zmq_setsockopt (server, ZMQ_RCVTIMEO, &timeout, sizeof (int));
46  assert (rc == 0);
47  rc = zmq_recv (server, buffer, 32, 0);
48  assert (rc == -1);
49  assert (zmq_errno () == EAGAIN);
50 
51  // Send message from server to client to test other direction
52  rc = zmq_setsockopt (server, ZMQ_SNDTIMEO, &timeout, sizeof (int));
53  assert (rc == 0);
54  rc = zmq_send (server, content, 32, ZMQ_SNDMORE);
55  assert (rc == -1);
56  assert (zmq_errno () == EAGAIN);
57 }
58 
59 template <class T>
60 static void run_test (int opt, T optval, int expected_error, int bounce_test)
61 {
62  int rc;
63 
64  void *ctx = zmq_ctx_new ();
65  assert (ctx);
66 
67  void *sb = zmq_socket (ctx, ZMQ_DEALER);
68  assert (sb);
69 
70  if (opt) {
71  rc = zmq_setsockopt(sb, opt, &optval, sizeof (optval));
72  if (expected_error) {
73  assert (rc == -1);
74  assert (zmq_errno () == expected_error);
75  }
76  else
77  assert (rc == 0);
78  }
79 
80  void *sc = zmq_socket (ctx, ZMQ_DEALER);
81  assert (sc);
82 
83  // If a test fails, don't hang for too long
84  int timeout = 2500;
85  rc = zmq_setsockopt (sb, ZMQ_RCVTIMEO, &timeout, sizeof (int));
86  assert (rc == 0);
87  rc = zmq_setsockopt (sb, ZMQ_SNDTIMEO, &timeout, sizeof (int));
88  assert (rc == 0);
89  rc = zmq_setsockopt (sc, ZMQ_RCVTIMEO, &timeout, sizeof (int));
90  assert (rc == 0);
91  rc = zmq_setsockopt (sc, ZMQ_SNDTIMEO, &timeout, sizeof (int));
92  assert (rc == 0);
93  int interval = -1;
94  rc = zmq_setsockopt (sc, ZMQ_RECONNECT_IVL, &interval, sizeof (int));
95  assert (rc == 0);
96 
97  if (bounce_test) {
98  const char* endpoint = "ipc://test_filter_ipc.sock";
99  int rc = zmq_bind (sb, endpoint);
100  assert (rc == 0);
101 
102  rc = zmq_connect (sc, endpoint);
103  assert (rc == 0);
104 
105  if (bounce_test > 0)
106  bounce (sb, sc);
107  else
108  bounce_fail (sb, sc);
109  }
110  close_zero_linger (sc);
111  close_zero_linger (sb);
112 
113  rc = zmq_ctx_term (ctx);
114  assert (rc == 0);
115 }
116 
117 int main (void)
118 {
119 #if !defined (ZMQ_HAVE_WINDOWS)
121 
122  // No filters
123  run_test<int> (0, 0, 0, 1);
124 
125 #if defined ZMQ_HAVE_SO_PEERCRED || defined ZMQ_HAVE_LOCAL_PEERCRED
126  // Get the group and supplimental groups of the process owner
127  gid_t groups[100];
128  int ngroups = getgroups(100, groups);
129  assert (ngroups != -1);
130  gid_t group = getgid(), supgroup = group, notgroup = group + 1;
131  for (int i = 0; i < ngroups; i++) {
132  if (supgroup == group && group != groups[i])
133  supgroup = groups[i];
134  if (notgroup <= groups[i])
135  notgroup = groups[i] + 1;
136  }
137 
138  // Test filter with UID of process owner
139  run_test<uid_t> (ZMQ_IPC_FILTER_UID, getuid(), 0, 1);
140  // Test filter with UID of another (possibly non-existent) user
141  run_test<uid_t> (ZMQ_IPC_FILTER_UID, getuid() + 1, 0, -1);
142  // Test filter with GID of process owner
143  run_test<gid_t> (ZMQ_IPC_FILTER_GID, group, 0, 1);
144  // Test filter with supplimental group of process owner
145  run_test<gid_t> (ZMQ_IPC_FILTER_GID, supgroup, 0, 1);
146  // Test filter with GID of another (possibly non-existent) group
147  run_test<gid_t> (ZMQ_IPC_FILTER_GID, notgroup, 0, -1);
148 # if defined ZMQ_HAVE_SO_PEERCRED
149  // Test filter with PID of current process
150  run_test<pid_t> (ZMQ_IPC_FILTER_PID, getpid(), 0, 1);
151  // Test filter with PID of another (possibly non-existent) process
152  run_test<pid_t> (ZMQ_IPC_FILTER_PID, getpid() + 1, 0, -1);
153 # else
154  // Setup of PID filter should fail with operation not supported error
155  run_test<pid_t> (ZMQ_IPC_FILTER_PID, getpid(), EINVAL, 0);
156 # endif
157 #else
158  run_test<uid_t> (ZMQ_IPC_FILTER_UID, 0, EINVAL, 0);
159  run_test<gid_t> (ZMQ_IPC_FILTER_GID, 0, EINVAL, 0);
160  run_test<pid_t> (ZMQ_IPC_FILTER_PID, 0, EINVAL, 0);
161 #endif // defined ZMQ_HAVE_SO_PEERCRED || defined ZMQ_HAVE_LOCAL_PEERCRED
162 
163 #endif
164  return 0 ;
165 }
166 
ZMQ_EXPORT int zmq_setsockopt(void *s, int option, const void *optval, size_t optvallen)
Definition: zmq.cpp:265
ZMQ_EXPORT void * zmq_ctx_new(void)
Definition: zmq.cpp:115
#define ZMQ_SNDMORE
Definition: zmq.h:346
#define ZMQ_DEALER
Definition: zmq.h:251
#define ZMQ_IPC_FILTER_GID
Definition: zmq.h:361
void setup_test_environment(void)
Definition: testutil.hpp:285
ZMQ_EXPORT int zmq_recv(void *s, void *buf, size_t len, int flags)
Definition: zmq.cpp:507
#define ZMQ_IPC_FILTER_UID
Definition: zmq.h:360
ZMQ_EXPORT int zmq_errno(void)
Definition: zmq.cpp:107
ZMQ_EXPORT void * zmq_socket(void *, int type)
Definition: zmq.cpp:244
#define ZMQ_SNDTIMEO
Definition: zmq.h:285
#define ZMQ_IPC_FILTER_PID
Definition: zmq.h:359
int main(void)
ZMQ_EXPORT int zmq_connect(void *s, const char *addr)
Definition: zmq.cpp:332
static void run_test(int opt, T optval, int expected_error, int bounce_test)
ZMQ_EXPORT int zmq_send(void *s, const void *buf, size_t len, int flags)
Definition: zmq.cpp:387
void close_zero_linger(void *socket)
Definition: testutil.hpp:275
void bounce(void *server, void *client)
Definition: testutil.hpp:73
ZMQ_EXPORT int zmq_bind(void *s, const char *addr)
Definition: zmq.cpp:321
ZMQ_EXPORT int zmq_ctx_term(void *context)
Definition: zmq.cpp:162
#define ZMQ_RECONNECT_IVL
Definition: zmq.h:277
#define ZMQ_RCVTIMEO
Definition: zmq.h:284
static void bounce_fail(void *server, void *client)