os/persistentdata/loggingservices/filelogger/TSRC/T_LOG.CPP
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/persistentdata/loggingservices/filelogger/TSRC/T_LOG.CPP	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,473 @@
     1.4 +// Copyright (c) 1997-2010 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +//
    1.18 +
    1.19 +
    1.20 +#include <e32test.h>
    1.21 +#include <flogger.h>
    1.22 +#include <f32file.h>
    1.23 +
    1.24 +GLDEF_D RTest test(_L("FLOGGER Test Code"));
    1.25 +GLDEF_D RFs fs;
    1.26 +
    1.27 +_LIT(KTestLogFileName1,"Log1.txt");
    1.28 +_LIT(KTestLogFileName2,"Log2.txt");
    1.29 +_LIT(KTestLogDir1,"Test1");
    1.30 +_LIT(KTestLogDir2,"Test2");
    1.31 +_LIT(KTestLogDir3,"Test3");
    1.32 +_LIT(KTestLogDir4,"Test4");
    1.33 +_LIT(KTestLogDir5,"Test5");
    1.34 +_LIT(KTestLogFullDir1,"c:\\Logs\\Test1\\");
    1.35 +_LIT(KTestLogFullDir2,"c:\\Logs\\Test2\\");
    1.36 +_LIT(KTestLogFullDir3,"c:\\Logs\\Test3\\");
    1.37 +_LIT(KTestLogFullDir4,"c:\\Logs\\Test4\\");
    1.38 +_LIT(KTestLogFullDir5,"c:\\Logs\\Test5\\");
    1.39 +_LIT(KTestLogFile1,"c:\\Logs\\Test1\\Log1.txt");
    1.40 +_LIT(KTestLogFile2,"c:\\Logs\\Test1\\Log2.txt");
    1.41 +_LIT(KTestLogFile3,"c:\\Logs\\Test2\\Log1.txt");
    1.42 +_LIT(KTestLogFile4,"c:\\Logs\\Test3\\Log1.txt");
    1.43 +
    1.44 +// 2.2.5 Wait for shutdown after closure of session 
    1.45 +LOCAL_C void TestWaitForShutDown()
    1.46 +	{
    1.47 +
    1.48 +	
    1.49 +	test.Printf(_L("\nWaiting for FLogger server shutdown"));
    1.50 +	TInt i;
    1.51 +	for (i=30; i>0; i--)
    1.52 +		{
    1.53 +		TFullName name=_L("*");
    1.54 +		_LIT(serverName,"FLogger server");
    1.55 +		name.Append(serverName);
    1.56 +		TFindThread find(name);
    1.57 +		if(find.Next(name)==KErrNone)
    1.58 +			test.Printf(_L("."));
    1.59 +		else
    1.60 +			break;
    1.61 +		User::After(1000000);
    1.62 +		}
    1.63 +	
    1.64 +	test(i);
    1.65 +	test.Printf(_L("\nFLogger Server Shutdown after %d secs\n"), 29-i);
    1.66 +	}
    1.67 +
    1.68 +LOCAL_C TInt DeleteFolder(const TDesC& aFolder)
    1.69 +	{
    1.70 +    CFileMan* fileman=NULL;
    1.71 +    TRAPD(ret,fileman=CFileMan::NewL(fs));
    1.72 +    ret=fileman->RmDir(aFolder);
    1.73 +    delete fileman;
    1.74 +    if (ret==KErrPathNotFound || ret==KErrNotFound)
    1.75 +        {
    1.76 +        return KErrNone;
    1.77 +        }
    1.78 +    return ret;
    1.79 +	}
    1.80 +
    1.81 +LOCAL_C void CreateFolderL(const TDesC& aFolder)
    1.82 +	{
    1.83 +
    1.84 +	TInt ret=fs.MkDirAll(aFolder);
    1.85 +	if (ret!=KErrNone && ret!=KErrAlreadyExists)
    1.86 +		User::Leave(ret);
    1.87 +	}
    1.88 +
    1.89 +LOCAL_C void WriteToLog(RFileLogger& aLog)
    1.90 +	{
    1.91 +
    1.92 +	// 16 bit writes
    1.93 +	TUint num1=100;
    1.94 +	aLog.WriteFormat(_L("Writing a number to the log: %d"),num1);
    1.95 +	test(aLog.LastError()==KErrNone);
    1.96 +	TUint num2=200;
    1.97 +	aLog.WriteFormat(_L("Writing two numbers to the log: %d, %d"),num1,num2);
    1.98 +	test(aLog.LastError()==KErrNone);
    1.99 +	aLog.SetDateAndTime(ETrue,ETrue);
   1.100 +	aLog.Write(_L("Line should begin with date and time"));
   1.101 +	test(aLog.LastError()==KErrNone);
   1.102 +	aLog.SetDateAndTime(ETrue,EFalse);
   1.103 +	aLog.Write(_L("Line should begin with date only"));
   1.104 +	test(aLog.LastError()==KErrNone);
   1.105 +	aLog.SetDateAndTime(EFalse,ETrue);
   1.106 +	aLog.Write(_L("Line should begin with time only"));
   1.107 +	test(aLog.LastError()==KErrNone);
   1.108 +	aLog.SetDateAndTime(EFalse,EFalse);
   1.109 +	aLog.Write(_L("Line should begin with nothing"));
   1.110 +	test(aLog.LastError()==KErrNone);
   1.111 +	aLog.Write(_L(""));
   1.112 +	test(aLog.LastError()==KErrNone);
   1.113 +	aLog.Write(_L("There should be a blank line above and below this one"));
   1.114 +	test(aLog.LastError()==KErrNone);
   1.115 +	aLog.Write(_L(""));
   1.116 +	test(aLog.LastError()==KErrNone);
   1.117 +
   1.118 +	// 8 bit writes
   1.119 +	aLog.WriteFormat(_L8("Writing a number to the log: %d"),num1);
   1.120 +	test(aLog.LastError()==KErrNone);
   1.121 +	aLog.WriteFormat(_L8("Writing two numbers to the log: %d, %d"),num1,num2);
   1.122 +	test(aLog.LastError()==KErrNone);
   1.123 +	aLog.SetDateAndTime(ETrue,ETrue);
   1.124 +	aLog.Write(_L8("Line should begin with date and time"));
   1.125 +	test(aLog.LastError()==KErrNone);
   1.126 +	aLog.SetDateAndTime(ETrue,EFalse);
   1.127 +	aLog.Write(_L8("Line should begin with date only"));
   1.128 +	test(aLog.LastError()==KErrNone);
   1.129 +	aLog.SetDateAndTime(EFalse,ETrue);
   1.130 +	aLog.Write(_L8("Line should begin with time only"));
   1.131 +	test(aLog.LastError()==KErrNone);
   1.132 +	aLog.SetDateAndTime(EFalse,EFalse);
   1.133 +	aLog.Write(_L8("Line should begin with nothing"));
   1.134 +	test(aLog.LastError()==KErrNone);
   1.135 +	aLog.Write(_L8(""));
   1.136 +	test(aLog.LastError()==KErrNone);
   1.137 +	aLog.Write(_L8("There should be a blank line above and below this one"));
   1.138 +	test(aLog.LastError()==KErrNone);
   1.139 +	aLog.Write(_L8(""));
   1.140 +	test(aLog.LastError()==KErrNone);
   1.141 +
   1.142 +// 2.2.1.5 Dump hexadecimal values
   1.143 +	// Hex dump
   1.144 +	const TText* hdr=_S("123456");
   1.145 +	const TText* mgn=_S("      ");
   1.146 +	const TUint8* ptr=_S8("abcdefghijklmnopqrstuvwxyz");
   1.147 +	TInt len=26;
   1.148 +	aLog.HexDump(hdr, mgn, ptr, len);
   1.149 +	test(aLog.LastError()==KErrNone);
   1.150 +	aLog.HexDump(NULL, mgn, ptr, len);
   1.151 +	test(aLog.LastError()==KErrNone);
   1.152 +	aLog.HexDump(hdr, NULL, ptr, len);
   1.153 +	test(aLog.LastError()==KErrNone);
   1.154 +	aLog.HexDump(hdr, mgn, NULL, len);
   1.155 +	test(aLog.LastError()==KErrNone);
   1.156 +	aLog.SetDateAndTime(ETrue,ETrue);
   1.157 +	aLog.HexDump(hdr, mgn, ptr, len);
   1.158 +	test(aLog.LastError()==KErrNone);
   1.159 +	aLog.HexDump(NULL, mgn, ptr, len);
   1.160 +	test(aLog.LastError()==KErrNone);
   1.161 +	aLog.HexDump(hdr, NULL, ptr, len);
   1.162 +	test(aLog.LastError()==KErrNone);
   1.163 +	aLog.HexDump(hdr, mgn, NULL, len);	
   1.164 +	test(aLog.LastError()==KErrNone);
   1.165 +	}
   1.166 +
   1.167 +// 2.2.1 Writing to two open log files at a time 
   1.168 +// 2.2.1.3 Files are different 
   1.169 +LOCAL_C void testTwoClientsL(TBool aValid)
   1.170 +	{
   1.171 +
   1.172 +	if (!aValid)
   1.173 +		test(DeleteFolder(KTestLogFullDir1)==KErrNone);
   1.174 +	else
   1.175 +		CreateFolderL(KTestLogFullDir1);
   1.176 +
   1.177 +	RFileLogger log1;
   1.178 +	test(log1.Connect()==KErrNone);
   1.179 +	RFileLogger log2;
   1.180 +	test(log2.Connect()==KErrNone);
   1.181 +
   1.182 +	log1.CreateLog(KTestLogDir1,KTestLogFileName1,EFileLoggingModeOverwrite);
   1.183 +	test(log1.LastError()==KErrNone);
   1.184 +	test(log1.LogValid()==aValid);
   1.185 +	
   1.186 +	log2.CreateLog(KTestLogDir1,KTestLogFileName2,EFileLoggingModeOverwrite);
   1.187 +	test(log2.LastError()==KErrNone);
   1.188 +	test(log2.LogValid()==aValid);
   1.189 +	
   1.190 +	log1.Write(_L("This is written to log 1"));
   1.191 +	test(log1.LastError()==KErrNone);
   1.192 +	test(log1.LogValid()==aValid);
   1.193 +	log2.Write(_L("This is written to log 2"));
   1.194 +	test(log2.LastError()==KErrNone);
   1.195 +	test(log2.LogValid()==aValid);
   1.196 +
   1.197 +	WriteToLog(log1);
   1.198 +	WriteToLog(log2);
   1.199 +
   1.200 +	log1.CloseLog();
   1.201 +	log2.CloseLog();
   1.202 +
   1.203 +	TUint temp;
   1.204 +	if (aValid)
   1.205 +		{
   1.206 +		test(fs.Att(KTestLogFile1,temp)==KErrNone);
   1.207 +		test(fs.Att(KTestLogFile2,temp)==KErrNone);
   1.208 +		}
   1.209 +	else
   1.210 +		{
   1.211 +		test(fs.Att(KTestLogFile1,temp)==KErrPathNotFound);
   1.212 +		test(fs.Att(KTestLogFile2,temp)==KErrPathNotFound);
   1.213 +		}
   1.214 +
   1.215 +	log1.Close();
   1.216 +	log2.Close();
   1.217 +	}
   1.218 +
   1.219 +// 2.2.2 Static appends 
   1.220 +LOCAL_C void testStaticWrites(TBool aValid)
   1.221 +	{
   1.222 +	
   1.223 +	if (!aValid)
   1.224 +		test(DeleteFolder(KTestLogFullDir2)==KErrNone);
   1.225 +	else
   1.226 +		{
   1.227 +		TRAPD(ret,(CreateFolderL(KTestLogFullDir2)));
   1.228 +		test(ret==KErrNone);
   1.229 +		}
   1.230 +
   1.231 +	RFileLogger::Write(KTestLogDir2,KTestLogFileName1,EFileLoggingModeAppend,_L("Writing to the log"));
   1.232 +	TUint num1=100;
   1.233 +	RFileLogger::WriteFormat(KTestLogDir2(),KTestLogFileName1(),EFileLoggingModeAppend,_L("Writing a number to the log: %d"),num1);
   1.234 +	TUint num2=200;
   1.235 +	RFileLogger::WriteFormat(KTestLogDir2(),KTestLogFileName1(),EFileLoggingModeAppend,_L("Writing two numbers to the log: %d, %d"),num1,num2);
   1.236 +
   1.237 +	RFileLogger::Write(KTestLogDir2,KTestLogFileName1,EFileLoggingModeAppend,_L8("Writing to the log"));
   1.238 +	RFileLogger::WriteFormat(KTestLogDir2(),KTestLogFileName1(),EFileLoggingModeAppend,_L8("Writing a number to the log: %d"),num1);
   1.239 +	RFileLogger::WriteFormat(KTestLogDir2(),KTestLogFileName1(),EFileLoggingModeAppend,_L8("Writing two numbers to the log: %d, %d"),num1,num2);
   1.240 +
   1.241 +	const TText* hdr=_S("123456");
   1.242 +	const TText* mgn=_S("      ");
   1.243 +	const TUint8* ptr=_S8("abcdefghijklmnopqrstuvwxyz");
   1.244 +	TInt len=26;
   1.245 +	RFileLogger::HexDump(KTestLogDir2(),KTestLogFileName1(),EFileLoggingModeAppend, hdr, mgn, ptr, len);
   1.246 +		
   1.247 +	TUint temp;
   1.248 +	if (aValid)
   1.249 +		test(fs.Att(KTestLogFile3,temp)==KErrNone);
   1.250 +	else
   1.251 +		test(fs.Att(KTestLogFile3,temp)==KErrPathNotFound);
   1.252 +	}
   1.253 +
   1.254 +// 2.2.1 Writing to two open log files at a time 
   1.255 +// 2.2.1.4 Files are the same 
   1.256 +LOCAL_C void testTwoClientsOneFileL(TBool aValid)
   1.257 +	{
   1.258 +
   1.259 +	if (!aValid)
   1.260 +		test(DeleteFolder(KTestLogFullDir3)==KErrNone);
   1.261 +	else
   1.262 +		CreateFolderL(KTestLogFullDir3);
   1.263 +
   1.264 +	RFileLogger log1;
   1.265 +	test(log1.Connect()==KErrNone);
   1.266 +	RFileLogger log2;
   1.267 +	test(log2.Connect()==KErrNone);
   1.268 +
   1.269 +	log1.CreateLog(KTestLogDir3,KTestLogFileName1,EFileLoggingModeOverwrite);
   1.270 +	test(log1.LastError()==KErrNone);
   1.271 +	test(log1.LogValid()==aValid);
   1.272 +	
   1.273 +	log2.CreateLog(KTestLogDir3,KTestLogFileName1,EFileLoggingModeOverwrite);
   1.274 +	test(log2.LastError()==KErrNone);
   1.275 +	test(log2.LogValid()==aValid);
   1.276 +	
   1.277 +	log1.Write(_L("This is written from log session 1"));
   1.278 +	test(log1.LastError()==KErrNone);
   1.279 +	test(log1.LogValid()==aValid);
   1.280 +	log2.Write(_L("This is written from log session 2"));
   1.281 +	test(log2.LastError()==KErrNone);
   1.282 +	test(log2.LogValid()==aValid);
   1.283 +
   1.284 +	WriteToLog(log1);
   1.285 +	WriteToLog(log2);
   1.286 +
   1.287 +	log1.CloseLog();
   1.288 +	log2.CloseLog();
   1.289 +
   1.290 +	TUint temp;
   1.291 +	if (aValid)
   1.292 +		test(fs.Att(KTestLogFile4,temp)==KErrNone);
   1.293 +	else
   1.294 +		test(fs.Att(KTestLogFile4,temp)==KErrPathNotFound);
   1.295 +
   1.296 +	log1.Close();
   1.297 +	log2.Close();
   1.298 +	}
   1.299 +
   1.300 +// 2.2.4 Invalid operations
   1.301 +//
   1.302 +// Check that buffers larger than KLogBufferSize can be written without error.
   1.303 +LOCAL_C void testInvalidOps1L()
   1.304 +	{
   1.305 +
   1.306 +	CreateFolderL(KTestLogFullDir4);
   1.307 +
   1.308 +	RFileLogger log;
   1.309 +	test(log.Connect()==KErrNone);
   1.310 +
   1.311 +	log.CreateLog(KTestLogDir4,KTestLogFileName1,EFileLoggingModeOverwrite);
   1.312 +	test(log.LastError()==KErrNone);
   1.313 +	test(log.LogValid());
   1.314 +
   1.315 +	TBuf<151> longBuf;
   1.316 +	TChar ch('X');
   1.317 +	longBuf.Fill(ch,151);
   1.318 +	test(longBuf.Length()==151);
   1.319 +	log.Write(longBuf);					// only those chars which will fit will be written
   1.320 +	test(log.LastError()==KErrNone);
   1.321 +
   1.322 +	TBuf8<151> longBuf8;
   1.323 +	longBuf8.Fill(ch,151);
   1.324 +	test(longBuf8.Length()==151);
   1.325 +	log.Write(longBuf8);				// only those chars which will fit will be written
   1.326 +	test(log.LastError()==KErrNone);
   1.327 +
   1.328 +	log.Close();
   1.329 +	}
   1.330 +	
   1.331 +// Check that overlong directory names as well as file names cause an overflow error.
   1.332 +LOCAL_C void testInvalidOps2L()
   1.333 +	{
   1.334 +
   1.335 +	CreateFolderL(KTestLogFullDir4);
   1.336 +
   1.337 +	RFileLogger log;
   1.338 +	test(log.Connect()==KErrNone);
   1.339 +
   1.340 +	TFileName dirName, fileName;
   1.341 +	dirName.SetLength(dirName.MaxLength());
   1.342 +	log.CreateLog(dirName,KTestLogFileName1,EFileLoggingModeOverwrite);
   1.343 +	test(log.LastError()==KErrOverflow);
   1.344 +
   1.345 +	fileName.SetLength(fileName.MaxLength());
   1.346 +	log.CreateLog(KTestLogDir1,fileName,EFileLoggingModeOverwrite);
   1.347 +	test(log.LastError()==KErrOverflow);
   1.348 +
   1.349 +	log.Close();
   1.350 +	}
   1.351 +
   1.352 +// 2.2.3 Access counting 
   1.353 +LOCAL_C void TestAccessCountingL(TBool aValid)
   1.354 +	{
   1.355 +
   1.356 +	if (aValid)
   1.357 +		CreateFolderL(KTestLogFullDir5);
   1.358 +	else
   1.359 +		DeleteFolder(KTestLogFullDir5);
   1.360 +
   1.361 +	RFileLogger log;
   1.362 +	test(log.Connect()==KErrNone);
   1.363 +
   1.364 +	log.CreateLog(KTestLogDir5,KTestLogFileName1,EFileLoggingModeOverwrite);
   1.365 +	test(log.LastError()==KErrNone);
   1.366 +
   1.367 +	log.CloseLog();
   1.368 +
   1.369 +	log.CreateLog(KTestLogDir5,KTestLogFileName1,EFileLoggingModeOverwrite);
   1.370 +	test(log.LastError()==KErrNone);
   1.371 +
   1.372 +	log.CloseLog();
   1.373 +
   1.374 +	log.CreateLog(KTestLogDir5,KTestLogFileName1,EFileLoggingModeOverwrite);
   1.375 +	test(log.LastError()==KErrNone);
   1.376 +
   1.377 +	log.Close();
   1.378 +
   1.379 +// Test closing log twice
   1.380 +
   1.381 +	test(log.Connect()==KErrNone);
   1.382 +
   1.383 +	log.CreateLog(KTestLogDir5,KTestLogFileName1,EFileLoggingModeOverwrite);
   1.384 +	test(log.LastError()==KErrNone);
   1.385 +
   1.386 +	log.CloseLog();
   1.387 +	log.CloseLog();			// timer will be running if file exists
   1.388 +	User::After(6000000);
   1.389 +	log.CloseLog();			// timer will have expired
   1.390 +	log.Close();
   1.391 +
   1.392 +// Test closing session twice
   1.393 +
   1.394 +	test(log.Connect()==KErrNone);
   1.395 +
   1.396 +	log.Close();
   1.397 +	log.Close();			// timer will be running
   1.398 +	TestWaitForShutDown();
   1.399 +	log.Close();			// timer will have expired
   1.400 +	}
   1.401 +
   1.402 +LOCAL_C void doExampleL()
   1.403 +	{
   1.404 +
   1.405 +	User::LeaveIfError(fs.Connect());
   1.406 +
   1.407 +	// delete all files and folders
   1.408 +	DeleteFolder(KTestLogFullDir1);	// ignore return value
   1.409 +	DeleteFolder(KTestLogFullDir2);	// ignore return value
   1.410 +	DeleteFolder(KTestLogFullDir3);	// ignore return value
   1.411 +	DeleteFolder(KTestLogFullDir4);
   1.412 +
   1.413 +	test.Printf(_L("\nDo tests with Folders not existing....\n"));
   1.414 +
   1.415 +	test.Start(_L("Two Sessions logging to different files: PDS-FILELOGGER-UT-4001"));
   1.416 +	testTwoClientsL(EFalse);	// folders do not exist
   1.417 +	
   1.418 +	test.Next(_L("Two sessions logging to one file: PDS-FILELOGGER-UT-4002"));
   1.419 +	testTwoClientsOneFileL(EFalse);		// folders do not exist
   1.420 +	
   1.421 +	test.Next(_L("Static logging: PDS-FILELOGGER-UT-4003"));
   1.422 +	testStaticWrites(EFalse);		// folders do not exist
   1.423 +
   1.424 +	test.Next(_L("Test Closing and Access Counting: PDS-FILELOGGER-UT-4004"));
   1.425 +	TestAccessCountingL(EFalse);
   1.426 +
   1.427 +	test.Printf(_L("\nDo tests with Folders existing....\n"));
   1.428 +
   1.429 +	test.Start(_L("Two Sessions logging to different files: PDS-FILELOGGER-UT-4001"));
   1.430 +	testTwoClientsL(ETrue);		// folders exist
   1.431 +
   1.432 +	test.Next(_L("Two sessions logging to one file: PDS-FILELOGGER-UT-4002"));
   1.433 +	testTwoClientsOneFileL(ETrue);		// folders exist
   1.434 +
   1.435 +	test.Next(_L("Static logging: PDS-FILELOGGER-UT-4003"));
   1.436 +	testStaticWrites(ETrue);		// folders exist
   1.437 +
   1.438 +	test.Next(_L("Invalid Operations: PDS-FILELOGGER-UT-4005"));
   1.439 +	testInvalidOps1L();
   1.440 +	test.Next(_L("Invalid Operations: PDS-FILELOGGER-UT-4006"));
   1.441 +	testInvalidOps2L();
   1.442 +
   1.443 +	test.Next(_L("Test Closing and Access Counting: PDS-FILELOGGER-UT-4004"));
   1.444 +	TestAccessCountingL(ETrue);
   1.445 +
   1.446 +	RFileLogger log;
   1.447 +	test(log.Connect()==KErrNone);
   1.448 +	log.Close();
   1.449 +
   1.450 +	TestWaitForShutDown();
   1.451 +	test.End();
   1.452 +
   1.453 +	fs.Close();
   1.454 +	}
   1.455 +
   1.456 +GLDEF_C TInt E32Main()
   1.457 +	{
   1.458 +
   1.459 +	CTrapCleanup* cleanup=CTrapCleanup::New();
   1.460 +
   1.461 +	__UHEAP_MARK;
   1.462 +	
   1.463 +	test.Title();
   1.464 +
   1.465 +	TRAPD(ret,doExampleL());
   1.466 +	test(ret==KErrNone);
   1.467 +	
   1.468 +    test.End();
   1.469 +	test.Close();
   1.470 +
   1.471 +	__UHEAP_MARKEND;
   1.472 +
   1.473 +	delete cleanup;
   1.474 +	User::Heap().Check();
   1.475 +	return KErrNone;
   1.476 +	}