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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
21 GLDEF_D RTest test(_L("FLOGGER Test Code"));
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");
41 // 2.2.5 Wait for shutdown after closure of session
42 LOCAL_C void TestWaitForShutDown()
46 test.Printf(_L("\nWaiting for FLogger server shutdown"));
50 TFullName name=_L("*");
51 _LIT(serverName,"FLogger server");
52 name.Append(serverName);
53 TFindThread find(name);
54 if(find.Next(name)==KErrNone)
62 test.Printf(_L("\nFLogger Server Shutdown after %d secs\n"), 29-i);
65 LOCAL_C TInt DeleteFolder(const TDesC& aFolder)
67 CFileMan* fileman=NULL;
68 TRAPD(ret,fileman=CFileMan::NewL(fs));
69 ret=fileman->RmDir(aFolder);
71 if (ret==KErrPathNotFound || ret==KErrNotFound)
78 LOCAL_C void CreateFolderL(const TDesC& aFolder)
81 TInt ret=fs.MkDirAll(aFolder);
82 if (ret!=KErrNone && ret!=KErrAlreadyExists)
86 LOCAL_C void WriteToLog(RFileLogger& aLog)
91 aLog.WriteFormat(_L("Writing a number to the log: %d"),num1);
92 test(aLog.LastError()==KErrNone);
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);
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);
113 test(aLog.LastError()==KErrNone);
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);
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);
137 test(aLog.LastError()==KErrNone);
139 // 2.2.1.5 Dump hexadecimal values
141 const TText* hdr=_S("123456");
142 const TText* mgn=_S(" ");
143 const TUint8* ptr=_S8("abcdefghijklmnopqrstuvwxyz");
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);
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)
170 test(DeleteFolder(KTestLogFullDir1)==KErrNone);
172 CreateFolderL(KTestLogFullDir1);
175 test(log1.Connect()==KErrNone);
177 test(log2.Connect()==KErrNone);
179 log1.CreateLog(KTestLogDir1,KTestLogFileName1,EFileLoggingModeOverwrite);
180 test(log1.LastError()==KErrNone);
181 test(log1.LogValid()==aValid);
183 log2.CreateLog(KTestLogDir1,KTestLogFileName2,EFileLoggingModeOverwrite);
184 test(log2.LastError()==KErrNone);
185 test(log2.LogValid()==aValid);
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);
203 test(fs.Att(KTestLogFile1,temp)==KErrNone);
204 test(fs.Att(KTestLogFile2,temp)==KErrNone);
208 test(fs.Att(KTestLogFile1,temp)==KErrPathNotFound);
209 test(fs.Att(KTestLogFile2,temp)==KErrPathNotFound);
216 // 2.2.2 Static appends
217 LOCAL_C void testStaticWrites(TBool aValid)
221 test(DeleteFolder(KTestLogFullDir2)==KErrNone);
224 TRAPD(ret,(CreateFolderL(KTestLogFullDir2)));
228 RFileLogger::Write(KTestLogDir2,KTestLogFileName1,EFileLoggingModeAppend,_L("Writing to the log"));
230 RFileLogger::WriteFormat(KTestLogDir2(),KTestLogFileName1(),EFileLoggingModeAppend,_L("Writing a number to the log: %d"),num1);
232 RFileLogger::WriteFormat(KTestLogDir2(),KTestLogFileName1(),EFileLoggingModeAppend,_L("Writing two numbers to the log: %d, %d"),num1,num2);
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);
238 const TText* hdr=_S("123456");
239 const TText* mgn=_S(" ");
240 const TUint8* ptr=_S8("abcdefghijklmnopqrstuvwxyz");
242 RFileLogger::HexDump(KTestLogDir2(),KTestLogFileName1(),EFileLoggingModeAppend, hdr, mgn, ptr, len);
246 test(fs.Att(KTestLogFile3,temp)==KErrNone);
248 test(fs.Att(KTestLogFile3,temp)==KErrPathNotFound);
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)
257 test(DeleteFolder(KTestLogFullDir3)==KErrNone);
259 CreateFolderL(KTestLogFullDir3);
262 test(log1.Connect()==KErrNone);
264 test(log2.Connect()==KErrNone);
266 log1.CreateLog(KTestLogDir3,KTestLogFileName1,EFileLoggingModeOverwrite);
267 test(log1.LastError()==KErrNone);
268 test(log1.LogValid()==aValid);
270 log2.CreateLog(KTestLogDir3,KTestLogFileName1,EFileLoggingModeOverwrite);
271 test(log2.LastError()==KErrNone);
272 test(log2.LogValid()==aValid);
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);
289 test(fs.Att(KTestLogFile4,temp)==KErrNone);
291 test(fs.Att(KTestLogFile4,temp)==KErrPathNotFound);
297 // 2.2.4 Invalid operations
299 // Check that buffers larger than KLogBufferSize can be written without error.
300 LOCAL_C void testInvalidOps1L()
303 CreateFolderL(KTestLogFullDir4);
306 test(log.Connect()==KErrNone);
308 log.CreateLog(KTestLogDir4,KTestLogFileName1,EFileLoggingModeOverwrite);
309 test(log.LastError()==KErrNone);
310 test(log.LogValid());
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);
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);
328 // Check that overlong directory names as well as file names cause an overflow error.
329 LOCAL_C void testInvalidOps2L()
332 CreateFolderL(KTestLogFullDir4);
335 test(log.Connect()==KErrNone);
337 TFileName dirName, fileName;
338 dirName.SetLength(dirName.MaxLength());
339 log.CreateLog(dirName,KTestLogFileName1,EFileLoggingModeOverwrite);
340 test(log.LastError()==KErrOverflow);
342 fileName.SetLength(fileName.MaxLength());
343 log.CreateLog(KTestLogDir1,fileName,EFileLoggingModeOverwrite);
344 test(log.LastError()==KErrOverflow);
349 // 2.2.3 Access counting
350 LOCAL_C void TestAccessCountingL(TBool aValid)
354 CreateFolderL(KTestLogFullDir5);
356 DeleteFolder(KTestLogFullDir5);
359 test(log.Connect()==KErrNone);
361 log.CreateLog(KTestLogDir5,KTestLogFileName1,EFileLoggingModeOverwrite);
362 test(log.LastError()==KErrNone);
366 log.CreateLog(KTestLogDir5,KTestLogFileName1,EFileLoggingModeOverwrite);
367 test(log.LastError()==KErrNone);
371 log.CreateLog(KTestLogDir5,KTestLogFileName1,EFileLoggingModeOverwrite);
372 test(log.LastError()==KErrNone);
376 // Test closing log twice
378 test(log.Connect()==KErrNone);
380 log.CreateLog(KTestLogDir5,KTestLogFileName1,EFileLoggingModeOverwrite);
381 test(log.LastError()==KErrNone);
384 log.CloseLog(); // timer will be running if file exists
385 User::After(6000000);
386 log.CloseLog(); // timer will have expired
389 // Test closing session twice
391 test(log.Connect()==KErrNone);
394 log.Close(); // timer will be running
395 TestWaitForShutDown();
396 log.Close(); // timer will have expired
399 LOCAL_C void doExampleL()
402 User::LeaveIfError(fs.Connect());
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);
410 test.Printf(_L("\nDo tests with Folders not existing....\n"));
412 test.Start(_L("Two Sessions logging to different files: PDS-FILELOGGER-UT-4001"));
413 testTwoClientsL(EFalse); // folders do not exist
415 test.Next(_L("Two sessions logging to one file: PDS-FILELOGGER-UT-4002"));
416 testTwoClientsOneFileL(EFalse); // folders do not exist
418 test.Next(_L("Static logging: PDS-FILELOGGER-UT-4003"));
419 testStaticWrites(EFalse); // folders do not exist
421 test.Next(_L("Test Closing and Access Counting: PDS-FILELOGGER-UT-4004"));
422 TestAccessCountingL(EFalse);
424 test.Printf(_L("\nDo tests with Folders existing....\n"));
426 test.Start(_L("Two Sessions logging to different files: PDS-FILELOGGER-UT-4001"));
427 testTwoClientsL(ETrue); // folders exist
429 test.Next(_L("Two sessions logging to one file: PDS-FILELOGGER-UT-4002"));
430 testTwoClientsOneFileL(ETrue); // folders exist
432 test.Next(_L("Static logging: PDS-FILELOGGER-UT-4003"));
433 testStaticWrites(ETrue); // folders exist
435 test.Next(_L("Invalid Operations: PDS-FILELOGGER-UT-4005"));
437 test.Next(_L("Invalid Operations: PDS-FILELOGGER-UT-4006"));
440 test.Next(_L("Test Closing and Access Counting: PDS-FILELOGGER-UT-4004"));
441 TestAccessCountingL(ETrue);
444 test(log.Connect()==KErrNone);
447 TestWaitForShutDown();
453 GLDEF_C TInt E32Main()
456 CTrapCleanup* cleanup=CTrapCleanup::New();
462 TRAPD(ret,doExampleL());
471 User::Heap().Check();