source: rtems/testsuites/libtests/block01/init.c @ 0b038bd4

Last change on this file since 0b038bd4 was 0b038bd4, checked in by Sebastian Huber <sebastian.huber@…>, on Aug 4, 2018 at 8:38:48 AM

libblock: Add RTEMS_DEPRECATED

Close #3358.

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