OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [fs/] [jffs2/] [current/] [tests/] [jffs2_2.c] - Blame information for rev 838

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
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 Free Software Foundation, 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      
16
// version.                                                                 
17
//
18
// eCos is distributed in the hope that it will be useful, but WITHOUT      
19
// ANY 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        
24
// along with eCos; if not, write to the Free Software Foundation, Inc.,    
25
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.            
26
//
27
// As a special exception, if other files instantiate templates or use      
28
// macros or inline functions from this file, or you compile this file      
29
// and link it with other works to produce a work based on this file,       
30
// this file does not by itself cause the resulting work to be covered by   
31
// the GNU General Public License. However the source code for this file    
32
// must still be made available in accordance with section (3) of the GNU   
33
// General Public License v2.                                               
34
//
35
// This exception does not invalidate any other reasons why a work based    
36
// on this file might be covered by the GNU General Public License.         
37
// -------------------------------------------                              
38
// ####ECOSGPLCOPYRIGHTEND####                                              
39
//==========================================================================
40
//#####DESCRIPTIONBEGIN####
41
//
42
// Author(s):           asl
43
// Contributors:        asl
44
// Date:                2004-03-29
45
// Purpose:             Test fseek on a filesystem
46
// Description:         This test uses the ramfs to check out the fseek
47
//                      operation on a filesystem.
48
//                      
49
//                      
50
//                      
51
//                      
52
//                      
53
//              
54
//
55
//####DESCRIPTIONEND####
56
//
57
//==========================================================================
58
 
59
#include <pkgconf/io_flash.h>
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
#include <pkgconf/fs_jffs2.h>   // Address of JFFS2
73
 
74
//==========================================================================
75
// Mount details
76
 
77
#define stringify2(_x_) #_x_
78
#define stringify(_x_) stringify2(_x_)
79
 
80
#if defined(CYGDAT_IO_FLASH_BLOCK_DEVICE_NAME_1)
81
# define JFFS2_TEST_DEV CYGDAT_IO_FLASH_BLOCK_DEVICE_NAME_1
82
#elif defined(CYGFUN_IO_FLASH_BLOCK_FROM_FIS)
83
# define JFFS2_TEST_DEV "/dev/flash/fis/jffs2test"
84
#else
85
// fall back to using a user set area in the first device (only)
86
# define JFFS2_TEST_DEV "/dev/flash/0/" stringify(CYGNUM_FS_JFFS2_TEST_OFFSET) "," stringify(CYGNUM_FS_JFFS2_TEST_LENGTH)
87
#endif
88
 
89
//==========================================================================
90
 
91
#define SHOW_RESULT( _fn, _res ) \
92
diag_printf("FAIL: " #_fn "() returned %ld %s\n", \
93
           (unsigned long)_res, _res<0?strerror(errno):"");
94
 
95
//==========================================================================
96
 
97
char buf[1024];
98
char buf1[1024];
99
 
100
//==========================================================================
101
// main
102
 
103
int main( int argc, char **argv )
104
{
105
    int err;
106
    FILE *stream;
107
    long pos;
108
    int i;
109
 
110
    CYG_TEST_INIT();
111
 
112
    // --------------------------------------------------------------
113
 
114
    CYG_TEST_INFO("mount /");
115
    err = mount( JFFS2_TEST_DEV, "/", "jffs2" );
116
 
117
    if( err < 0 ) SHOW_RESULT( mount, err );
118
 
119
    CYG_TEST_INFO("creating /fseek");
120
    stream = fopen("/fseek","w+");
121
    if (!stream) {
122
      diag_printf("FAIL: fopen() returned NULL, %s\n", strerror(errno));
123
      CYG_TEST_FINISH("done");          \
124
    }
125
 
126
    /* Write a buffer full of cyclic numbers */
127
    for (i = 0; i < sizeof(buf); i++) {
128
      buf[i] = i % 256;
129
    }
130
 
131
    CYG_TEST_INFO("writing test pattern");
132
    err=fwrite(buf,sizeof(buf), 1, stream);
133
    if ( err < 0 ) SHOW_RESULT( fwrite, err );
134
 
135
    /* The current position should be the same size as the buffer */
136
    pos = ftell(stream);
137
 
138
    if (pos < 0) SHOW_RESULT( ftell, pos );
139
    if (pos != sizeof(buf))
140
      diag_printf("<FAIL>: ftell is not telling the truth.");
141
 
142
    CYG_TEST_INFO("fseek()ing to beginning and writing");
143
 
144
    /* Seek back to the beginning of the file */
145
    err = fseek(stream, 0, SEEK_SET);
146
    if ( err < 0 ) SHOW_RESULT( fseek, err );
147
 
148
    pos = ftell(stream);
149
 
150
    if (pos < 0) SHOW_RESULT( ftell, pos );
151
    if (pos != 0) CYG_TEST_FAIL("ftell is not telling the truth");
152
 
153
    /* Write 4 zeros to the beginning of the file */
154
    for (i = 0; i < 4; i++) {
155
      buf[i] = 0;
156
    }
157
 
158
    err = fwrite(buf, 4, 1, stream);
159
    if ( err < 0 ) SHOW_RESULT( fwrite, err );
160
 
161
    /* Check the pointer is at 4 */
162
    pos = ftell(stream);
163
 
164
    if (pos < 0) SHOW_RESULT( ftell, pos );
165
    if (pos != 4)  CYG_TEST_FAIL("ftell is not telling the truth");
166
 
167
    CYG_TEST_INFO("closing file");
168
 
169
    /* Close the file, open it up again and read it back */
170
    err = fclose(stream);
171
    if (err != 0) SHOW_RESULT( fclose, err );
172
 
173
    CYG_TEST_INFO("open file /fseek");
174
    stream = fopen("/fseek", "r+");
175
    if (!stream) {
176
      diag_printf("<FAIL>: fopen() returned NULL, %s\n", strerror(errno));
177
    }
178
 
179
    err = fread(buf1,sizeof(buf1),1, stream);
180
    if (err != 1) SHOW_RESULT( fread, err );
181
 
182
    CYG_TEST_INFO("Comparing contents");
183
    if (memcmp(buf, buf1, sizeof(buf1))) {
184
      CYG_TEST_FAIL("File contents inconsistent");
185
    }
186
 
187
    CYG_TEST_INFO("closing file");
188
 
189
    err = fclose(stream);
190
    if (err != 0) SHOW_RESULT( fclose, err );
191
 
192
    CYG_TEST_INFO("open file /fseek");
193
    stream = fopen("/fseek", "r+");
194
    if (!stream) {
195
      diag_printf("<FAIL>: fopen() returned NULL, %s\n", strerror(errno));
196
    }
197
 
198
    CYG_TEST_INFO("fseek()ing past the end to create a hole");
199
    /* Seek 1K after the end of the file */
200
    err = fseek(stream, sizeof(buf), SEEK_END);
201
    if ( err < 0 ) SHOW_RESULT( fseek, err );
202
 
203
    pos = ftell(stream);
204
 
205
    if (pos < 0) SHOW_RESULT( ftell, pos );
206
    if (pos != (2*sizeof(buf))) CYG_TEST_FAIL("ftell is not telling the truth");
207
 
208
    CYG_TEST_INFO("writing test pattern");
209
    err=fwrite(buf,sizeof(buf), 1, stream);
210
    if ( err < 0 ) SHOW_RESULT( fwrite, err );
211
 
212
    pos = ftell(stream);
213
 
214
    if (pos < 0) SHOW_RESULT( ftell, pos );
215
    if (pos != (3*sizeof(buf))) CYG_TEST_FAIL("ftell is not telling the truth");
216
 
217
    CYG_TEST_INFO("closing file");
218
    err = fclose(stream);
219
    if (err != 0) SHOW_RESULT( fclose, err );
220
 
221
    CYG_TEST_INFO("open file /fseek");
222
    stream = fopen("/fseek", "r+");
223
    if (!stream) {
224
      diag_printf("<FAIL>: fopen() returned NULL, %s\n", strerror(errno));
225
    }
226
 
227
    err = fread(buf1,sizeof(buf1),1, stream);
228
    if (err != 1) SHOW_RESULT( fread, err );
229
 
230
    CYG_TEST_INFO("Comparing contents");
231
    if (memcmp(buf, buf1, sizeof(buf1))) {
232
      CYG_TEST_FAIL("File contents inconsistent");
233
    }
234
 
235
    err = fread(buf1,sizeof(buf1),1, stream);
236
    if (err != 1) SHOW_RESULT( fread, err );
237
 
238
    for (i = 0; i< sizeof(buf); i++) {
239
      if (buf1[i] != 0)
240
        CYG_TEST_FAIL("Hole does not contain zeros");
241
    }
242
 
243
    err = fread(buf1,sizeof(buf1),1, stream);
244
    if (err != 1) SHOW_RESULT( fread, err );
245
 
246
    if (memcmp(buf, buf1, sizeof(buf1))) {
247
      CYG_TEST_FAIL("File contents inconsistent");
248
    }
249
 
250
    CYG_TEST_INFO("closing file");
251
 
252
    /* Close the file */
253
    err = fclose(stream);
254
    if (err != 0) SHOW_RESULT( fclose, err );
255
 
256
    CYG_TEST_INFO("umount /");
257
    err = umount( "/" );
258
    if( err < 0 ) SHOW_RESULT( umount, err );
259
 
260
    CYG_TEST_PASS_FINISH("jffs2_2");
261
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.