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