1 | /* |
---|
2 | * Copyright 2010, Alin Rus <alin.codejunkie@gmail.com> |
---|
3 | * |
---|
4 | * The license and distribution terms for this file may be |
---|
5 | * found in the file LICENSE in this distribution or at |
---|
6 | * http://www.rtems.org/license/LICENSE. |
---|
7 | */ |
---|
8 | |
---|
9 | #ifdef HAVE_CONFIG_H |
---|
10 | #include "config.h" |
---|
11 | #endif |
---|
12 | |
---|
13 | #define CONFIGURE_INIT |
---|
14 | #include "system.h" |
---|
15 | #include <rtems.h> |
---|
16 | #include "tmacros.h" |
---|
17 | #include <rtems/posix/aio_misc.h> |
---|
18 | #include <aio.h> |
---|
19 | #include <stdlib.h> |
---|
20 | #include <unistd.h> |
---|
21 | #include <stdio.h> |
---|
22 | #include <sched.h> |
---|
23 | #include <fcntl.h> |
---|
24 | |
---|
25 | const char rtems_test_name[] = "PSXAIO 1"; |
---|
26 | |
---|
27 | #define BUFSIZE 512 |
---|
28 | #define WRONG_FD 404 |
---|
29 | |
---|
30 | /* forward declarations to avoid warnings */ |
---|
31 | struct aiocb *create_aiocb(int fd); |
---|
32 | void free_aiocb(struct aiocb *aiocbp); |
---|
33 | |
---|
34 | struct aiocb * |
---|
35 | create_aiocb (int fd) |
---|
36 | { |
---|
37 | struct aiocb *aiocbp; |
---|
38 | |
---|
39 | aiocbp = malloc (sizeof (struct aiocb)); |
---|
40 | memset (aiocbp, 0, sizeof (struct aiocb)); |
---|
41 | aiocbp->aio_buf = malloc (BUFSIZE * sizeof (char)); |
---|
42 | aiocbp->aio_nbytes = BUFSIZE; |
---|
43 | aiocbp->aio_offset = 0; |
---|
44 | aiocbp->aio_reqprio = 0; |
---|
45 | aiocbp->aio_fildes = fd; |
---|
46 | |
---|
47 | return aiocbp; |
---|
48 | } |
---|
49 | |
---|
50 | void |
---|
51 | free_aiocb (struct aiocb *aiocbp) |
---|
52 | { |
---|
53 | free ((char*) aiocbp->aio_buf); |
---|
54 | free (aiocbp); |
---|
55 | } |
---|
56 | |
---|
57 | void * |
---|
58 | POSIX_Init (void *argument) |
---|
59 | { |
---|
60 | int result, fd; |
---|
61 | struct aiocb *aiocbp; |
---|
62 | int status; |
---|
63 | |
---|
64 | rtems_aio_init (); |
---|
65 | |
---|
66 | status = mkdir ("/tmp", S_IRWXU); |
---|
67 | rtems_test_assert (!status); |
---|
68 | |
---|
69 | fd = open ("/tmp/aio_fildes", O_RDWR|O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO); |
---|
70 | rtems_test_assert ( fd != -1); |
---|
71 | |
---|
72 | TEST_BEGIN(); |
---|
73 | |
---|
74 | puts (" Init: EBADF TESTS "); |
---|
75 | |
---|
76 | aiocbp = create_aiocb (WRONG_FD); |
---|
77 | status = aio_write (aiocbp); |
---|
78 | rtems_test_assert (status == -1); |
---|
79 | |
---|
80 | /* Bad file descriptor */ |
---|
81 | puts ("Init: aio_write() - EBADF "); |
---|
82 | |
---|
83 | result = aio_return (aiocbp); |
---|
84 | rtems_test_assert (result == -1); |
---|
85 | status = aio_error (aiocbp); |
---|
86 | rtems_test_assert (status == EBADF); |
---|
87 | |
---|
88 | status = aio_read (aiocbp); |
---|
89 | rtems_test_assert (status == -1); |
---|
90 | |
---|
91 | /* Bad file descriptor */ |
---|
92 | puts ("Init: aio_read() - EBADF "); |
---|
93 | |
---|
94 | result = aio_return (aiocbp); |
---|
95 | rtems_test_assert (result == -1); |
---|
96 | status = aio_error (aiocbp); |
---|
97 | rtems_test_assert (status == EBADF); |
---|
98 | |
---|
99 | status = aio_cancel (WRONG_FD, NULL); |
---|
100 | rtems_test_assert (status == -1); |
---|
101 | |
---|
102 | /* Bad file descriptor */ |
---|
103 | puts ("Init: aio_cancel() - EBADF "); |
---|
104 | |
---|
105 | result = aio_return (aiocbp); |
---|
106 | rtems_test_assert (result == -1); |
---|
107 | status = aio_error (aiocbp); |
---|
108 | rtems_test_assert (status == EBADF); |
---|
109 | |
---|
110 | status = aio_fsync (O_SYNC, aiocbp); |
---|
111 | rtems_test_assert (status == -1); |
---|
112 | |
---|
113 | /* Bad file descriptor */ |
---|
114 | puts ("Init: aio_fsync() - EBADF "); |
---|
115 | |
---|
116 | result = aio_return (aiocbp); |
---|
117 | rtems_test_assert (result == -1); |
---|
118 | status = aio_error (aiocbp); |
---|
119 | rtems_test_assert (status == EBADF); |
---|
120 | |
---|
121 | free_aiocb (aiocbp); |
---|
122 | |
---|
123 | /* Invalid offset */ |
---|
124 | puts ("Init: aio_write() - EINVAL [aio_offset]"); |
---|
125 | |
---|
126 | aiocbp = create_aiocb (fd); |
---|
127 | aiocbp->aio_offset = -1; |
---|
128 | status = aio_write (aiocbp); |
---|
129 | rtems_test_assert (status == -1); |
---|
130 | |
---|
131 | result = aio_return (aiocbp); |
---|
132 | rtems_test_assert (result == -1); |
---|
133 | status = aio_error (aiocbp); |
---|
134 | rtems_test_assert (status == EINVAL); |
---|
135 | |
---|
136 | /* Invalid offset */ |
---|
137 | puts ("Init: aio_read() - EINVAL [aio_offset]"); |
---|
138 | |
---|
139 | status = aio_read (aiocbp); |
---|
140 | rtems_test_assert (status == -1); |
---|
141 | |
---|
142 | result = aio_return (aiocbp); |
---|
143 | rtems_test_assert (result == -1); |
---|
144 | status = aio_error (aiocbp); |
---|
145 | rtems_test_assert (status == EINVAL); |
---|
146 | |
---|
147 | free_aiocb (aiocbp); |
---|
148 | |
---|
149 | /* Invalid request priority */ |
---|
150 | puts ("Init: aio_write() - EINVAL [aio_reqprio]"); |
---|
151 | |
---|
152 | aiocbp = create_aiocb (fd); |
---|
153 | aiocbp->aio_reqprio = AIO_PRIO_DELTA_MAX + 1; |
---|
154 | status = aio_write (aiocbp); |
---|
155 | rtems_test_assert (status == -1); |
---|
156 | |
---|
157 | result = aio_return (aiocbp); |
---|
158 | rtems_test_assert (result == -1); |
---|
159 | status = aio_error (aiocbp); |
---|
160 | rtems_test_assert (status == EINVAL); |
---|
161 | |
---|
162 | /* Invalid request priority */ |
---|
163 | puts ("Init: aio_read() - EINVAL [aio_reqprio]"); |
---|
164 | |
---|
165 | status = aio_read (aiocbp); |
---|
166 | rtems_test_assert (status == -1); |
---|
167 | |
---|
168 | result = aio_return (aiocbp); |
---|
169 | rtems_test_assert (result == -1); |
---|
170 | status = aio_error (aiocbp); |
---|
171 | rtems_test_assert (status == EINVAL); |
---|
172 | |
---|
173 | /* Invalid request aio_cancel */ |
---|
174 | puts ("Init: aio_cancel() - EINVAL "); |
---|
175 | |
---|
176 | status = aio_cancel (WRONG_FD, aiocbp); |
---|
177 | rtems_test_assert (status == -1); |
---|
178 | |
---|
179 | result = aio_return (aiocbp); |
---|
180 | rtems_test_assert (result == -1); |
---|
181 | status = aio_error (aiocbp); |
---|
182 | rtems_test_assert (status == EINVAL); |
---|
183 | |
---|
184 | /* Invalid operation to aio_fsync */ |
---|
185 | puts ("Init: aio_fsync() - EINVAL "); |
---|
186 | status = aio_fsync (-1, aiocbp); |
---|
187 | rtems_test_assert (status == -1); |
---|
188 | |
---|
189 | result = aio_return (aiocbp); |
---|
190 | rtems_test_assert (result == -1); |
---|
191 | status = aio_error (aiocbp); |
---|
192 | rtems_test_assert (status == EINVAL); |
---|
193 | |
---|
194 | free_aiocb (aiocbp); |
---|
195 | |
---|
196 | TEST_END(); |
---|
197 | |
---|
198 | close (fd); |
---|
199 | rtems_test_exit (0); |
---|
200 | |
---|
201 | return NULL; |
---|
202 | |
---|
203 | } |
---|