0
|
1 |
// Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
2 |
// All rights reserved.
|
|
3 |
// This component and the accompanying materials are made available
|
|
4 |
// under the terms of the License "Eclipse Public License v1.0"
|
|
5 |
// which accompanies this distribution, and is available
|
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
7 |
//
|
|
8 |
// Initial Contributors:
|
|
9 |
// Nokia Corporation - initial contribution.
|
|
10 |
//
|
|
11 |
// Contributors:
|
|
12 |
//
|
|
13 |
// Description:
|
|
14 |
// f32test\server\b_file.cpp
|
|
15 |
//
|
|
16 |
//
|
|
17 |
|
149
|
18 |
#define __E32TEST_EXTENSION__
|
0
|
19 |
#include <f32file.h>
|
|
20 |
#include <e32math.h>
|
|
21 |
#include <e32test.h>
|
|
22 |
#include "t_server.h"
|
|
23 |
|
|
24 |
GLDEF_D RTest test(_L("B_FILE"));
|
|
25 |
|
|
26 |
LOCAL_D RFile TheFile;
|
|
27 |
LOCAL_D TInt bret; // Expected error return
|
|
28 |
LOCAL_D TInt aret; // Actual error return
|
|
29 |
LOCAL_D TBuf8<10000> tbuf; // Test buffer
|
|
30 |
LOCAL_D TPtrC tbin(_S("\\F32-TST\\TEST.BIN"));
|
|
31 |
LOCAL_D TPtrC rndm(_S("\\F32-TST\\RANDOM.TST"));
|
|
32 |
LOCAL_D TPtrC tzzz(_S("\\F32-TST\\ZZZZZZ.ZZZ"));
|
|
33 |
LOCAL_D const TInt KRandomNumbers=1024;
|
|
34 |
|
|
35 |
LOCAL_C void bopen(TUint aMode)
|
|
36 |
//
|
|
37 |
// Open the binary file.
|
|
38 |
//
|
|
39 |
{
|
|
40 |
|
|
41 |
aret=TheFile.Open(TheFs,tbin,aMode);
|
|
42 |
test(aret==bret);
|
|
43 |
}
|
|
44 |
|
|
45 |
LOCAL_C void bcreate(TUint aMode)
|
|
46 |
//
|
|
47 |
// Open the binary file.
|
|
48 |
//
|
|
49 |
{
|
|
50 |
|
|
51 |
aret=TheFile.Create(TheFs,tbin,aMode);
|
|
52 |
test(aret==bret);
|
|
53 |
}
|
|
54 |
|
|
55 |
LOCAL_C void breplace(TUint aMode)
|
|
56 |
//
|
|
57 |
// Open the binary file.
|
|
58 |
//
|
|
59 |
{
|
|
60 |
|
|
61 |
aret=TheFile.Replace(TheFs,tbin,aMode);
|
|
62 |
test(aret==bret);
|
|
63 |
}
|
|
64 |
|
|
65 |
LOCAL_C void bwrite(TInt aLength)
|
|
66 |
//
|
|
67 |
// Write aLength bytes of test data at current position.
|
|
68 |
//
|
|
69 |
{
|
|
70 |
|
|
71 |
CheckDisk();
|
|
72 |
test.Printf(_L("bwrite1,len=%u\n"),aLength);
|
|
73 |
TInt pos=0; // Relative position zero
|
|
74 |
aret=TheFile.Seek(ESeekCurrent,pos);
|
|
75 |
test.Printf(_L("bwrite2,pos=%u\n"),pos);
|
149
|
76 |
test_KErrNone(aret);
|
0
|
77 |
TInt count=pos&0xff;
|
|
78 |
tbuf.SetLength(aLength);
|
|
79 |
TText8* p=(TText8*)tbuf.Ptr();
|
|
80 |
TText8* pE=p+aLength;
|
|
81 |
while (p<pE)
|
|
82 |
{
|
|
83 |
*p++=(TText8)count++;
|
|
84 |
count&=0xff;
|
|
85 |
}
|
|
86 |
test.Printf(_L("bwrite3\n"));
|
|
87 |
aret=TheFile.Write(tbuf);
|
|
88 |
test.Printf(_L("bwrite4\n"));
|
|
89 |
test(aret==bret);
|
|
90 |
CheckDisk();
|
|
91 |
}
|
|
92 |
|
|
93 |
LOCAL_C void bread(TInt aLength)
|
|
94 |
//
|
|
95 |
// Read and check aLength bytes of test data at current position.
|
|
96 |
//
|
|
97 |
{
|
|
98 |
|
|
99 |
CheckDisk();
|
|
100 |
TInt pos=0; // Relative position zero
|
|
101 |
aret=TheFile.Seek(ESeekCurrent,pos);
|
149
|
102 |
test_KErrNone(aret);
|
0
|
103 |
TInt count=pos&0xff;
|
|
104 |
aret=TheFile.Read(tbuf,aLength);
|
|
105 |
if (bret<KErrNone)
|
|
106 |
{
|
|
107 |
test(bret==aret);
|
|
108 |
return;
|
|
109 |
}
|
|
110 |
test(((TInt)tbuf.Length())==bret);
|
|
111 |
const TText8* p=tbuf.Ptr();
|
|
112 |
const TText8* pE=p+bret;
|
|
113 |
while (p<pE)
|
|
114 |
{
|
|
115 |
if (*p++!=count++)
|
|
116 |
test.Panic(_L("bread data different"));
|
|
117 |
count&=0xff;
|
|
118 |
}
|
|
119 |
CheckDisk();
|
|
120 |
}
|
|
121 |
|
|
122 |
LOCAL_C void bposa(TInt aPos)
|
|
123 |
//
|
|
124 |
// Position absolute.
|
|
125 |
//
|
|
126 |
{
|
|
127 |
|
|
128 |
CheckDisk();
|
|
129 |
TInt newpos=aPos;
|
|
130 |
aret=TheFile.Seek(ESeekStart,newpos);
|
149
|
131 |
test_KErrNone(aret);
|
0
|
132 |
test(newpos==aPos);
|
|
133 |
CheckDisk();
|
|
134 |
}
|
|
135 |
|
|
136 |
LOCAL_C void bclose()
|
|
137 |
//
|
|
138 |
// Close the file.
|
|
139 |
//
|
|
140 |
{
|
|
141 |
|
|
142 |
CheckDisk();
|
|
143 |
TheFile.Close();
|
|
144 |
CheckDisk();
|
|
145 |
}
|
|
146 |
|
|
147 |
LOCAL_C void btest1(TUint aMode)
|
|
148 |
//
|
|
149 |
// Binary file tests.
|
|
150 |
//
|
|
151 |
{
|
|
152 |
|
|
153 |
test.Start(_L("BTEST1..."));
|
|
154 |
bret=0;
|
|
155 |
breplace(aMode|EFileWrite);
|
|
156 |
bret=0; bread(1);
|
|
157 |
bret=0; bwrite(1); bposa(0l);
|
|
158 |
bret=1; bread(2);
|
|
159 |
bret=0; bread(1);
|
|
160 |
bret=0; bposa(0l);
|
|
161 |
bret=1; bread(1);
|
|
162 |
bret=0; bread(1); bret=0;
|
|
163 |
bclose();
|
|
164 |
bret=KErrAlreadyExists;bcreate(aMode|EFileWrite);bret=0;
|
|
165 |
bopen(aMode|EFileRead);
|
|
166 |
bret=KErrAccessDenied; bwrite(1); bret=0;
|
|
167 |
bclose();
|
|
168 |
aret=TheFile.Open(TheFs,tzzz,EFileRead);
|
149
|
169 |
test_Value(aret, aret == KErrNotFound);
|
0
|
170 |
test.End();
|
|
171 |
}
|
|
172 |
|
|
173 |
LOCAL_C void btest2(TUint aMode)
|
|
174 |
//
|
|
175 |
// Binary file tests.
|
|
176 |
//
|
|
177 |
{
|
|
178 |
|
|
179 |
test.Start(_L("BTEST2..."));
|
|
180 |
bret=0;
|
|
181 |
breplace(aMode|EFileWrite);
|
|
182 |
bwrite(11);
|
|
183 |
bposa(0);
|
|
184 |
bret=5; bread(5); bret=0;
|
|
185 |
bwrite(45);
|
|
186 |
bposa(5);
|
|
187 |
bret=45; bread(45); bret=0;
|
|
188 |
bwrite(1000);
|
|
189 |
bposa(600);
|
|
190 |
bret=300; bread(300); bret=0;
|
|
191 |
bclose();
|
|
192 |
bopen(aMode|EFileWrite);
|
|
193 |
bposa(5);
|
|
194 |
bret=5; bread(5);
|
|
195 |
bret=1000; bread(1000); bret=0;
|
|
196 |
bclose();
|
|
197 |
bopen(aMode|EFileWrite);
|
|
198 |
bposa(KMaxTInt);
|
|
199 |
bwrite(50);
|
|
200 |
bposa(0);
|
|
201 |
bret=1100; bread(1100); bret=0;
|
|
202 |
aret=TheFile.Flush();
|
149
|
203 |
test_KErrNone(aret);
|
0
|
204 |
aret=TheFile.SetSize(2000);
|
149
|
205 |
test_KErrNone(aret);
|
0
|
206 |
TInt pos=0;
|
|
207 |
aret=TheFile.Seek(ESeekCurrent,pos);
|
149
|
208 |
test_Value(aret, aret == KErrNone && pos==1100);
|
0
|
209 |
pos=0;
|
|
210 |
aret=TheFile.Seek(ESeekEnd,pos);
|
149
|
211 |
test_Value(aret, aret == KErrNone && pos==2000);
|
0
|
212 |
bclose();
|
|
213 |
test.End();
|
|
214 |
}
|
|
215 |
|
|
216 |
LOCAL_C void rndtest(TUint aMode)
|
|
217 |
//
|
|
218 |
// Tests the file handling by writing a file of random numbers,
|
|
219 |
// closing the file, reseeding the random number generator with
|
|
220 |
// the same number, then reading the file back again, checking
|
|
221 |
// it against the generator.
|
|
222 |
//
|
|
223 |
{
|
|
224 |
|
|
225 |
TInt cnt;
|
|
226 |
test.Start(_L("RNDTEST..."));
|
|
227 |
TInt64 seed(0),zero(0);
|
|
228 |
aret=TheFile.Replace(TheFs,rndm,EFileWrite|aMode);
|
149
|
229 |
test_KErrNone(aret);
|
0
|
230 |
for (cnt=0;cnt<KRandomNumbers;cnt++)
|
|
231 |
{
|
|
232 |
TBuf8<0x10> b;
|
|
233 |
b.Format(TPtrC8((TUint8*)"%8x"),Math::Rand(seed));
|
|
234 |
aret=TheFile.Write(b);
|
149
|
235 |
test_KErrNone(aret);
|
0
|
236 |
}
|
|
237 |
TheFile.Close();
|
|
238 |
//
|
|
239 |
test.Next(_L("Reading back"));
|
|
240 |
seed=zero;
|
|
241 |
aret=TheFile.Open(TheFs,rndm,aMode);
|
149
|
242 |
test_KErrNone(aret);
|
0
|
243 |
for (cnt=0;cnt<KRandomNumbers;cnt++)
|
|
244 |
{
|
|
245 |
TBuf8<8> b;
|
|
246 |
b.Format(TPtrC8((TUint8*)"%8x"),Math::Rand(seed));
|
|
247 |
TBuf8<8> r;
|
|
248 |
aret=TheFile.Read(r);
|
149
|
249 |
test_KErrNone(aret);
|
0
|
250 |
test(b==r);
|
|
251 |
}
|
|
252 |
TheFile.Close();
|
|
253 |
aret=TheFs.Delete(rndm);
|
149
|
254 |
test_KErrNone(aret);
|
0
|
255 |
//
|
|
256 |
test.End();
|
|
257 |
}
|
|
258 |
|
|
259 |
LOCAL_C void testAutoClose()
|
|
260 |
//
|
|
261 |
// Tests TAutoClose template class
|
|
262 |
//
|
|
263 |
{
|
|
264 |
|
|
265 |
test.Start(_L("TAutoClose..."));
|
|
266 |
TAutoClose<RFile> f;
|
|
267 |
aret=f.iObj.Replace(TheFs,rndm,EFileWrite);
|
149
|
268 |
test_KErrNone(aret);
|
0
|
269 |
TInt64 seed;
|
|
270 |
for (TInt cnt=0;cnt<KRandomNumbers;cnt++)
|
|
271 |
{
|
|
272 |
TBuf8<0x10> b;
|
|
273 |
b.Format(TPtrC8((TUint8*)"%8x"),Math::Rand(seed));
|
|
274 |
aret=f.iObj.Write(b);
|
149
|
275 |
test_KErrNone(aret);
|
0
|
276 |
}
|
|
277 |
test.End();
|
|
278 |
}
|
|
279 |
|
|
280 |
LOCAL_C void readWithNegativeLengthTest()
|
|
281 |
{
|
|
282 |
test.Start(_L("Read with Negative Length Test..."));
|
|
283 |
TInt ret;
|
|
284 |
TRequestStatus status = KRequestPending;
|
|
285 |
TheFile.Open(TheFs,tbin,EFileRead);
|
|
286 |
ret = TheFile.Read(0,tbuf,-1); // sync
|
149
|
287 |
test_Value(ret, ret == KErrArgument);
|
0
|
288 |
TheFile.Read(0,tbuf,-1,status); // async
|
|
289 |
User::WaitForRequest(status);
|
|
290 |
test(status.Int() == KErrArgument);
|
|
291 |
TheFile.Close();
|
|
292 |
test.End();
|
|
293 |
}
|
|
294 |
|
|
295 |
LOCAL_C void readWithNegativeLengthTestForEmptyFile()
|
|
296 |
|
|
297 |
{
|
|
298 |
|
|
299 |
test.Start(_L("Read with Negative Length Test(For EmptyFile)..."));
|
|
300 |
RFile f;
|
|
301 |
MakeFile(_L("C:\\F32-TST\\TFILE\\hello2.txt"));
|
|
302 |
TInt r=f.Open(TheFs,_L("C:\\F32-TST\\TFILE\\hello2.txt"),EFileRead);
|
149
|
303 |
test_KErrNone(r);
|
0
|
304 |
|
|
305 |
TBuf8<0x100> a;
|
|
306 |
test.Next(_L("Check Negative length when file is empty"));
|
|
307 |
r=f.Read(a, -10);
|
149
|
308 |
test_Value(r, r == KErrArgument);
|
0
|
309 |
r=f.Read(0,a, -1);
|
149
|
310 |
test_Value(r, r == KErrArgument);
|
0
|
311 |
r=f.Read(0,a, -10);
|
149
|
312 |
test_Value(r, r == KErrArgument);
|
0
|
313 |
TRequestStatus stat1;
|
|
314 |
f.Read(0,a,-5,stat1);
|
|
315 |
User::WaitForRequest(stat1);
|
|
316 |
test(stat1.Int() == KErrArgument);
|
|
317 |
f.Read(a,-5,stat1);
|
|
318 |
User::WaitForRequest(stat1);
|
|
319 |
test(stat1.Int() == KErrArgument);
|
|
320 |
f.Close();
|
|
321 |
test.End();
|
|
322 |
|
|
323 |
}
|
|
324 |
|
|
325 |
GLDEF_C void CallTestsL()
|
|
326 |
//
|
|
327 |
// Call tests that may leave
|
|
328 |
//
|
|
329 |
{
|
|
330 |
|
|
331 |
testAutoClose();
|
|
332 |
btest1(EFileStream);
|
|
333 |
btest2(EFileStream);
|
|
334 |
btest1(EFileStreamText);
|
|
335 |
btest2(EFileStreamText);
|
|
336 |
rndtest(EFileStream);
|
|
337 |
readWithNegativeLengthTest();
|
|
338 |
readWithNegativeLengthTestForEmptyFile();
|
|
339 |
|
|
340 |
}
|