baseintegtests/baseintegrationtest/testsuites/fat32/src/basetestfat32writefile.cpp
changeset 0 a41df078684a
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 2006-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 // Performs actions related to writing a file
       
    15 // 
       
    16 //
       
    17 
       
    18 #include "basetestfat32writefile.h"
       
    19 
       
    20 static RRawDisk TheDisk;
       
    21 static RFile TheFile;
       
    22 
       
    23 /**
       
    24 Class Constructor
       
    25 */		    
       
    26 CBaseTestFat32WriteFile::CBaseTestFat32WriteFile() 
       
    27 	{
       
    28 	SetTestStepName(KTestStepWriteFile);
       
    29 	}
       
    30 	
       
    31 /**
       
    32 Class Destructor
       
    33 */
       
    34 CBaseTestFat32WriteFile::~CBaseTestFat32WriteFile() 
       
    35 	{
       
    36 	}
       
    37 
       
    38 
       
    39 /** 
       
    40 The function performs the following actions
       
    41 1. If there is a file specified in the ini file then 
       
    42 	-> create a file with that filename 
       
    43 	-> Else create a file name ReadTestFile.txt
       
    44 2. Get the file action required from the ini file and carry out that action
       
    45 
       
    46 @return EPass if test passes and EFail if test fails
       
    47 */ 
       
    48 TVerdict CBaseTestFat32WriteFile::doTestStepL()
       
    49 	{
       
    50 	TInt r = 0;
       
    51 	TInt errcode;
       
    52 	SetTestStepResult(EFail);
       
    53 	_LIT(KAction,"Action");
       
    54 	TBuf<10> actionAfterCreate;
       
    55 	TPtrC16 action = actionAfterCreate;
       
    56 	_LIT(KName,"SetFileName");
       
    57 	_LIT(KPath,"%c:\\");
       
    58 	TBuf<10> setFileName;
       
    59 	TPtrC16 name = setFileName;
       
    60 	TBuf<255> fileName;
       
    61 	_LIT(KCreatedFileName,"%c:\\ReadTestFile.txt");
       
    62 
       
    63 	TBool alright = GetStringFromConfig(ConfigSection(), KName, name);
       
    64 	if (alright)
       
    65 		{
       
    66 		fileName.Format(KPath, (TUint)iDriveToTest);
       
    67 		fileName.Append(name);
       
    68 		}
       
    69 	else
       
    70 		{
       
    71 		fileName.Format(KCreatedFileName, (TUint)iDriveToTest);
       
    72 		}
       
    73 	TInt res = TheFile.Replace(iTheFs,fileName,EFileWrite);
       
    74 	TheFile.Close();
       
    75 	TBool alright2 = GetStringFromConfig(ConfigSection(), KAction, action);
       
    76 	if (alright2)
       
    77 		{
       
    78 		errcode = SetAttribs(fileName);
       
    79 		if (action == _L("Write"))
       
    80 			{
       
    81 			errcode = WriteFile(fileName);
       
    82 			}
       
    83 		if (action == _L("DirList"))
       
    84 			{
       
    85 			errcode = DirList(fileName);
       
    86 			}
       
    87 		if (action == _L("CheckAtt"))
       
    88 			{
       
    89 			errcode = SetAttribs(fileName);
       
    90 			}
       
    91 		if (action == _L("CheckCode"))
       
    92 			{
       
    93 			errcode = CheckErrCode(res);
       
    94 			}
       
    95 		if (action == _L("MakeDirectory"))
       
    96 			{
       
    97 			errcode = MakeDirectory(_L("TestDirectory\\"));
       
    98 			}
       
    99 		if (action == _L("MakeTwoDirectory"))
       
   100 			{
       
   101 			errcode = MakeDirectory(_L("TestDirectory\\"));
       
   102 			if (errcode == KErrNone)
       
   103 				{
       
   104 				errcode = MakeDirectory(_L("TestDirectory\\TestDirectory2\\"));
       
   105 				}
       
   106 			}
       
   107 		if (action == _L("SetLabel"))
       
   108 			{
       
   109 			errcode = SetLabel(_L("TestLabel"));
       
   110 			}
       
   111 		if (action == _L("SetTwoLabels"))
       
   112 			{
       
   113 			errcode = SetLabel(_L("TestLabel"));
       
   114 			if (errcode == KErrNone)
       
   115 				{
       
   116 				errcode = SetLabel(_L("TestSecondLabel"));
       
   117 				}
       
   118 			}
       
   119 		if (action == _L("SetFileSize"))
       
   120 			{
       
   121 			errcode = SetFileSize(fileName);
       
   122 			}
       
   123 		if (action == _L("DeleteFile"))
       
   124 			{
       
   125 			errcode = DeleteFile(fileName);
       
   126 			}
       
   127 		r = CheckErrCode(errcode);
       
   128 		if(r != KErrNone)
       
   129 			{
       
   130 			_LIT(KErrorWriteFile, "Error writing the file, Error Code = %d");
       
   131 			INFO_PRINTF2(KErrorWriteFile, r);
       
   132 			SetTestStepResult(EFail);
       
   133 			}
       
   134 		else
       
   135 			{
       
   136 			SetTestStepResult(EPass);
       
   137 			_LIT(KReadPass, "Read Passed");
       
   138 			INFO_PRINTF1(KReadPass);
       
   139 			}
       
   140 		}
       
   141 	else
       
   142 		{
       
   143 		r = CheckErrCode(res);
       
   144 		if(r != KErrNone)
       
   145 			{
       
   146 			_LIT(KErrorWriteFile, "Error writing the file, Error Code = %d");
       
   147 			INFO_PRINTF2(KErrorWriteFile, r);
       
   148 			SetTestStepResult(EFail);
       
   149 			}
       
   150 		else
       
   151 			{
       
   152 			_LIT(KReadPass, "Read Passed");
       
   153 			INFO_PRINTF1(KReadPass);
       
   154 			SetTestStepResult(EPass);
       
   155 			}					
       
   156 		}
       
   157 	return TestStepResult(); 
       
   158 	}
       
   159 
       
   160 /** 
       
   161 Open the file
       
   162 
       
   163 @param aFile The name of the file to write 
       
   164 
       
   165 @return KErrNone if successful
       
   166 */
       
   167 TInt CBaseTestFat32WriteFile::WriteFile(const TDesC16& aFile)
       
   168 	{
       
   169 	TBuf8<255> temp;
       
   170 	temp.Copy(aFile);
       
   171 	TInt r = 0; 
       
   172 	r = TheFile.Open(iTheFs,aFile,EFileWrite);
       
   173 	if(r != KErrNone)
       
   174 		{
       
   175 		_LIT(KErrorOpenFile, "Unable to open the file, Error Code = %d");
       
   176 		INFO_PRINTF2(KErrorOpenFile, r);
       
   177 		return r; 
       
   178 		}
       
   179 	TheFile.Close();
       
   180 	return r; 
       
   181 	}
       
   182 
       
   183 /** 
       
   184 Get the attribute to set from the ini file and set the attributes of the file
       
   185 
       
   186 @param aFile The name of the file whos attributes are to be set 
       
   187 
       
   188 @return KErrNone if successful
       
   189 */
       
   190 TInt CBaseTestFat32WriteFile::SetAttribs(const TDesC16& aFile)
       
   191 	{		
       
   192 	TBuf8<255> temp;
       
   193 	temp.Copy(aFile);
       
   194 	TInt r = 0; 
       
   195 	TUint setMask = KEntryAttNormal;
       
   196 	_LIT(KAtt,"Attributes");
       
   197 	TBuf<10> fileAttributes;
       
   198 	TPtrC16 attributes = fileAttributes;
       
   199 
       
   200 	TBool alright3 = GetStringFromConfig(ConfigSection(), KAtt, attributes);
       
   201 	if (alright3)
       
   202 		{
       
   203 		if (attributes == _L("Normal"))
       
   204 			{
       
   205 			setMask = KEntryAttNormal;
       
   206 			}	
       
   207 		else if (attributes == _L("ReadOnly"))
       
   208 			{
       
   209 			setMask = KEntryAttReadOnly;
       
   210 			}
       
   211 		else if (attributes == _L("Hidden"))
       
   212 			{
       
   213 			setMask = KEntryAttHidden;
       
   214 			}
       
   215 		else if (attributes == _L("System"))
       
   216 			{
       
   217 			
       
   218 			setMask = KEntryAttSystem;
       
   219 			}
       
   220 		else if (attributes == _L("Dir"))
       
   221 			{
       
   222 			setMask = KEntryAttDir;
       
   223 			}
       
   224 			
       
   225 		r = TheFile.Open(iTheFs,aFile,EFileWrite);
       
   226 		if (r != KErrNone)
       
   227 			{
       
   228 			_LIT(KOpenFail, "Cannot open the file, error = %d");
       
   229 			INFO_PRINTF2(KOpenFail, r);
       
   230 			return r;
       
   231 			}
       
   232 		r = TheFile.SetAtt(setMask, 0);
       
   233 		if (r != KErrNone)
       
   234 			{
       
   235 			_LIT(KSetAttFail, "Cannot set the attributes, error = %d");
       
   236 			INFO_PRINTF2(KSetAttFail, r);
       
   237 			return r;
       
   238 			}
       
   239 			
       
   240 		TheFile.Close();
       
   241 		
       
   242 		r = CheckAtt(aFile, setMask);	
       
   243 		if (r != KErrNone)
       
   244 			{
       
   245 			_LIT(KAttFail, "Cannot check the attributes, error = %d");
       
   246 			INFO_PRINTF2(KAttFail, r);
       
   247 			return r;
       
   248 			}
       
   249 		}
       
   250 	else
       
   251 		{
       
   252 		_LIT(KNoAttribs, "No attributes specified in the ini file");
       
   253 		INFO_PRINTF1(KNoAttribs);
       
   254 		return KErrNone;	
       
   255 		}
       
   256 	return r; 
       
   257 	}
       
   258 
       
   259 /** 
       
   260 Check the attributes of the file and compare them to the attributes expected
       
   261 
       
   262 @param aFile The name of the file whos attributes are to be checked 
       
   263 
       
   264 @return KErrNone if successful
       
   265 */
       
   266 TInt CBaseTestFat32WriteFile::CheckAtt(const TDesC16& aFile, TUint setMask)
       
   267 	{
       
   268 	TInt r = 0;
       
   269 	TUint fileAttributes;
       
   270 	r = iTheFs.Att(aFile, fileAttributes);
       
   271 	if (r != KErrNone)
       
   272 		{
       
   273 		_LIT(KGetAttFail, "Failed to get the file attributes");
       
   274 		INFO_PRINTF1(KGetAttFail);
       
   275 		return KErrGeneral;
       
   276 		}
       
   277 	else
       
   278 		{
       
   279 		if (fileAttributes&setMask)
       
   280 			{
       
   281 			_LIT(KAttChanged, "Attributes changed correctly");
       
   282 			INFO_PRINTF1(KAttChanged);
       
   283 			return r;
       
   284 			}
       
   285 		else
       
   286 			{
       
   287 			_LIT(KAttNotChanged, "Attributes have not been changed");
       
   288 			INFO_PRINTF1(KAttNotChanged);
       
   289 			return KErrGeneral;
       
   290 			}
       
   291 		}
       
   292 	}
       
   293 
       
   294 /** 
       
   295 Searching the directory to see if it contains the file who's attributes 
       
   296 were set as hidden. 
       
   297 
       
   298 @param aFile The name of the file to search for 
       
   299 
       
   300 @return KErrNone if successful
       
   301 */
       
   302 TInt CBaseTestFat32WriteFile::DirList(const TDesC16& aFile)
       
   303 	{
       
   304 
       
   305 	TInt i; 	
       
   306 	CDir* dirPtr;	
       
   307 	TBuf<10> dir1;
       
   308 	dir1 = iSessionPath;
       
   309 	dir1.Append(_L("*.*"));
       
   310 	TBuf<20> buf;
       
   311 	buf.Append(aFile);
       
   312 	buf.Delete(0,3);
       
   313 	TInt match = 0;
       
   314 	TInt r = iTheFs.GetDir(dir1,KEntryAttMatchExclude|(KEntryAttHidden|0x24),ESortNone,dirPtr);
       
   315 	TInt num = dirPtr->Count();
       
   316 	TEntry entry;
       
   317 	for (i=0; i<num; i++)
       
   318 		{
       
   319 		entry=(*dirPtr)[i];
       
   320 		if (entry.iName == _L("ReadTestFile.txt"))
       
   321 			{
       
   322 			match = match + 1;
       
   323 			}
       
   324 		}
       
   325 	if (match > 0)
       
   326 		{	
       
   327 		_LIT(KFileFound, "File found");
       
   328 		INFO_PRINTF1(KFileFound);
       
   329 		return KErrNone;
       
   330 		}
       
   331 	else
       
   332 		{
       
   333 		_LIT(KFileNotFound, "File not found");
       
   334 		INFO_PRINTF1(KFileNotFound);
       
   335 		return KErrNotFound;
       
   336 		}
       
   337 	}
       
   338 
       
   339 /** 
       
   340 Compare the error code that is expected to the one returned from the action
       
   341 
       
   342 @param aReturnCode The return value that is to be compared 
       
   343 
       
   344 @return KErrNone if successful
       
   345 */
       
   346 TInt CBaseTestFat32WriteFile::CheckErrCode(TInt aReturnCode)
       
   347 	{
       
   348 	_LIT(KCheckCode,"CheckCode");
       
   349 	TInt checkcode;
       
   350 	TBool alright4 = GetIntFromConfig(ConfigSection(), KCheckCode, checkcode);
       
   351 	if (alright4)
       
   352 		{
       
   353 		if (aReturnCode == checkcode)
       
   354 			{
       
   355 			_LIT(KCorrectCode, "Correct error code = %d");
       
   356 			INFO_PRINTF2(KCorrectCode, aReturnCode);
       
   357 			return KErrNone;
       
   358 			}
       
   359 		else
       
   360 			{	
       
   361 			_LIT(KBadCode, "Incorrect error code = %d");
       
   362 			INFO_PRINTF2(KBadCode, aReturnCode);
       
   363 			return KErrGeneral;
       
   364 			}
       
   365 		}
       
   366 	else 
       
   367 		{
       
   368 		_LIT(KNoIniCode, "CheckCode not specified in ini file");
       
   369 		INFO_PRINTF1(KNoIniCode);
       
   370 		return KErrGeneral;
       
   371 		}		
       
   372 	}
       
   373 
       
   374 /** 
       
   375 Creating a directory
       
   376 
       
   377 @param aDir The name of the directory to create 
       
   378 
       
   379 @return KErrNone if successful
       
   380 */	
       
   381 TInt CBaseTestFat32WriteFile::MakeDirectory(const TDesC16& aDir)
       
   382 	{
       
   383 	TInt r = 0;
       
   384 	TBuf<255> dirName;
       
   385 	dirName.Append(iSessionPath);
       
   386 	dirName.Append(aDir);
       
   387 	TUint setMask = KEntryAttDir;
       
   388 	r = iTheFs.MkDir(dirName);
       
   389 	if (r != KErrNone)
       
   390 		{
       
   391 		_LIT(KMkDirFail, "Failed make the directory");
       
   392 		INFO_PRINTF1(KMkDirFail);
       
   393 		return r;
       
   394 		}	
       
   395 	r = CheckAtt(dirName, setMask);	
       
   396 	if (r != KErrNone)
       
   397 		{
       
   398 		_LIT(KChkAttFail, "Failed to check the attributes");
       
   399 		INFO_PRINTF1(KChkAttFail);
       
   400 		return r;
       
   401 		}
       
   402 	return r; 
       
   403 	}
       
   404 
       
   405 /** 
       
   406 Setting a volume label
       
   407 
       
   408 @param aLabel The name of the lable to set
       
   409 
       
   410 @return KErrNone if successful
       
   411 */		
       
   412 TInt CBaseTestFat32WriteFile::SetLabel(const TDesC16& aLabel)
       
   413 	{
       
   414 	TInt r = 0;
       
   415 	r = iTheFs.SetVolumeLabel(aLabel, CurrentDrive());
       
   416 	if (r != KErrNone)
       
   417 		{
       
   418 		_LIT(KSetLabelFail, "Failed to set volume label");
       
   419 		INFO_PRINTF1(KSetLabelFail);
       
   420 		return r;
       
   421 		}
       
   422 	return r; 
       
   423 	}
       
   424 
       
   425 /** 
       
   426 Setting the size of the file
       
   427 
       
   428 @param aFile The name of the file whos size to set
       
   429 
       
   430 @return KErrNone if successful
       
   431 */			
       
   432 TInt CBaseTestFat32WriteFile::SetFileSize(const TDesC16& aFile)
       
   433 	{
       
   434 	TInt r = 0;
       
   435 	r = TheFile.Open(iTheFs,aFile,EFileWrite);
       
   436 	if (r != KErrNone)
       
   437 		{
       
   438 		_LIT(KOpenFail, "Failed to open the file");
       
   439 		INFO_PRINTF1(KOpenFail);
       
   440 		return r;
       
   441 		}
       
   442 	TInt fileSize = (iBPB_SecPerClus/2) * 3 * 1024; // File accomodates 3 clusters
       
   443 	r = TheFile.SetSize(fileSize);
       
   444 	if (r != KErrNone)
       
   445 		{
       
   446 		_LIT(KSetSizeFail, "Failed set the size of the file");
       
   447 		INFO_PRINTF1(KSetSizeFail);
       
   448 		return r;
       
   449 		}
       
   450 	TheFile.Close();
       
   451 	r = iTheFs.FinaliseDrive(CurrentDrive(), RFs::EFinal_RW);
       
   452 	return r; 
       
   453 	}
       
   454 
       
   455 /** 
       
   456 Delete the file
       
   457 
       
   458 @param aFile The name of the file to delete
       
   459 
       
   460 @return KErrNone if successful
       
   461 */				
       
   462 TInt CBaseTestFat32WriteFile::DeleteFile(const TDesC16& aFile)
       
   463 	{
       
   464 	TInt r = 0;
       
   465 	r = iTheFs.Delete(aFile);
       
   466 	if (r != KErrNone)
       
   467 		{
       
   468 		_LIT(KDeleteFail, "Failed delete the file");
       
   469 		INFO_PRINTF1(KDeleteFail);
       
   470 		return r;
       
   471 		}
       
   472 	return r; 
       
   473 	}
       
   474