]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/fs/jffs2/v2_0/tests/jffs2_2.c
Initial revision
[karo-tx-redboot.git] / packages / fs / jffs2 / v2_0 / tests / jffs2_2.c
1 //==========================================================================
2 //
3 //      jffs2_2.c
4 //
5 //      Test fseek on a filesystem
6 //
7 //==========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 2004 Andrew Lunn
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):           asl
45 // Contributors:        asl
46 // Date:                2004-03-29
47 // Purpose:             Test fseek on a filesystem
48 // Description:         This test uses the ramfs to check out the fseek
49 //                      operation on a filesystem.
50 //                      
51 //                      
52 //                      
53 //                      
54 //                      
55 //              
56 //
57 //####DESCRIPTIONEND####
58 //
59 //==========================================================================
60 #include <stdio.h>
61 #include <unistd.h>
62 #include <fcntl.h>
63 #include <errno.h>
64 #include <string.h>
65
66 #include <cyg/fileio/fileio.h>
67 #include <cyg/io/flash.h>
68
69 #include <cyg/infra/testcase.h>
70 #include <cyg/infra/diag.h>            // HAL polled output
71 //==========================================================================
72
73 #define SHOW_RESULT( _fn, _res ) \
74 diag_printf("FAIL: " #_fn "() returned %ld %s\n", \
75            (unsigned long)_res, _res<0?strerror(errno):"");
76
77 //==========================================================================
78
79 char buf[1024];
80 char buf1[1024];
81
82 //==========================================================================
83 // main
84
85 int main( int argc, char **argv )
86 {
87     int err;
88     FILE *stream;
89     long pos;
90     int i;
91     
92     CYG_TEST_INIT();
93
94     // --------------------------------------------------------------
95
96     CYG_TEST_INFO("mount /");    
97     err = mount( CYGDAT_IO_FLASH_BLOCK_DEVICE_NAME_1, "/", "jffs2" );
98
99     if( err < 0 ) SHOW_RESULT( mount, err );    
100     
101     CYG_TEST_INFO("creating /fseek");    
102     stream = fopen("/fseek","w+");
103     if (!stream) {
104       diag_printf("FAIL: fopen() returned NULL, %s\n", strerror(errno));
105       CYG_TEST_FINISH("done");          \
106     }
107
108     /* Write a buffer full of cyclic numbers */
109     for (i = 0; i < sizeof(buf); i++) {
110       buf[i] = i % 256;
111     }
112     
113     CYG_TEST_INFO("writing test pattern");    
114     err=fwrite(buf,sizeof(buf), 1, stream);
115     if ( err < 0 ) SHOW_RESULT( fwrite, err );
116     
117     /* The current position should be the same size as the buffer */
118     pos = ftell(stream);
119     
120     if (pos < 0) SHOW_RESULT( ftell, pos );
121     if (pos != sizeof(buf))
122       diag_printf("<FAIL>: ftell is not telling the truth.");
123     
124     CYG_TEST_INFO("fseek()ing to beginning and writing");    
125
126     /* Seek back to the beginning of the file */
127     err = fseek(stream, 0, SEEK_SET);
128     if ( err < 0 ) SHOW_RESULT( fseek, err );
129
130     pos = ftell(stream);
131     
132     if (pos < 0) SHOW_RESULT( ftell, pos );
133     if (pos != 0) CYG_TEST_FAIL("ftell is not telling the truth");
134
135     /* Write 4 zeros to the beginning of the file */
136     for (i = 0; i < 4; i++) {
137       buf[i] = 0;
138     }
139
140     err = fwrite(buf, 4, 1, stream);
141     if ( err < 0 ) SHOW_RESULT( fwrite, err );
142
143     /* Check the pointer is at 4 */
144     pos = ftell(stream);
145     
146     if (pos < 0) SHOW_RESULT( ftell, pos );
147     if (pos != 4)  CYG_TEST_FAIL("ftell is not telling the truth");
148
149     CYG_TEST_INFO("closing file");
150     
151     /* Close the file, open it up again and read it back */
152     err = fclose(stream);
153     if (err != 0) SHOW_RESULT( fclose, err );
154
155     CYG_TEST_INFO("open file /fseek");
156     stream = fopen("/fseek", "r+");
157     if (!stream) {
158       diag_printf("<FAIL>: fopen() returned NULL, %s\n", strerror(errno));
159     }
160
161     err = fread(buf1,sizeof(buf1),1, stream);
162     if (err != 1) SHOW_RESULT( fread, err );
163     
164     CYG_TEST_INFO("Comparing contents");
165     if (memcmp(buf, buf1, sizeof(buf1))) {
166       CYG_TEST_FAIL("File contents inconsistent");
167     }
168     
169     CYG_TEST_INFO("closing file");
170
171     err = fclose(stream);
172     if (err != 0) SHOW_RESULT( fclose, err );
173
174     CYG_TEST_INFO("open file /fseek");
175     stream = fopen("/fseek", "r+");
176     if (!stream) {
177       diag_printf("<FAIL>: fopen() returned NULL, %s\n", strerror(errno));
178     }
179
180     CYG_TEST_INFO("fseek()ing past the end to create a hole");
181     /* Seek 1K after the end of the file */
182     err = fseek(stream, sizeof(buf), SEEK_END);
183     if ( err < 0 ) SHOW_RESULT( fseek, err );
184
185     pos = ftell(stream);
186     
187     if (pos < 0) SHOW_RESULT( ftell, pos );
188     if (pos != (2*sizeof(buf))) CYG_TEST_FAIL("ftell is not telling the truth");
189     
190     CYG_TEST_INFO("writing test pattern");    
191     err=fwrite(buf,sizeof(buf), 1, stream);
192     if ( err < 0 ) SHOW_RESULT( fwrite, err );
193     
194     pos = ftell(stream);
195     
196     if (pos < 0) SHOW_RESULT( ftell, pos );
197     if (pos != (3*sizeof(buf))) CYG_TEST_FAIL("ftell is not telling the truth");
198
199     CYG_TEST_INFO("closing file");
200     err = fclose(stream);
201     if (err != 0) SHOW_RESULT( fclose, err );
202
203     CYG_TEST_INFO("open file /fseek");
204     stream = fopen("/fseek", "r+");
205     if (!stream) {
206       diag_printf("<FAIL>: fopen() returned NULL, %s\n", strerror(errno));
207     }
208     
209     err = fread(buf1,sizeof(buf1),1, stream);
210     if (err != 1) SHOW_RESULT( fread, err );
211     
212     CYG_TEST_INFO("Comparing contents");
213     if (memcmp(buf, buf1, sizeof(buf1))) {
214       CYG_TEST_FAIL("File contents inconsistent");
215     }
216
217     err = fread(buf1,sizeof(buf1),1, stream);
218     if (err != 1) SHOW_RESULT( fread, err );
219     
220     for (i = 0; i< sizeof(buf); i++) {
221       if (buf1[i] != 0)
222         CYG_TEST_FAIL("Hole does not contain zeros");
223     }
224     
225     err = fread(buf1,sizeof(buf1),1, stream);
226     if (err != 1) SHOW_RESULT( fread, err );
227     
228     if (memcmp(buf, buf1, sizeof(buf1))) {
229       CYG_TEST_FAIL("File contents inconsistent");
230     }
231
232     CYG_TEST_INFO("closing file");
233
234     /* Close the file */
235     err = fclose(stream);
236     if (err != 0) SHOW_RESULT( fclose, err );
237     
238     CYG_TEST_INFO("umount /");    
239     err = umount( "/" );
240     if( err < 0 ) SHOW_RESULT( umount, err );    
241     
242     CYG_TEST_PASS_FINISH("jffs2_2");
243 }