Update contrib.
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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // f32test\server\t_rename.cpp
21 #include "f32_test_utils.h"
23 using namespace F32_Test_Utils;
26 RTest test(_L("T_RENAME"));
28 TBuf8<26> alphaBuffer=_L8("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
29 TPtr8 alphaPtr((TText8*)alphaBuffer.Ptr(),alphaBuffer.Size(),alphaBuffer.Size());
31 TBuf8<17> BeckBuffer=_L8("A Devil's Haircut");
32 TPtr8 BeckPtr((TText8*)BeckBuffer.Ptr(),BeckBuffer.Size(),BeckBuffer.Size());
35 static void CreateTestFiles()
37 test.Next(_L("Create test files"));
38 TInt r=TheFs.MkDir(_L("\\F32-TST\\"));
39 test(r==KErrNone || r==KErrAlreadyExists);
43 // Create \\SessionPath\\testfile
44 r=file.Replace(TheFs,_L("\\F32-TST\\testfile"),EFileRead|EFileWrite);
46 r=file.Write(BeckPtr);
50 // Create \\SessionPath\\rfsfile
51 r=file.Replace(TheFs,_L("\\F32-TST\\rfsfile"),EFileRead|EFileWrite);
53 r=file.Write(BeckPtr);
57 // Create \\SessionPath\\eikfile
58 r=file.Replace(TheFs,_L("\\F32-TST\\eikfile"),EFileRead|EFileWrite);
60 r=file.Write(BeckPtr);
66 static TInt CountFiles(TPtrC aDirectory, TPtrC aFileName)
68 // Return the number of files of aFileName found in aDirectory
73 TFileName sessionPath;
74 TInt r=TheFs.SessionPath(sessionPath);
76 TFileName path=_L("?:");
77 path[0]=sessionPath[0];
79 if (path[path.Length()-1]==KPathDelimiter)
82 path.Append(_L("\\*"));
84 r=dir.Open(TheFs,path,KEntryAttMaskSupported);
88 r=TheFs.GetDir(path,KEntryAttMaskSupported,ESortByName,anEntryList);
91 // Sets the new length of path to the position of the last path delimiter +1
92 path.SetLength(path.LocateReverse(KPathDelimiter)+1);
96 TInt count=anEntryList->Count();
97 for (TInt j=0;j<count;j++)
99 entry=anEntryList->operator[](j);
100 if ((entry.iName)==(aFileName))
109 static void TestRFileRename()
111 // Test RFile::Rename() function
114 test.Next(_L("Rename file with DOS compatible name using RFile function"));
118 r=file.Open(TheFs,_L("\\F32-TST\\testfile"),EFileRead|EFileWrite);
121 r=file.Rename(_L("\\F32-TST\\TESTFILE"));
126 test.Next(_L("Write in some data"));
127 r=file.Open(TheFs,_L("\\F32-TST\\TESTFILE"),EFileRead|EFileWrite);
130 r=file.Write(alphaPtr);
137 static void TestRFsRename()
139 // Test RFs::Rename() function
142 test.Next(_L("Rename file with DOS compatible name using RFs function"));
145 r=TheFs.Rename(_L("\\F32-TST\\rfsfile"),_L("\\F32-TST\\RFSFILE"));
149 test.Next(_L("Write in some data"));
150 r=file.Open(TheFs,_L("\\F32-TST\\RFSFILE"),EFileRead|EFileWrite);
153 r=file.Write(alphaPtr);
159 static void TestEikonRename()
161 // Test EIKON style rename by creating a new file, and copying old data into new file
164 test.Next(_L("Rename file with DOS compatible name simulating EIKON"));
168 test.Next(_L("Create a new file with DOS compatible equivalent name"));
169 r=file.Create(TheFs,_L("\\F32-TST\\EIKFILE"),EFileRead|EFileWrite);
170 test((r==KErrNone)||(r==KErrAlreadyExists));
173 test.Next(_L("Copy data from original file into new file"));
174 r=TheFs.Replace(_L("\\F32-TST\\eikfile"),_L("\\F32-TST\\EIKFILE"));
177 test.Next(_L("Open the new file and write into it"));
178 r=file.Open(TheFs,_L("\\F32-TST\\EIKFILE"),EFileRead|EFileWrite);
181 r=file.Write(alphaPtr);
188 static void TestReplaceAndRename()
190 // Tests the bug which allows 2 files of the same name to be created has been fixed
196 // *************************************************************************
197 // First test with a non DOS compatible name renamed to a DOS compatible name
198 test.Next(_L("Rename test to TEST and replace temp with TEST"));
199 r=file.Replace(TheFs,_L("\\F32-TST\\test"),EFileWrite);
201 r=file.Write(BeckPtr);
205 r=TheFs.Rename(_L("\\F32-TST\\test"),_L("\\F32-TST\\TEST"));
207 r=file.Replace(TheFs,_L("\\F32-TST\\temp"),EFileWrite);
209 r=file.Write(alphaPtr);
213 // Replace(oldName, newName)
214 // Copy oldName to newName (ie temp to TEST)
215 // If TEST does not exist, it is created and then temp's attributes etc are copied into it
216 // then temp is deleted. If it does exist, it must be closed
217 // The bug created a second file of the same name
218 r=TheFs.Replace(_L("\\F32-TST\\temp"),_L("\\F32-TST\\TEST"));
221 // Check that there's only one file named TEST
223 fileCount=CountFiles(_L("\\F32-TST\\"),_L("TEST"));
225 r=TheFs.Delete(_L("\\F32-TST\\TEST"));
227 fileCount=CountFiles(_L("\\F32-TST\\"),_L("TEST"));
232 //*****************************************************
233 // The same test but with different source directories
234 test.Next(_L("Rename test to and replace \\SYSTEM\\temp with TEST"));
235 r=file.Replace(TheFs,_L("\\F32-TST\\test"),EFileWrite);
237 r=file.Write(BeckPtr);
241 r=TheFs.Rename(_L("\\F32-TST\\test"),_L("\\F32-TST\\TEST"));
243 r=file.Replace(TheFs,_L("\\F32-TST\\SYSTEM\\temp"),EFileWrite);
245 r=file.Write(alphaPtr);
249 // The bug created a second file of the same name
250 r=TheFs.Replace(_L("\\F32-TST\\SYSTEM\\temp"),_L("\\F32-TST\\TEST"));
254 fileCount=CountFiles(_L("\\F32-TST\\"),_L("TEST"));
256 r=TheFs.Delete(_L("\\F32-TST\\TEST"));
258 fileCount=CountFiles(_L("\\F32-TST\\"),_L("TEST"));
260 // Test that system directory is now empty
262 fileCount=CountFiles(_L("\\F32-TST\\SYSTEM\\"),_L("temp"));
266 // *************************************************************************
267 // Test with a DOS compatible name renamed to a different DOS compatible name
268 test.Next(_L("Rename little to BIG and replace temp with BIG"));
269 r=file.Replace(TheFs,_L("\\F32-TST\\little"),EFileWrite);
271 r=file.Write(BeckPtr);
275 // Test a long path (>250 chrs)
276 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"));
277 test(r==KErrBadName);
279 r=TheFs.Rename(_L("\\F32-TST\\little"),_L("\\F32-TST\\BIG"));
281 r=file.Replace(TheFs,_L("\\F32-TST\\temp"),EFileWrite);
283 r=file.Write(alphaPtr);
287 r=TheFs.Replace(_L("\\F32-TST\\temp"),_L("\\F32-TST\\BIG"));
291 fileCount=CountFiles(_L("\\F32-TST\\"),_L("BIG"));
293 r=TheFs.Delete(_L("\\F32-TST\\BIG"));
295 fileCount=CountFiles(_L("\\F32-TST\\"),_L("BIG"));
299 // ***********************************
300 // Test with a non-DOS compatible name
301 test.Next(_L("Rename veryLongFileName to VERYLONGFILENAME"));
302 r=file.Replace(TheFs,_L("\\F32-TST\\veryLongFileName"),EFileWrite);
304 r=file.Write(BeckPtr);
308 r=TheFs.Rename(_L("\\F32-TST\\veryLongFileName"),_L("\\F32-TST\\VERYLONGFILENAME"));
310 r=file.Replace(TheFs,_L("\\F32-TST\\temp"),EFileWrite);
312 r=file.Write(alphaPtr);
316 r=TheFs.Replace(_L("\\F32-TST\\temp"),_L("\\F32-TST\\VERYLONGFILENAME"));
320 fileCount=CountFiles(_L("\\F32-TST\\"),_L("VERYLONGFILENAME"));
322 r=TheFs.Delete(_L("\\F32-TST\\VERYLONGFILENAME"));
324 fileCount=CountFiles(_L("\\F32-TST\\"),_L("VERYLONGFILENAME"));
328 // *******************************
329 // Test with a DOS compatible name
330 test.Next(_L("Rename FILE to FILE and replace temp with FILE"));
331 r=file.Replace(TheFs,_L("\\F32-TST\\FILE"),EFileWrite);
333 r=file.Write(BeckPtr);
337 r=TheFs.Rename(_L("\\F32-TST\\FILE"),_L("\\F32-TST\\FILE"));
339 r=file.Replace(TheFs,_L("\\F32-TST\\temp"),EFileWrite);
341 r=file.Write(alphaPtr);
345 r=TheFs.Replace(_L("\\F32-TST\\temp"),_L("\\F32-TST\\FILE"));
349 fileCount=CountFiles(_L("\\F32-TST\\"),_L("FILE"));
351 r=TheFs.Delete(_L("\\F32-TST\\FILE"));
353 fileCount=CountFiles(_L("\\F32-TST\\"),_L("FILE"));
357 // **************************************************
358 // Test with a DOS compatible name which is kept open
359 test.Next(_L("Rename test1 to TEST1 and replace temp1 with TEST1 while it's open"));
360 r=file.Replace(TheFs,_L("\\F32-TST\\test1"),EFileWrite);
362 r=file.Write(BeckPtr);
366 r=TheFs.Rename(_L("\\F32-TST\\test1"),_L("\\F32-TST\\TEST1"));
369 // Try with the file open
371 r=openFile.Open(TheFs,_L("\\F32-TST\\TEST1"),EFileRead|EFileWrite);
374 r=file.Replace(TheFs,_L("\\F32-TST\\temp"),EFileWrite);
376 r=file.Write(alphaPtr);
380 r=TheFs.Replace(_L("\\F32-TST\\temp"),_L("\\F32-TST\\TEST1"));
381 test(r==KErrInUse); // Fails as it should! But not intuitive bearing in mind the other bug...
385 r=TheFs.Replace(_L("\\F32-TST\\temp"),_L("\\F32-TST\\TEST1"));
389 fileCount=CountFiles(_L("\\F32-TST\\"),_L("TEST1"));
391 r=TheFs.Delete(_L("\\F32-TST\\TEST1"));
393 fileCount=CountFiles(_L("\\F32-TST\\"),_L("TEST1"));
400 void CallTestsL(void)
404 test.Start(_L("Testing rename"));
406 //-- set up console output
407 F32_Test_Utils::SetConsole(test.Console());
409 TInt nRes=TheFs.CharToDrive(gDriveToTest, gDriveNum);
410 test(nRes==KErrNone);
412 PrintDrvInfo(TheFs, gDriveNum);
414 if(!Is_Win32(TheFs, gDriveNum))
416 nRes = FormatDrive(TheFs, gDriveNum, ETrue);
417 test(nRes==KErrNone);
421 MakeDir(_L("\\F32-TST\\SYSTEM\\"));
426 TestReplaceAndRename();
428 if(!Is_Win32(TheFs, gDriveNum))
430 nRes = FormatDrive(TheFs, gDriveNum, ETrue);
431 test(nRes==KErrNone);