source: rtems/testsuites/libtests/syscall01/init.c @ c4b8b147

5
Last change on this file since c4b8b147 was c4b8b147, checked in by Sebastian Huber <sebastian.huber@…>, on 11/03/17 at 07:35:38

tests: Use simple console driver

Update #3170.
Update #3199.

  • Property mode set to 100644
File size: 7.1 KB
Line 
1/*
2 * Copyright (c) 2012-2015 embedded brains GmbH.  All rights reserved.
3 *
4 *  embedded brains GmbH
5 *  Dornierstr. 4
6 *  82178 Puchheim
7 *  Germany
8 *  <rtems@embedded-brains.de>
9 *
10 * The license and distribution terms for this file may be
11 * found in the file LICENSE in this distribution or at
12 * http://www.rtems.org/license/LICENSE.
13 */
14
15#ifdef HAVE_CONFIG_H
16  #include "config.h"
17#endif
18
19#include "tmacros.h"
20
21#include <sys/select.h>
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <netinet/in.h>
25#include <errno.h>
26#include <fcntl.h>
27#include <unistd.h>
28
29#include <rtems/libio.h>
30#include <rtems/rtems_bsdnet.h>
31
32const char rtems_test_name[] = "SYSCALL 1";
33
34static const char open_driver_path [] = "/dev/open_driver";
35
36struct rtems_bsdnet_config rtems_bsdnet_config;
37
38typedef struct {
39  rtems_id main_task;
40  rtems_id close_task;
41  int fd;
42} test_context;
43
44static test_context test_instance;
45
46static void test_sync(void)
47{
48  int rv;
49  char buf [1];
50  ssize_t n;
51  int fd;
52
53  fd = open(open_driver_path, O_RDWR);
54  rtems_test_assert(fd >= 0);
55
56  errno = 0;
57  n = send(fd, buf, sizeof(buf), 0);
58  rtems_test_assert(n == -1);
59  rtems_test_assert(errno == ENOTSOCK);
60
61  errno = 0;
62  n = recv(fd, buf, sizeof(buf), 0);
63  rtems_test_assert(n == -1);
64  rtems_test_assert(errno == ENOTSOCK);
65
66  rv = close(fd);
67  rtems_test_assert(rv == 0);
68
69  fd = socket(PF_INET, SOCK_DGRAM, 0);
70  rtems_test_assert(fd >= 0);
71
72  errno = 0;
73  rv = fsync(fd);
74  rtems_test_assert(rv == -1);
75  rtems_test_assert(errno == EINVAL);
76
77  errno = 0;
78  rv = fdatasync(fd);
79  rtems_test_assert(rv == -1);
80  rtems_test_assert(errno == EINVAL);
81
82  rv = close(fd);
83  rtems_test_assert(rv == 0);
84}
85
86static void close_task(rtems_task_argument arg)
87{
88  test_context *ctx = (test_context *) arg;
89
90  while (true) {
91    rtems_status_code sc;
92    int rv;
93
94    sc = rtems_event_transient_receive(RTEMS_WAIT, RTEMS_NO_TIMEOUT);
95    rtems_test_assert(sc == RTEMS_SUCCESSFUL);
96
97    rv = close(ctx->fd);
98    rtems_test_assert(rv == 0);
99
100    sc = rtems_event_transient_send(ctx->main_task);
101    rtems_test_assert(sc == RTEMS_SUCCESSFUL);
102  }
103}
104
105static void request_close(test_context *ctx)
106{
107  rtems_status_code sc;
108
109  sc = rtems_event_transient_send(ctx->close_task);
110  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
111}
112
113static void wait_for_close_task(void)
114{
115  rtems_status_code sc;
116
117  sc = rtems_event_transient_receive(RTEMS_WAIT, RTEMS_NO_TIMEOUT);
118  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
119}
120
121static void test_accept_and_close(test_context *ctx)
122{
123  int rv;
124  int fd;
125  struct sockaddr_in addr;
126  socklen_t addrlen = sizeof(addr);
127
128  ctx->fd = socket(PF_INET, SOCK_STREAM, 0);
129  rtems_test_assert(ctx->fd >= 0);
130
131  rv = listen(ctx->fd, 1);
132  rtems_test_assert(rv == 0);
133
134  request_close(ctx);
135
136  errno = 0;
137  fd = accept(ctx->fd, (struct sockaddr *) &addr, &addrlen);
138  rtems_test_assert(fd == -1);
139  rtems_test_assert(errno == ENXIO);
140
141  errno = 0;
142  fd = accept(ctx->fd, (struct sockaddr *) &addr, &addrlen);
143  rtems_test_assert(fd == -1);
144  rtems_test_assert(errno == EBADF);
145
146  wait_for_close_task();
147}
148
149static void test_connect_and_close(test_context *ctx)
150{
151  int rv;
152  struct sockaddr_in addr;
153  socklen_t addrlen = sizeof(addr);
154
155  ctx->fd = socket(PF_INET, SOCK_STREAM, 0);
156  rtems_test_assert(ctx->fd >= 0);
157
158  memset(&addr, 0, sizeof(addr));
159  addr.sin_family = AF_INET;
160  addr.sin_port = htons(1234);
161  addr.sin_addr.s_addr = htonl(INADDR_ANY);
162
163  request_close(ctx);
164
165  errno = 0;
166  rv = connect(ctx->fd, (struct sockaddr *) &addr, addrlen);
167  rtems_test_assert(rv == -1);
168  rtems_test_assert(errno == ENXIO);
169
170  errno = 0;
171  rv = connect(ctx->fd, (struct sockaddr *) &addr, addrlen);
172  rtems_test_assert(rv == -1);
173  rtems_test_assert(errno == EBADF);
174
175  wait_for_close_task();
176}
177
178static void test_recv_and_close(test_context *ctx)
179{
180  int rv;
181  struct sockaddr_in addr;
182  socklen_t addrlen = sizeof(addr);
183  char buf[1];
184  ssize_t n;
185
186  ctx->fd = socket(PF_INET, SOCK_DGRAM, 0);
187  rtems_test_assert(ctx->fd >= 0);
188
189  memset(&addr, 0, sizeof(addr));
190  addr.sin_family = AF_INET;
191  addr.sin_port = htons(1234);
192  addr.sin_addr.s_addr = htonl(INADDR_ANY);
193
194  rv = bind(ctx->fd, (struct sockaddr *) &addr, addrlen);
195  rtems_test_assert(rv == 0);
196
197  request_close(ctx);
198
199  errno = 0;
200  n = recv(ctx->fd, &buf[0], sizeof(buf), 0);
201  rtems_test_assert(n == -1);
202  rtems_test_assert(errno == ENXIO);
203
204  errno = 0;
205  n = recv(ctx->fd, &buf[0], sizeof(buf), 0);
206  rtems_test_assert(n == -1);
207  rtems_test_assert(errno == EBADF);
208
209  wait_for_close_task();
210}
211
212static void test_select_and_close(test_context *ctx)
213{
214  int rv;
215  struct sockaddr_in addr;
216  socklen_t addrlen = sizeof(addr);
217  int nfds;
218  struct fd_set set;
219
220  ctx->fd = socket(PF_INET, SOCK_DGRAM, 0);
221  rtems_test_assert(ctx->fd >= 0);
222
223  memset(&addr, 0, sizeof(addr));
224  addr.sin_family = AF_INET;
225  addr.sin_port = htons(1234);
226  addr.sin_addr.s_addr = htonl(INADDR_ANY);
227
228  rv = bind(ctx->fd, (struct sockaddr *) &addr, addrlen);
229  rtems_test_assert(rv == 0);
230
231  nfds = ctx->fd + 1;
232  FD_ZERO(&set);
233  FD_SET(ctx->fd, &set);
234
235  request_close(ctx);
236
237  errno = 0;
238  rv = select(nfds, &set, NULL, NULL, NULL);
239  rtems_test_assert(rv == -1);
240  rtems_test_assert(errno == EBADF);
241
242  wait_for_close_task();
243}
244
245static void Init(rtems_task_argument arg)
246{
247  test_context *ctx = &test_instance;
248  rtems_status_code sc;
249  int rv;
250
251  TEST_BEGIN();
252
253  ctx->main_task = rtems_task_self();
254
255  sc = rtems_task_create(
256    rtems_build_name('C', 'L', 'O', 'S'),
257    2,
258    RTEMS_MINIMUM_STACK_SIZE,
259    RTEMS_DEFAULT_MODES,
260    RTEMS_DEFAULT_ATTRIBUTES,
261    &ctx->close_task
262  );
263  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
264
265  sc = rtems_task_start(
266    ctx->close_task,
267    close_task,
268    (rtems_task_argument) ctx
269  );
270  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
271
272  rv = rtems_bsdnet_initialize_network();
273  rtems_test_assert(rv == 0);
274
275  test_sync();
276  test_accept_and_close(ctx);
277  test_connect_and_close(ctx);
278  test_recv_and_close(ctx);
279  test_select_and_close(ctx);
280
281  sc = rtems_task_delete(ctx->close_task);
282  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
283
284  TEST_END();
285
286  rtems_test_exit(0);
287}
288
289static rtems_device_driver open_driver_initialize(
290  rtems_device_major_number major,
291  rtems_device_minor_number minor,
292  void *arg
293)
294{
295  rtems_status_code sc = rtems_io_register_name(open_driver_path, major, 0);
296  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
297
298  return RTEMS_SUCCESSFUL;
299}
300
301static rtems_device_driver open_driver_open(
302  rtems_device_major_number major,
303  rtems_device_minor_number minor,
304  void *arg
305)
306{
307  rtems_libio_open_close_args_t *oc = arg;
308
309  oc->iop->data0 = 1;
310  oc->iop->data1 = (void *) 1;
311
312  return RTEMS_SUCCESSFUL;
313}
314
315#define OPEN_DRIVER { \
316  .initialization_entry = open_driver_initialize, \
317  .open_entry = open_driver_open \
318}
319
320#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
321#define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
322#define CONFIGURE_APPLICATION_EXTRA_DRIVERS OPEN_DRIVER
323
324#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 4
325
326#define CONFIGURE_MAXIMUM_TASKS 3
327
328#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
329
330#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
331
332#define CONFIGURE_INIT_TASK_ATTRIBUTES RTEMS_FLOATING_POINT
333
334#define CONFIGURE_INIT
335
336#include <rtems/confdefs.h>
Note: See TracBrowser for help on using the repository browser.