os/persistentdata/loggingservices/filelogger/TSRC/T_LOG.CPP
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 1997-2010 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 //
    15 
    16 
    17 #include <e32test.h>
    18 #include <flogger.h>
    19 #include <f32file.h>
    20 
    21 GLDEF_D RTest test(_L("FLOGGER Test Code"));
    22 GLDEF_D RFs fs;
    23 
    24 _LIT(KTestLogFileName1,"Log1.txt");
    25 _LIT(KTestLogFileName2,"Log2.txt");
    26 _LIT(KTestLogDir1,"Test1");
    27 _LIT(KTestLogDir2,"Test2");
    28 _LIT(KTestLogDir3,"Test3");
    29 _LIT(KTestLogDir4,"Test4");
    30 _LIT(KTestLogDir5,"Test5");
    31 _LIT(KTestLogFullDir1,"c:\\Logs\\Test1\\");
    32 _LIT(KTestLogFullDir2,"c:\\Logs\\Test2\\");
    33 _LIT(KTestLogFullDir3,"c:\\Logs\\Test3\\");
    34 _LIT(KTestLogFullDir4,"c:\\Logs\\Test4\\");
    35 _LIT(KTestLogFullDir5,"c:\\Logs\\Test5\\");
    36 _LIT(KTestLogFile1,"c:\\Logs\\Test1\\Log1.txt");
    37 _LIT(KTestLogFile2,"c:\\Logs\\Test1\\Log2.txt");
    38 _LIT(KTestLogFile3,"c:\\Logs\\Test2\\Log1.txt");
    39 _LIT(KTestLogFile4,"c:\\Logs\\Test3\\Log1.txt");
    40 
    41 // 2.2.5 Wait for shutdown after closure of session 
    42 LOCAL_C void TestWaitForShutDown()
    43 	{
    44 
    45 	
    46 	test.Printf(_L("\nWaiting for FLogger server shutdown"));
    47 	TInt i;
    48 	for (i=30; i>0; i--)
    49 		{
    50 		TFullName name=_L("*");
    51 		_LIT(serverName,"FLogger server");
    52 		name.Append(serverName);
    53 		TFindThread find(name);
    54 		if(find.Next(name)==KErrNone)
    55 			test.Printf(_L("."));
    56 		else
    57 			break;
    58 		User::After(1000000);
    59 		}
    60 	
    61 	test(i);
    62 	test.Printf(_L("\nFLogger Server Shutdown after %d secs\n"), 29-i);
    63 	}
    64 
    65 LOCAL_C TInt DeleteFolder(const TDesC& aFolder)
    66 	{
    67     CFileMan* fileman=NULL;
    68     TRAPD(ret,fileman=CFileMan::NewL(fs));
    69     ret=fileman->RmDir(aFolder);
    70     delete fileman;
    71     if (ret==KErrPathNotFound || ret==KErrNotFound)
    72         {
    73         return KErrNone;
    74         }
    75     return ret;
    76 	}
    77 
    78 LOCAL_C void CreateFolderL(const TDesC& aFolder)
    79 	{
    80 
    81 	TInt ret=fs.MkDirAll(aFolder);
    82 	if (ret!=KErrNone && ret!=KErrAlreadyExists)
    83 		User::Leave(ret);
    84 	}
    85 
    86 LOCAL_C void WriteToLog(RFileLogger& aLog)
    87 	{
    88 
    89 	// 16 bit writes
    90 	TUint num1=100;
    91 	aLog.WriteFormat(_L("Writing a number to the log: %d"),num1);
    92 	test(aLog.LastError()==KErrNone);
    93 	TUint num2=200;
    94 	aLog.WriteFormat(_L("Writing two numbers to the log: %d, %d"),num1,num2);
    95 	test(aLog.LastError()==KErrNone);
    96 	aLog.SetDateAndTime(ETrue,ETrue);
    97 	aLog.Write(_L("Line should begin with date and time"));
    98 	test(aLog.LastError()==KErrNone);
    99 	aLog.SetDateAndTime(ETrue,EFalse);
   100 	aLog.Write(_L("Line should begin with date only"));
   101 	test(aLog.LastError()==KErrNone);
   102 	aLog.SetDateAndTime(EFalse,ETrue);
   103 	aLog.Write(_L("Line should begin with time only"));
   104 	test(aLog.LastError()==KErrNone);
   105 	aLog.SetDateAndTime(EFalse,EFalse);
   106 	aLog.Write(_L("Line should begin with nothing"));
   107 	test(aLog.LastError()==KErrNone);
   108 	aLog.Write(_L(""));
   109 	test(aLog.LastError()==KErrNone);
   110 	aLog.Write(_L("There should be a blank line above and below this one"));
   111 	test(aLog.LastError()==KErrNone);
   112 	aLog.Write(_L(""));
   113 	test(aLog.LastError()==KErrNone);
   114 
   115 	// 8 bit writes
   116 	aLog.WriteFormat(_L8("Writing a number to the log: %d"),num1);
   117 	test(aLog.LastError()==KErrNone);
   118 	aLog.WriteFormat(_L8("Writing two numbers to the log: %d, %d"),num1,num2);
   119 	test(aLog.LastError()==KErrNone);
   120 	aLog.SetDateAndTime(ETrue,ETrue);
   121 	aLog.Write(_L8("Line should begin with date and time"));
   122 	test(aLog.LastError()==KErrNone);
   123 	aLog.SetDateAndTime(ETrue,EFalse);
   124 	aLog.Write(_L8("Line should begin with date only"));
   125 	test(aLog.LastError()==KErrNone);
   126 	aLog.SetDateAndTime(EFalse,ETrue);
   127 	aLog.Write(_L8("Line should begin with time only"));
   128 	test(aLog.LastError()==KErrNone);
   129 	aLog.SetDateAndTime(EFalse,EFalse);
   130 	aLog.Write(_L8("Line should begin with nothing"));
   131 	test(aLog.LastError()==KErrNone);
   132 	aLog.Write(_L8(""));
   133 	test(aLog.LastError()==KErrNone);
   134 	aLog.Write(_L8("There should be a blank line above and below this one"));
   135 	test(aLog.LastError()==KErrNone);
   136 	aLog.Write(_L8(""));
   137 	test(aLog.LastError()==KErrNone);
   138 
   139 // 2.2.1.5 Dump hexadecimal values
   140 	// Hex dump
   141 	const TText* hdr=_S("123456");
   142 	const TText* mgn=_S("      ");
   143 	const TUint8* ptr=_S8("abcdefghijklmnopqrstuvwxyz");
   144 	TInt len=26;
   145 	aLog.HexDump(hdr, mgn, ptr, len);
   146 	test(aLog.LastError()==KErrNone);
   147 	aLog.HexDump(NULL, mgn, ptr, len);
   148 	test(aLog.LastError()==KErrNone);
   149 	aLog.HexDump(hdr, NULL, ptr, len);
   150 	test(aLog.LastError()==KErrNone);
   151 	aLog.HexDump(hdr, mgn, NULL, len);
   152 	test(aLog.LastError()==KErrNone);
   153 	aLog.SetDateAndTime(ETrue,ETrue);
   154 	aLog.HexDump(hdr, mgn, ptr, len);
   155 	test(aLog.LastError()==KErrNone);
   156 	aLog.HexDump(NULL, mgn, ptr, len);
   157 	test(aLog.LastError()==KErrNone);
   158 	aLog.HexDump(hdr, NULL, ptr, len);
   159 	test(aLog.LastError()==KErrNone);
   160 	aLog.HexDump(hdr, mgn, NULL, len);	
   161 	test(aLog.LastError()==KErrNone);
   162 	}
   163 
   164 // 2.2.1 Writing to two open log files at a time 
   165 // 2.2.1.3 Files are different 
   166 LOCAL_C void testTwoClientsL(TBool aValid)
   167 	{
   168 
   169 	if (!aValid)
   170 		test(DeleteFolder(KTestLogFullDir1)==KErrNone);
   171 	else
   172 		CreateFolderL(KTestLogFullDir1);
   173 
   174 	RFileLogger log1;
   175 	test(log1.Connect()==KErrNone);
   176 	RFileLogger log2;
   177 	test(log2.Connect()==KErrNone);
   178 
   179 	log1.CreateLog(KTestLogDir1,KTestLogFileName1,EFileLoggingModeOverwrite);
   180 	test(log1.LastError()==KErrNone);
   181 	test(log1.LogValid()==aValid);
   182 	
   183 	log2.CreateLog(KTestLogDir1,KTestLogFileName2,EFileLoggingModeOverwrite);
   184 	test(log2.LastError()==KErrNone);
   185 	test(log2.LogValid()==aValid);
   186 	
   187 	log1.Write(_L("This is written to log 1"));
   188 	test(log1.LastError()==KErrNone);
   189 	test(log1.LogValid()==aValid);
   190 	log2.Write(_L("This is written to log 2"));
   191 	test(log2.LastError()==KErrNone);
   192 	test(log2.LogValid()==aValid);
   193 
   194 	WriteToLog(log1);
   195 	WriteToLog(log2);
   196 
   197 	log1.CloseLog();
   198 	log2.CloseLog();
   199 
   200 	TUint temp;
   201 	if (aValid)
   202 		{
   203 		test(fs.Att(KTestLogFile1,temp)==KErrNone);
   204 		test(fs.Att(KTestLogFile2,temp)==KErrNone);
   205 		}
   206 	else
   207 		{
   208 		test(fs.Att(KTestLogFile1,temp)==KErrPathNotFound);
   209 		test(fs.Att(KTestLogFile2,temp)==KErrPathNotFound);
   210 		}
   211 
   212 	log1.Close();
   213 	log2.Close();
   214 	}
   215 
   216 // 2.2.2 Static appends 
   217 LOCAL_C void testStaticWrites(TBool aValid)
   218 	{
   219 	
   220 	if (!aValid)
   221 		test(DeleteFolder(KTestLogFullDir2)==KErrNone);
   222 	else
   223 		{
   224 		TRAPD(ret,(CreateFolderL(KTestLogFullDir2)));
   225 		test(ret==KErrNone);
   226 		}
   227 
   228 	RFileLogger::Write(KTestLogDir2,KTestLogFileName1,EFileLoggingModeAppend,_L("Writing to the log"));
   229 	TUint num1=100;
   230 	RFileLogger::WriteFormat(KTestLogDir2(),KTestLogFileName1(),EFileLoggingModeAppend,_L("Writing a number to the log: %d"),num1);
   231 	TUint num2=200;
   232 	RFileLogger::WriteFormat(KTestLogDir2(),KTestLogFileName1(),EFileLoggingModeAppend,_L("Writing two numbers to the log: %d, %d"),num1,num2);
   233 
   234 	RFileLogger::Write(KTestLogDir2,KTestLogFileName1,EFileLoggingModeAppend,_L8("Writing to the log"));
   235 	RFileLogger::WriteFormat(KTestLogDir2(),KTestLogFileName1(),EFileLoggingModeAppend,_L8("Writing a number to the log: %d"),num1);
   236 	RFileLogger::WriteFormat(KTestLogDir2(),KTestLogFileName1(),EFileLoggingModeAppend,_L8("Writing two numbers to the log: %d, %d"),num1,num2);
   237 
   238 	const TText* hdr=_S("123456");
   239 	const TText* mgn=_S("      ");
   240 	const TUint8* ptr=_S8("abcdefghijklmnopqrstuvwxyz");
   241 	TInt len=26;
   242 	RFileLogger::HexDump(KTestLogDir2(),KTestLogFileName1(),EFileLoggingModeAppend, hdr, mgn, ptr, len);
   243 		
   244 	TUint temp;
   245 	if (aValid)
   246 		test(fs.Att(KTestLogFile3,temp)==KErrNone);
   247 	else
   248 		test(fs.Att(KTestLogFile3,temp)==KErrPathNotFound);
   249 	}
   250 
   251 // 2.2.1 Writing to two open log files at a time 
   252 // 2.2.1.4 Files are the same 
   253 LOCAL_C void testTwoClientsOneFileL(TBool aValid)
   254 	{
   255 
   256 	if (!aValid)
   257 		test(DeleteFolder(KTestLogFullDir3)==KErrNone);
   258 	else
   259 		CreateFolderL(KTestLogFullDir3);
   260 
   261 	RFileLogger log1;
   262 	test(log1.Connect()==KErrNone);
   263 	RFileLogger log2;
   264 	test(log2.Connect()==KErrNone);
   265 
   266 	log1.CreateLog(KTestLogDir3,KTestLogFileName1,EFileLoggingModeOverwrite);
   267 	test(log1.LastError()==KErrNone);
   268 	test(log1.LogValid()==aValid);
   269 	
   270 	log2.CreateLog(KTestLogDir3,KTestLogFileName1,EFileLoggingModeOverwrite);
   271 	test(log2.LastError()==KErrNone);
   272 	test(log2.LogValid()==aValid);
   273 	
   274 	log1.Write(_L("This is written from log session 1"));
   275 	test(log1.LastError()==KErrNone);
   276 	test(log1.LogValid()==aValid);
   277 	log2.Write(_L("This is written from log session 2"));
   278 	test(log2.LastError()==KErrNone);
   279 	test(log2.LogValid()==aValid);
   280 
   281 	WriteToLog(log1);
   282 	WriteToLog(log2);
   283 
   284 	log1.CloseLog();
   285 	log2.CloseLog();
   286 
   287 	TUint temp;
   288 	if (aValid)
   289 		test(fs.Att(KTestLogFile4,temp)==KErrNone);
   290 	else
   291 		test(fs.Att(KTestLogFile4,temp)==KErrPathNotFound);
   292 
   293 	log1.Close();
   294 	log2.Close();
   295 	}
   296 
   297 // 2.2.4 Invalid operations
   298 //
   299 // Check that buffers larger than KLogBufferSize can be written without error.
   300 LOCAL_C void testInvalidOps1L()
   301 	{
   302 
   303 	CreateFolderL(KTestLogFullDir4);
   304 
   305 	RFileLogger log;
   306 	test(log.Connect()==KErrNone);
   307 
   308 	log.CreateLog(KTestLogDir4,KTestLogFileName1,EFileLoggingModeOverwrite);
   309 	test(log.LastError()==KErrNone);
   310 	test(log.LogValid());
   311 
   312 	TBuf<151> longBuf;
   313 	TChar ch('X');
   314 	longBuf.Fill(ch,151);
   315 	test(longBuf.Length()==151);
   316 	log.Write(longBuf);					// only those chars which will fit will be written
   317 	test(log.LastError()==KErrNone);
   318 
   319 	TBuf8<151> longBuf8;
   320 	longBuf8.Fill(ch,151);
   321 	test(longBuf8.Length()==151);
   322 	log.Write(longBuf8);				// only those chars which will fit will be written
   323 	test(log.LastError()==KErrNone);
   324 
   325 	log.Close();
   326 	}
   327 	
   328 // Check that overlong directory names as well as file names cause an overflow error.
   329 LOCAL_C void testInvalidOps2L()
   330 	{
   331 
   332 	CreateFolderL(KTestLogFullDir4);
   333 
   334 	RFileLogger log;
   335 	test(log.Connect()==KErrNone);
   336 
   337 	TFileName dirName, fileName;
   338 	dirName.SetLength(dirName.MaxLength());
   339 	log.CreateLog(dirName,KTestLogFileName1,EFileLoggingModeOverwrite);
   340 	test(log.LastError()==KErrOverflow);
   341 
   342 	fileName.SetLength(fileName.MaxLength());
   343 	log.CreateLog(KTestLogDir1,fileName,EFileLoggingModeOverwrite);
   344 	test(log.LastError()==KErrOverflow);
   345 
   346 	log.Close();
   347 	}
   348 
   349 // 2.2.3 Access counting 
   350 LOCAL_C void TestAccessCountingL(TBool aValid)
   351 	{
   352 
   353 	if (aValid)
   354 		CreateFolderL(KTestLogFullDir5);
   355 	else
   356 		DeleteFolder(KTestLogFullDir5);
   357 
   358 	RFileLogger log;
   359 	test(log.Connect()==KErrNone);
   360 
   361 	log.CreateLog(KTestLogDir5,KTestLogFileName1,EFileLoggingModeOverwrite);
   362 	test(log.LastError()==KErrNone);
   363 
   364 	log.CloseLog();
   365 
   366 	log.CreateLog(KTestLogDir5,KTestLogFileName1,EFileLoggingModeOverwrite);
   367 	test(log.LastError()==KErrNone);
   368 
   369 	log.CloseLog();
   370 
   371 	log.CreateLog(KTestLogDir5,KTestLogFileName1,EFileLoggingModeOverwrite);
   372 	test(log.LastError()==KErrNone);
   373 
   374 	log.Close();
   375 
   376 // Test closing log twice
   377 
   378 	test(log.Connect()==KErrNone);
   379 
   380 	log.CreateLog(KTestLogDir5,KTestLogFileName1,EFileLoggingModeOverwrite);
   381 	test(log.LastError()==KErrNone);
   382 
   383 	log.CloseLog();
   384 	log.CloseLog();			// timer will be running if file exists
   385 	User::After(6000000);
   386 	log.CloseLog();			// timer will have expired
   387 	log.Close();
   388 
   389 // Test closing session twice
   390 
   391 	test(log.Connect()==KErrNone);
   392 
   393 	log.Close();
   394 	log.Close();			// timer will be running
   395 	TestWaitForShutDown();
   396 	log.Close();			// timer will have expired
   397 	}
   398 
   399 LOCAL_C void doExampleL()
   400 	{
   401 
   402 	User::LeaveIfError(fs.Connect());
   403 
   404 	// delete all files and folders
   405 	DeleteFolder(KTestLogFullDir1);	// ignore return value
   406 	DeleteFolder(KTestLogFullDir2);	// ignore return value
   407 	DeleteFolder(KTestLogFullDir3);	// ignore return value
   408 	DeleteFolder(KTestLogFullDir4);
   409 
   410 	test.Printf(_L("\nDo tests with Folders not existing....\n"));
   411 
   412 	test.Start(_L("Two Sessions logging to different files: PDS-FILELOGGER-UT-4001"));
   413 	testTwoClientsL(EFalse);	// folders do not exist
   414 	
   415 	test.Next(_L("Two sessions logging to one file: PDS-FILELOGGER-UT-4002"));
   416 	testTwoClientsOneFileL(EFalse);		// folders do not exist
   417 	
   418 	test.Next(_L("Static logging: PDS-FILELOGGER-UT-4003"));
   419 	testStaticWrites(EFalse);		// folders do not exist
   420 
   421 	test.Next(_L("Test Closing and Access Counting: PDS-FILELOGGER-UT-4004"));
   422 	TestAccessCountingL(EFalse);
   423 
   424 	test.Printf(_L("\nDo tests with Folders existing....\n"));
   425 
   426 	test.Start(_L("Two Sessions logging to different files: PDS-FILELOGGER-UT-4001"));
   427 	testTwoClientsL(ETrue);		// folders exist
   428 
   429 	test.Next(_L("Two sessions logging to one file: PDS-FILELOGGER-UT-4002"));
   430 	testTwoClientsOneFileL(ETrue);		// folders exist
   431 
   432 	test.Next(_L("Static logging: PDS-FILELOGGER-UT-4003"));
   433 	testStaticWrites(ETrue);		// folders exist
   434 
   435 	test.Next(_L("Invalid Operations: PDS-FILELOGGER-UT-4005"));
   436 	testInvalidOps1L();
   437 	test.Next(_L("Invalid Operations: PDS-FILELOGGER-UT-4006"));
   438 	testInvalidOps2L();
   439 
   440 	test.Next(_L("Test Closing and Access Counting: PDS-FILELOGGER-UT-4004"));
   441 	TestAccessCountingL(ETrue);
   442 
   443 	RFileLogger log;
   444 	test(log.Connect()==KErrNone);
   445 	log.Close();
   446 
   447 	TestWaitForShutDown();
   448 	test.End();
   449 
   450 	fs.Close();
   451 	}
   452 
   453 GLDEF_C TInt E32Main()
   454 	{
   455 
   456 	CTrapCleanup* cleanup=CTrapCleanup::New();
   457 
   458 	__UHEAP_MARK;
   459 	
   460 	test.Title();
   461 
   462 	TRAPD(ret,doExampleL());
   463 	test(ret==KErrNone);
   464 	
   465     test.End();
   466 	test.Close();
   467 
   468 	__UHEAP_MARKEND;
   469 
   470 	delete cleanup;
   471 	User::Heap().Check();
   472 	return KErrNone;
   473 	}