|
1 // Copyright (c) 1999-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_rename.cpp |
|
15 // |
|
16 // |
|
17 |
|
18 #include <f32file.h> |
|
19 #include <e32test.h> |
|
20 #include "t_server.h" |
|
21 |
|
22 #if defined(__WINS__) |
|
23 #define WIN32_LEAN_AND_MEAN |
|
24 #pragma warning (disable:4201) // warning C4201: nonstandard extension used : nameless struct/union |
|
25 #pragma warning (default:4201) // warning C4201: nonstandard extension used : nameless struct/union |
|
26 #endif |
|
27 |
|
28 #if defined(_UNICODE) |
|
29 #if !defined(UNICODE) |
|
30 #define UNICODE |
|
31 #endif |
|
32 #endif |
|
33 |
|
34 GLDEF_D RTest test(_L("T_RENAME")); |
|
35 |
|
36 TBuf8<26> alphaBuffer=_L8("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); |
|
37 TPtr8 alphaPtr((TText8*)alphaBuffer.Ptr(),alphaBuffer.Size(),alphaBuffer.Size()); |
|
38 |
|
39 TBuf8<17> BeckBuffer=_L8("A Devil's Haircut"); |
|
40 TPtr8 BeckPtr((TText8*)BeckBuffer.Ptr(),BeckBuffer.Size(),BeckBuffer.Size()); |
|
41 |
|
42 /* |
|
43 |
|
44 What this test is for: |
|
45 Tests bug fix for the bug which created two files of the same name |
|
46 |
|
47 */ |
|
48 |
|
49 LOCAL_C void CreateTestFiles() |
|
50 // |
|
51 // |
|
52 // |
|
53 { |
|
54 test.Next(_L("Create test files")); |
|
55 TInt r=TheFs.MkDir(_L("\\F32-TST\\")); |
|
56 test(r==KErrNone || r==KErrAlreadyExists); |
|
57 |
|
58 RFile file; |
|
59 |
|
60 // Create \\SessionPath\\testfile |
|
61 r=file.Replace(TheFs,_L("\\F32-TST\\testfile"),EFileRead|EFileWrite); |
|
62 test(r==KErrNone); |
|
63 r=file.Write(BeckPtr); |
|
64 test(r==KErrNone); |
|
65 file.Close(); |
|
66 |
|
67 // Create \\SessionPath\\rfsfile |
|
68 r=file.Replace(TheFs,_L("\\F32-TST\\rfsfile"),EFileRead|EFileWrite); |
|
69 test(r==KErrNone); |
|
70 r=file.Write(BeckPtr); |
|
71 test(r==KErrNone); |
|
72 file.Close(); |
|
73 |
|
74 // Create \\SessionPath\\eikfile |
|
75 r=file.Replace(TheFs,_L("\\F32-TST\\eikfile"),EFileRead|EFileWrite); |
|
76 test(r==KErrNone); |
|
77 r=file.Write(BeckPtr); |
|
78 test(r==KErrNone); |
|
79 file.Close(); |
|
80 |
|
81 } |
|
82 /* |
|
83 LOCAL_C void CleanUp() |
|
84 // |
|
85 // Delete any files created by the tests |
|
86 // |
|
87 { |
|
88 TInt r=TheFs.Delete(_L("\\F32-TST\\TESTFILE")); |
|
89 test(r==KErrNone); |
|
90 r=TheFs.Delete(_L("\\F32-TST\\RFSFILE")); |
|
91 test(r==KErrNone); |
|
92 r=TheFs.Delete(_L("\\F32-TST\\EIKFILE")); |
|
93 test(r==KErrNone); |
|
94 r=TheFs.Delete(_L("\\F32-TST\\TEST")); |
|
95 test(r==KErrNone); |
|
96 r=TheFs.RmDir(_L("\\F32-TST\\SYSTEM\\")); |
|
97 test(r==KErrNone); |
|
98 } |
|
99 */ |
|
100 |
|
101 LOCAL_C TInt CountFiles(TPtrC aDirectory, TPtrC aFileName) |
|
102 // |
|
103 // Return the number of files of aFileName found in aDirectory |
|
104 // |
|
105 { |
|
106 RDir dir; |
|
107 TFileName sessionPath; |
|
108 TInt r=TheFs.SessionPath(sessionPath); |
|
109 test(r==KErrNone); |
|
110 TFileName path=_L("?:"); |
|
111 path[0]=sessionPath[0]; |
|
112 path+=aDirectory; |
|
113 if (path[path.Length()-1]==KPathDelimiter) |
|
114 path.Append('*'); |
|
115 else |
|
116 path.Append(_L("\\*")); |
|
117 |
|
118 r=dir.Open(TheFs,path,KEntryAttMaskSupported); |
|
119 test(r==KErrNone); |
|
120 |
|
121 CDir* anEntryList; |
|
122 r=TheFs.GetDir(path,KEntryAttMaskSupported,ESortByName,anEntryList); |
|
123 test(r==KErrNone); |
|
124 |
|
125 // Sets the new length of path to the position of the last path delimiter +1 |
|
126 path.SetLength(path.LocateReverse(KPathDelimiter)+1); |
|
127 TInt fileCount=0; |
|
128 TEntry entry; |
|
129 |
|
130 TInt count=anEntryList->Count(); |
|
131 for (TInt j=0;j<count;j++) |
|
132 { |
|
133 entry=anEntryList->operator[](j); |
|
134 if ((entry.iName)==(aFileName)) |
|
135 fileCount++; |
|
136 } |
|
137 |
|
138 dir.Close(); |
|
139 delete anEntryList; |
|
140 return(fileCount); |
|
141 } |
|
142 |
|
143 LOCAL_C void TestRFileRename() |
|
144 // |
|
145 // Test RFile::Rename() function |
|
146 // |
|
147 { |
|
148 test.Next(_L("Rename file with DOS compatible name using RFile function")); |
|
149 TInt r; |
|
150 RFile file; |
|
151 |
|
152 r=file.Open(TheFs,_L("\\F32-TST\\testfile"),EFileRead|EFileWrite); |
|
153 test(r==KErrNone); |
|
154 |
|
155 r=file.Rename(_L("\\F32-TST\\TESTFILE")); |
|
156 test(r==KErrNone); |
|
157 |
|
158 file.Close(); |
|
159 |
|
160 test.Next(_L("Write in some data")); |
|
161 r=file.Open(TheFs,_L("\\F32-TST\\TESTFILE"),EFileRead|EFileWrite); |
|
162 test(r==KErrNone); |
|
163 |
|
164 r=file.Write(alphaPtr); |
|
165 test(r==KErrNone); |
|
166 |
|
167 file.Close(); |
|
168 } |
|
169 |
|
170 |
|
171 LOCAL_C void TestRFsRename() |
|
172 // |
|
173 // Test RFs::Rename() function |
|
174 // |
|
175 { |
|
176 test.Next(_L("Rename file with DOS compatible name using RFs function")); |
|
177 TInt r; |
|
178 |
|
179 r=TheFs.Rename(_L("\\F32-TST\\rfsfile"),_L("\\F32-TST\\RFSFILE")); |
|
180 test(r==KErrNone); |
|
181 |
|
182 RFile file; |
|
183 test.Next(_L("Write in some data")); |
|
184 r=file.Open(TheFs,_L("\\F32-TST\\RFSFILE"),EFileRead|EFileWrite); |
|
185 test(r==KErrNone); |
|
186 |
|
187 r=file.Write(alphaPtr); |
|
188 test(r==KErrNone); |
|
189 |
|
190 file.Close(); |
|
191 } |
|
192 |
|
193 LOCAL_C void TestEikonRename() |
|
194 // |
|
195 // Test EIKON style rename by creating a new file, and copying old data into new file |
|
196 // |
|
197 { |
|
198 test.Next(_L("Rename file with DOS compatible name simulating EIKON")); |
|
199 TInt r; |
|
200 RFile file; |
|
201 |
|
202 test.Next(_L("Create a new file with DOS compatible equivalent name")); |
|
203 r=file.Create(TheFs,_L("\\F32-TST\\EIKFILE"),EFileRead|EFileWrite); |
|
204 test((r==KErrNone)||(r==KErrAlreadyExists)); |
|
205 file.Close(); |
|
206 |
|
207 test.Next(_L("Copy data from original file into new file")); |
|
208 r=TheFs.Replace(_L("\\F32-TST\\eikfile"),_L("\\F32-TST\\EIKFILE")); |
|
209 test(r==KErrNone); |
|
210 |
|
211 test.Next(_L("Open the new file and write into it")); |
|
212 r=file.Open(TheFs,_L("\\F32-TST\\EIKFILE"),EFileRead|EFileWrite); |
|
213 test(r==KErrNone); |
|
214 |
|
215 r=file.Write(alphaPtr); |
|
216 test(r==KErrNone); |
|
217 |
|
218 file.Close(); |
|
219 } |
|
220 |
|
221 |
|
222 LOCAL_C void TestReplaceAndRename() |
|
223 // |
|
224 // Tests the bug which allows 2 files of the same name to be created has been fixed |
|
225 // |
|
226 { |
|
227 TInt r; |
|
228 RFile file; |
|
229 |
|
230 // ************************************************************************* |
|
231 // First test with a non DOS compatible name renamed to a DOS compatible name |
|
232 test.Next(_L("Rename test to TEST and replace temp with TEST")); |
|
233 r=file.Replace(TheFs,_L("\\F32-TST\\test"),EFileWrite); |
|
234 test(r==KErrNone); |
|
235 r=file.Write(BeckPtr); |
|
236 test(r==KErrNone); |
|
237 file.Close(); |
|
238 |
|
239 r=TheFs.Rename(_L("\\F32-TST\\test"),_L("\\F32-TST\\TEST")); |
|
240 test(r==KErrNone); |
|
241 r=file.Replace(TheFs,_L("\\F32-TST\\temp"),EFileWrite); |
|
242 test(r==KErrNone); |
|
243 r=file.Write(alphaPtr); |
|
244 test(r==KErrNone); |
|
245 file.Close(); |
|
246 |
|
247 // Replace(oldName, newName) |
|
248 // Copy oldName to newName (ie temp to TEST) |
|
249 // If TEST does not exist, it is created and then temp's attributes etc are copied into it |
|
250 // then temp is deleted. If it does exist, it must be closed |
|
251 // The bug created a second file of the same name |
|
252 r=TheFs.Replace(_L("\\F32-TST\\temp"),_L("\\F32-TST\\TEST")); |
|
253 test(r==KErrNone); |
|
254 |
|
255 // Check that there's only one file named TEST |
|
256 TInt fileCount=0; |
|
257 fileCount=CountFiles(_L("\\F32-TST\\"),_L("TEST")); |
|
258 test(fileCount==1); |
|
259 r=TheFs.Delete(_L("\\F32-TST\\TEST")); |
|
260 test(r==KErrNone); |
|
261 fileCount=CountFiles(_L("\\F32-TST\\"),_L("TEST")); |
|
262 test(fileCount==0); |
|
263 test(r==KErrNone); |
|
264 |
|
265 |
|
266 //***************************************************** |
|
267 // The same test but with different source directories |
|
268 test.Next(_L("Rename test to and replace \\SYSTEM\\temp with TEST")); |
|
269 r=file.Replace(TheFs,_L("\\F32-TST\\test"),EFileWrite); |
|
270 test(r==KErrNone); |
|
271 r=file.Write(BeckPtr); |
|
272 test(r==KErrNone); |
|
273 file.Close(); |
|
274 |
|
275 r=TheFs.Rename(_L("\\F32-TST\\test"),_L("\\F32-TST\\TEST")); |
|
276 test(r==KErrNone); |
|
277 r=file.Replace(TheFs,_L("\\F32-TST\\SYSTEM\\temp"),EFileWrite); |
|
278 test(r==KErrNone); |
|
279 r=file.Write(alphaPtr); |
|
280 test(r==KErrNone); |
|
281 file.Close(); |
|
282 |
|
283 // The bug created a second file of the same name |
|
284 r=TheFs.Replace(_L("\\F32-TST\\SYSTEM\\temp"),_L("\\F32-TST\\TEST")); |
|
285 test(r==KErrNone); |
|
286 |
|
287 fileCount=0; |
|
288 fileCount=CountFiles(_L("\\F32-TST\\"),_L("TEST")); |
|
289 test(fileCount==1); |
|
290 r=TheFs.Delete(_L("\\F32-TST\\TEST")); |
|
291 test(r==KErrNone); |
|
292 fileCount=CountFiles(_L("\\F32-TST\\"),_L("TEST")); |
|
293 test(fileCount==0); |
|
294 // Test that system directory is now empty |
|
295 fileCount=0; |
|
296 fileCount=CountFiles(_L("\\F32-TST\\SYSTEM\\"),_L("temp")); |
|
297 test(fileCount==0); |
|
298 test(r==KErrNone); |
|
299 |
|
300 // ************************************************************************* |
|
301 // Test with a DOS compatible name renamed to a different DOS compatible name |
|
302 test.Next(_L("Rename little to BIG and replace temp with BIG")); |
|
303 r=file.Replace(TheFs,_L("\\F32-TST\\little"),EFileWrite); |
|
304 test(r==KErrNone); |
|
305 r=file.Write(BeckPtr); |
|
306 test(r==KErrNone); |
|
307 file.Close(); |
|
308 |
|
309 // Test a long path (>250 chrs) |
|
310 r=TheFs.Rename(_L("\\F32-TST\\little"),_L("\\F32-TST\\PLATTEST\\FileStore\\TestData\\20495_Folder\\PLATTEST\\FileStore\\TestData\\20495_Folder\\PLATTEST\\FileStore\\TestData\\20495_Folder\\PLATTEST\\FileStore\\TestData\\20495_Folder\\PLATTEST\\FileStore\\TestData\\20495_Folder\\PLATTEST\\FileStore\\TestData\\20495_Folder\\PLATTEST\\FileStore\\TestData\\20495_Folder\\PLATTEST\\FileStore\\TestData\\20495_Folder\\PLATTEST\\FileStore\\TestData\\20495_Folder\\PLATTEST\\FileStore\\TestData\\20495_Folder\\PLATTEST\\FileStore\\TestData\\0495_Folder\\PLATTEST\\FileStore\\TestData\\20495_Folder\\PLATTEST\\FileStore\\TestData\\20495_Folder\\PLATTEST\\FileStore\\TestData\\20495_Folder\\PLATTEST\\FileStore\\TestData\\20495_Folder\\PLATTEST\\FileStore\\TestData\\20495_Folder\\PLATTEST\\FileStore\\TestData\\20495_Folder\\middle.gif")); |
|
311 test(r==KErrBadName); |
|
312 |
|
313 r=TheFs.Rename(_L("\\F32-TST\\little"),_L("\\F32-TST\\BIG")); |
|
314 test(r==KErrNone); |
|
315 r=file.Replace(TheFs,_L("\\F32-TST\\temp"),EFileWrite); |
|
316 test(r==KErrNone); |
|
317 r=file.Write(alphaPtr); |
|
318 test(r==KErrNone); |
|
319 file.Close(); |
|
320 |
|
321 r=TheFs.Replace(_L("\\F32-TST\\temp"),_L("\\F32-TST\\BIG")); |
|
322 test(r==KErrNone); |
|
323 |
|
324 fileCount=0; |
|
325 fileCount=CountFiles(_L("\\F32-TST\\"),_L("BIG")); |
|
326 test(fileCount==1); |
|
327 r=TheFs.Delete(_L("\\F32-TST\\BIG")); |
|
328 test(r==KErrNone); |
|
329 fileCount=CountFiles(_L("\\F32-TST\\"),_L("BIG")); |
|
330 test(fileCount==0); |
|
331 test(r==KErrNone); |
|
332 |
|
333 // *********************************** |
|
334 // Test with a non-DOS compatible name |
|
335 test.Next(_L("Rename veryLongFileName to VERYLONGFILENAME")); |
|
336 r=file.Replace(TheFs,_L("\\F32-TST\\veryLongFileName"),EFileWrite); |
|
337 test(r==KErrNone); |
|
338 r=file.Write(BeckPtr); |
|
339 test(r==KErrNone); |
|
340 file.Close(); |
|
341 |
|
342 r=TheFs.Rename(_L("\\F32-TST\\veryLongFileName"),_L("\\F32-TST\\VERYLONGFILENAME")); |
|
343 test(r==KErrNone); |
|
344 r=file.Replace(TheFs,_L("\\F32-TST\\temp"),EFileWrite); |
|
345 test(r==KErrNone); |
|
346 r=file.Write(alphaPtr); |
|
347 test(r==KErrNone); |
|
348 file.Close(); |
|
349 |
|
350 r=TheFs.Replace(_L("\\F32-TST\\temp"),_L("\\F32-TST\\VERYLONGFILENAME")); |
|
351 test(r==KErrNone); |
|
352 |
|
353 fileCount=0; |
|
354 fileCount=CountFiles(_L("\\F32-TST\\"),_L("VERYLONGFILENAME")); |
|
355 test(fileCount==1); |
|
356 r=TheFs.Delete(_L("\\F32-TST\\VERYLONGFILENAME")); |
|
357 test(r==KErrNone); |
|
358 fileCount=CountFiles(_L("\\F32-TST\\"),_L("VERYLONGFILENAME")); |
|
359 test(fileCount==0); |
|
360 test(r==KErrNone); |
|
361 |
|
362 // ******************************* |
|
363 // Test with a DOS compatible name |
|
364 test.Next(_L("Rename FILE to FILE and replace temp with FILE")); |
|
365 r=file.Replace(TheFs,_L("\\F32-TST\\FILE"),EFileWrite); |
|
366 test(r==KErrNone); |
|
367 r=file.Write(BeckPtr); |
|
368 test(r==KErrNone); |
|
369 file.Close(); |
|
370 |
|
371 r=TheFs.Rename(_L("\\F32-TST\\FILE"),_L("\\F32-TST\\FILE")); |
|
372 test(r==KErrNone); |
|
373 r=file.Replace(TheFs,_L("\\F32-TST\\temp"),EFileWrite); |
|
374 test(r==KErrNone); |
|
375 r=file.Write(alphaPtr); |
|
376 test(r==KErrNone); |
|
377 file.Close(); |
|
378 |
|
379 r=TheFs.Replace(_L("\\F32-TST\\temp"),_L("\\F32-TST\\FILE")); |
|
380 test(r==KErrNone); |
|
381 |
|
382 fileCount=0; |
|
383 fileCount=CountFiles(_L("\\F32-TST\\"),_L("FILE")); |
|
384 test(fileCount==1); |
|
385 r=TheFs.Delete(_L("\\F32-TST\\FILE")); |
|
386 test(r==KErrNone); |
|
387 fileCount=CountFiles(_L("\\F32-TST\\"),_L("FILE")); |
|
388 test(fileCount==0); |
|
389 test(r==KErrNone); |
|
390 |
|
391 // ************************************************** |
|
392 // Test with a DOS compatible name which is kept open |
|
393 test.Next(_L("Rename test1 to TEST1 and replace temp1 with TEST1 while it's open")); |
|
394 r=file.Replace(TheFs,_L("\\F32-TST\\test1"),EFileWrite); |
|
395 test(r==KErrNone); |
|
396 r=file.Write(BeckPtr); |
|
397 test(r==KErrNone); |
|
398 file.Close(); |
|
399 |
|
400 r=TheFs.Rename(_L("\\F32-TST\\test1"),_L("\\F32-TST\\TEST1")); |
|
401 test(r==KErrNone); |
|
402 |
|
403 // Try with the file open |
|
404 RFile openFile; |
|
405 r=openFile.Open(TheFs,_L("\\F32-TST\\TEST1"),EFileRead|EFileWrite); |
|
406 test(r==KErrNone); |
|
407 |
|
408 r=file.Replace(TheFs,_L("\\F32-TST\\temp"),EFileWrite); |
|
409 test(r==KErrNone); |
|
410 r=file.Write(alphaPtr); |
|
411 test(r==KErrNone); |
|
412 file.Close(); |
|
413 |
|
414 r=TheFs.Replace(_L("\\F32-TST\\temp"),_L("\\F32-TST\\TEST1")); |
|
415 test(r==KErrInUse); // Fails as it should! But not intuitive bearing in mind the other bug... |
|
416 |
|
417 openFile.Close(); |
|
418 |
|
419 r=TheFs.Replace(_L("\\F32-TST\\temp"),_L("\\F32-TST\\TEST1")); |
|
420 test(r==KErrNone); |
|
421 |
|
422 fileCount=0; |
|
423 fileCount=CountFiles(_L("\\F32-TST\\"),_L("TEST1")); |
|
424 test(fileCount==1); |
|
425 r=TheFs.Delete(_L("\\F32-TST\\TEST1")); |
|
426 test(r==KErrNone); |
|
427 fileCount=CountFiles(_L("\\F32-TST\\"),_L("TEST1")); |
|
428 test(fileCount==0); |
|
429 test(r==KErrNone); |
|
430 |
|
431 // Clean up |
|
432 RFormat format; |
|
433 TInt count; |
|
434 TFileName sessionPath; |
|
435 r=TheFs.SessionPath(sessionPath); |
|
436 r=format.Open(TheFs,sessionPath,EQuickFormat,count); |
|
437 if (r == KErrAccessDenied) |
|
438 return; |
|
439 test(r==KErrNone); |
|
440 while(count && r==KErrNone) |
|
441 r=format.Next(count); |
|
442 format.Close(); |
|
443 } |
|
444 |
|
445 |
|
446 GLDEF_C void CallTestsL(void) |
|
447 // |
|
448 // Do all tests |
|
449 // |
|
450 { |
|
451 |
|
452 test.Title(); |
|
453 test.Start(_L("Testing rename")); |
|
454 |
|
455 TheFs.MkDir(_L("\\F32-TST\\SYSTEM\\")); |
|
456 CreateTestFiles(); |
|
457 TestRFsRename(); |
|
458 TestRFileRename(); |
|
459 TestEikonRename(); |
|
460 TestReplaceAndRename(); |
|
461 |
|
462 test.End(); |
|
463 test.Close(); |
|
464 } |
|
465 |