|
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\t_lock.cpp |
|
15 // |
|
16 // |
|
17 |
|
18 #include <f32file.h> |
|
19 #include <e32test.h> |
|
20 #include "t_server.h" |
|
21 |
|
22 |
|
23 class RFileTest : public RFile |
|
24 { |
|
25 public: |
|
26 RFileTest(const TDesC& aName); |
|
27 RFileTest& Replace(const TDesC& aName); |
|
28 RFileTest& Open(const TDesC& aName); |
|
29 RFileTest& Lock(TInt aPos,TInt aLen=1); |
|
30 RFileTest& LockE(TInt aPos,TInt aLen=1); |
|
31 RFileTest& UnLock(TInt aPos,TInt aLen=1); |
|
32 RFileTest& UnLockE(TInt aPos,TInt aLen=1); |
|
33 RFileTest& LockEA(TInt aPos,TInt aLen=1); |
|
34 RFileTest& UnLockEA(TInt aPos,TInt aLen=1); |
|
35 RFileTest& Write(TInt aPos,TInt aLen=1); |
|
36 RFileTest& WriteE(TInt aPos,TInt aLen=1); |
|
37 RFileTest& Read(TInt aPos,TInt aLen=1); |
|
38 RFileTest& ReadE(TInt aPos,TInt aLen=1); |
|
39 RFileTest& Size(TInt aSize); |
|
40 RFileTest& SizeE(TInt aSize); |
|
41 private: |
|
42 TName iName; |
|
43 }; |
|
44 |
|
45 GLDEF_D RTest test(_L("T_LOCK")); |
|
46 LOCAL_D RFileTest Test1(_L("File 1")); |
|
47 LOCAL_D RFileTest Test2(_L("File 2")); |
|
48 LOCAL_D TBuf8<0x100> Pattern; |
|
49 LOCAL_D TBuf8<0x100> Buffer; |
|
50 |
|
51 LOCAL_C void DoFormat() |
|
52 // |
|
53 // Format the ramdisk |
|
54 // |
|
55 { |
|
56 |
|
57 TInt count; |
|
58 RFormat format; |
|
59 #if defined(__WINS__) |
|
60 TInt r=format.Open(TheFs,_L("Y:\\"),EHighDensity,count); |
|
61 #else |
|
62 TInt r=format.Open(TheFs,_L("C:\\"),EHighDensity,count); |
|
63 #endif |
|
64 test(r==KErrNone); |
|
65 while(count) |
|
66 { |
|
67 r=format.Next(count); |
|
68 test(r==KErrNone); |
|
69 } |
|
70 format.Close(); |
|
71 } |
|
72 |
|
73 LOCAL_C void MakeTestDirectory() |
|
74 // |
|
75 // Format the ramdisk if it is corrupt |
|
76 // |
|
77 { |
|
78 |
|
79 TPtrC testDir=_L("\\F32-TST\\"); |
|
80 TInt r=TheFs.MkDir(testDir); |
|
81 if (r==KErrNone || r==KErrAlreadyExists) |
|
82 return; |
|
83 test.Next(_L("Formatting disk")); |
|
84 DoFormat(); |
|
85 r=TheFs.MkDir(testDir); |
|
86 test(r==KErrNone); |
|
87 } |
|
88 |
|
89 RFileTest::RFileTest(const TDesC& aName) |
|
90 // |
|
91 // Constructor |
|
92 // |
|
93 : iName(aName) |
|
94 {} |
|
95 |
|
96 RFileTest& RFileTest::Replace(const TDesC& aName) |
|
97 // |
|
98 // Replace a file. |
|
99 // |
|
100 { |
|
101 |
|
102 test.Printf(_L("%S replace %S\n"),&iName,&aName); |
|
103 TInt r=RFile::Replace(TheFs,aName,EFileStream|EFileWrite|EFileShareAny); |
|
104 test(r==KErrNone); |
|
105 return(*this); |
|
106 } |
|
107 |
|
108 RFileTest& RFileTest::Open(const TDesC& aName) |
|
109 // |
|
110 // Open a file. |
|
111 // |
|
112 { |
|
113 |
|
114 test.Printf(_L("%S open %S\n"),&iName,&aName); |
|
115 TInt r=RFile::Open(TheFs,aName,EFileStream|EFileWrite|EFileShareAny); |
|
116 test(r==KErrNone); |
|
117 return(*this); |
|
118 } |
|
119 |
|
120 RFileTest& RFileTest::Lock(TInt aPos,TInt aLen) |
|
121 // |
|
122 // Set a lock on the file. Expected not to fail. |
|
123 // |
|
124 { |
|
125 |
|
126 test.Printf(_L("%S lock %08x-%08x\n"),&iName,aPos,aPos+aLen-1); |
|
127 TInt r=RFile::Lock(aPos,aLen); |
|
128 test(r==KErrNone); |
|
129 return(*this); |
|
130 } |
|
131 |
|
132 RFileTest& RFileTest::LockE(TInt aPos,TInt aLen) |
|
133 // |
|
134 // Set a lock on the file. Expected to fail. |
|
135 // |
|
136 { |
|
137 |
|
138 test.Printf(_L("%S lockE %08x-%08x\n"),&iName,aPos,aPos+aLen-1); |
|
139 TInt r=RFile::Lock(aPos,aLen); |
|
140 test(r==KErrLocked); |
|
141 return(*this); |
|
142 } |
|
143 |
|
144 RFileTest& RFileTest::UnLock(TInt aPos,TInt aLen) |
|
145 // |
|
146 // Unlock the file. Expected not to fail. |
|
147 // |
|
148 { |
|
149 |
|
150 test.Printf(_L("%S ulock %08x-%08x\n"),&iName,aPos,aPos+aLen-1); |
|
151 TInt r=RFile::UnLock(aPos,aLen); |
|
152 test(r==KErrNone); |
|
153 return(*this); |
|
154 } |
|
155 |
|
156 RFileTest& RFileTest::UnLockE(TInt aPos,TInt aLen) |
|
157 // |
|
158 // Unlock the file. Expected to fail. |
|
159 // |
|
160 { |
|
161 |
|
162 test.Printf(_L("%S ulockE %08x-%08x\n"),&iName,aPos,aPos+aLen-1); |
|
163 TInt r=RFile::UnLock(aPos,aLen); |
|
164 test(r==KErrNotFound); |
|
165 return(*this); |
|
166 } |
|
167 |
|
168 |
|
169 RFileTest& RFileTest::LockEA(TInt aPos,TInt aLen) |
|
170 // |
|
171 // Set a lock on the file. Expected to fail with KErrArgument. |
|
172 // |
|
173 { |
|
174 |
|
175 test.Printf(_L("%S lock %08x-%08x\n"),&iName,aPos,aPos+aLen-1); |
|
176 TInt r=RFile::Lock(aPos,aLen); |
|
177 test(r==KErrArgument); |
|
178 return(*this); |
|
179 } |
|
180 |
|
181 RFileTest& RFileTest::UnLockEA(TInt aPos,TInt aLen) |
|
182 // |
|
183 // Unlock the file. Expected to fail with KErrArgument. |
|
184 // |
|
185 { |
|
186 |
|
187 test.Printf(_L("%S ulock %08x-%08x\n"),&iName,aPos,aPos+aLen-1); |
|
188 TInt r=RFile::UnLock(aPos,aLen); |
|
189 test(r==KErrArgument); |
|
190 return(*this); |
|
191 } |
|
192 |
|
193 RFileTest& RFileTest::Write(TInt aPos,TInt aLen) |
|
194 // |
|
195 // Write to the file. Expected not to fail. |
|
196 // |
|
197 { |
|
198 |
|
199 test.Printf(_L("%S write %08x-%08x\n"),&iName,aPos,aPos+aLen-1); |
|
200 TInt r=RFile::Write(aPos,Pattern,aLen); |
|
201 test(r==KErrNone); |
|
202 return(*this); |
|
203 } |
|
204 |
|
205 RFileTest& RFileTest::WriteE(TInt aPos,TInt aLen) |
|
206 // |
|
207 // Write to the file. Expected to fail. |
|
208 // |
|
209 { |
|
210 |
|
211 test.Printf(_L("%S writeE %08x-%08x\n"),&iName,aPos,aPos+aLen-1); |
|
212 TInt r=RFile::Write(aPos,Pattern,aLen); |
|
213 test(r==KErrLocked); |
|
214 return(*this); |
|
215 } |
|
216 |
|
217 RFileTest& RFileTest::Read(TInt aPos,TInt aLen) |
|
218 // |
|
219 // Read from the file. Expected not to fail. |
|
220 // |
|
221 { |
|
222 |
|
223 test.Printf(_L("%S read %08x-%08x\n"),&iName,aPos,aPos+aLen-1); |
|
224 TInt r=RFile::Read(aPos,Buffer,aLen); |
|
225 test(r==KErrNone); |
|
226 return(*this); |
|
227 } |
|
228 |
|
229 RFileTest& RFileTest::ReadE(TInt aPos,TInt aLen) |
|
230 // |
|
231 // Read from the file. Expected to fail. |
|
232 // |
|
233 { |
|
234 |
|
235 test.Printf(_L("%S readE %08x-%08x\n"),&iName,aPos,aPos+aLen-1); |
|
236 TInt r=RFile::Read(aPos,Buffer,aLen); |
|
237 test(r==KErrLocked); |
|
238 return(*this); |
|
239 } |
|
240 |
|
241 RFileTest& RFileTest::Size(TInt aSize) |
|
242 // |
|
243 // Set the size of the file. Expected not to fail. |
|
244 // |
|
245 { |
|
246 |
|
247 test.Printf(_L("%S size %08x\n"),&iName,aSize); |
|
248 TInt r=RFile::SetSize(aSize); |
|
249 test(r==KErrNone); |
|
250 return(*this); |
|
251 } |
|
252 |
|
253 RFileTest& RFileTest::SizeE(TInt aSize) |
|
254 // |
|
255 // Set the size of the file. Expected to fail. |
|
256 // |
|
257 { |
|
258 |
|
259 test.Printf(_L("%S sizeE %08x\n"),&iName,aSize); |
|
260 TInt r=RFile::SetSize(aSize); |
|
261 test(r==KErrLocked); |
|
262 return(*this); |
|
263 } |
|
264 |
|
265 LOCAL_C void setup() |
|
266 // |
|
267 // Create the test files. |
|
268 // |
|
269 { |
|
270 |
|
271 test.Start(_L("Settting test environment")); |
|
272 // |
|
273 Test1.Replace(_L("\\F32-TST\\LOCK.TST")); |
|
274 Test2.Open(_L("\\F32-TST\\LOCK.TST")); |
|
275 // |
|
276 test.Next(_L("Creating test pattern")); |
|
277 Pattern.SetLength(Pattern.MaxLength()); |
|
278 for (TInt i=0;i<Pattern.MaxLength();i++) |
|
279 Pattern[i]=(TText8)i; |
|
280 // |
|
281 test.End(); |
|
282 } |
|
283 |
|
284 LOCAL_C void testLock1() |
|
285 // |
|
286 // Test file sharing. |
|
287 // |
|
288 { |
|
289 |
|
290 test.Start(_L("Test locking 1")); |
|
291 // |
|
292 test.Next(_L("Single file tests")); |
|
293 Test1.UnLockE(0).Lock(0).LockE(0).UnLock(0); |
|
294 Test1.Lock(0,0x100).Lock(0x100,0x100).Lock(0x200,0x100); |
|
295 Test1.UnLock(0x100,0x100).UnLock(0x200,0x100).UnLock(0,0x100); |
|
296 Test1.Lock(0,0x100).Lock(0x200,0x100).Lock(0x100,0x100); |
|
297 Test1.UnLock(0x200,0x100).UnLock(0x100,0x100).UnLock(0,0x100); |
|
298 Test1.Lock(0,0x100).Lock(0x200,0x100); |
|
299 Test1.LockE(0x100,0x101).LockE(0x180,0x100).LockE(0x80,0x100); |
|
300 Test1.Lock(0x400,0x100).LockE(0x180,0x400).LockE(0,0x400); |
|
301 Test1.UnLock(0x0,0x100).UnLock(0x200,0x100).UnLock(0x400,0x100); |
|
302 Test1.LockEA(0x40000000,0x40000002).LockEA(0x7FFFFFFC,0x10).LockEA(0x7FFFFFFF,0x100); |
|
303 Test1.UnLockEA(0x40000000,0x40000001).UnLockEA(0x7FFFFFFF,0x100).UnLockEA(0x7FFFFFFE,0x05); |
|
304 // |
|
305 test.Next(_L("Multi file tests")); |
|
306 Test1.Lock(0); |
|
307 Test2.LockE(0); |
|
308 Test1.UnLock(0); |
|
309 Test2.Lock(0); |
|
310 Test1.LockE(0); |
|
311 Test2.UnLock(0); |
|
312 // |
|
313 test.End(); |
|
314 } |
|
315 |
|
316 GLDEF_C void CallTestsL() |
|
317 // |
|
318 // Do all tests |
|
319 // |
|
320 { |
|
321 |
|
322 MakeTestDirectory(); |
|
323 setup(); |
|
324 testLock1(); |
|
325 |
|
326 Test1.Close(); |
|
327 Test2.Close(); |
|
328 } |