1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @ingroup tests |
---|
5 | * |
---|
6 | * @brief Block device tests. |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * Copyright (c) 2012 embedded brains GmbH. All rights reserved. |
---|
11 | * |
---|
12 | * embedded brains GmbH |
---|
13 | * Obere Lagerstr. 30 |
---|
14 | * 82178 Puchheim |
---|
15 | * Germany |
---|
16 | * <rtems@embedded-brains.de> |
---|
17 | * |
---|
18 | * The license and distribution terms for this file may be |
---|
19 | * found in the file LICENSE in this distribution or at |
---|
20 | * http://www.rtems.org/license/LICENSE. |
---|
21 | */ |
---|
22 | |
---|
23 | #ifdef HAVE_CONFIG_H |
---|
24 | #include "config.h" |
---|
25 | #endif |
---|
26 | |
---|
27 | #include "tmacros.h" |
---|
28 | |
---|
29 | #include <sys/stat.h> |
---|
30 | #include <stdio.h> |
---|
31 | #include <fcntl.h> |
---|
32 | #include <stdlib.h> |
---|
33 | #include <unistd.h> |
---|
34 | |
---|
35 | #include <rtems.h> |
---|
36 | #include <rtems/ramdisk.h> |
---|
37 | #include <rtems/diskdevs.h> |
---|
38 | #include <rtems/malloc.h> |
---|
39 | |
---|
40 | const char rtems_test_name[] = "BLOCK 11"; |
---|
41 | |
---|
42 | /* forward declarations to avoid warnings */ |
---|
43 | static rtems_task Init(rtems_task_argument argument); |
---|
44 | |
---|
45 | #define ASSERT_SC(sc) rtems_test_assert((sc) == RTEMS_SUCCESSFUL) |
---|
46 | |
---|
47 | #define CHUNK_MAX 32U |
---|
48 | |
---|
49 | #define AREA_SIZE ((CHUNK_MAX * (CHUNK_MAX + 1U)) / 2U) |
---|
50 | |
---|
51 | #define BLOCK_SIZE 4U |
---|
52 | |
---|
53 | #define BLOCK_COUNT (AREA_SIZE / BLOCK_SIZE) |
---|
54 | |
---|
55 | static const char rda [] = "/dev/rda"; |
---|
56 | |
---|
57 | static const char rda1 [] = "/dev/rda1"; |
---|
58 | |
---|
59 | static const char dev_invalid [] = "/dev/invalid"; |
---|
60 | |
---|
61 | static const char not_exist [] = "not_exist"; |
---|
62 | |
---|
63 | static const char not_blkdev [] = "not_blkdev"; |
---|
64 | |
---|
65 | static const char invalid_blkdev [] = "invalid_blkdev"; |
---|
66 | |
---|
67 | static long area_a [AREA_SIZE / sizeof(long)]; |
---|
68 | |
---|
69 | static long area_b [AREA_SIZE / sizeof(long)]; |
---|
70 | |
---|
71 | static rtems_status_code invalid_initialize( |
---|
72 | rtems_device_major_number major, |
---|
73 | rtems_device_minor_number minor, |
---|
74 | void *arg |
---|
75 | ) |
---|
76 | { |
---|
77 | rtems_status_code sc; |
---|
78 | |
---|
79 | sc = rtems_io_register_name(&dev_invalid[0], major, 0); |
---|
80 | ASSERT_SC(sc); |
---|
81 | |
---|
82 | return sc; |
---|
83 | } |
---|
84 | |
---|
85 | static rtems_status_code invalid_control( |
---|
86 | rtems_device_major_number major, |
---|
87 | rtems_device_minor_number minor, |
---|
88 | void *arg |
---|
89 | ) |
---|
90 | { |
---|
91 | return RTEMS_INVALID_NUMBER; |
---|
92 | } |
---|
93 | |
---|
94 | static void area_init(long *area) |
---|
95 | { |
---|
96 | size_t i; |
---|
97 | size_t n = AREA_SIZE / sizeof(long); |
---|
98 | |
---|
99 | for (i = 0; i < n; ++i) { |
---|
100 | area [i] = mrand48(); |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | static void area_read(int fd, long *area) |
---|
105 | { |
---|
106 | size_t i; |
---|
107 | size_t n = CHUNK_MAX; |
---|
108 | char *dst = (char *) area; |
---|
109 | |
---|
110 | for (i = 0; i <= n; ++i) { |
---|
111 | ssize_t m = read(fd, dst, i); |
---|
112 | rtems_test_assert(m == (ssize_t) i); |
---|
113 | dst += i; |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | static void area_write(int fd, const long *area) |
---|
118 | { |
---|
119 | size_t i; |
---|
120 | size_t n = CHUNK_MAX; |
---|
121 | const char *src = (const char *) area; |
---|
122 | |
---|
123 | for (i = 0; i <= n; ++i) { |
---|
124 | ssize_t m = write(fd, src, i); |
---|
125 | rtems_test_assert(m == (ssize_t) i); |
---|
126 | src += i; |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | static void area_compare(const long *area_a, const long *area_b, bool equal) |
---|
131 | { |
---|
132 | bool actual_equal = memcmp(area_a, area_b, AREA_SIZE) == 0; |
---|
133 | rtems_test_assert(actual_equal == equal); |
---|
134 | } |
---|
135 | |
---|
136 | static void test_blkdev_imfs_read_and_write(void) |
---|
137 | { |
---|
138 | rtems_status_code sc; |
---|
139 | int rv; |
---|
140 | ramdisk *rd; |
---|
141 | int fd; |
---|
142 | off_t off; |
---|
143 | |
---|
144 | rd = ramdisk_allocate(area_a, BLOCK_SIZE, BLOCK_COUNT, false); |
---|
145 | rtems_test_assert(rd != NULL); |
---|
146 | |
---|
147 | ramdisk_enable_free_at_delete_request(rd); |
---|
148 | |
---|
149 | sc = rtems_blkdev_create( |
---|
150 | rda, |
---|
151 | BLOCK_SIZE, |
---|
152 | BLOCK_COUNT, |
---|
153 | ramdisk_ioctl, |
---|
154 | rd |
---|
155 | ); |
---|
156 | ASSERT_SC(sc); |
---|
157 | |
---|
158 | fd = open(rda, O_RDWR); |
---|
159 | rtems_test_assert(fd >= 0); |
---|
160 | |
---|
161 | area_init(area_a); |
---|
162 | area_read(fd, area_b); |
---|
163 | area_compare(area_a, area_b, true); |
---|
164 | |
---|
165 | off = lseek(fd, 0, SEEK_SET); |
---|
166 | rtems_test_assert(off == 0); |
---|
167 | |
---|
168 | area_init(area_b); |
---|
169 | area_write(fd, area_b); |
---|
170 | area_compare(area_a, area_b, false); |
---|
171 | |
---|
172 | rv = close(fd); |
---|
173 | rtems_test_assert(rv == 0); |
---|
174 | |
---|
175 | rv = unlink(rda); |
---|
176 | rtems_test_assert(rv == 0); |
---|
177 | |
---|
178 | area_compare(area_a, area_b, true); |
---|
179 | } |
---|
180 | |
---|
181 | static void test_blkdev_imfs_parameters(void) |
---|
182 | { |
---|
183 | rtems_status_code sc; |
---|
184 | int rv; |
---|
185 | ramdisk *rd; |
---|
186 | int fd; |
---|
187 | rtems_disk_device *dd; |
---|
188 | struct stat st; |
---|
189 | |
---|
190 | rd = ramdisk_allocate(NULL, BLOCK_SIZE, BLOCK_COUNT, false); |
---|
191 | rtems_test_assert(rd != NULL); |
---|
192 | |
---|
193 | ramdisk_enable_free_at_delete_request(rd); |
---|
194 | |
---|
195 | sc = rtems_blkdev_create( |
---|
196 | rda, |
---|
197 | BLOCK_SIZE, |
---|
198 | BLOCK_COUNT, |
---|
199 | ramdisk_ioctl, |
---|
200 | rd |
---|
201 | ); |
---|
202 | ASSERT_SC(sc); |
---|
203 | |
---|
204 | sc = rtems_blkdev_create_partition( |
---|
205 | rda1, |
---|
206 | rda, |
---|
207 | 1, |
---|
208 | BLOCK_COUNT - 1 |
---|
209 | ); |
---|
210 | ASSERT_SC(sc); |
---|
211 | |
---|
212 | fd = open(rda, O_RDWR); |
---|
213 | rtems_test_assert(fd >= 0); |
---|
214 | |
---|
215 | rv = fstat(fd, &st); |
---|
216 | rtems_test_assert(rv == 0); |
---|
217 | |
---|
218 | rv = rtems_disk_fd_get_disk_device(fd, &dd); |
---|
219 | rtems_test_assert(rv == 0); |
---|
220 | |
---|
221 | rtems_test_assert(rtems_disk_get_driver_data(dd) == rd); |
---|
222 | rtems_test_assert(rtems_disk_get_device_identifier(dd) == st.st_rdev); |
---|
223 | rtems_test_assert(rtems_disk_get_media_block_size(dd) == BLOCK_SIZE); |
---|
224 | rtems_test_assert(rtems_disk_get_block_size(dd) == BLOCK_SIZE); |
---|
225 | rtems_test_assert(rtems_disk_get_block_begin(dd) == 0); |
---|
226 | rtems_test_assert(rtems_disk_get_block_count(dd) == BLOCK_COUNT); |
---|
227 | |
---|
228 | rv = close(fd); |
---|
229 | rtems_test_assert(rv == 0); |
---|
230 | |
---|
231 | fd = open(rda1, O_RDWR); |
---|
232 | rtems_test_assert(fd >= 0); |
---|
233 | |
---|
234 | rv = fstat(fd, &st); |
---|
235 | rtems_test_assert(rv == 0); |
---|
236 | |
---|
237 | rv = rtems_disk_fd_get_disk_device(fd, &dd); |
---|
238 | rtems_test_assert(rv == 0); |
---|
239 | |
---|
240 | rtems_test_assert(rtems_disk_get_driver_data(dd) == rd); |
---|
241 | rtems_test_assert(rtems_disk_get_device_identifier(dd) == st.st_rdev); |
---|
242 | rtems_test_assert(rtems_disk_get_media_block_size(dd) == BLOCK_SIZE); |
---|
243 | rtems_test_assert(rtems_disk_get_block_size(dd) == BLOCK_SIZE); |
---|
244 | rtems_test_assert(rtems_disk_get_block_begin(dd) == 1); |
---|
245 | rtems_test_assert(rtems_disk_get_block_count(dd) == BLOCK_COUNT - 1); |
---|
246 | |
---|
247 | rv = close(fd); |
---|
248 | rtems_test_assert(rv == 0); |
---|
249 | |
---|
250 | rv = unlink(rda1); |
---|
251 | rtems_test_assert(rv == 0); |
---|
252 | |
---|
253 | rv = unlink(rda); |
---|
254 | rtems_test_assert(rv == 0); |
---|
255 | } |
---|
256 | |
---|
257 | static void test_blkdev_imfs_errors(void) |
---|
258 | { |
---|
259 | static uintptr_t disk_size [] = { sizeof(rtems_disk_device) + sizeof(int) }; |
---|
260 | |
---|
261 | rtems_status_code sc; |
---|
262 | int rv; |
---|
263 | ramdisk *rd; |
---|
264 | void *opaque; |
---|
265 | struct stat st; |
---|
266 | |
---|
267 | rd = ramdisk_allocate(NULL, BLOCK_SIZE, BLOCK_COUNT, false); |
---|
268 | rtems_test_assert(rd != NULL); |
---|
269 | |
---|
270 | ramdisk_enable_free_at_delete_request(rd); |
---|
271 | |
---|
272 | sc = rtems_blkdev_create( |
---|
273 | rda, |
---|
274 | 0, |
---|
275 | BLOCK_COUNT, |
---|
276 | ramdisk_ioctl, |
---|
277 | rd |
---|
278 | ); |
---|
279 | rtems_test_assert(sc == RTEMS_INVALID_NUMBER); |
---|
280 | |
---|
281 | sc = rtems_blkdev_create( |
---|
282 | rda, |
---|
283 | BLOCK_SIZE, |
---|
284 | 0, |
---|
285 | ramdisk_ioctl, |
---|
286 | rd |
---|
287 | ); |
---|
288 | rtems_test_assert(sc == RTEMS_INVALID_NUMBER); |
---|
289 | |
---|
290 | opaque = rtems_heap_greedy_allocate(NULL, 0); |
---|
291 | sc = rtems_blkdev_create( |
---|
292 | rda, |
---|
293 | BLOCK_SIZE, |
---|
294 | BLOCK_COUNT, |
---|
295 | ramdisk_ioctl, |
---|
296 | rd |
---|
297 | ); |
---|
298 | rtems_test_assert(sc == RTEMS_NO_MEMORY); |
---|
299 | rtems_heap_greedy_free(opaque); |
---|
300 | |
---|
301 | opaque = rtems_heap_greedy_allocate(disk_size, 1); |
---|
302 | sc = rtems_blkdev_create( |
---|
303 | rda, |
---|
304 | BLOCK_SIZE, |
---|
305 | BLOCK_COUNT, |
---|
306 | ramdisk_ioctl, |
---|
307 | rd |
---|
308 | ); |
---|
309 | rtems_test_assert(sc == RTEMS_UNSATISFIED); |
---|
310 | rtems_heap_greedy_free(opaque); |
---|
311 | |
---|
312 | sc = rtems_blkdev_create( |
---|
313 | rda, |
---|
314 | BLOCK_SIZE, |
---|
315 | BLOCK_COUNT, |
---|
316 | ramdisk_ioctl, |
---|
317 | rd |
---|
318 | ); |
---|
319 | ASSERT_SC(sc); |
---|
320 | |
---|
321 | sc = rtems_blkdev_create_partition( |
---|
322 | rda1, |
---|
323 | not_exist, |
---|
324 | 0, |
---|
325 | BLOCK_COUNT |
---|
326 | ); |
---|
327 | rtems_test_assert(sc == RTEMS_INVALID_ID); |
---|
328 | |
---|
329 | rv = lstat(&dev_invalid[0], &st); |
---|
330 | rtems_test_assert(rv == 0); |
---|
331 | |
---|
332 | rv = mknod(not_blkdev, S_IFREG | S_IRWXU | S_IRWXG | S_IRWXO, st.st_rdev); |
---|
333 | rtems_test_assert(rv == 0); |
---|
334 | |
---|
335 | sc = rtems_blkdev_create_partition( |
---|
336 | rda1, |
---|
337 | not_blkdev, |
---|
338 | 0, |
---|
339 | BLOCK_COUNT |
---|
340 | ); |
---|
341 | rtems_test_assert(sc == RTEMS_INVALID_NODE); |
---|
342 | |
---|
343 | rv = mknod(invalid_blkdev, S_IFBLK | S_IRWXU | S_IRWXG | S_IRWXO, st.st_rdev); |
---|
344 | rtems_test_assert(rv == 0); |
---|
345 | |
---|
346 | sc = rtems_blkdev_create_partition( |
---|
347 | rda1, |
---|
348 | invalid_blkdev, |
---|
349 | 0, |
---|
350 | BLOCK_COUNT |
---|
351 | ); |
---|
352 | rtems_test_assert(sc == RTEMS_NOT_IMPLEMENTED); |
---|
353 | |
---|
354 | sc = rtems_blkdev_create_partition( |
---|
355 | rda1, |
---|
356 | rda, |
---|
357 | 0, |
---|
358 | 0 |
---|
359 | ); |
---|
360 | rtems_test_assert(sc == RTEMS_INVALID_NUMBER); |
---|
361 | |
---|
362 | sc = rtems_blkdev_create_partition( |
---|
363 | rda1, |
---|
364 | rda, |
---|
365 | BLOCK_COUNT, |
---|
366 | BLOCK_COUNT |
---|
367 | ); |
---|
368 | rtems_test_assert(sc == RTEMS_INVALID_NUMBER); |
---|
369 | |
---|
370 | sc = rtems_blkdev_create_partition( |
---|
371 | rda1, |
---|
372 | rda, |
---|
373 | 0, |
---|
374 | BLOCK_COUNT + 1 |
---|
375 | ); |
---|
376 | rtems_test_assert(sc == RTEMS_INVALID_NUMBER); |
---|
377 | |
---|
378 | opaque = rtems_heap_greedy_allocate(NULL, 0); |
---|
379 | sc = rtems_blkdev_create_partition( |
---|
380 | rda1, |
---|
381 | rda, |
---|
382 | 0, |
---|
383 | BLOCK_COUNT |
---|
384 | ); |
---|
385 | rtems_test_assert(sc == RTEMS_NO_MEMORY); |
---|
386 | rtems_heap_greedy_free(opaque); |
---|
387 | |
---|
388 | opaque = rtems_heap_greedy_allocate(disk_size, 1); |
---|
389 | sc = rtems_blkdev_create_partition( |
---|
390 | rda1, |
---|
391 | rda, |
---|
392 | 0, |
---|
393 | BLOCK_COUNT |
---|
394 | ); |
---|
395 | rtems_test_assert(sc == RTEMS_UNSATISFIED); |
---|
396 | rtems_heap_greedy_free(opaque); |
---|
397 | |
---|
398 | rv = unlink(rda); |
---|
399 | rtems_test_assert(rv == 0); |
---|
400 | } |
---|
401 | |
---|
402 | static rtems_task Init(rtems_task_argument argument) |
---|
403 | { |
---|
404 | TEST_BEGIN(); |
---|
405 | |
---|
406 | test_blkdev_imfs_read_and_write(); |
---|
407 | test_blkdev_imfs_parameters(); |
---|
408 | test_blkdev_imfs_errors(); |
---|
409 | |
---|
410 | TEST_END(); |
---|
411 | exit(0); |
---|
412 | } |
---|
413 | |
---|
414 | #define CONFIGURE_INIT |
---|
415 | |
---|
416 | #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER |
---|
417 | #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER |
---|
418 | #define CONFIGURE_APPLICATION_NEEDS_LIBBLOCK |
---|
419 | |
---|
420 | #define CONFIGURE_APPLICATION_EXTRA_DRIVERS \ |
---|
421 | { .initialization_entry = invalid_initialize, \ |
---|
422 | .control_entry = invalid_control } |
---|
423 | |
---|
424 | #define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 5 |
---|
425 | |
---|
426 | #define CONFIGURE_MAXIMUM_TASKS 1 |
---|
427 | |
---|
428 | #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION |
---|
429 | |
---|
430 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
431 | |
---|
432 | #include <rtems/confdefs.h> |
---|