]> git.karo-electronics.de Git - karo-tx-redboot.git/blob - packages/fs/rom/v2_0/tests/romfs1.c
1d32bb0421c0703c8dbf65a98b8304c90cf97e4c
[karo-tx-redboot.git] / packages / fs / rom / v2_0 / tests / romfs1.c
1 //==========================================================================
2 //
3 //      romfs1.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 // Copyright (C) 2004 eCosCentric Limited
13 //
14 // eCos is free software; you can redistribute it and/or modify it under
15 // the terms of the GNU General Public License as published by the Free
16 // Software Foundation; either version 2 or (at your option) any later version.
17 //
18 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
19 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
21 // for more details.
22 //
23 // You should have received a copy of the GNU General Public License along
24 // with eCos; if not, write to the Free Software Foundation, Inc.,
25 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 //
27 // As a special exception, if other files instantiate templates or use macros
28 // or inline functions from this file, or you compile this file and link it
29 // with other works to produce a work based on this file, this file does not
30 // by itself cause the resulting work to be covered by the GNU General Public
31 // License. However the source code for this file must still be made available
32 // in accordance with section (3) of the GNU General Public License.
33 //
34 // This exception does not invalidate any other reasons why a work based on
35 // this file might be covered by the GNU General Public License.
36 //
37 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
38 // at http://sources.redhat.com/ecos/ecos-license/
39 // -------------------------------------------
40 //####ECOSGPLCOPYRIGHTEND####
41 //==========================================================================
42 //#####DESCRIPTIONBEGIN####
43 //
44 // Author(s):           nickg
45 // Contributors:        nickg, richard.panton@3glab.com, jlarmour
46 // Date:                2000-05-25
47 // Purpose:             Test fileio system
48 // Description:         This test uses the testfs to check out the initialization
49 //                      and basic operation of the fileio system
50 //
51 //####DESCRIPTIONEND####
52 //
53 //==========================================================================
54
55 #include <pkgconf/hal.h>
56 #include <pkgconf/io_fileio.h>
57 #include <pkgconf/isoinfra.h>
58 #include <pkgconf/system.h>
59
60 #include <unistd.h>
61 #include <fcntl.h>
62 #include <sys/stat.h>
63 #include <errno.h>
64 #include <string.h>
65 #include <dirent.h>
66 #include <stdio.h>
67
68 #include <cyg/fileio/fileio.h>
69
70 #include <cyg/infra/cyg_type.h>
71 #include <cyg/infra/testcase.h>
72 #include <cyg/infra/diag.h>            // HAL polled output
73
74 // Test ROMFS data. Two example data files are generated so that
75 // the test will work on both big-endian and little-endian targets.
76 #if (CYG_BYTEORDER == CYG_LSBFIRST)
77 # include <cyg/romfs/testromfs_le.h>
78 #else
79 # include <cyg/romfs/testromfs_be.h>
80 #endif
81
82 //==========================================================================
83
84 MTAB_ENTRY( romfs_mte1,
85                    "/",
86                    "romfs",
87                    "",
88                    (CYG_ADDRWORD) &filedata[0] );
89
90
91 //==========================================================================
92
93 #define SHOW_RESULT( _fn, _res ) \
94 diag_printf("<FAIL>: " #_fn "() returned %d %s\n", _res, _res<0?strerror(errno):"");
95
96 #define CHKFAIL_TYPE( _fn, _res, _type ) { \
97 if ( _res != -1 ) \
98     diag_printf("<FAIL>: " #_fn "() returned %d (expected -1)\n", _res); \
99 else if ( errno != _type ) \
100     diag_printf("<FAIL>: " #_fn "() failed with errno %d (%s),\n    expected %d (%s)\n", errno, strerror(errno), _type, strerror(_type) ); \
101 }
102
103 //==========================================================================
104
105 #define IOSIZE  100
106
107 #define LONGNAME1       "long_file_name_that_should_take_up_more_than_one_directory_entry_1"
108 #define LONGNAME2       "long_file_name_that_should_take_up_more_than_one_directory_entry_2"
109
110
111 //==========================================================================
112
113 #ifndef CYGINT_ISO_STRING_STRFUNCS
114
115 char *strcat( char *s1, const char *s2 )
116 {
117     char *s = s1;
118     while( *s1 ) s1++;
119     while( (*s1++ = *s2++) != 0);
120     return s;
121 }
122
123 #endif
124
125 //==========================================================================
126
127 static void listdir( char *name, int statp )
128 {
129     int err;
130     DIR *dirp;
131     
132     diag_printf("<INFO>: reading directory %s\n",name);
133     
134     dirp = opendir( name );
135     if( dirp == NULL ) SHOW_RESULT( opendir, -1 );
136
137     for(;;)
138     {
139         struct dirent *entry = readdir( dirp );
140         
141         if( entry == NULL )
142             break;
143
144         diag_printf("<INFO>: entry %14s",entry->d_name);
145         if( statp )
146         {
147             char fullname[PATH_MAX];
148             struct stat sbuf;
149
150             if( name[0] )
151             {
152                 strcpy(fullname, name );
153                 if( !(name[0] == '/' && name[1] == 0 ) )
154                     strcat(fullname, "/" );
155             }
156             else fullname[0] = 0;
157             
158             strcat(fullname, entry->d_name );
159             
160             err = stat( fullname, &sbuf );
161             if( err < 0 )
162             {
163                 if( errno == ENOSYS )
164                     diag_printf(" <no status available>");
165                 else SHOW_RESULT( stat, err );
166             }
167             else
168             {
169                 diag_printf(" [mode %08x ino %08x nlink %d size %d]",
170                             sbuf.st_mode,sbuf.st_ino,sbuf.st_nlink,sbuf.st_size);
171             }
172         }
173
174         diag_printf("\n");
175     }
176
177     err = closedir( dirp );
178     if( err < 0 ) SHOW_RESULT( stat, err );
179 }
180
181 //==========================================================================
182
183 #ifdef CYGPKG_FS_RAM
184 static void copyfile( char *name2, char *name1 )
185 {
186
187     int err;
188     char buf[IOSIZE];
189     int fd1, fd2;
190     ssize_t done, wrote;
191
192     diag_printf("<INFO>: copy file %s -> %s\n",name2,name1);
193
194     err = access( name1, F_OK );
195     if( err < 0 && errno != EACCES ) SHOW_RESULT( access, err );
196
197     err = access( name2, F_OK );
198     if( err != 0 ) SHOW_RESULT( access, err );
199     
200     fd1 = open( name1, O_WRONLY|O_CREAT );
201     if( fd1 < 0 ) SHOW_RESULT( open, fd1 );
202
203     fd2 = open( name2, O_RDONLY );
204     if( fd2 < 0 ) SHOW_RESULT( open, fd2 );
205     
206     for(;;)
207     {
208         done = read( fd2, buf, IOSIZE );
209         if( done < 0 ) SHOW_RESULT( read, done );
210
211         if( done == 0 ) break;
212
213         wrote = write( fd1, buf, done );
214         if( wrote != done ) SHOW_RESULT( write, wrote );
215
216         if( wrote != done ) break;
217     }
218
219     err = close( fd1 );
220     if( err < 0 ) SHOW_RESULT( close, err );
221
222     err = close( fd2 );
223     if( err < 0 ) SHOW_RESULT( close, err );
224     
225 }
226 #endif
227
228 //==========================================================================
229
230 static void comparefiles( char *name2, char *name1 )
231 {
232     int err;
233     char buf1[IOSIZE];
234     char buf2[IOSIZE];
235     int fd1, fd2;
236     ssize_t done1, done2;
237     int i;
238
239     diag_printf("<INFO>: compare files %s == %s\n",name2,name1);
240
241     err = access( name1, F_OK );
242     if( err != 0 ) SHOW_RESULT( access, err );
243
244     err = access( name1, F_OK );
245     if( err != 0 ) SHOW_RESULT( access, err );
246     
247     fd1 = open( name1, O_RDONLY );
248     if( fd1 < 0 ) SHOW_RESULT( open, fd1 );
249
250     fd2 = open( name2, O_RDONLY );
251     if( fd2 < 0 ) SHOW_RESULT( open, fd2 );
252     
253     for(;;)
254     {
255         done1 = read( fd1, buf1, IOSIZE );
256         if( done1 < 0 ) SHOW_RESULT( read, done1 );
257
258         done2 = read( fd2, buf2, IOSIZE );
259         if( done2 < 0 ) SHOW_RESULT( read, done2 );
260
261         if( done1 != done2 )
262             diag_printf("Files different sizes\n");
263         
264         if( done1 == 0 ) break;
265
266         for( i = 0; i < done1; i++ )
267             if( buf1[i] != buf2[i] )
268             {
269                 diag_printf("buf1[%d](%02x) != buf1[%d](%02x)\n",i,buf1[i],i,buf2[i]);
270                 CYG_TEST_FAIL("Data in files not equal\n");
271             }
272     }
273
274     err = close( fd1 );
275     if( err < 0 ) SHOW_RESULT( close, err );
276
277     err = close( fd2 );
278     if( err < 0 ) SHOW_RESULT( close, err );
279     
280 }
281
282 //==========================================================================
283 // main
284
285 int main( int argc, char **argv )
286 {
287     int err;
288     char address[16];
289
290     CYG_TEST_INIT();
291
292     // --------------------------------------------------------------
293
294     diag_printf("<INFO>: ROMFS root follows\n");
295     listdir( "/", true );
296
297     diag_printf("<INFO>: cd /etc\n" );
298     err = chdir( "/etc" );
299     if ( err < 0 ) {
300         SHOW_RESULT( chdir, err );
301         CYG_TEST_FAIL_FINISH("romfs1");
302     }
303
304     diag_printf("<INFO>: ROMFS list of '' follows\n");
305     listdir( "", true );
306
307     diag_printf("<INFO>: ROMFS list of /etc follows\n");
308     listdir( "/etc", true );
309
310     diag_printf("<INFO>: ROMFS list of . follows\n");
311     listdir( ".", true );
312     
313 #ifdef CYGPKG_FS_RAM
314     err = mount( "", "/var", "ramfs" );
315     if( err < 0 ) SHOW_RESULT( mount, err );
316
317     copyfile( "/etc/passwd", "/var/passwd_copy" );
318
319     comparefiles( "/etc/passwd", "/var/passwd_copy" );
320 #endif
321     
322     diag_printf("<INFO>: ROMFS list of / follows\n");
323 #ifdef CYGPKG_FS_RAM
324     diag_printf("<INFO>: Note that /var now gives stat() info for RAMFS\n");
325 #endif
326     listdir( "/", true );
327
328     diag_printf("<INFO>: Mount ROMFS again onto /mnt\n");
329     sprintf( address, "%p", (void*)&filedata[0] );
330     err = mount( address, "/mnt", "romfs" );
331     if( err < 0 ) SHOW_RESULT( mount, err );    
332
333     comparefiles( "/etc/passwd", "/mnt/etc/passwd" );
334
335
336     err = mkdir( "/foo", 0 );
337     CHKFAIL_TYPE( mkdir, err, EROFS );
338
339     err = rename( "/var", "/tmp" );     // RAMFS is mounted here
340 #ifdef CYGPKG_FS_RAM
341     CHKFAIL_TYPE( rename, err, EXDEV );
342 #else
343     CHKFAIL_TYPE( rename, err, EROFS );
344 #endif
345
346     err = rename( "/var/passwd_copy", "/mnt/etc/passwd_copy" );
347     CHKFAIL_TYPE( rename, err, EXDEV );
348
349     err = rename( "/etc", "/tmp" );
350     CHKFAIL_TYPE( rename, err, EROFS );
351
352     diag_printf("<INFO>: cd /etc\n");
353     err = chdir( "/etc" );
354     if( err < 0 ) SHOW_RESULT( chdir, err );
355
356     err = chdir( "/mnt/etc" );
357     if( err < 0 ) SHOW_RESULT( chdir, err );
358
359     listdir( ".", true );
360
361     diag_printf("<INFO>: unlink /tmp\n");        
362     err = unlink( "/tmp" );
363     CHKFAIL_TYPE( unlink, err, EROFS );
364
365     diag_printf("<INFO>: mount random area\n");
366     sprintf(address, "%p", (void*)(&filedata[0] + 0x100));
367     err = mount( address, "/tmp", "romfs" );
368     CHKFAIL_TYPE( mount, err, ENOENT );
369
370     err = umount( "/mnt" );
371     if( err < 0 ) SHOW_RESULT( umount, err );    
372
373     err = umount( "/var" );
374 #ifdef CYGPKG_FS_RAM
375     if( err < 0 ) SHOW_RESULT( umount, err );    
376 #else
377     CHKFAIL_TYPE( umount, err, EINVAL );
378 #endif
379
380     err = umount( "/" );
381     if( err < 0 ) SHOW_RESULT( umount, err );    
382
383
384     CYG_TEST_PASS_FINISH("romfs1");
385 }
386
387 // -------------------------------------------------------------------------
388 // EOF romfs1.c