1 |
786 |
skrzyp |
//==========================================================================
|
2 |
|
|
//
|
3 |
|
|
// ramfs3.c
|
4 |
|
|
//
|
5 |
|
|
// Test fileio system, especially lseek calls.
|
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, 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): nickg, asl, Paluch Sebastian
|
43 |
|
|
// Contributors: nickg
|
44 |
|
|
// Date: 2006-10-05
|
45 |
|
|
// Purpose: Test fileio system
|
46 |
|
|
// Description: This test uses the testfs to check out the initialization
|
47 |
|
|
// and basic operation of the fileio system
|
48 |
|
|
//
|
49 |
|
|
//
|
50 |
|
|
//
|
51 |
|
|
//
|
52 |
|
|
//
|
53 |
|
|
//
|
54 |
|
|
//
|
55 |
|
|
//####DESCRIPTIONEND####
|
56 |
|
|
//
|
57 |
|
|
//==========================================================================
|
58 |
|
|
|
59 |
|
|
#include <stdio.h>
|
60 |
|
|
#include <unistd.h>
|
61 |
|
|
#include <fcntl.h>
|
62 |
|
|
#include <errno.h>
|
63 |
|
|
#include <string.h>
|
64 |
|
|
|
65 |
|
|
#include <cyg/fileio/fileio.h>
|
66 |
|
|
|
67 |
|
|
#include <cyg/infra/testcase.h>
|
68 |
|
|
#include <cyg/infra/diag.h> // HAL polled output
|
69 |
|
|
//==========================================================================
|
70 |
|
|
|
71 |
|
|
#define SHOW_RESULT( _fn, _res ) \
|
72 |
|
|
diag_printf("FAIL: " #_fn "() returned %ld %s\n", \
|
73 |
|
|
(unsigned long)_res, _res<0?strerror(errno):"");
|
74 |
|
|
|
75 |
|
|
//==========================================================================
|
76 |
|
|
|
77 |
|
|
cyg_uint8 buf[256];
|
78 |
|
|
cyg_uint8 buf1[256];
|
79 |
|
|
|
80 |
|
|
//==========================================================================
|
81 |
|
|
// main
|
82 |
|
|
|
83 |
|
|
int main( int argc, char **argv )
|
84 |
|
|
{
|
85 |
|
|
int err;
|
86 |
|
|
FILE *stream;
|
87 |
|
|
long pos;
|
88 |
|
|
unsigned int i;
|
89 |
|
|
char header[3];
|
90 |
|
|
|
91 |
|
|
CYG_TEST_INIT();
|
92 |
|
|
|
93 |
|
|
// --------------------------------------------------------------
|
94 |
|
|
|
95 |
|
|
CYG_TEST_INFO("mount /");
|
96 |
|
|
err = mount( "", "/", "ramfs" );
|
97 |
|
|
|
98 |
|
|
if( err < 0 ) SHOW_RESULT( mount, err );
|
99 |
|
|
|
100 |
|
|
CYG_TEST_INFO("creating /fseek");
|
101 |
|
|
stream = fopen("/fseek","w+");
|
102 |
|
|
if (!stream) {
|
103 |
|
|
SHOW_RESULT( fopen, NULL);
|
104 |
|
|
CYG_TEST_FAIL_FINISH("done");\
|
105 |
|
|
}
|
106 |
|
|
|
107 |
|
|
for (i = 0; i < sizeof(buf); i++) {
|
108 |
|
|
buf[i] = i % 256;
|
109 |
|
|
}
|
110 |
|
|
|
111 |
|
|
CYG_TEST_INFO("writing test pattern");
|
112 |
|
|
err=fwrite(buf,sizeof(buf), 1, stream);
|
113 |
|
|
if ( err < 0 ) SHOW_RESULT( fwrite, err );
|
114 |
|
|
|
115 |
|
|
pos = ftell(stream);
|
116 |
|
|
if (pos < 0) SHOW_RESULT( ftell, pos );
|
117 |
|
|
if (pos != sizeof(buf))
|
118 |
|
|
diag_printf("<FAIL>: ftell is not telling the truth.");
|
119 |
|
|
|
120 |
|
|
CYG_TEST_INFO("fseek()ing to 85");
|
121 |
|
|
err = fseek(stream, 85, SEEK_SET);
|
122 |
|
|
if ( err < 0 ) SHOW_RESULT( fseek, err );
|
123 |
|
|
|
124 |
|
|
pos = ftell(stream);
|
125 |
|
|
if (pos < 0) SHOW_RESULT( ftell, pos );
|
126 |
|
|
if (pos != 85) CYG_TEST_FAIL("ftell is not telling the truth");
|
127 |
|
|
|
128 |
|
|
err = fread(header,3,1,stream);
|
129 |
|
|
if ( err < 0 ) SHOW_RESULT( fwrite, err );
|
130 |
|
|
if ((header[0] != 85) ||
|
131 |
|
|
(header[1] != 86) ||
|
132 |
|
|
(header[2] != 87))
|
133 |
|
|
CYG_TEST_FAIL("Read returned false data");
|
134 |
|
|
|
135 |
|
|
pos = ftell(stream);
|
136 |
|
|
if (pos < 0) SHOW_RESULT( ftell, pos );
|
137 |
|
|
if (pos != 88) CYG_TEST_FAIL("ftell is not telling the truth");
|
138 |
|
|
|
139 |
|
|
for (i = 88; i < 161; i++) {
|
140 |
|
|
buf[i] = 0x42;
|
141 |
|
|
}
|
142 |
|
|
|
143 |
|
|
CYG_TEST_INFO("writing");
|
144 |
|
|
err = fwrite(buf+88, 73, 1, stream);
|
145 |
|
|
if ( err < 0 ) SHOW_RESULT( fwrite, err );
|
146 |
|
|
|
147 |
|
|
pos = ftell(stream);
|
148 |
|
|
if (pos < 0) SHOW_RESULT( ftell, pos );
|
149 |
|
|
if (pos != 161) CYG_TEST_FAIL("ftell is not telling the truth");
|
150 |
|
|
|
151 |
|
|
CYG_TEST_INFO("closing file");
|
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 |
|
|
SHOW_RESULT( fopen, NULL);
|
159 |
|
|
CYG_TEST_FAIL_FINISH("done");\
|
160 |
|
|
}
|
161 |
|
|
|
162 |
|
|
CYG_TEST_INFO("Seeking to beginning of file");
|
163 |
|
|
err = fseek(stream, 0, SEEK_SET);
|
164 |
|
|
if ( err < 0 ) SHOW_RESULT( fseek, err );
|
165 |
|
|
|
166 |
|
|
CYG_TEST_INFO("Reading buf1");
|
167 |
|
|
err = fread(buf1,sizeof(buf1),1, stream);
|
168 |
|
|
if (err != 1) SHOW_RESULT( fread, err );
|
169 |
|
|
|
170 |
|
|
CYG_TEST_INFO("Comparing contents");
|
171 |
|
|
if (memcmp(buf, buf1, sizeof(buf1)))
|
172 |
|
|
CYG_TEST_FAIL("File contents inconsistent");
|
173 |
|
|
|
174 |
|
|
CYG_TEST_INFO("closing file");
|
175 |
|
|
err = fclose(stream);
|
176 |
|
|
if (err != 0) SHOW_RESULT( fclose, err );
|
177 |
|
|
|
178 |
|
|
CYG_TEST_INFO("umount /");
|
179 |
|
|
err = umount( "/" );
|
180 |
|
|
if( err < 0 ) SHOW_RESULT( umount, err );
|
181 |
|
|
|
182 |
|
|
CYG_TEST_PASS_FINISH("ramfs3");
|
183 |
|
|
}
|