Update contrib.
2 * Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of the License "Eclipse Public License v1.0"
6 * which accompanies this distribution, and is available
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
22 #include "MTestInterface.h"
23 #include <ecom/ecom.h>
25 RTest test(_L("CT Framework Tests"));
32 const TInt gInterfaceA = 0x101f4e50;
33 const TInt gInterfaceB = 0x101f4e51;
34 const TInt gInterfaceC = 0x101f4e52;
36 const TInt gAttribute1 = 0x101f4e4a;
37 const TInt gAttribute2 = 0x101f4e4b;
39 const TInt gImplementation6 = 0x101f4e4c;
40 const TInt gImplementation5 = 0x101f4e4d;
42 class CTestConsole:public CConsoleBase
46 static CTestConsole* NewL(CConsoleBase* aCon);
47 TInt Create(const TDesC16& aTitle,TSize aSize) {return iCon->Create(aTitle,aSize);};
48 void Read(TRequestStatus& aStatus) {iCon->Read(aStatus);};
49 void ReadCancel(void) {iCon->ReadCancel();};
50 void Write(const TDesC16& aString);
51 TPoint CursorPos(void) const {return iCon->CursorPos();};
52 void SetCursorPosAbs(const TPoint& aPos) {iCon->SetCursorPosAbs(aPos);};
53 void SetCursorPosRel(const TPoint& aPos) {iCon->SetCursorPosRel(aPos);};
54 void SetCursorHeight(TInt aHeight) {iCon->SetCursorHeight(aHeight);};
55 void SetTitle(const TDesC16& aTitle) {iCon->SetTitle(aTitle);};
56 void ClearScreen(void) {iCon->ClearScreen();};
57 void ClearToEndOfLine(void) {iCon->ClearToEndOfLine();};
58 TSize ScreenSize(void) const {return iCon->ScreenSize();};
59 TKeyCode KeyCode(void) const {return iCon->KeyCode();};
60 TUint KeyModifiers(void) const {return iCon->KeyModifiers();};
62 void SetLogFile(RFile* aFile);
69 CTestConsole* CTestConsole::NewL(CConsoleBase* aCon)
73 self=new (ELeave) CTestConsole;
79 CTestConsole::CTestConsole(void):CConsoleBase()
84 CTestConsole::~CTestConsole(void)
94 void CTestConsole::Write(const TDesC16& aString)
100 if ((iFile)&&(gLogging))
103 TPtr8 ptr(space,200);
109 void CTestConsole::SetLogFile(RFile* aFile)
115 template <class T> class TTestArray
118 TTestArray(T* aArray, TInt aCount);
121 static TInt Count(const CBase* aThat);
122 static const TAny* Get(const CBase* aThat, TInt aIndex);
128 template <class T> TTestArray<T>::TTestArray(T* aArray, TInt aCount)
129 : iArray(aArray), iCount(aCount)
133 template <class T> TInt TTestArray<T>::Count(const CBase* aThat)
135 return reinterpret_cast<const TTestArray*>(aThat)->iCount;
138 template <class T> const TAny* TTestArray<T>::Get(const CBase* aThat, TInt aIndex)
140 return &reinterpret_cast<const TTestArray*>(aThat)->iArray[aIndex];
143 template <class T> TArray<T> TTestArray<T>::Array()
145 return TArray<T>(Count, Get, reinterpret_cast<CBase*>(this));
148 /** A filter that includes nothing (as a random easy-to-write test filter */
149 class TNoTokenTypes : public MCTTokenTypeFilter
152 /** Always returns EFalse. */
153 virtual TBool Accept(const CCTTokenTypeInfo& aTokenType) const;
157 TBool TNoTokenTypes::Accept(const CCTTokenTypeInfo&) const
162 class CStopScheduler : public CActive
170 void CStopScheduler::RunL()
172 CActiveScheduler::Stop();
175 CStopScheduler::CStopScheduler()
176 : CActive(EPriorityIdle)
178 CActiveScheduler::Add(this);
180 TRequestStatus* r = &iStatus;
181 User::RequestComplete(r, KErrNone);
184 TInt TokenTypeInfoListTestsL()
186 test.Printf(_L("1.1,Testing getting all token types,"));
187 RCPointerArray<CCTTokenTypeInfo> myTokenTypes;
188 CleanupClosePushL(myTokenTypes);
190 CCTTokenTypeInfo::ListL(myTokenTypes);
192 if (myTokenTypes.Count() < 6)
194 CleanupStack::PopAndDestroy();
195 test.Printf(_L("FAILED\r\n"));
199 myTokenTypes.ResetAndDestroy();
200 test.Printf(_L("PASSED\r\n"));
202 test.Printf(_L("1.2,Testing user-supplied filter,"));
203 TNoTokenTypes filter;
204 CCTTokenTypeInfo::ListL(myTokenTypes, filter);
206 if (myTokenTypes.Count() > 0)
208 CleanupStack::PopAndDestroy();
209 test.Printf(_L("FAILED\r\n"));
213 myTokenTypes.ResetAndDestroy();
214 test.Printf(_L("PASSED\r\n"));
216 test.Printf(_L("1.3,Testing finding filters matching 1 interface,"));
218 TUid aa = {gInterfaceA};
219 User::LeaveIfError(a.Append(aa));
220 CleanupClosePushL(a);
221 TCTFindTokenTypesByInterface findA(a.Array());
222 CCTTokenTypeInfo::ListL(myTokenTypes, findA);
223 if (myTokenTypes.Count() != 4)
225 CleanupStack::PopAndDestroy(2);
226 test.Printf(_L("FAILED\r\n"));
229 TInt findAResults[] = {1,4,5,6};
230 TInt flag[] = {1,1,1,1};
231 for (TInt ii = 0; ii < 4; ii++)
235 TBuf<20> matchString;
236 _LIT(format, "Test Token Type %d");
237 matchString.Format(format, findAResults[iii]);
238 TBuf<20> idisplay_name= myTokenTypes[ii]->Label();
240 if (matchString.Compare(idisplay_name))
249 matchString.Format(format, findAResults[iii+1]);
250 if (matchString.Compare(idisplay_name))
259 matchString.Format(format, findAResults[iii+2]);
260 if (matchString.Compare(idisplay_name))
269 matchString.Format(format, findAResults[iii+3]);
270 if (matchString.Compare(idisplay_name))
281 CleanupStack::PopAndDestroy(2);
282 test.Printf(_L("FAILED\r\n"));
287 myTokenTypes.ResetAndDestroy();
288 test.Printf(_L("PASSED\r\n"));
290 test.Printf(_L("1.5,Testing finding filters matching 2 interfaces,"));
291 TUid aAndB[] = {{gInterfaceA}, {gInterfaceB}};
292 TTestArray<TUid> aAndBArray(aAndB, 2);
293 TCTFindTokenTypesByInterface findAAndB(aAndBArray.Array());
294 CCTTokenTypeInfo::ListL(myTokenTypes, findAAndB);
295 if (myTokenTypes.Count() != 2)
297 CleanupStack::PopAndDestroy(2);
298 test.Printf(_L("FAILED\r\n"));
301 TInt findAAndBResults[] = {4,6};
302 TInt flag1[] = {1,1};
303 for (TInt jj = 0; jj < 2; jj++)
307 TBuf<20> matchString;
308 _LIT(format, "Test Token Type %d");
309 matchString.Format(format, findAAndBResults[jjj]);
310 TBuf<20> idisplay_name = myTokenTypes[jj]->Label();
311 if (matchString.Compare(idisplay_name))
320 matchString.Format(format, findAAndBResults[jjj+1]);
321 if (matchString.Compare(idisplay_name))
332 CleanupStack::PopAndDestroy(2);
333 test.Printf(_L("FAILED\r\n"));
338 // NOTE No ResetAndDestroy
339 test.Printf(_L("PASSED\r\n"));
341 test.Printf(_L("1.6,Testing appending interface infos,"));
342 CCTTokenTypeInfo::ListL(myTokenTypes, findA);
343 if (myTokenTypes.Count() != 6)
345 CleanupStack::PopAndDestroy(2);
346 test.Printf(_L("FAILED\r\n"));
349 TInt flag2[] = {1,1,1,1};
350 for (TInt kk = 0; kk < 4; kk++)
354 TBuf<20> matchString;
355 _LIT(format, "Test Token Type %d");
356 matchString.Format(format, findAResults[kkk]);
357 TBuf<20> idisplay_name = myTokenTypes[kk+2]->Label();
358 if (matchString.Compare(idisplay_name))
367 matchString.Format(format, findAResults[kkk+1]);
368 if (matchString.Compare(idisplay_name))
377 matchString.Format(format, findAResults[kkk+2]);
378 if (matchString.Compare(idisplay_name))
387 matchString.Format(format, findAResults[kkk+3]);
388 if (matchString.Compare(idisplay_name))
399 CleanupStack::PopAndDestroy(2);
400 test.Printf(_L("FAILED\r\n"));
405 test.Printf(_L("PASSED\r\n"));
407 myTokenTypes.ResetAndDestroy();
408 test.Printf(_L("1.7,Testing filtering by interface and attribute,"));
409 TCTTokenTypeAttribute att = {{gAttribute1}, 1};
410 RArray<TCTTokenTypeAttribute> attArray(sizeof(TCTTokenTypeAttribute), &att,
412 TCTFindTokenTypesByInterfaceAndAttribute findAWithAtt1(a.Array(),
414 CCTTokenTypeInfo::ListL(myTokenTypes, findAWithAtt1);
415 if (myTokenTypes.Count() != 1)
417 CleanupStack::PopAndDestroy(2);
418 test.Printf(_L("FAILED\r\n"));
421 _LIT(KMatch, "Test Token Type 6");
422 if (myTokenTypes[0]->Label() != KMatch)
424 CleanupStack::PopAndDestroy(2);
425 test.Printf(_L("FAILED\r\n"));
428 test.Printf(_L("PASSED\r\n"));
430 CleanupStack::PopAndDestroy(2);
434 TInt TokenTypeInfoTestsL()
436 test.Printf(_L("2.1,Testing token type for tests,"));
437 RCPointerArray<CCTTokenTypeInfo> myTokenTypes;
438 CleanupClosePushL(myTokenTypes);
439 TUid aABndC[] = {{gInterfaceA}, {gInterfaceB}, {gInterfaceC}};
440 TTestArray<TUid> aABndCArray(aABndC, 3);
441 TCTFindTokenTypesByInterface findABAndC(aABndCArray.Array());
442 CCTTokenTypeInfo::ListL(myTokenTypes, findABAndC);
443 if (myTokenTypes.Count() != 1)
445 CleanupStack::PopAndDestroy();
446 test.Printf(_L("FAILED\r\n"));
449 test.Printf(_L("PASSED\r\n"));
451 test.Printf(_L("2.2,Test UID function,"));
452 CCTTokenTypeInfo* info = myTokenTypes[0];
453 if (info->Type().iUid != gImplementation6)
455 CleanupStack::PopAndDestroy();
456 test.Printf(_L("FAILED\r\n"));
459 test.Printf(_L("PASSED\r\n"));
461 test.Printf(_L("2.3,Test getting an array of interfaces,"));
462 RArray<TUid> interfaces = info->Interfaces();
463 if (interfaces.Count() != 3)
465 CleanupStack::PopAndDestroy();
466 test.Printf(_L("FAILED\r\n"));
469 TUid refInterfaces[] = {{gInterfaceA}, {gInterfaceB}, {gInterfaceC}};
470 for (TInt ii = 0; ii < 3; ii++)
472 if (interfaces[ii] != refInterfaces[ii])
474 CleanupStack::PopAndDestroy();
475 test.Printf(_L("FAILED\r\n"));
479 test.Printf(_L("PASSED\r\n"));
481 test.Printf(_L("2.4,Test getting an array of attributes,"));
482 RArray<TCTTokenTypeAttribute> attributes = info->Attributes();
483 if (attributes.Count() != 2)
485 CleanupStack::PopAndDestroy();
486 test.Printf(_L("FAILED\r\n"));
489 TCTTokenTypeAttribute refAttributes[] =
490 {{{gAttribute2}, 2}, {{gAttribute1}, 1}};
491 for (TInt jj = 0; jj < 2; jj++)
493 if (attributes[jj].iUID != refAttributes[jj].iUID ||
494 attributes[jj].iVal != refAttributes[jj].iVal)
496 CleanupStack::PopAndDestroy();
497 test.Printf(_L("FAILED\r\n"));
501 test.Printf(_L("PASSED\r\n"));
503 test.Printf(_L("2.5,Test getting the label,"));
504 _LIT(KLabel, "Test Token Type 6");
505 if (info->Label() != KLabel)
507 CleanupStack::PopAndDestroy();
508 test.Printf(_L("FAILED\r\n"));
511 test.Printf(_L("PASSED\r\n"));
513 test.Printf(_L("2.6,Test getting the type of the token type,"));
514 TUid impl6 = {gImplementation6};
515 if (info->Type() != impl6)
517 CleanupStack::PopAndDestroy();
518 test.Printf(_L("FAILED\r\n"));
521 test.Printf(_L("PASSED\r\n"));
523 CleanupStack::PopAndDestroy();
528 TInt TestTokenTypeL(MCTTokenType* aTokenType, TInt aNum)
530 test.Printf(_L("3.4.1,Getting token info,"));
531 RCPointerArray<HBufC> tokenInfo;
532 CleanupClosePushL(tokenInfo);
533 TRequestStatus status = KRequestPending;
534 aTokenType->List(tokenInfo, status);
535 // Cancel call is pointelss as it does nothing in the test
536 // plugin. But it won't do any harm to check it doesn't explode.
537 aTokenType->CancelList();
538 User::WaitForRequest(status);
539 if (status.Int() != KErrNone || tokenInfo.Count() != 1)
541 CleanupStack::PopAndDestroy();
542 test.Printf(_L("FAILED\r\n"));
545 test.Printf(_L("PASSED\r\n"));
547 test.Printf(_L("3.4.1,Checking token type name,"));
549 _LIT(KLabelFormat, "Test Token Type %d");
550 buf.Format(KLabelFormat, aNum);
551 if (aTokenType->Label().Compare(buf))
553 CleanupStack::PopAndDestroy();
554 test.Printf(_L("FAILED\r\n"));
557 test.Printf(_L("PASSED\r\n"));
559 test.Printf(_L("3.4.2,Checking token type Type UID,"));
560 TUid uid = {gImplementation5 + 5 - aNum};
561 if (aTokenType->Type() != uid)
563 CleanupStack::PopAndDestroy();
564 test.Printf(_L("FAILED\r\n"));
567 test.Printf(_L("PASSED\r\n"));
569 test.Printf(_L("3.4.3,Checking token name,"));
570 _LIT(KFormat, "Test Token %d");
571 buf.Format(KFormat, aNum);
572 if (buf.Compare(*tokenInfo[0]))
574 CleanupStack::PopAndDestroy();
575 test.Printf(_L("FAILED\r\n"));
578 test.Printf(_L("PASSED\r\n"));
580 test.Printf(_L("3.4.4,Opening token,"));
581 status = KRequestPending;
583 aTokenType->OpenToken(*tokenInfo[0], token, status);
584 // Cancel call will fail as token isn't opened asynchronous. Check
585 // that the reference counting still works.
586 aTokenType->CancelOpenToken();
587 User::WaitForRequest(status);
588 if (status.Int() != KErrNone)
590 CleanupStack::PopAndDestroy();
591 test.Printf(_L("FAILED\r\n"));
595 test.Printf(_L("PASSED\r\n"));
597 test.Printf(_L("3.4.5,Opening token by handle,"));
598 status = KRequestPending;
599 TCTTokenHandle handle(aTokenType->Type(), 1);
600 aTokenType->OpenToken(handle, token, status);
601 User::WaitForRequest(status);
602 if (status.Int() != KErrNone)
604 CleanupStack::PopAndDestroy();
605 test.Printf(_L("FAILED\r\n"));
608 test.Printf(_L("PASSED\r\n"));
610 test.Printf(_L("3.4.6,Checking token's TokenType() function,"));
611 if (&token->TokenType() != aTokenType)
613 CleanupStack::PopAndDestroy();
614 test.Printf(_L("FAILED\r\n"));
617 test.Printf(_L("PASSED\r\n"));
619 _LIT(KVersion, "The Ultimate Version");
620 _LIT(KSerial, "Serial No. 1");
621 _LIT(KManufacturer, "ACME Corporation");
623 test.Printf(_L("3.4.6.1,Checking token's Information() function,"));
624 if (token->Information(MCTToken::EVersion) != KVersion ||
625 token->Information(MCTToken::ESerialNo) != KSerial ||
626 token->Information(MCTToken::EManufacturer) != KManufacturer)
628 CleanupStack::PopAndDestroy();
629 test.Printf(_L("FAILED\r\n"));
632 test.Printf(_L("PASSED\r\n"));
634 test.Printf(_L("3.4.7,Registering for removal notification,"));
635 // This is another test to check that an API doesn't crash. We
636 // call the base NotifyOnRemoval/CancelNotify functions and check
637 // they do nothing! There's no point in creating derived versions
638 // that do something as that would be testing the test plugin, not
640 TRequestStatus removalStatus;
641 token->NotifyOnRemoval(removalStatus);
642 token->CancelNotify();
643 test.Printf(_L("PASSED\r\n"));
645 test.Printf(_L("3.4.9,Testing cancellation of async interface opening,"));
646 MTestInterface* interface;
647 MCTTokenInterface* temp;
648 TUid intB = {gInterfaceB};
649 token->GetInterface(intB, temp, status);
650 token->CancelGetInterface();
651 test.Printf(_L("PASSED\r\n"));
653 test.Printf(_L("3.4.10,Opening an interface,"));
654 status = KRequestPending;
655 TUid intC = {gInterfaceC};
656 token->GetInterface(intC, temp, status);
657 interface = static_cast<MTestInterface*>(temp);
658 // Cancel call is pointelss as it does nothing in the test
659 // plugin. But it won't do any harm to check it doesn't explode.
660 token->CancelGetInterface();
661 CleanupReleasePushL(*interface);
663 User::WaitForRequest(status);
664 if (status.Int() != KErrNone)
666 CleanupStack::PopAndDestroy(2);
667 test.Printf(_L("FAILED\r\n"));
670 test.Printf(_L("PASSED\r\n"));
672 test.Printf(_L("3.4.11,Checking interface name,"));
673 if (buf.Compare(interface->Label()))
675 CleanupStack::PopAndDestroy(2);
676 test.Printf(_L("FAILED\r\n"));
679 test.Printf(_L("PASSED\r\n"));
681 test.Printf(_L("3.4.12,Getting an object,"));
682 MTestObject* object = interface->ObjectL();
684 test.Printf(_L("PASSED\r\n"));
686 CleanupStack::PopAndDestroy(2);
690 TInt TokenTypeTestsL()
692 test.Printf(_L("3.1,Getting token types for tests,"));
694 RCPointerArray<CCTTokenTypeInfo> myTokenTypes;
695 CleanupClosePushL(myTokenTypes);
696 TUid aABndC[] = {{gInterfaceA}, {gInterfaceB}, {gInterfaceC}};
697 TTestArray<TUid> aABandCArray(aABndC, 3);
698 TCTFindTokenTypesByInterface findABAndC(aABandCArray.Array());
699 CCTTokenTypeInfo::ListL(myTokenTypes, findABAndC);
700 if (myTokenTypes.Count() != 1)
702 CleanupStack::PopAndDestroy();
703 test.Printf(_L("FAILED\r\n"));
706 test.Printf(_L("PASSED\r\n"));
708 test.Printf(_L("3.2,Loading token type 6,"));
709 MCTTokenType* token6 = MCTTokenType::NewL(*myTokenTypes[0], fs);
712 CleanupStack::PopAndDestroy();
713 test.Printf(_L("FAILED\r\n"));
716 CleanupReleasePushL(*token6);
717 test.Printf(_L("PASSED\r\n"));
719 TInt ret = TestTokenTypeL(token6, 6);
722 CleanupStack::PopAndDestroy(2);
726 test.Printf(_L("3.3,Loading token type 5,"));
727 TUid UID5 = {gImplementation5};
728 MCTTokenType* token5 = MCTTokenType::NewL(UID5, fs);
731 CleanupStack::PopAndDestroy(2);
732 test.Printf(_L("FAILED\r\n"));
735 CleanupReleasePushL(*token5);
736 test.Printf(_L("PASSED\r\n"));
738 ret = TestTokenTypeL(token5, 5);
739 CleanupStack::PopAndDestroy(3);
744 TInt MemoryTestL(TInt (*aFnToTest)())
747 for (TInt ii = 1; ; ii++)
750 test.Printf(_L("."));
752 test.Printf(_L("*"));
754 test.Printf(_L("\r\n"));
757 __UHEAP_FAILNEXT(ii);
758 TRAPD(err,aFnToTest());
760 REComSession::FinalClose();
762 User::Heap().Check();
764 if (err != KErrNoMemory)
766 test.Printf(_L("\r\n"));
775 CActiveScheduler* as = new(ELeave) CActiveScheduler;
776 CActiveScheduler::Install(as);
781 ret = TokenTypeInfoListTestsL();
782 REComSession::FinalClose();
786 test.Printf(_L("1.9,ERROR %d in Info List test,FAILED\r\n"),ret);
791 test.Printf(_L("1.9,Info List test,PASSED\r\n"),ret);
794 ret = TokenTypeInfoTestsL();
795 REComSession::FinalClose();
799 test.Printf(_L("2.9,ERROR %d in Info test,FAILED\r\n"),ret);
804 test.Printf(_L("2.9,Info test,PASSED\r\n"),ret);
808 ret = TokenTypeTestsL();
809 REComSession::FinalClose();
813 test.Printf(_L("3.9,ERROR %d in token test,FAILED\r\n"),ret);
818 test.Printf(_L("3.9,token test,PASSED\r\n"),ret);
821 ret = MemoryTestL(TokenTypeInfoListTestsL);
824 test.Printf(_L("4.9,ERROR %d in Info List memory test,FAILED\r\n"),ret);
829 test.Printf(_L("4.9,Info List memory test,PASSED\r\n"),ret);
831 ret = MemoryTestL(TokenTypeTestsL);
834 test.Printf(_L("5.9,ERROR %d in Token Type memory test,FAILED\r\n"),ret);
839 test.Printf(_L("5.9,Token Type memory test,PASSED\r\n"),ret);
842 test.Printf(_L("%d tests failed out of 44 hardcoded\r\n"), errors);
847 test.Printf(_L("%d TESTS FAILED\r\n"),errors);
851 test.Printf(_L("ALL TESTS PASSED\r\n"));
857 GLDEF_C TInt E32Main(void)
860 CTrapCleanup* cleanup;
861 cleanup=CTrapCleanup::New();
862 test.Start(_L(" @SYMTestCaseID:SEC-CRYPTOTOKENS-CTFRAMEWORK-0001 Starting token framework tests\r\n"));
863 CTestConsole* con=NULL;
864 TRAPD(ret,con=CTestConsole::NewL(test.Console()));
868 User::LeaveIfError(fs.Connect());
870 file=new (ELeave) RFile;
871 TDriveUnit sysDrive (fs.GetSystemDrive());
872 TDriveName driveName(sysDrive.Name());
873 TBuf<64> logFile (driveName);
874 logFile.Append(_L("\\T_CTFrameworkLog.txt"));
875 User::LeaveIfError(file->Replace(fs, logFile, EFileShareAny|EFileWrite));
876 con->SetLogFile(file);
878 test.SetConsole(con);
882 test.Printf(_L("Unexpected leave\r\n"));