]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/io/fileio/v2_0/tests/fileio1.c
unified MX27, MX25, MX37 trees
[karo-tx-redboot.git] / packages / io / fileio / v2_0 / tests / fileio1.c
1 //==========================================================================
2 //
3 //      fileio1.c
4 //
5 //      Test fileio system
6 //
7 //==========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
12 //
13 // eCos is free software; you can redistribute it and/or modify it under
14 // the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 2 or (at your option) any later version.
16 //
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20 // for more details.
21 //
22 // You should have received a copy of the GNU General Public License along
23 // with eCos; if not, write to the Free Software Foundation, Inc.,
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25 //
26 // As a special exception, if other files instantiate templates or use macros
27 // or inline functions from this file, or you compile this file and link it
28 // with other works to produce a work based on this file, this file does not
29 // by itself cause the resulting work to be covered by the GNU General Public
30 // License. However the source code for this file must still be made available
31 // in accordance with section (3) of the GNU General Public License.
32 //
33 // This exception does not invalidate any other reasons why a work based on
34 // this file might be covered by the GNU General Public License.
35 //
36 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37 // at http://sources.redhat.com/ecos/ecos-license/
38 // -------------------------------------------
39 //####ECOSGPLCOPYRIGHTEND####
40 //==========================================================================
41 //#####DESCRIPTIONBEGIN####
42 //
43 // Author(s):           nickg
44 // Contributors:        nickg
45 // Date:                2000-05-25
46 // Purpose:             Test fileio system
47 // Description:         This test uses the testfs to check out the initialization
48 //                      and basic operation of the fileio system
49 //                      
50 //                      
51 //                      
52 //                      
53 //                      
54 //              
55 //
56 //####DESCRIPTIONEND####
57 //
58 //==========================================================================
59
60 #include <pkgconf/hal.h>
61 #include <pkgconf/io_fileio.h>
62
63 #include <cyg/infra/cyg_trac.h>        // tracing macros
64 #include <cyg/infra/cyg_ass.h>         // assertion macros
65
66 #include <stdio.h>
67 #include <unistd.h>
68 #include <fcntl.h>
69 #include <sys/stat.h>
70 #include <errno.h>
71 #include <string.h>
72
73 #include <cyg/infra/testcase.h>
74 #include <cyg/infra/diag.h>            // HAL polled output
75
76 //==========================================================================
77 // Include the test filesystem.
78 // If we could make tests out of multiple files, then we could just link
79 // against the object file for this rather than including it.
80
81 #include "testfs.c"
82
83 //==========================================================================
84
85 #define SHOW_RESULT( _fn, _res ) \
86 diag_printf("<INFO>: " #_fn "() returned %ld %s\n", (long)_res, _res<0?strerror(errno):"");
87
88 //==========================================================================
89
90 #define IOSIZE  100
91
92 //==========================================================================
93
94 static void listdir( char *name, int statp )
95 {
96     int err;
97     DIR *dirp;
98     
99     diag_printf("<INFO>: reading directory %s\n",name);
100     
101     dirp = opendir( name );
102     if( dirp == NULL ) SHOW_RESULT( opendir, -1 );
103
104     for(;;)
105     {
106         struct dirent *entry = readdir( dirp );
107         
108         if( entry == NULL )
109             break;
110
111         diag_printf("<INFO>: entry %14s",entry->d_name);
112         if( statp )
113         {
114             char fullname[PATH_MAX];
115             struct stat sbuf;
116
117             if( name[0] )
118             {
119                 strcpy(fullname, name );
120                 if( !(name[0] == '/' && name[1] == 0 ) )
121                     strcat(fullname, "/" );
122             }
123             else fullname[0] = 0;
124             
125             strcat(fullname, entry->d_name );
126             
127             err = stat( fullname, &sbuf );
128             if( err < 0 )
129             {
130                 if( errno == ENOSYS )
131                     diag_printf(" <no status available>");
132                 else SHOW_RESULT( stat, err );
133             }
134             else
135             {
136                 diag_printf(" [mode %08x nlink %d size %ld]",
137                             sbuf.st_mode,sbuf.st_nlink,sbuf.st_size);
138             }
139         }
140
141         diag_printf("\n");
142     }
143
144     err = closedir( dirp );
145     if( err < 0 ) SHOW_RESULT( stat, err );
146 }
147
148 //==========================================================================
149
150 static void createfile( char *name, size_t size )
151 {
152     char buf[IOSIZE];
153     int fd;
154     ssize_t wrote;
155     int i;
156     int err;
157
158     diag_printf("<INFO>: create file %s size %zd\n",name,size);
159
160     err = access( name, F_OK );
161     if( err < 0 && errno != EACCES ) SHOW_RESULT( access, err );
162     
163     for( i = 0; i < IOSIZE; i++ ) buf[i] = i%256;
164  
165     fd = open( name, O_WRONLY|O_CREAT );
166     if( fd < 0 ) SHOW_RESULT( open, fd );
167
168     while( size > 0 )
169     {
170         ssize_t len = size;
171         if ( len > IOSIZE ) len = IOSIZE;
172         
173         wrote = write( fd, buf, len );
174         if( wrote != len ) SHOW_RESULT( write, wrote );        
175
176         size -= wrote;
177     }
178
179     err = close( fd );
180     if( err < 0 ) SHOW_RESULT( close, err );
181 }
182
183 //==========================================================================
184
185 static void maxfile( char *name )
186 {
187     char buf[IOSIZE];
188     int fd;
189     ssize_t wrote;
190     int i;
191     int err;
192     size_t size = 0;
193     
194     diag_printf("<INFO>: create maximal file %s\n",name);
195
196     err = access( name, F_OK );
197     if( err < 0 && errno != EACCES ) SHOW_RESULT( access, err );
198     
199     for( i = 0; i < IOSIZE; i++ ) buf[i] = i%256;
200  
201     fd = open( name, O_WRONLY|O_CREAT );
202     if( fd < 0 ) SHOW_RESULT( open, fd );
203
204     do
205     {
206         wrote = write( fd, buf, IOSIZE );
207         if( wrote < 0 ) SHOW_RESULT( write, wrote );        
208
209         size += wrote;
210         
211     } while( wrote == IOSIZE );
212
213     diag_printf("<INFO>: file size == %zd\n",size);
214
215     err = close( fd );
216     if( err < 0 ) SHOW_RESULT( close, err );
217 }
218
219 //==========================================================================
220
221 static void checkfile( char *name )
222 {
223     char buf[IOSIZE];
224     int fd;
225     ssize_t done;
226     int i;
227     int err;
228
229     diag_printf("<INFO>: check file %s\n",name);
230     
231     err = access( name, F_OK );
232     if( err != 0 ) SHOW_RESULT( access, err );
233
234     fd = open( name, O_RDONLY );
235     if( fd < 0 ) SHOW_RESULT( open, fd );
236
237     for(;;)
238     {
239         done = read( fd, buf, IOSIZE );
240         if( done < 0 ) SHOW_RESULT( read, done );
241
242         if( done == 0 ) break;
243
244         for( i = 0; i < done; i++ )
245             if( buf[i] != i%256 )
246             {
247                 diag_printf("buf[%d](%02x) != %02x\n",i,buf[i],i%256);
248                 CYG_TEST_FAIL("Data read not equal to data written\n");
249             }
250     }
251
252     err = close( fd );
253     if( err < 0 ) SHOW_RESULT( close, err );
254 }
255
256 //==========================================================================
257
258 static void copyfile( char *name2, char *name1 )
259 {
260
261     int err;
262     char buf[IOSIZE];
263     int fd1, fd2;
264     ssize_t done, wrote;
265
266     diag_printf("<INFO>: copy file %s -> %s\n",name2,name1);
267
268     err = access( name1, F_OK );
269     if( err < 0 && errno != EACCES ) SHOW_RESULT( access, err );
270
271     err = access( name2, F_OK );
272     if( err != 0 ) SHOW_RESULT( access, err );
273     
274     fd1 = open( name1, O_WRONLY|O_CREAT );
275     if( fd1 < 0 ) SHOW_RESULT( open, fd1 );
276
277     fd2 = open( name2, O_RDONLY );
278     if( fd2 < 0 ) SHOW_RESULT( open, fd2 );
279     
280     for(;;)
281     {
282         done = read( fd2, buf, IOSIZE );
283         if( done < 0 ) SHOW_RESULT( read, done );
284
285         if( done == 0 ) break;
286
287         wrote = write( fd1, buf, done );
288         if( wrote != done ) SHOW_RESULT( write, wrote );
289
290         if( wrote != done ) break;
291     }
292
293     err = close( fd1 );
294     if( err < 0 ) SHOW_RESULT( close, err );
295
296     err = close( fd2 );
297     if( err < 0 ) SHOW_RESULT( close, err );
298     
299 }
300
301 //==========================================================================
302
303 static void comparefiles( char *name2, char *name1 )
304 {
305     int err;
306     char buf1[IOSIZE];
307     char buf2[IOSIZE];
308     int fd1, fd2;
309     ssize_t done1, done2;
310     int i;
311
312     diag_printf("<INFO>: compare files %s == %s\n",name2,name1);
313
314     err = access( name1, F_OK );
315     if( err != 0 ) SHOW_RESULT( access, err );
316
317     err = access( name1, F_OK );
318     if( err != 0 ) SHOW_RESULT( access, err );
319     
320     fd1 = open( name1, O_RDONLY );
321     if( fd1 < 0 ) SHOW_RESULT( open, fd1 );
322
323     fd2 = open( name2, O_RDONLY );
324     if( fd2 < 0 ) SHOW_RESULT( open, fd2 );
325     
326     for(;;)
327     {
328         done1 = read( fd1, buf1, IOSIZE );
329         if( done1 < 0 ) SHOW_RESULT( read, done1 );
330
331         done2 = read( fd2, buf2, IOSIZE );
332         if( done2 < 0 ) SHOW_RESULT( read, done2 );
333
334         if( done1 != done2 )
335             diag_printf("Files different sizes\n");
336         
337         if( done1 == 0 ) break;
338
339         for( i = 0; i < done1; i++ )
340             if( buf1[i] != buf2[i] )
341             {
342                 diag_printf("buf1[%d](%02x) != buf1[%d](%02x)\n",i,buf1[i],i,buf2[i]);
343                 CYG_TEST_FAIL("Data in files not equal\n");
344             }
345     }
346
347     err = close( fd1 );
348     if( err < 0 ) SHOW_RESULT( close, err );
349
350     err = close( fd2 );
351     if( err < 0 ) SHOW_RESULT( close, err );
352     
353 }
354
355 //==========================================================================
356 // main
357
358 int cyg_user_start(void)
359 {
360     int err;
361
362     CYG_TEST_INIT();
363
364     // --------------------------------------------------------------
365
366     createfile( "/foo", 202 );
367     checkfile( "foo" );
368     copyfile( "foo", "fee");
369     checkfile( "fee" );
370     comparefiles( "foo", "/fee" );
371     
372     err = mkdir( "/bar", 0 );
373     if( err < 0 ) SHOW_RESULT( mkdir, err );
374
375     listdir( "/" , false);
376
377     copyfile( "fee", "/bar/fum" );
378     checkfile( "bar/fum" );
379     comparefiles( "/fee", "bar/fum" );
380
381     
382     err = chdir( "bar" );
383     if( err < 0 ) SHOW_RESULT( chdir, err );
384     
385     err = rename( "/foo", "bundy" );
386     if( err < 0 ) SHOW_RESULT( rename, err );
387
388     listdir( "/", true );
389     listdir( "" , true );
390
391     checkfile( "/bar/bundy" );
392     comparefiles("/fee", "bundy" );
393
394     testfs_dump();
395     
396     // --------------------------------------------------------------
397     
398     err = unlink( "/fee" );
399     if( err < 0 ) SHOW_RESULT( unlink, err );
400
401     err = unlink( "fum" );
402     if( err < 0 ) SHOW_RESULT( unlink, err );
403
404     err = unlink( "/bar/bundy" );
405     if( err < 0 ) SHOW_RESULT( unlink, err );
406
407     err = chdir( "/" );
408     if( err < 0 ) SHOW_RESULT( chdir, err );
409     
410     err = rmdir( "/bar" );
411     if( err < 0 ) SHOW_RESULT( rmdir, err );
412
413     listdir( "/", false );
414
415     // --------------------------------------------------------------
416
417     err = mount( "", "/ram", "testfs" );
418     if( err < 0 ) SHOW_RESULT( mount, err );    
419
420     createfile( "/ram/tinky", 456 );
421     copyfile( "/ram/tinky", "/ram/laalaa" );
422     checkfile( "/ram/tinky");
423     checkfile( "/ram/laalaa");
424     comparefiles( "/ram/tinky", "/ram/laalaa" );
425     comparefiles( "/ram/tinky", "ram/laalaa" );
426     comparefiles( "ram/tinky", "/ram/laalaa" );
427
428     err = chdir( "/ram" );
429     if( err < 0 ) SHOW_RESULT( chdir, err );
430
431     createfile( "tinky", 678 );
432     checkfile( "tinky" );
433
434     maxfile( "dipsy" );
435     checkfile( "dipsy" );
436     copyfile( "dipsy", "po" );
437     checkfile( "po" );
438     comparefiles( "dipsy", "po" );
439
440     testfs_dump();
441     
442     // --------------------------------------------------------------
443     
444     err = unlink( "tinky" );
445     if( err < 0 ) SHOW_RESULT( unlink, err );
446
447     err = unlink( "dipsy" );
448     if( err < 0 ) SHOW_RESULT( unlink, err );
449
450     err = unlink( "po" );
451     if( err < 0 ) SHOW_RESULT( unlink, err );
452     
453     err = unlink( "laalaa" );
454     if( err < 0 ) SHOW_RESULT( unlink, err );
455     
456     err = chdir( "/" );
457     if( err < 0 ) SHOW_RESULT( chdir, err );
458
459     err = umount( "/ram" );
460     if( err < 0 ) SHOW_RESULT( umount, err );    
461     
462     CYG_TEST_PASS_FINISH("fileio1");
463 }
464
465 // -------------------------------------------------------------------------
466 // EOF fileio1.c