networkingtestandutils/networkingintegrationtest/IntegrationTestUtils/TestUtils.cpp
changeset 0 af10295192d8
equal deleted inserted replaced
-1:000000000000 0:af10295192d8
       
     1 // Copyright (c) 2003-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 "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 // This contains CTestCase which is the base class for all the TestCase DLLs
       
    15 // 
       
    16 //
       
    17 
       
    18 /**
       
    19  @file TestUtils.cpp
       
    20 */
       
    21 
       
    22 // EPOC includes
       
    23 #include <e32base.h>
       
    24 
       
    25 #include <e32err.h>
       
    26 
       
    27 // Test system includes
       
    28 #include "../inc/Log.h"
       
    29 #include "../inc/TestUtils.h"
       
    30 
       
    31 #include <f32file.h>
       
    32 
       
    33 EXPORT_C CTestUtils * CTestUtils::NewL( CLog * aLogSystem)
       
    34 /**
       
    35 Static constructor for CTestUtils.
       
    36 
       
    37 @param aLogSystem A pointer to the test framework logging system.
       
    38 */
       
    39 {
       
    40 	CTestUtils * self = new(ELeave) CTestUtils;
       
    41 	CleanupStack::PushL(self);
       
    42 	self->Construct( aLogSystem);
       
    43 	CleanupStack::Pop();
       
    44 	return self;
       
    45 }
       
    46 
       
    47 void CTestUtils::Construct( CLog * aLogSystem)
       
    48 /**
       
    49 Second phase constructor for CTestUtils.
       
    50 
       
    51 @param aLogSystem A pointer to the test framework logging system.
       
    52 */
       
    53 {
       
    54 	iLogSystem = aLogSystem;
       
    55 }
       
    56 
       
    57 EXPORT_C void CTestUtils::RunUtils( const TDesC& aText  )
       
    58 /**
       
    59 External interface for run utils.
       
    60 
       
    61 @param aText Text specifying the utility required and any parameters.
       
    62 @note This traps any leaves that may occur in the Test utilities.
       
    63 */
       
    64 	{
       
    65 	// now execute util inside a trap 
       
    66 	TRAPD( r, RunUtilsL( aText ) )
       
    67 
       
    68 	if (r!= KErrNone)
       
    69 		{
       
    70 		iLogSystem->Log(_L("Warning test utils :%S left!"), &aText );
       
    71 		}
       
    72 	}
       
    73 
       
    74 void CTestUtils::RunUtilsL( const TDesC& aText  )
       
    75 /**
       
    76 Run a test utility.
       
    77 
       
    78 @param aText Text specifying the utility required and any parameters.
       
    79 @note This method can leave.
       
    80 */
       
    81 	{
       
    82 	// use Tlex to decode the cmd line
       
    83 	TLex lex(aText);
       
    84 
       
    85 	// start at the begining
       
    86 	TPtrC token=lex.NextToken();
       
    87 
       
    88 	// step over the keyword
       
    89 	token.Set(lex.NextToken());
       
    90 
       
    91 	// check which util required, then get any required parmeters
       
    92 	if ( token.FindF( _L("ChangeDir")) != KErrNotFound )
       
    93 		{
       
    94 		// get the parameter
       
    95 		token.Set(lex.NextToken());
       
    96 
       
    97 		ChangeDirL(token);
       
    98 		}
       
    99 	else if ( token.FindF( _L("CopyFile")) != KErrNotFound )
       
   100 		{
       
   101 		// get the parameter
       
   102 		TPtrC file1=lex.NextToken();
       
   103 		TPtrC file2=lex.NextToken();
       
   104 
       
   105 		CopyFileL(file1, file2);
       
   106 		}
       
   107 	else if ( token.FindF( _L("MkDir")) != KErrNotFound )
       
   108 		{
       
   109 		// get the parameter
       
   110 		token.Set(lex.NextToken());
       
   111 
       
   112 		MakedirL(token);
       
   113 		}
       
   114 	else if ( token.FindF( _L("delete")) != KErrNotFound )
       
   115 		{
       
   116 		// get the parameter
       
   117 		token.Set(lex.NextToken());
       
   118 
       
   119 		DeleteFileL(token);
       
   120 		}
       
   121 	else if ( token.FindF( _L("makereadwrite")) != KErrNotFound )
       
   122 		{
       
   123 		// get the parameter
       
   124 		token.Set(lex.NextToken());
       
   125 
       
   126 		MakeReadWriteL(token);
       
   127 		}
       
   128 	else
       
   129 		{
       
   130 		iLogSystem->Log( _L("Failed to decode run_utils command %S"), &aText );
       
   131 		}
       
   132 	}
       
   133 
       
   134 
       
   135 void CTestUtils::ChangeDirL (const TDesC& aDirname) 
       
   136 /**
       
   137 Change current working directory.
       
   138 This method can leave.
       
   139 
       
   140 @param aDirname The name of the target directory.
       
   141 @note This defaults to the C: drive.
       
   142 */
       
   143 	{
       
   144 	iLogSystem->Log( _L("\'ChangeDir %S\' failed, functionality not supported in EKA2"), &aDirname);
       
   145 	_LIT(KChangeDirPanic, "ChangeDir");
       
   146 	User::Panic(KChangeDirPanic, KErrNotSupported);
       
   147 	}
       
   148 
       
   149 void CTestUtils::MakedirL (const TDesC& aDirname) 
       
   150 /**
       
   151 Make new directory.
       
   152 This method can leave.
       
   153 
       
   154 @param aDirname The name of the new directory to be created.
       
   155 @note This defaults to the C: drive.
       
   156 */
       
   157 	{
       
   158 	// parse the filenames
       
   159 	_LIT(KDefault,"C:\\"); 
       
   160 	TParse FullFileName;
       
   161 	TInt returnCode = FullFileName.Set( aDirname, &KDefault, NULL );
       
   162 	if ( returnCode != KErrNone )
       
   163 		{
       
   164 		TPtrC Errortxt = CLog::EpocErrorToText(returnCode);
       
   165 		iLogSystem->Log( _L("Failed decode full path name %S - %S"), 
       
   166 			&FullFileName.FullName(), 
       
   167 			&Errortxt );
       
   168 		}
       
   169 
       
   170 	// create a fileserver
       
   171 	RFs  FileSystem;
       
   172 
       
   173 	// connect to file server
       
   174 	returnCode=FileSystem.Connect();
       
   175 	if ( returnCode != KErrNone )
       
   176 		return;
       
   177 
       
   178 	// now create the new directory
       
   179 	returnCode = FileSystem.MkDir( FullFileName.DriveAndPath() ); 
       
   180 
       
   181 	// check for errors
       
   182 	if (returnCode == KErrNone )
       
   183 		{
       
   184 		// display full (including path) file name
       
   185 		iLogSystem->Log( _L("made directory %S"), &FullFileName.FullName()  );	
       
   186 		}
       
   187 	else
       
   188 		{		
       
   189 		TPtrC Errortxt = CLog::EpocErrorToText(returnCode);
       
   190 
       
   191 		iLogSystem->Log( _L("error %S making dir %S"), 
       
   192 			&Errortxt, 
       
   193 			&FullFileName.FullName()  );
       
   194 		}
       
   195 
       
   196 	}
       
   197 
       
   198 void CTestUtils::CopyFileL (const TDesC& anOld,const TDesC& aNew) 
       
   199 /**
       
   200 Copy a file
       
   201 This method can leave.
       
   202 
       
   203 @param anOld The source name of file to be copied.
       
   204 @param aNew The target name of file to be copied.
       
   205 @note This defaults to the C: drive.
       
   206 */
       
   207 	{
       
   208 	iLogSystem->Log( _L("copy file from %S to %S"), &anOld, &aNew  );	
       
   209 
       
   210 	// create a fileserver
       
   211 	RFs  FileSystem;
       
   212 
       
   213 	// connect to file server
       
   214 	TInt returnCode=FileSystem.Connect();
       
   215 
       
   216 	// create a file manager
       
   217 	CFileMan * FileMan = CFileMan::NewL( FileSystem );
       
   218 	if (returnCode != KErrNone )
       
   219 		{
       
   220 		// error opening FileManager
       
   221 		iLogSystem->Log( _L("error opening file manager")); 
       
   222 		}
       
   223 
       
   224 	// parse the filenames
       
   225 	_LIT(KRelated,"C:\\"); 
       
   226 	TParse Source;
       
   227 	returnCode = Source.Set( anOld, &KRelated, NULL );
       
   228 	if ( returnCode != KErrNone )
       
   229 		{
       
   230 		TPtrC Errortxt = CLog::EpocErrorToText(returnCode);
       
   231 		iLogSystem->Log( _L("Failed to parse %S - %S"), 
       
   232 			&anOld, 
       
   233 			&Errortxt );
       
   234 		}
       
   235  
       
   236 	// parse the filenames
       
   237 	TParse Target;
       
   238 	returnCode = Target.Set( aNew, &KRelated, NULL );
       
   239 	if ( returnCode != KErrNone )
       
   240 		{
       
   241 		TPtrC Errortxt = CLog::EpocErrorToText(returnCode);
       
   242 		iLogSystem->Log( _L("Failed to parse %S - %S"), 
       
   243 			&aNew, 
       
   244 			&Errortxt );
       
   245 		}
       
   246 
       
   247 	// do the copy
       
   248 	returnCode=FileMan->Copy(Source.FullName(), 
       
   249 		Target.FullName(), 
       
   250 		CFileMan::EOverWrite );
       
   251 
       
   252 	if ( returnCode != KErrNone )
       
   253 		{
       
   254 		TPtrC CopyErrortxt = CLog::EpocErrorToText(returnCode);
       
   255 
       
   256 		iLogSystem->Log( _L("Failed to copy %S to %S - %S"), 
       
   257 			&Source.FullName(), 
       
   258 			&Target.FullName(),
       
   259 			&CopyErrortxt );
       
   260 		}
       
   261 	else
       
   262 		{
       
   263 		iLogSystem->Log( _L("Copied file from %S to %S"), 
       
   264 			&Source.FullName(), 
       
   265 			&Target.FullName() );
       
   266 
       
   267 		}
       
   268 
       
   269 	// close the file system
       
   270 	FileSystem.Close();
       
   271 
       
   272 	delete FileMan;
       
   273 	}
       
   274 
       
   275 void CTestUtils::DeleteFileL (const TDesC& aFile) 
       
   276 /**
       
   277 Delete a file
       
   278 This method can leave.
       
   279 
       
   280 @param aFile Name of file to be deleted.
       
   281 @note This defaults to the C: drive.
       
   282 */
       
   283 	{
       
   284 	// parse the filenames
       
   285 	_LIT(KDefault,"C:\\"); 
       
   286 	TParse FullFileName;
       
   287 	TInt returnCode = FullFileName.Set( aFile, &KDefault, NULL );
       
   288 	if ( returnCode != KErrNone )
       
   289 		{
       
   290 		TPtrC Errortxt = CLog::EpocErrorToText(returnCode);
       
   291 		iLogSystem->Log( _L("Failed decode full path name %S - %S"), 
       
   292 			&FullFileName.FullName(), 
       
   293 			&Errortxt );
       
   294 		}
       
   295 	
       
   296 
       
   297 	// create a fileserver
       
   298 	RFs  FileSystem;
       
   299 
       
   300 	// connect to file server
       
   301 	returnCode=FileSystem.Connect();
       
   302 	if ( returnCode != KErrNone )
       
   303 		{
       
   304 		iLogSystem->Log( _L("error connecting to FileSystem")  );	
       
   305 		return;
       
   306 		}
       
   307 
       
   308 	// now do the delete
       
   309 	returnCode = FileSystem.Delete(FullFileName.FullName()); 
       
   310 
       
   311 	// check for errors
       
   312 	if (returnCode == KErrNone )
       
   313 		{
       
   314 		// display full (including path) file name
       
   315 		iLogSystem->Log( _L("deleted %S"), &FullFileName.FullName()  );	
       
   316 		}
       
   317 	else
       
   318 		{		
       
   319 		TPtrC Errortxt = CLog::EpocErrorToText(returnCode);
       
   320 
       
   321 		iLogSystem->Log( _L("error %S deleting %S"), 
       
   322 			&Errortxt, 
       
   323 			&FullFileName.FullName()  );
       
   324 		}
       
   325 
       
   326 	}
       
   327 
       
   328 void CTestUtils::MakeReadWriteL (const TDesC& aFile) 
       
   329 /**
       
   330 Enable a file for read and write.
       
   331 This method can leave.
       
   332 
       
   333 @param aFile Name of file.
       
   334 @note This defaults to the C: drive.
       
   335 */
       
   336 	{
       
   337 	// parse the filenames
       
   338 	_LIT(KDefault,"C:\\"); 
       
   339 	TParse FullFileName;
       
   340 	TInt returnCode = FullFileName.Set( aFile, &KDefault, NULL );
       
   341 	if ( returnCode != KErrNone )
       
   342 		{
       
   343 		TPtrC Errortxt = CLog::EpocErrorToText(returnCode);
       
   344 		iLogSystem->Log( _L("Failed decode full path name %S - %S"), 
       
   345 			&FullFileName.FullName(), 
       
   346 			&Errortxt );
       
   347 		}
       
   348 	
       
   349 
       
   350 	// create a fileserver
       
   351 	RFs  FileSystem;
       
   352 
       
   353 	// connect to file server
       
   354 	returnCode=FileSystem.Connect();
       
   355 	if ( returnCode != KErrNone )
       
   356 		{
       
   357 		iLogSystem->Log( _L("error connecting to FileSystem")  );	
       
   358 		return;
       
   359 		}
       
   360 
       
   361 	// now do the delete
       
   362 	returnCode = FileSystem.SetAtt(FullFileName.FullName(),0, KEntryAttReadOnly ); 
       
   363 
       
   364 	// check for errors
       
   365 	if (returnCode == KErrNone )
       
   366 		{
       
   367 		// display full (including path) file name
       
   368 		iLogSystem->Log( _L("Made %S RW"), &FullFileName.FullName()  );	
       
   369 		}
       
   370 	else
       
   371 		{		
       
   372 		TPtrC Errortxt = CLog::EpocErrorToText(returnCode);
       
   373 
       
   374 		iLogSystem->Log( _L("error %S making %S RW"), 
       
   375 			&Errortxt, 
       
   376 			&FullFileName.FullName()  );
       
   377 		}
       
   378 
       
   379 	}