source: rtems-libbsd/testsuite/program01/test_main.c @ 3d91340

55-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 3d91340 was 3d91340, checked in by Christian Mauderer <Christian.Mauderer@…>, on Jul 15, 2016 at 9:39:48 AM

rtemsbsd: Add call to program with a data restore

This adds the rtems_bsd_program_call_main_width_data_restore function.

  • Property mode set to 100644
File size: 8.1 KB
Line 
1/*
2 * Copyright (c) 2013 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 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <err.h>
33#include <assert.h>
34#include <errno.h>
35#include <stdlib.h>
36#include <unistd.h>
37#include <syslog.h>
38#include <sys/socket.h>
39#include <fcntl.h>
40
41#define RTEMS_BSD_PROGRAM_NO_EXIT_WRAP
42#define RTEMS_BSD_PROGRAM_NO_PRINTF_WRAP
43#include <machine/rtems-bsd-program.h>
44#include <machine/pcpu.h>
45
46#include <rtems.h>
47#include <rtems/libcsupport.h>
48
49#define TEST_NAME "LIBBSD SYSCALLS 1"
50
51typedef void (*no_mem_test_body)(int fd);
52
53typedef struct {
54        no_mem_test_body body;
55        int fd;
56        rtems_id master_task;
57} no_mem_test;
58
59static void
60no_mem_task(rtems_task_argument arg)
61{
62        const no_mem_test *self = (const no_mem_test *) arg;
63        rtems_status_code sc;
64        void *greedy;
65
66        assert(rtems_configuration_get_unified_work_area());
67
68        greedy = rtems_workspace_greedy_allocate(NULL, 0);
69        (*self->body)(self->fd);
70        rtems_workspace_greedy_free(greedy);
71
72        sc = rtems_event_transient_send(self->master_task);
73        assert(sc == RTEMS_SUCCESSFUL);
74
75        sc = rtems_task_suspend(RTEMS_SELF);
76        assert(sc == RTEMS_SUCCESSFUL);
77}
78
79void
80do_no_mem_test(no_mem_test_body body, int fd)
81{
82        no_mem_test test = {
83                .body = body,
84                .fd = fd,
85                .master_task = rtems_task_self()
86        };
87        rtems_status_code sc;
88        rtems_id id;
89        rtems_resource_snapshot snapshot;
90
91        rtems_resource_snapshot_take(&snapshot);
92
93        sc = rtems_task_create(
94                rtems_build_name('N', 'M', 'E', 'M'),
95                RTEMS_MINIMUM_PRIORITY,
96                RTEMS_MINIMUM_STACK_SIZE,
97                RTEMS_DEFAULT_MODES,
98                RTEMS_FLOATING_POINT,
99                &id
100        );
101        assert(sc == RTEMS_SUCCESSFUL);
102
103        sc = rtems_task_start(id, no_mem_task, (rtems_task_argument) &test);
104        assert(sc == RTEMS_SUCCESSFUL);
105
106        sc = rtems_event_transient_receive(RTEMS_WAIT, RTEMS_NO_TIMEOUT);
107        assert(sc == RTEMS_SUCCESSFUL);
108
109        sc = rtems_task_delete(id);
110        assert(sc == RTEMS_SUCCESSFUL);
111
112        assert(rtems_resource_snapshot_check(&snapshot));
113}
114
115static const char prog_name[] = "prog";
116
117static int
118invalid_prog(void *ctx)
119{
120        (void) ctx;
121
122        assert(0);
123}
124
125static int
126invalid_main(int argc, char **argv)
127{
128        (void) argc;
129        (void) argv;
130
131        assert(0);
132}
133
134static void *const some_context = (void *) 0xcafe;
135
136static int
137some_prog(void *ctx)
138{
139        assert(ctx == some_context);
140        assert(strcmp(rtems_bsd_program_get_name(), prog_name) == 0);
141        assert(rtems_bsd_program_get_context() == some_context);
142        errno = 0;
143        rtems_bsd_program_exit(456);
144}
145
146static const int some_argc = 1;
147
148static char *some_argv[] = { "a", NULL };
149
150static int
151some_main(int argc, char **argv)
152{
153        assert(argc == some_argc);
154        assert(argv == some_argv);
155        assert(strcmp(rtems_bsd_program_get_name(), prog_name) == 0);
156        errno = 0;
157        rtems_bsd_program_exit(789);
158}
159
160static void
161no_mem_bsd_program(int fd)
162{
163        (void) fd;
164
165        assert(rtems_bsd_program_call(prog_name, invalid_prog, NULL)
166            == EXIT_FAILURE);
167        assert(rtems_bsd_program_call_main(prog_name, invalid_main, some_argc,
168            some_argv) == EXIT_FAILURE);
169        assert(strcmp(rtems_bsd_program_get_name(), "?") == 0);
170        assert(rtems_bsd_program_get_context() == NULL);
171}
172
173#define OVERWRITE_CONTENT       "Some test pattern"
174#define OVERWRITE_AFTER_RESTORE "xxxxxtestxxxxxxxx"
175#define OVERWRITE_RESTORE_FIRST (5)
176#define OVERWRITE_RESTORE_SIZE (4)
177static const char overwrite_compare[] = OVERWRITE_AFTER_RESTORE;
178static char overwrite_me[] = OVERWRITE_CONTENT;
179
180static int
181overwrite_main(int argc, char **argv)
182{
183        size_t len = strlen(overwrite_me);
184        memset(overwrite_me, 'x', len);
185        assert(strcmp(overwrite_me, overwrite_compare) != 0);
186        errno = 0;
187        rtems_bsd_program_exit(1012);
188}
189
190static void
191test_bsd_program(void)
192{
193        rtems_resource_snapshot snapshot;
194        int exit_code;
195        void *greedy;
196        char *invalid_argv[2] = { "a", "b" };
197        struct thread *td;
198
199        assert(rtems_configuration_get_unified_work_area());
200
201        puts("test BSD program");
202
203        /* Needs to be called once before the snapshot is taken so that the
204         * thread structure is already created. */
205        td = rtems_bsd_get_curthread_or_null();
206        assert(td != NULL);
207
208        rtems_resource_snapshot_take(&snapshot);
209
210        do_no_mem_test(no_mem_bsd_program, -1);
211
212        greedy = rtems_workspace_greedy_allocate(NULL, 0);
213        no_mem_bsd_program(-1);
214        rtems_workspace_greedy_free(greedy);
215
216        assert(rtems_resource_snapshot_check(&snapshot));
217
218        errno = 0;
219        exit_code = rtems_bsd_program_call_main(prog_name, NULL, 1, invalid_argv);
220        assert(errno == EFAULT);
221        assert(exit_code == EXIT_FAILURE);
222
223        assert(rtems_resource_snapshot_check(&snapshot));
224
225        errno = EINVAL;
226        exit_code = rtems_bsd_program_call(prog_name, some_prog, some_context);
227        assert(errno == 0);
228        assert(exit_code == 456);
229        assert(strcmp(rtems_bsd_program_get_name(), "?") == 0);
230        assert(rtems_bsd_program_get_context() == NULL);
231
232        assert(rtems_resource_snapshot_check(&snapshot));
233
234        errno = EINVAL;
235        exit_code = rtems_bsd_program_call_main(prog_name, some_main,
236            some_argc, some_argv);
237        assert(errno == 0);
238        assert(exit_code == 789);
239        assert(strcmp(rtems_bsd_program_get_name(), "?") == 0);
240        assert(rtems_bsd_program_get_context() == NULL);
241
242        assert(rtems_resource_snapshot_check(&snapshot));
243
244        exit_code = rtems_bsd_program_call_main_with_data_restore(prog_name,
245            overwrite_main, some_argc, some_argv,
246            overwrite_me + OVERWRITE_RESTORE_FIRST, OVERWRITE_RESTORE_SIZE);
247        assert(errno == 0);
248        assert(exit_code == 1012);
249        assert(strcmp(rtems_bsd_program_get_name(), "?") == 0);
250        assert(rtems_bsd_program_get_context() == NULL);
251        assert(strcmp(overwrite_me, overwrite_compare) == 0);
252
253        assert(rtems_resource_snapshot_check(&snapshot));
254}
255
256static void
257test_warn(void)
258{
259        puts("test warn");
260
261        errno = EAGAIN;
262        warn("%s", "warn");
263
264        errno = ENAMETOOLONG;
265        warn(NULL);
266
267        errno = 0;
268        warnc(EDOM, "%s", "warnc");
269
270        errno = 0;
271        warnc(ERANGE, NULL);
272
273        warnx("%s", "warnx");
274
275        warnx(NULL);
276}
277
278static int
279call_err(void *ctx)
280{
281        errno = EAGAIN;
282        err(10, "%s", "call_err");
283}
284
285static int
286call_err_null(void *ctx)
287{
288        errno = ENAMETOOLONG;
289        err(11, NULL);
290}
291
292static int
293call_errc(void *ctx)
294{
295        errc(12, EDOM, "%s", "call_errc");
296}
297
298static int
299call_errc_null(void *ctx)
300{
301        errc(13, ERANGE, NULL);
302}
303
304static int
305call_errx(void *ctx)
306{
307        errx(14, "%s", "call_errx");
308}
309
310static int
311call_errx_null(void *ctx)
312{
313        errx(15, NULL);
314}
315
316static void
317test_err(void)
318{
319        int exit_code;
320
321        puts("test err");
322
323        exit_code = rtems_bsd_program_call("err", call_err, NULL);
324        assert(exit_code == 10);
325
326        exit_code = rtems_bsd_program_call("err", call_err_null, NULL);
327        assert(exit_code == 11);
328
329        exit_code = rtems_bsd_program_call("errc", call_errc, NULL);
330        assert(exit_code == 12);
331
332        exit_code = rtems_bsd_program_call("errc", call_errc_null, NULL);
333        assert(exit_code == 13);
334
335        exit_code = rtems_bsd_program_call("errx", call_errx, NULL);
336        assert(exit_code == 14);
337
338        exit_code = rtems_bsd_program_call("errx", call_errx_null, NULL);
339        assert(exit_code == 15);
340}
341
342static void
343test_main(void)
344{
345        test_bsd_program();
346        test_warn();
347        test_err();
348
349        exit(0);
350}
351
352#include <rtems/bsd/test/default-init.h>
Note: See TracBrowser for help on using the repository browser.