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

5
Last change on this file since c4b8b147 was c4b8b147, checked in by Sebastian Huber <sebastian.huber@…>, on 11/03/17 at 07:35:38

tests: Use simple console driver

Update #3170.
Update #3199.

  • Property mode set to 100644
File size: 11.5 KB
Line 
1/*
2 * Copyright (c) 2012 embedded brains GmbH.  All rights reserved.
3 *
4 *  embedded brains GmbH
5 *  Obere Lagerstr. 30
6 *  82178 Puchheim
7 *  Germany
8 *  <rtems@embedded-brains.de>
9 *
10 * The license and distribution terms for this file may be
11 * found in the file LICENSE in this distribution or at
12 * http://www.rtems.org/license/LICENSE.
13 */
14
15#ifdef HAVE_CONFIG_H
16  #include "config.h"
17#endif
18
19#include <fcntl.h>
20#include <rtems/blkdev.h>
21#include "rtems/sparse-disk.h"
22
23#include "tmacros.h"
24
25const char rtems_test_name[] = "SPARSEDISK 1";
26
27/* Number of bytes for test pattern within a sparse disk container */
28#define STATIC_PATTERN_SIZE 4096
29
30/* Block size used for the sparse disk in a sparse disk container */
31#define STATIC_BLOCK_SIZE 4096
32
33/* Number of block allocated for the sparse disk in a sparse disk container */
34#define STATIC_ALLOCATED_BLOCK_COUNT 1
35
36/* Blocks simulated by the sparse disk in a disk container */
37#define STATIC_SIMULATED_BLOCK_COUNT 4096
38
39/*
40 * Container which cotains a sparse disk + memory for key table and data as would get
41 * allocated by rtems_sparse_disk_create() + memory for a memory test pattern
42 * By using this container white box testing of a sparse disk becomes possible
43 */
44typedef struct {
45  rtems_sparse_disk sparse_disk;
46  rtems_sparse_disk_key keytable[STATIC_ALLOCATED_BLOCK_COUNT];
47  uint8_t data[STATIC_BLOCK_SIZE * STATIC_ALLOCATED_BLOCK_COUNT];
48  uint8_t pattern[STATIC_PATTERN_SIZE];
49} sparse_disk_container;
50
51/*
52 * Black box test the disk parameters of a sparse disk
53 */
54static void test_disk_params(
55  const int               file_descriptor,
56  const uint32_t          block_size,
57  const uint32_t          media_block_size,
58  const rtems_blkdev_bnum block_number )
59{
60  int                rv;
61  uint32_t           value       = 0;
62  rtems_disk_device *fd_dd       = NULL;
63  rtems_blkdev_bnum  block_count = 0;
64
65
66  rv = rtems_disk_fd_get_media_block_size( file_descriptor, &value );
67  rtems_test_assert( 0 == rv );
68  rtems_test_assert( media_block_size == value );
69
70  value = 0;
71  rv    = rtems_disk_fd_get_block_size( file_descriptor, &value );
72  rtems_test_assert( 0 == rv );
73  rtems_test_assert( block_size == value );
74
75  block_count = 0;
76  rv          = rtems_disk_fd_get_block_count( file_descriptor, &block_count );
77  rtems_test_assert( 0 == rv );
78  rtems_test_assert( block_number == block_count );
79
80  rv = rtems_disk_fd_get_disk_device( file_descriptor, &fd_dd );
81  rtems_test_assert( 0 == rv );
82  rtems_test_assert( NULL != fd_dd );
83}
84
85/*
86 * Verify that writing to a sparse disk delivers expected results
87 */
88static void test_writing(
89  const int               file_descriptor,
90  const uint32_t          block_size,
91  const rtems_blkdev_bnum blocks_allocated )
92{
93  int               rv;
94  rtems_blkdev_bnum block_count = 0;
95  unsigned int      byte_count;
96  off_t             file_pos;
97  uint8_t           buff[block_size];
98
99
100  /* Write a pattern to all allocated blocks */
101  for ( block_count = 0; block_count < blocks_allocated; block_count++ ) {
102    file_pos = (off_t) block_count * block_size;
103    rv       = lseek( file_descriptor, file_pos, SEEK_SET );
104    rtems_test_assert( file_pos == rv );
105
106    rv = read( file_descriptor, buff, block_size );
107    rtems_test_assert( block_size == rv );
108
109    for ( byte_count = 0;
110          byte_count < ( block_size / sizeof( byte_count ) );
111          byte_count++ ) {
112      memcpy( buff + ( byte_count * sizeof( byte_count ) ), &byte_count,
113              sizeof( byte_count ) );
114    }
115
116    rv = lseek( file_descriptor, file_pos, SEEK_SET );
117    rtems_test_assert( file_pos == rv );
118
119    rv = write( file_descriptor, buff, block_size );
120    rtems_test_assert( block_size == rv );
121  }
122}
123
124/*
125 * Verify that black box reading for a sparse disk delivers expected results
126 */
127static void test_reading(
128  const int               file_descriptor,
129  const uint32_t          block_size,
130  const rtems_blkdev_bnum blocks_allocated,
131  const uint8_t           fill_pattern )
132{
133  int               rv;
134  rtems_blkdev_bnum block_count = 0;
135  unsigned int      byte_count;
136  off_t             file_pos;
137  uint8_t           buff[block_size];
138  uint32_t          value = 0;
139
140
141  rv = fsync( file_descriptor );
142  rtems_test_assert( 0 == rv );
143
144  /* Read back the patterns */
145  for ( block_count = 0; block_count < blocks_allocated; block_count++ ) {
146    file_pos = (off_t) block_count * block_size;
147    value    = lseek( file_descriptor, file_pos, SEEK_SET );
148    rtems_test_assert( file_pos == value );
149
150    rv = read( file_descriptor, &buff, block_size );
151    rtems_test_assert( block_size <= rv );
152
153    for ( byte_count = 0;
154          byte_count < ( block_size / sizeof( byte_count ) );
155          byte_count++ ) {
156      rv = memcmp( buff + ( byte_count * sizeof( byte_count ) ),
157                   &byte_count,
158                   sizeof( byte_count ) );
159      rtems_test_assert( 0 == rv );
160    }
161  }
162
163  /* Try to read from unallocated block */
164  file_pos = (off_t) block_count * block_size;
165  rv       = lseek( file_descriptor, file_pos, SEEK_SET );
166  rtems_test_assert( file_pos == rv );
167
168  rv = read( file_descriptor, buff, block_size );
169  rtems_test_assert( block_size == rv );
170
171  for ( byte_count = 0; byte_count < block_size; ++byte_count )
172    rtems_test_assert( fill_pattern == buff[byte_count] );
173}
174
175/*
176 * Do black box io testing on a sparse disk
177 */
178static void test_device_io( const char *device_name,
179  const uint32_t                        block_size,
180  const uint32_t                        media_block_size,
181  const rtems_blkdev_bnum               block_number,
182  const rtems_blkdev_bnum               blocks_allocated,
183  const uint8_t                         fill_pattern )
184{
185  int rv;
186  int file_descriptor;
187
188
189  file_descriptor = open( device_name, O_RDWR );
190  rtems_test_assert( 0 <= file_descriptor );
191
192  test_disk_params(
193    file_descriptor,
194    block_size,
195    media_block_size,
196    block_number
197    );
198
199  test_writing(
200    file_descriptor,
201    block_size,
202    blocks_allocated
203    );
204
205  test_reading(
206    file_descriptor,
207    block_size,
208    blocks_allocated,
209    fill_pattern
210    );
211
212  rv = close( file_descriptor );
213  rtems_test_assert( 0 == rv );
214}
215
216/*
217 * In white box testing verify the key table of the sparse disk is correct
218 */
219static void test_static_key_table(
220  const sparse_disk_container *disk_container,
221  const rtems_blkdev_bnum      blocks_allocated,
222  const uint32_t               block_size )
223{
224  unsigned int i;
225
226
227  for ( i = 0; i < blocks_allocated; ++i ) {
228    rtems_test_assert( i == disk_container->keytable[i].block );
229    rtems_test_assert(
230      &disk_container->data[i * block_size]
231      == disk_container->keytable[i].data );
232  }
233}
234
235/*
236 * Verify the test pattern used in white box testing is as expected
237 */
238static void test_static_pattern(
239  const unsigned int pattern_size,
240  const uint8_t     *pattern )
241{
242  unsigned int i;
243
244
245  for ( i = 0; i < pattern_size; ++i )
246    rtems_test_assert( ( (uint8_t) ( pattern_size - 1 - i ) ) == pattern[i] );
247}
248
249/*
250 * Read write testing with a statically allocated disk. Thus white box testing can be done
251 */
252static void test_with_whitebox( const char *device_name )
253{
254  rtems_status_code     sc;
255  int                   rv;
256  unsigned int          i;
257  sparse_disk_container disk_container;
258  int                   file_descriptor;
259  rtems_blkdev_bnum     block_count  = 0;
260  unsigned int          byte_count;
261  uint8_t               fill_pattern = 0;
262
263
264  memset( disk_container.data, 0, sizeof( disk_container.data ) );
265  memset( disk_container.keytable, 0, sizeof( disk_container.keytable ) );
266
267  for ( i = 0; i < STATIC_PATTERN_SIZE; ++i )
268    disk_container.pattern[i] = (uint8_t) ( STATIC_PATTERN_SIZE - 1 - i );
269
270  sc = rtems_sparse_disk_register(
271    "/dev/sda1",
272    &disk_container.sparse_disk,
273    STATIC_BLOCK_SIZE,
274    STATIC_ALLOCATED_BLOCK_COUNT,
275    STATIC_SIMULATED_BLOCK_COUNT,
276    fill_pattern,
277    NULL
278    );
279  rtems_test_assert( RTEMS_SUCCESSFUL == sc );
280
281  test_static_key_table(
282    &disk_container,
283    STATIC_ALLOCATED_BLOCK_COUNT,
284    STATIC_BLOCK_SIZE
285    );
286
287  for ( i = 0; i < ( STATIC_BLOCK_SIZE * STATIC_ALLOCATED_BLOCK_COUNT ); ++i )
288    rtems_test_assert( 0 == disk_container.data[i] );
289
290  test_static_pattern(
291    STATIC_PATTERN_SIZE,
292    &disk_container.pattern[0]
293    );
294
295  file_descriptor = open( device_name, O_RDWR );
296  rtems_test_assert( 0 <= file_descriptor );
297
298  test_disk_params(
299    file_descriptor,
300    STATIC_BLOCK_SIZE,
301    STATIC_BLOCK_SIZE,
302    STATIC_SIMULATED_BLOCK_COUNT
303    );
304
305  test_writing(
306    file_descriptor,
307    STATIC_BLOCK_SIZE,
308    STATIC_ALLOCATED_BLOCK_COUNT
309    );
310
311  test_reading(
312    file_descriptor,
313    STATIC_BLOCK_SIZE,
314    STATIC_ALLOCATED_BLOCK_COUNT,
315    fill_pattern
316    );
317
318  rv = close( file_descriptor );
319  rtems_test_assert( 0 == rv );
320
321  test_static_key_table(
322    &disk_container,
323    STATIC_ALLOCATED_BLOCK_COUNT,
324    STATIC_BLOCK_SIZE
325    );
326
327  for ( block_count = 0;
328        block_count < STATIC_ALLOCATED_BLOCK_COUNT;
329        block_count++ ) {
330    for ( byte_count = 0;
331          byte_count < ( STATIC_BLOCK_SIZE / sizeof( byte_count ) );
332          byte_count++ ) {
333      rv = memcmp( &disk_container.data[byte_count * sizeof( byte_count )],
334                   &byte_count,
335                   sizeof( byte_count ) );
336      rtems_test_assert( 0 == rv );
337    }
338  }
339
340  test_static_pattern(
341    STATIC_PATTERN_SIZE,
342    &disk_container.pattern[0]
343    );
344}
345
346/*
347 * The test sequence
348 */
349static
350void test( void )
351{
352  rtems_status_code sc;
353  int               rv;
354  char              device_name[] = "/dev/sda1";
355  uint32_t          block_size;
356  rtems_blkdev_bnum block_number;
357  rtems_blkdev_bnum blocks_allocated;
358  int               file_descriptor;
359  uint8_t           fill_pattern = 0;
360
361
362  sc = rtems_disk_io_initialize();
363  rtems_test_assert( sc == RTEMS_SUCCESSFUL );
364
365  block_size       = 512;
366  block_number     = 4 * 2 * 1024;
367  blocks_allocated = 8;
368  sc               = rtems_sparse_disk_create_and_register(
369    "/dev/sda1",
370    block_size,
371    blocks_allocated,
372    block_number,
373    fill_pattern
374    );
375  rtems_test_assert( RTEMS_SUCCESSFUL == sc );
376
377  /* Test reading and writing with sector size 512 and 8 such sectors
378   * allocated. Block size will default to 512 */
379  test_device_io(
380    device_name,
381    block_size,
382    block_size,
383    block_number,
384    blocks_allocated,
385    fill_pattern
386    );
387
388  file_descriptor = open( device_name, O_RDWR );
389  rtems_test_assert( 0 <= file_descriptor );
390
391  rv = rtems_disk_fd_set_block_size( file_descriptor,
392                                     blocks_allocated * block_size );
393  rtems_test_assert( 0 == rv );
394
395  rv = close( file_descriptor );
396  rtems_test_assert( 0 == rv );
397
398  /* Block size was increased to 4k. Thus all to allocated disk space
399   * corresponds to one block. Repeat the read write tests */
400  test_device_io(
401    device_name,
402    block_size * blocks_allocated,
403    block_size,
404    block_number,
405    1,
406    fill_pattern
407    );
408
409  rv = unlink( device_name );
410  rtems_test_assert( 0 == rv );
411
412  /* Do testing with a statically allocated disk. This permits white box
413   * testing */
414  test_with_whitebox( device_name );
415}
416
417static void Init( rtems_task_argument arg )
418{
419  (void) arg;
420  TEST_BEGIN();
421
422  test();
423
424  TEST_END();
425
426  rtems_test_exit( 0 );
427}
428
429#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
430#define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
431#define CONFIGURE_APPLICATION_NEEDS_LIBBLOCK
432
433#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 4
434
435#define CONFIGURE_MAXIMUM_TASKS 1
436#define CONFIGURE_MAXIMUM_SEMAPHORES 1
437
438#define CONFIGURE_INIT_TASK_STACK_SIZE ( 16 * 1024 )
439
440#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
441
442#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
443
444#define CONFIGURE_INIT
445
446#include <rtems/confdefs.h>
Note: See TracBrowser for help on using the repository browser.