os/kernelhwsrv/kerneltest/f32test/server/t_rename.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
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".
     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 #include "f32_test_utils.h"
    22 
    23 using namespace F32_Test_Utils;
    24 TInt gDriveNum = -1;
    25 
    26 RTest test(_L("T_RENAME"));
    27 
    28 TBuf8<26> alphaBuffer=_L8("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    29 TPtr8 alphaPtr((TText8*)alphaBuffer.Ptr(),alphaBuffer.Size(),alphaBuffer.Size());
    30 
    31 TBuf8<17> BeckBuffer=_L8("A Devil's Haircut");
    32 TPtr8 BeckPtr((TText8*)BeckBuffer.Ptr(),BeckBuffer.Size(),BeckBuffer.Size());
    33 
    34 
    35 static void CreateTestFiles()
    36 	{
    37 	test.Next(_L("Create test files"));
    38 	TInt r=TheFs.MkDir(_L("\\F32-TST\\"));
    39 	test(r==KErrNone || r==KErrAlreadyExists);
    40 
    41 	RFile file;
    42 
    43 //	Create \\SessionPath\\testfile
    44 	r=file.Replace(TheFs,_L("\\F32-TST\\testfile"),EFileRead|EFileWrite);
    45 	test(r==KErrNone);
    46 	r=file.Write(BeckPtr);
    47 	test(r==KErrNone);
    48 	file.Close();
    49 	
    50 //	Create \\SessionPath\\rfsfile
    51 	r=file.Replace(TheFs,_L("\\F32-TST\\rfsfile"),EFileRead|EFileWrite);
    52 	test(r==KErrNone);
    53 	r=file.Write(BeckPtr);
    54 	test(r==KErrNone);
    55 	file.Close();
    56 
    57 //	Create \\SessionPath\\eikfile
    58 	r=file.Replace(TheFs,_L("\\F32-TST\\eikfile"),EFileRead|EFileWrite);
    59 	test(r==KErrNone);
    60 	r=file.Write(BeckPtr);
    61 	test(r==KErrNone);
    62 	file.Close();
    63 
    64 	}
    65 
    66 static TInt CountFiles(TPtrC aDirectory, TPtrC aFileName)
    67 //
    68 //	Return the number of files of aFileName found in aDirectory
    69 //	
    70 	{
    71 	
    72     RDir dir;
    73 	TFileName sessionPath;
    74 	TInt r=TheFs.SessionPath(sessionPath);
    75 	test(r==KErrNone);
    76 	TFileName path=_L("?:");
    77 	path[0]=sessionPath[0];
    78 	path+=aDirectory;
    79 	if (path[path.Length()-1]==KPathDelimiter)
    80 		path.Append('*');
    81 	else
    82 		path.Append(_L("\\*"));
    83 		
    84 	r=dir.Open(TheFs,path,KEntryAttMaskSupported);
    85 	test(r==KErrNone);
    86 
    87 	CDir* anEntryList;
    88 	r=TheFs.GetDir(path,KEntryAttMaskSupported,ESortByName,anEntryList);
    89 	test(r==KErrNone);
    90 
    91 //	Sets the new length of path to the position of the last path delimiter +1
    92 	path.SetLength(path.LocateReverse(KPathDelimiter)+1);
    93 	TInt fileCount=0; 
    94 	TEntry entry;
    95 		
    96 	TInt count=anEntryList->Count();
    97 	for (TInt j=0;j<count;j++)
    98 		{
    99 		entry=anEntryList->operator[](j);
   100 		if ((entry.iName)==(aFileName))
   101 			fileCount++;
   102 		}
   103 	
   104 	dir.Close();
   105 	delete anEntryList;
   106 	return(fileCount);
   107 	}
   108 
   109 static void TestRFileRename()
   110 //
   111 //	Test RFile::Rename() function
   112 //
   113 	{
   114 	test.Next(_L("Rename file with DOS compatible name using RFile function"));
   115 	TInt r;
   116 	RFile file;
   117 	
   118 	r=file.Open(TheFs,_L("\\F32-TST\\testfile"),EFileRead|EFileWrite);
   119 	test(r==KErrNone);
   120 
   121 	r=file.Rename(_L("\\F32-TST\\TESTFILE"));
   122 	test(r==KErrNone);
   123 
   124 	file.Close();
   125 
   126 	test.Next(_L("Write in some data"));
   127 	r=file.Open(TheFs,_L("\\F32-TST\\TESTFILE"),EFileRead|EFileWrite);
   128 	test(r==KErrNone);
   129 
   130 	r=file.Write(alphaPtr);
   131 	test(r==KErrNone);
   132 
   133 	file.Close();
   134 	}
   135 
   136 
   137 static void TestRFsRename()
   138 //
   139 //	Test RFs::Rename() function
   140 //
   141 	{
   142 	test.Next(_L("Rename file with DOS compatible name using RFs function"));
   143 	TInt r;
   144 		
   145 	r=TheFs.Rename(_L("\\F32-TST\\rfsfile"),_L("\\F32-TST\\RFSFILE"));
   146 	test(r==KErrNone);
   147 
   148 	RFile file;
   149 	test.Next(_L("Write in some data"));
   150 	r=file.Open(TheFs,_L("\\F32-TST\\RFSFILE"),EFileRead|EFileWrite);
   151 	test(r==KErrNone);
   152 
   153 	r=file.Write(alphaPtr);
   154 	test(r==KErrNone);
   155 
   156 	file.Close();
   157 	}
   158 
   159 static void TestEikonRename()
   160 //
   161 //	Test EIKON style rename by creating a new file, and copying old data into new file
   162 //
   163 	{
   164 	test.Next(_L("Rename file with DOS compatible name simulating EIKON"));
   165 	TInt r;
   166 	RFile file;
   167 	
   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));
   171 	file.Close();
   172 
   173 	test.Next(_L("Copy data from original file into new file"));
   174 	r=TheFs.Replace(_L("\\F32-TST\\eikfile"),_L("\\F32-TST\\EIKFILE"));
   175 	test(r==KErrNone);
   176 
   177 	test.Next(_L("Open the new file and write into it"));
   178 	r=file.Open(TheFs,_L("\\F32-TST\\EIKFILE"),EFileRead|EFileWrite);
   179 	test(r==KErrNone);
   180 
   181 	r=file.Write(alphaPtr);
   182 	test(r==KErrNone);
   183 
   184 	file.Close();
   185 	}
   186 
   187 
   188 static void TestReplaceAndRename()
   189 //
   190 //	Tests the bug which allows 2 files of the same name to be created has been fixed
   191 //
   192 	{
   193 	TInt r;
   194 	RFile file;
   195 
   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);
   200 	test(r==KErrNone);
   201 	r=file.Write(BeckPtr);
   202 	test(r==KErrNone);
   203 	file.Close();
   204 
   205 	r=TheFs.Rename(_L("\\F32-TST\\test"),_L("\\F32-TST\\TEST"));
   206 	test(r==KErrNone);
   207 	r=file.Replace(TheFs,_L("\\F32-TST\\temp"),EFileWrite);
   208 	test(r==KErrNone);
   209 	r=file.Write(alphaPtr);
   210 	test(r==KErrNone);
   211 	file.Close();
   212 
   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"));
   219 	test(r==KErrNone);
   220 
   221 //	Check that there's only one file named TEST
   222 	TInt fileCount=0;
   223 	fileCount=CountFiles(_L("\\F32-TST\\"),_L("TEST"));
   224 	test(fileCount==1);
   225 	r=TheFs.Delete(_L("\\F32-TST\\TEST"));
   226 	test(r==KErrNone);
   227 	fileCount=CountFiles(_L("\\F32-TST\\"),_L("TEST"));
   228 	test(fileCount==0);
   229 	test(r==KErrNone);
   230 	
   231 
   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);
   236 	test(r==KErrNone);
   237 	r=file.Write(BeckPtr);
   238 	test(r==KErrNone);
   239 	file.Close();
   240 
   241 	r=TheFs.Rename(_L("\\F32-TST\\test"),_L("\\F32-TST\\TEST"));
   242 	test(r==KErrNone);
   243 	r=file.Replace(TheFs,_L("\\F32-TST\\SYSTEM\\temp"),EFileWrite);
   244 	test(r==KErrNone);
   245 	r=file.Write(alphaPtr);
   246 	test(r==KErrNone);
   247 	file.Close();
   248 	
   249 //	The bug created a second file of the same name	
   250 	r=TheFs.Replace(_L("\\F32-TST\\SYSTEM\\temp"),_L("\\F32-TST\\TEST"));
   251 	test(r==KErrNone);
   252 
   253 	fileCount=0;
   254 	fileCount=CountFiles(_L("\\F32-TST\\"),_L("TEST"));
   255 	test(fileCount==1);
   256 	r=TheFs.Delete(_L("\\F32-TST\\TEST"));
   257 	test(r==KErrNone);
   258 	fileCount=CountFiles(_L("\\F32-TST\\"),_L("TEST"));
   259 	test(fileCount==0);
   260 //	Test that system directory is now empty	
   261 	fileCount=0;
   262 	fileCount=CountFiles(_L("\\F32-TST\\SYSTEM\\"),_L("temp"));
   263 	test(fileCount==0);
   264 	test(r==KErrNone);
   265 
   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);
   270 	test(r==KErrNone);
   271 	r=file.Write(BeckPtr);
   272 	test(r==KErrNone);
   273 	file.Close();
   274 
   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);
   278 
   279 	r=TheFs.Rename(_L("\\F32-TST\\little"),_L("\\F32-TST\\BIG"));
   280 	test(r==KErrNone);
   281 	r=file.Replace(TheFs,_L("\\F32-TST\\temp"),EFileWrite);
   282 	test(r==KErrNone);
   283 	r=file.Write(alphaPtr);
   284 	test(r==KErrNone);
   285 	file.Close();
   286 	
   287 	r=TheFs.Replace(_L("\\F32-TST\\temp"),_L("\\F32-TST\\BIG"));
   288 	test(r==KErrNone);
   289 
   290 	fileCount=0;
   291 	fileCount=CountFiles(_L("\\F32-TST\\"),_L("BIG"));
   292 	test(fileCount==1);
   293 	r=TheFs.Delete(_L("\\F32-TST\\BIG"));
   294 	test(r==KErrNone);
   295 	fileCount=CountFiles(_L("\\F32-TST\\"),_L("BIG"));
   296 	test(fileCount==0);
   297 	test(r==KErrNone);
   298 
   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);
   303 	test(r==KErrNone);
   304 	r=file.Write(BeckPtr);
   305 	test(r==KErrNone);
   306 	file.Close();
   307 
   308 	r=TheFs.Rename(_L("\\F32-TST\\veryLongFileName"),_L("\\F32-TST\\VERYLONGFILENAME"));
   309 	test(r==KErrNone);
   310 	r=file.Replace(TheFs,_L("\\F32-TST\\temp"),EFileWrite);
   311 	test(r==KErrNone);
   312 	r=file.Write(alphaPtr);
   313 	test(r==KErrNone);
   314 	file.Close();
   315 	
   316 	r=TheFs.Replace(_L("\\F32-TST\\temp"),_L("\\F32-TST\\VERYLONGFILENAME"));
   317 	test(r==KErrNone);
   318 
   319 	fileCount=0;
   320 	fileCount=CountFiles(_L("\\F32-TST\\"),_L("VERYLONGFILENAME"));
   321 	test(fileCount==1);
   322 	r=TheFs.Delete(_L("\\F32-TST\\VERYLONGFILENAME"));
   323 	test(r==KErrNone);
   324 	fileCount=CountFiles(_L("\\F32-TST\\"),_L("VERYLONGFILENAME"));
   325 	test(fileCount==0);
   326 	test(r==KErrNone);
   327 
   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);
   332 	test(r==KErrNone);
   333 	r=file.Write(BeckPtr);
   334 	test(r==KErrNone);
   335 	file.Close();
   336 
   337 	r=TheFs.Rename(_L("\\F32-TST\\FILE"),_L("\\F32-TST\\FILE"));
   338 	test(r==KErrNone);
   339 	r=file.Replace(TheFs,_L("\\F32-TST\\temp"),EFileWrite);
   340 	test(r==KErrNone);
   341 	r=file.Write(alphaPtr);
   342 	test(r==KErrNone);
   343 	file.Close();
   344 	
   345 	r=TheFs.Replace(_L("\\F32-TST\\temp"),_L("\\F32-TST\\FILE"));
   346 	test(r==KErrNone);
   347 
   348 	fileCount=0;
   349 	fileCount=CountFiles(_L("\\F32-TST\\"),_L("FILE"));
   350 	test(fileCount==1);
   351 	r=TheFs.Delete(_L("\\F32-TST\\FILE"));
   352 	test(r==KErrNone);
   353 	fileCount=CountFiles(_L("\\F32-TST\\"),_L("FILE"));
   354 	test(fileCount==0);
   355 	test(r==KErrNone);
   356 
   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);
   361 	test(r==KErrNone);
   362 	r=file.Write(BeckPtr);
   363 	test(r==KErrNone);
   364 	file.Close();
   365 
   366 	r=TheFs.Rename(_L("\\F32-TST\\test1"),_L("\\F32-TST\\TEST1"));
   367 	test(r==KErrNone);
   368 
   369 //	Try with the file open
   370 	RFile openFile;
   371 	r=openFile.Open(TheFs,_L("\\F32-TST\\TEST1"),EFileRead|EFileWrite);
   372 	test(r==KErrNone);
   373 
   374 	r=file.Replace(TheFs,_L("\\F32-TST\\temp"),EFileWrite);
   375 	test(r==KErrNone);
   376 	r=file.Write(alphaPtr);
   377 	test(r==KErrNone);
   378 	file.Close();
   379 	
   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...
   382 
   383 	openFile.Close();
   384 	
   385 	r=TheFs.Replace(_L("\\F32-TST\\temp"),_L("\\F32-TST\\TEST1"));
   386 	test(r==KErrNone);
   387 
   388 	fileCount=0;
   389 	fileCount=CountFiles(_L("\\F32-TST\\"),_L("TEST1"));
   390 	test(fileCount==1);
   391 	r=TheFs.Delete(_L("\\F32-TST\\TEST1"));
   392 	test(r==KErrNone);
   393 	fileCount=CountFiles(_L("\\F32-TST\\"),_L("TEST1"));
   394 	test(fileCount==0);
   395 	test(r==KErrNone);
   396 
   397 	}
   398 
   399 
   400 void CallTestsL(void)
   401 	{
   402 	
   403 	test.Title();
   404 	test.Start(_L("Testing rename"));
   405 
   406     //-- set up console output
   407     F32_Test_Utils::SetConsole(test.Console());
   408 
   409     TInt nRes=TheFs.CharToDrive(gDriveToTest, gDriveNum);
   410     test(nRes==KErrNone);
   411     
   412     PrintDrvInfo(TheFs, gDriveNum);
   413 
   414     if(!Is_Win32(TheFs, gDriveNum))
   415         {
   416         nRes = FormatDrive(TheFs, gDriveNum, ETrue);
   417         test(nRes==KErrNone);
   418         }
   419     
   420 
   421 	MakeDir(_L("\\F32-TST\\SYSTEM\\"));
   422 	CreateTestFiles();
   423 	TestRFsRename();
   424 	TestRFileRename();
   425 	TestEikonRename();
   426 	TestReplaceAndRename();
   427 
   428     if(!Is_Win32(TheFs, gDriveNum))
   429         {
   430         nRes = FormatDrive(TheFs, gDriveNum, ETrue);
   431         test(nRes==KErrNone);
   432         }
   433 
   434 	test.End();
   435 	test.Close();
   436 	}
   437