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

Last change on this file since c4b8b147 was c4b8b147, checked in by Sebastian Huber <sebastian.huber@…>, on Nov 3, 2017 at 7:35:38 AM

tests: Use simple console driver

Update #3170.
Update #3199.

  • Property mode set to 100644
File size: 7.1 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup tests
5 *
6 * @brief Block device tests.
7 */
8
9/*
10 * Copyright (c) 2009-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 <sys/stat.h>
28#include <stdio.h>
29#include <fcntl.h>
30#include "tmacros.h"
31
32#include <rtems.h>
33#include <rtems/ramdisk.h>
34#include <rtems/diskdevs.h>
35
36const char rtems_test_name[] = "BLOCK 1";
37
38/* forward declarations to avoid warnings */
39static rtems_task Init(rtems_task_argument argument);
40
41#define ASSERT_SC(sc) rtems_test_assert((sc) == RTEMS_SUCCESSFUL)
42
43#define ASSERT_SC_EQ(sc, sc_expected) rtems_test_assert((sc) == (sc_expected))
44
45#define BLOCK_SIZE 512U
46
47#define BLOCK_COUNT 16U
48
49static void test_block_io_control_api(dev_t dev, ramdisk *rd)
50{
51  rtems_status_code sc = RTEMS_SUCCESSFUL;
52  rtems_disk_device *dd = NULL;
53  rtems_disk_device *fd_dd = NULL;
54  int fd = -1;
55  int rv = -1;
56  uint32_t value = 0;
57  rtems_blkdev_bnum block_count = 0;
58
59  sc = rtems_disk_create_phys(dev, BLOCK_SIZE, BLOCK_COUNT, ramdisk_ioctl, rd, "/dev/rda");
60  ASSERT_SC(sc);
61
62  dd = rtems_disk_obtain(dev);
63  rtems_test_assert(dd != NULL);
64
65  fd = open("/dev/rda", O_RDWR);
66  rtems_test_assert(fd >= 0);
67
68  value = 0;
69  rv = rtems_disk_fd_get_media_block_size(fd, &value);
70  rtems_test_assert(rv == 0);
71  rtems_test_assert(value == BLOCK_SIZE);
72
73  value = 0;
74  rv = rtems_disk_fd_get_block_size(fd, &value);
75  rtems_test_assert(rv == 0);
76  rtems_test_assert(value == BLOCK_SIZE);
77
78  value = 1024;
79  rv = rtems_disk_fd_set_block_size(fd, value);
80  rtems_test_assert(rv == 0);
81
82  value = 0;
83  rv = rtems_disk_fd_get_block_size(fd, &value);
84  rtems_test_assert(rv == 0);
85  rtems_test_assert(value == 1024);
86
87  block_count = 0;
88  rv = rtems_disk_fd_get_block_count(fd, &block_count);
89  rtems_test_assert(rv == 0);
90  rtems_test_assert(block_count == BLOCK_COUNT);
91
92  rv = rtems_disk_fd_get_disk_device(fd, &fd_dd);
93  rtems_test_assert(rv == 0);
94  rtems_test_assert(fd_dd == dd);
95
96  rv = rtems_disk_fd_sync(fd);
97  rtems_test_assert(rv == 0);
98
99  rv = close(fd);
100  rtems_test_assert(rv == 0);
101
102  sc = rtems_disk_release(dd);
103  ASSERT_SC(sc);
104
105  sc = rtems_disk_delete(dev);
106  ASSERT_SC(sc);
107}
108
109static void test_diskdevs(void)
110{
111  rtems_status_code sc = RTEMS_SUCCESSFUL;
112  rtems_device_major_number major = 0;
113  rtems_device_minor_number minor = 0;
114  rtems_disk_device *physical_dd = NULL;
115  rtems_disk_device *logical_dd = NULL;
116  rtems_disk_device *dd = NULL;
117  dev_t physical_dev = 0;
118  dev_t logical_dev = 0;
119  dev_t logical_2_dev = 0;
120  dev_t const big_major_dev = rtems_filesystem_make_dev_t((rtems_device_major_number) -2, 0);
121  dev_t const big_minor_dev = rtems_filesystem_make_dev_t(0, (rtems_device_minor_number) -2);
122  ramdisk *const rd = ramdisk_allocate(NULL, BLOCK_SIZE, BLOCK_COUNT, false);
123
124  rtems_test_assert(rd != NULL);
125
126  sc = rtems_disk_io_initialize();
127  ASSERT_SC(sc);
128
129  sc = rtems_io_register_driver(0, &ramdisk_ops, &major);
130  ASSERT_SC(sc);
131
132  physical_dev = rtems_filesystem_make_dev_t(major, minor);
133  logical_dev = rtems_filesystem_make_dev_t(major, minor + 1);
134  logical_2_dev = rtems_filesystem_make_dev_t(major, minor + 2);
135
136  /* Consistency checks for physical disks creation */
137
138  sc = rtems_disk_create_phys(physical_dev, BLOCK_SIZE, BLOCK_COUNT, NULL, rd, "/dev/rda");
139  ASSERT_SC_EQ(sc, RTEMS_INVALID_ADDRESS);
140
141  sc = rtems_disk_create_phys(physical_dev, 0, BLOCK_COUNT, ramdisk_ioctl, rd, "/dev/rda");
142  ASSERT_SC_EQ(sc, RTEMS_INVALID_NUMBER);
143
144  sc = rtems_disk_create_phys(big_major_dev, BLOCK_SIZE, BLOCK_COUNT, ramdisk_ioctl, rd, "/dev/rda");
145  ASSERT_SC_EQ(sc, RTEMS_NO_MEMORY);
146
147  sc = rtems_disk_create_phys(big_minor_dev, BLOCK_SIZE, BLOCK_COUNT, ramdisk_ioctl, rd, "/dev/rda");
148  ASSERT_SC_EQ(sc, RTEMS_NO_MEMORY);
149
150  sc = rtems_disk_create_phys(physical_dev, BLOCK_SIZE, BLOCK_COUNT, ramdisk_ioctl, rd, NULL);
151  ASSERT_SC(sc);
152
153  sc = rtems_disk_create_phys(physical_dev, BLOCK_SIZE, BLOCK_COUNT, ramdisk_ioctl, rd, NULL);
154  ASSERT_SC_EQ(sc, RTEMS_RESOURCE_IN_USE);
155
156  sc = rtems_disk_delete(physical_dev);
157  ASSERT_SC(sc);
158
159  /* Consistency checks for logical disks creation */
160
161  sc = rtems_disk_create_log(logical_dev, physical_dev, 0, 1, "/dev/rda1");
162  ASSERT_SC_EQ(sc, RTEMS_INVALID_ID);
163
164  sc = rtems_disk_create_phys(physical_dev, BLOCK_SIZE, BLOCK_COUNT, ramdisk_ioctl, rd, "/dev/rda");
165  ASSERT_SC(sc);
166
167  sc = rtems_disk_create_log(big_major_dev, physical_dev, 0, 1, "/dev/rda1");
168  ASSERT_SC_EQ(sc, RTEMS_NO_MEMORY);
169
170  sc = rtems_disk_create_log(big_minor_dev, physical_dev, 0, 1, "/dev/rda1");
171  ASSERT_SC_EQ(sc, RTEMS_NO_MEMORY);
172
173  sc = rtems_disk_create_log(logical_dev, physical_dev, BLOCK_COUNT, 0, "/dev/rda1");
174  ASSERT_SC_EQ(sc, RTEMS_INVALID_NUMBER);
175
176  sc = rtems_disk_create_log(logical_dev, physical_dev, 0, BLOCK_COUNT + 1, "/dev/rda1");
177  ASSERT_SC_EQ(sc, RTEMS_INVALID_NUMBER);
178
179  sc = rtems_disk_create_log(logical_dev, physical_dev, 1, BLOCK_COUNT, "/dev/rda1");
180  ASSERT_SC_EQ(sc, RTEMS_INVALID_NUMBER);
181
182  sc = rtems_disk_create_log(logical_dev, physical_dev, 0, 1, "/dev/rda1");
183  ASSERT_SC(sc);
184
185  sc = rtems_disk_create_log(logical_dev, physical_dev, 0, 1, "/dev/rda1");
186  ASSERT_SC_EQ(sc, RTEMS_RESOURCE_IN_USE);
187
188  sc = rtems_disk_create_log(logical_2_dev, logical_dev, 0, 1, "/dev/rda2");
189  ASSERT_SC_EQ(sc, RTEMS_INVALID_ID);
190
191  sc = rtems_disk_delete(logical_dev);
192  ASSERT_SC(sc);
193
194  /* Consistency checks delete */
195
196  sc = rtems_disk_create_log(logical_dev, physical_dev, 0, 1, "/dev/rda1");
197  ASSERT_SC(sc);
198
199  physical_dd = rtems_disk_obtain(physical_dev);
200  rtems_test_assert(physical_dd != NULL && physical_dd->uses == 2);
201
202  sc = rtems_disk_release(physical_dd);
203  ASSERT_SC(sc);
204
205  logical_dd = rtems_disk_obtain(logical_dev);
206  rtems_test_assert(logical_dd != NULL && logical_dd->uses == 1);
207
208  sc = rtems_disk_delete(physical_dev);
209  ASSERT_SC(sc);
210
211  sc = rtems_disk_create_phys(physical_dev, BLOCK_SIZE, BLOCK_COUNT, ramdisk_ioctl, rd, "/dev/rda");
212  ASSERT_SC_EQ(sc, RTEMS_RESOURCE_IN_USE);
213
214  dd = rtems_disk_obtain(physical_dev);
215  rtems_test_assert(dd == NULL);
216
217  dd = rtems_disk_obtain(logical_dev);
218  rtems_test_assert(dd == NULL);
219
220  sc = rtems_disk_release(logical_dd);
221  ASSERT_SC(sc);
222
223  /* Test block IO control API */
224
225  test_block_io_control_api(physical_dev, rd);
226
227  /* Cleanup */
228
229  sc = rtems_io_unregister_driver(major);
230  ASSERT_SC(sc);
231
232  ramdisk_free(rd);
233
234  sc = rtems_disk_io_done();
235  ASSERT_SC(sc);
236}
237
238static rtems_task Init(rtems_task_argument argument)
239{
240  TEST_BEGIN();
241
242  test_diskdevs();
243
244  TEST_END();
245
246  exit(0);
247}
248
249#define CONFIGURE_INIT
250
251#define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER
252#define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
253#define CONFIGURE_APPLICATION_NEEDS_LIBBLOCK
254
255#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 4
256
257#define CONFIGURE_MAXIMUM_TASKS 1
258#define CONFIGURE_MAXIMUM_DRIVERS 2
259
260#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
261
262#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
263
264#include <rtems/confdefs.h>
Note: See TracBrowser for help on using the repository browser.