summaryrefslogtreecommitdiffstats
path: root/testsuites/libtests/syscall01/init.c
blob: f2ddbbeb97a08abeb290b3bfccdb98d3e3aaee43 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
 * Copyright (c) 2012-2015 embedded brains GmbH.  All rights reserved.
 *
 *  embedded brains GmbH
 *  Dornierstr. 4
 *  82178 Puchheim
 *  Germany
 *  <rtems@embedded-brains.de>
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rtems.org/license/LICENSE.
 */

#ifdef HAVE_CONFIG_H
  #include "config.h"
#endif

#include "tmacros.h"

#include <sys/select.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>

#include <rtems/libio.h>
#include <rtems/rtems_bsdnet.h>

const char rtems_test_name[] = "SYSCALL 1";

static const char open_driver_path [] = "/dev/open_driver";

struct rtems_bsdnet_config rtems_bsdnet_config;

typedef struct {
  rtems_id main_task;
  rtems_id close_task;
  int fd;
} test_context;

static test_context test_instance;

static void test_sync(void)
{
  int rv;
  char buf [1];
  ssize_t n;
  int fd;

  fd = open(open_driver_path, O_RDWR);
  rtems_test_assert(fd >= 0);

  errno = 0;
  n = send(fd, buf, sizeof(buf), 0);
  rtems_test_assert(n == -1);
  rtems_test_assert(errno == ENOTSOCK);

  errno = 0;
  n = recv(fd, buf, sizeof(buf), 0);
  rtems_test_assert(n == -1);
  rtems_test_assert(errno == ENOTSOCK);

  rv = close(fd);
  rtems_test_assert(rv == 0);

  fd = socket(PF_INET, SOCK_DGRAM, 0);
  rtems_test_assert(fd >= 0);

  errno = 0;
  rv = fsync(fd);
  rtems_test_assert(rv == -1);
  rtems_test_assert(errno == EINVAL);

  errno = 0;
  rv = fdatasync(fd);
  rtems_test_assert(rv == -1);
  rtems_test_assert(errno == EINVAL);

  rv = close(fd);
  rtems_test_assert(rv == 0);
}

static void close_task(rtems_task_argument arg)
{
  test_context *ctx = (test_context *) arg;

  while (true) {
    rtems_status_code sc;
    int rv;

    sc = rtems_event_transient_receive(RTEMS_WAIT, RTEMS_NO_TIMEOUT);
    rtems_test_assert(sc == RTEMS_SUCCESSFUL);

    rv = close(ctx->fd);
    rtems_test_assert(rv == 0);

    sc = rtems_event_transient_send(ctx->main_task);
    rtems_test_assert(sc == RTEMS_SUCCESSFUL);
  }
}

static void request_close(test_context *ctx)
{
  rtems_status_code sc;

  sc = rtems_event_transient_send(ctx->close_task);
  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
}

static void wait_for_close_task(void)
{
  rtems_status_code sc;

  sc = rtems_event_transient_receive(RTEMS_WAIT, RTEMS_NO_TIMEOUT);
  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
}

static void test_accept_and_close(test_context *ctx)
{
  int rv;
  int fd;
  struct sockaddr_in addr;
  socklen_t addrlen = sizeof(addr);

  ctx->fd = socket(PF_INET, SOCK_STREAM, 0);
  rtems_test_assert(ctx->fd >= 0);

  rv = listen(ctx->fd, 1);
  rtems_test_assert(rv == 0);

  request_close(ctx);

  errno = 0;
  fd = accept(ctx->fd, (struct sockaddr *) &addr, &addrlen);
  rtems_test_assert(fd == -1);
  rtems_test_assert(errno == ENXIO);

  errno = 0;
  fd = accept(ctx->fd, (struct sockaddr *) &addr, &addrlen);
  rtems_test_assert(fd == -1);
  rtems_test_assert(errno == EBADF);

  wait_for_close_task();
}

static void test_connect_and_close(test_context *ctx)
{
  int rv;
  struct sockaddr_in addr;
  socklen_t addrlen = sizeof(addr);

  ctx->fd = socket(PF_INET, SOCK_STREAM, 0);
  rtems_test_assert(ctx->fd >= 0);

  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(1234);
  addr.sin_addr.s_addr = htonl(INADDR_ANY);

  request_close(ctx);

  errno = 0;
  rv = connect(ctx->fd, (struct sockaddr *) &addr, addrlen);
  rtems_test_assert(rv == -1);
  rtems_test_assert(errno == ENXIO);

  errno = 0;
  rv = connect(ctx->fd, (struct sockaddr *) &addr, addrlen);
  rtems_test_assert(rv == -1);
  rtems_test_assert(errno == EBADF);

  wait_for_close_task();
}

static void test_recv_and_close(test_context *ctx)
{
  int rv;
  struct sockaddr_in addr;
  socklen_t addrlen = sizeof(addr);
  char buf[1];
  ssize_t n;

  ctx->fd = socket(PF_INET, SOCK_DGRAM, 0);
  rtems_test_assert(ctx->fd >= 0);

  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(1234);
  addr.sin_addr.s_addr = htonl(INADDR_ANY);

  rv = bind(ctx->fd, (struct sockaddr *) &addr, addrlen);
  rtems_test_assert(rv == 0);

  request_close(ctx);

  errno = 0;
  n = recv(ctx->fd, &buf[0], sizeof(buf), 0);
  rtems_test_assert(n == -1);
  rtems_test_assert(errno == ENXIO);

  errno = 0;
  n = recv(ctx->fd, &buf[0], sizeof(buf), 0);
  rtems_test_assert(n == -1);
  rtems_test_assert(errno == EBADF);

  wait_for_close_task();
}

static void test_select_and_close(test_context *ctx)
{
  int rv;
  struct sockaddr_in addr;
  socklen_t addrlen = sizeof(addr);
  int nfds;
  struct fd_set set;

  ctx->fd = socket(PF_INET, SOCK_DGRAM, 0);
  rtems_test_assert(ctx->fd >= 0);

  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(1234);
  addr.sin_addr.s_addr = htonl(INADDR_ANY);

  rv = bind(ctx->fd, (struct sockaddr *) &addr, addrlen);
  rtems_test_assert(rv == 0);

  nfds = ctx->fd + 1;
  FD_ZERO(&set);
  FD_SET(ctx->fd, &set);

  request_close(ctx);

  errno = 0;
  rv = select(nfds, &set, NULL, NULL, NULL);
  rtems_test_assert(rv == -1);
  rtems_test_assert(errno == EBADF);

  wait_for_close_task();
}

static void Init(rtems_task_argument arg)
{
  test_context *ctx = &test_instance;
  rtems_status_code sc;
  int rv;

  TEST_BEGIN();

  ctx->main_task = rtems_task_self();

  sc = rtems_task_create(
    rtems_build_name('C', 'L', 'O', 'S'),
    2,
    RTEMS_MINIMUM_STACK_SIZE,
    RTEMS_DEFAULT_MODES,
    RTEMS_DEFAULT_ATTRIBUTES,
    &ctx->close_task
  );
  rtems_test_assert(sc == RTEMS_SUCCESSFUL);

  sc = rtems_task_start(
    ctx->close_task,
    close_task,
    (rtems_task_argument) ctx
  );
  rtems_test_assert(sc == RTEMS_SUCCESSFUL);

  rv = rtems_bsdnet_initialize_network();
  rtems_test_assert(rv == 0);

  test_sync();
  test_accept_and_close(ctx);
  test_connect_and_close(ctx);
  test_recv_and_close(ctx);
  test_select_and_close(ctx);

  sc = rtems_task_delete(ctx->close_task);
  rtems_test_assert(sc == RTEMS_SUCCESSFUL);

  TEST_END();

  rtems_test_exit(0);
}

static rtems_device_driver open_driver_initialize(
  rtems_device_major_number major,
  rtems_device_minor_number minor,
  void *arg
)
{
  rtems_status_code sc = rtems_io_register_name(open_driver_path, major, 0);
  rtems_test_assert(sc == RTEMS_SUCCESSFUL);

  return RTEMS_SUCCESSFUL;
}

static rtems_device_driver open_driver_open(
  rtems_device_major_number major,
  rtems_device_minor_number minor,
  void *arg
)
{
  rtems_libio_open_close_args_t *oc = arg;

  oc->iop->data0 = 1;
  oc->iop->data1 = (void *) 1;

  return RTEMS_SUCCESSFUL;
}

#define OPEN_DRIVER { \
  .initialization_entry = open_driver_initialize, \
  .open_entry = open_driver_open \
}

#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
#define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
#define CONFIGURE_APPLICATION_EXTRA_DRIVERS OPEN_DRIVER

#define CONFIGURE_MAXIMUM_FILE_DESCRIPTORS 4

#define CONFIGURE_MAXIMUM_TASKS 3

#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION

#define CONFIGURE_RTEMS_INIT_TASKS_TABLE

#define CONFIGURE_INIT_TASK_ATTRIBUTES RTEMS_FLOATING_POINT

#define CONFIGURE_INIT

#include <rtems/confdefs.h>