1 //==========================================================================
5 // Test fseek on a filesystem
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
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.
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
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.
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.
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.
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####
47 // Purpose: Test fseek on a filesystem
48 // Description: This test uses the ramfs to check out the fseek
49 // operation on a filesystem.
57 //####DESCRIPTIONEND####
59 //==========================================================================
66 #include <cyg/fileio/fileio.h>
67 #include <cyg/io/flash.h>
69 #include <cyg/infra/testcase.h>
70 #include <cyg/infra/diag.h> // HAL polled output
71 //==========================================================================
73 #define SHOW_RESULT( _fn, _res ) \
74 diag_printf("FAIL: " #_fn "() returned %ld %s\n", \
75 (unsigned long)_res, _res<0?strerror(errno):"");
77 //==========================================================================
82 //==========================================================================
85 int main( int argc, char **argv )
94 // --------------------------------------------------------------
96 CYG_TEST_INFO("mount /");
97 err = mount( CYGDAT_IO_FLASH_BLOCK_DEVICE_NAME_1, "/", "jffs2" );
99 if( err < 0 ) SHOW_RESULT( mount, err );
101 CYG_TEST_INFO("creating /fseek");
102 stream = fopen("/fseek","w+");
104 diag_printf("FAIL: fopen() returned NULL, %s\n", strerror(errno));
105 CYG_TEST_FINISH("done"); \
108 /* Write a buffer full of cyclic numbers */
109 for (i = 0; i < sizeof(buf); i++) {
113 CYG_TEST_INFO("writing test pattern");
114 err=fwrite(buf,sizeof(buf), 1, stream);
115 if ( err < 0 ) SHOW_RESULT( fwrite, err );
117 /* The current position should be the same size as the buffer */
120 if (pos < 0) SHOW_RESULT( ftell, pos );
121 if (pos != sizeof(buf))
122 diag_printf("<FAIL>: ftell is not telling the truth.");
124 CYG_TEST_INFO("fseek()ing to beginning and writing");
126 /* Seek back to the beginning of the file */
127 err = fseek(stream, 0, SEEK_SET);
128 if ( err < 0 ) SHOW_RESULT( fseek, err );
132 if (pos < 0) SHOW_RESULT( ftell, pos );
133 if (pos != 0) CYG_TEST_FAIL("ftell is not telling the truth");
135 /* Write 4 zeros to the beginning of the file */
136 for (i = 0; i < 4; i++) {
140 err = fwrite(buf, 4, 1, stream);
141 if ( err < 0 ) SHOW_RESULT( fwrite, err );
143 /* Check the pointer is at 4 */
146 if (pos < 0) SHOW_RESULT( ftell, pos );
147 if (pos != 4) CYG_TEST_FAIL("ftell is not telling the truth");
149 CYG_TEST_INFO("closing file");
151 /* Close the file, open it up again and read it back */
152 err = fclose(stream);
153 if (err != 0) SHOW_RESULT( fclose, err );
155 CYG_TEST_INFO("open file /fseek");
156 stream = fopen("/fseek", "r+");
158 diag_printf("<FAIL>: fopen() returned NULL, %s\n", strerror(errno));
161 err = fread(buf1,sizeof(buf1),1, stream);
162 if (err != 1) SHOW_RESULT( fread, err );
164 CYG_TEST_INFO("Comparing contents");
165 if (memcmp(buf, buf1, sizeof(buf1))) {
166 CYG_TEST_FAIL("File contents inconsistent");
169 CYG_TEST_INFO("closing file");
171 err = fclose(stream);
172 if (err != 0) SHOW_RESULT( fclose, err );
174 CYG_TEST_INFO("open file /fseek");
175 stream = fopen("/fseek", "r+");
177 diag_printf("<FAIL>: fopen() returned NULL, %s\n", strerror(errno));
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 );
187 if (pos < 0) SHOW_RESULT( ftell, pos );
188 if (pos != (2*sizeof(buf))) CYG_TEST_FAIL("ftell is not telling the truth");
190 CYG_TEST_INFO("writing test pattern");
191 err=fwrite(buf,sizeof(buf), 1, stream);
192 if ( err < 0 ) SHOW_RESULT( fwrite, err );
196 if (pos < 0) SHOW_RESULT( ftell, pos );
197 if (pos != (3*sizeof(buf))) CYG_TEST_FAIL("ftell is not telling the truth");
199 CYG_TEST_INFO("closing file");
200 err = fclose(stream);
201 if (err != 0) SHOW_RESULT( fclose, err );
203 CYG_TEST_INFO("open file /fseek");
204 stream = fopen("/fseek", "r+");
206 diag_printf("<FAIL>: fopen() returned NULL, %s\n", strerror(errno));
209 err = fread(buf1,sizeof(buf1),1, stream);
210 if (err != 1) SHOW_RESULT( fread, err );
212 CYG_TEST_INFO("Comparing contents");
213 if (memcmp(buf, buf1, sizeof(buf1))) {
214 CYG_TEST_FAIL("File contents inconsistent");
217 err = fread(buf1,sizeof(buf1),1, stream);
218 if (err != 1) SHOW_RESULT( fread, err );
220 for (i = 0; i< sizeof(buf); i++) {
222 CYG_TEST_FAIL("Hole does not contain zeros");
225 err = fread(buf1,sizeof(buf1),1, stream);
226 if (err != 1) SHOW_RESULT( fread, err );
228 if (memcmp(buf, buf1, sizeof(buf1))) {
229 CYG_TEST_FAIL("File contents inconsistent");
232 CYG_TEST_INFO("closing file");
235 err = fclose(stream);
236 if (err != 0) SHOW_RESULT( fclose, err );
238 CYG_TEST_INFO("umount /");
240 if( err < 0 ) SHOW_RESULT( umount, err );
242 CYG_TEST_PASS_FINISH("jffs2_2");