os/security/authorisation/userpromptservice/policies/test/tupspolicies/source/tupsplugins.cpp
First public contribution.
2 * Copyright (c) 2007-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.
19 #include "tupsplugins.h"
21 #include <ecom/ecom.h>
22 #include <ups/upsconst.h>
23 #include <ups/fingerprint.h>
24 #include <ups/upserr.h>
25 #include "cleanuputils.h"
28 using namespace UserPromptService;
30 _LIT(KRequestsSection, "requests");
32 void CTestPlugins::SetupL()
34 Initialise policy library objects.
38 User::LeaveIfError(iFs.Connect());
39 iActiveWaiter = new(ELeave)CActiveWaiter(Logger());
41 // Dummy decision record for ForcePromptL
42 iDecisionRecord = CDecisionRecord::NewL(TSecureId(0), TUid::Null(), TUid::Null(),
43 TSecureId(0), _L8("foo"), KNullDesC8, KNullDesC, EFalse, 0, 0);
46 void CTestPlugins::TearDownL()
48 Destroy policy library objects.
51 iRequests.ResetAndDestroy();
52 iExpectedValues.ResetAndDestroy();
53 delete iDecisionRecord;
61 void CTestPlugins::DoCancel()
65 void CTestPlugins::TestCacheL()
72 ASSERT_TRUE(iConfig.GetInt(KRequestsSection,_L("numrequests"), numRequests));
73 ASSERT_TRUE(numRequests > 0);
75 LoadTestDataL(numRequests);
76 for (TInt i = 0; i < numRequests; ++i)
78 TBuf<16> section(_L("request"));
81 // Is the test expected to fail ?
82 iExpectedError = KErrNone;
83 (void) iConfig.GetInt(section, _L("expectederror"), iExpectedError);
85 if (! iInteractive) GetResponseL(section);
89 INFO_PRINTF2(_L("Running OOM test for request %d"), i);
90 passed &= DoOomRequestL(*iExpectedValues[i], *iRequests[i]);
94 TRAPD(err, passed &= DoRequestL(*iExpectedValues[i], *iRequests[i]));
95 if (err == KErrNoMemory) User::Leave(err);
96 passed &= CheckExpectedError(Logger(), iExpectedError, err);
101 User::Leave(KErrTEFUnitFail);
105 void CTestPlugins::LoadTestDataL(TInt aNumRequests)
107 (void) iConfig.GetBool(KRequestsSection,_L("oom"), iOom);
108 // Gets the scripted prompt response
109 (void) iConfig.GetBool(KRequestsSection,_L("interactive"), iInteractive);
111 // Read all the requests up front
112 for (TInt i = 0; i < aNumRequests; ++i)
114 TBuf<16> section(_L("request"));
115 section.AppendNum(i);
117 GetExpectedValuesL(section);
118 CPromptRequest* req = GetRequestParamsLC(section, iExpectedValues[i]->iClientEntity);
119 iRequests.AppendL(req);
120 CleanupStack::Pop(req);
124 void CTestPlugins::GetResponseL(const TDesC& aSection)
127 TBool ret = iConfig.GetString(aSection, _L("response"), response);
129 iResponse = static_cast<CPolicy::TOptions>(StringToOptionL(response));
132 TBuf<21> name(_L("!:\\upsrefnotifier.txt"));
133 name[0] = iFs.GetSystemDriveChar();
134 User::LeaveIfError(file.Replace(iFs, name, EFileShareExclusive | EFileWrite));
135 CleanupClosePushL(file);
137 HBufC* opt = OptionToString(iResponse).AllocLC();
138 User::LeaveIfError(file.Write(opt->Des().Collapse()));
140 if (! iOom) INFO_PRINTF2(_L("Scripted prompt response = %S"), &OptionToString(iResponse));
141 CleanupStack::PopAndDestroy(2, &file); // file, opt
144 TBool CTestPlugins::DoRequestL(const TExpectedValue& aExpected, const CPromptRequest& aRequest)
148 CPluginManager* pluginManager = CPluginManager::NewL();
149 CleanupStack::PushL(pluginManager);
150 CPolicyCache* policyCache = CPolicyCache::NewL(iFs, _L("\\tups\\tpolicies\\policies\\"));
151 CleanupStack::PushL(policyCache);
153 TBool protectedSid = ((aRequest.ClientSid().iId & 0x80000000) == 0);
154 if (protectedSid != aRequest.IsClientSidProtected())
156 ERR_PRINTF3(_L("Expected value for CPromptRequest::IsProtectedSid() = %d, actual value = %d"),
157 protectedSid, aRequest.IsClientSidProtected());
161 const CPolicy* policy = policyCache->MatchL(aRequest);
162 ASSERT_NOT_NULL(policy);
163 // Attempt to load the policy a second time, should get the same
164 // policy object and policy cache should not leak.
166 ASSERT_EQUALS(policy, policyCache->MatchL(aRequest));
169 // It should also be safe to load the service configuration if we have loaded a single
171 RArray<TServiceConfig> configs;
172 CleanupClosePushL(configs);
173 policyCache->ServiceConfigL(aRequest.ServerSid().iId, configs);
174 ASSERT_TRUE(configs.Count() > 0); // If policy was loaded there must be at least one service config
175 CleanupStack::PopAndDestroy(&configs);
177 passed &= CheckPolicyL(aExpected, policy);
179 if (policy->PromptRequired())
181 // Just verify that GetExtension doesn't crash
182 TAny* extensionPtr(0);
183 CPlugin<CPolicyEvaluator>* policyEvaluator =
184 pluginManager->CreateEvaluatorL(policy->PolicyEvaluator());
185 CleanupStack::PushL(policyEvaluator);
186 policyEvaluator->Imp().GetExtension(0, extensionPtr, NULL);
188 RPointerArray<CFingerprint> fingerprints;
189 CleanupResetAndDestroyPushL(fingerprints);
190 const CClientEntity* clientEntity(0);
191 const TAny* dialogCreatorParams(0);
193 policyEvaluator->Imp().GenerateFingerprints(aRequest, *policy,
194 fingerprints, clientEntity, dialogCreatorParams, iActiveWaiter->iStatus);
195 iActiveWaiter->WaitActive(KErrNone);
197 if (! iOom) INFO_PRINTF3(_L("Policy Evaluator 0x%08x: num fingerprints = %d"),
198 policy->PolicyEvaluator(), fingerprints.Count());
200 if (aExpected.iClientEntity.Length())
204 ERR_PRINTF1(_L("Client entity expected"));
207 else if (clientEntity->Name() != aExpected.iClientEntity)
209 ERR_PRINTF1(_L("Cliententity name mismatch"));
213 else if (aExpected.iClientEntity.Length() == 0 && clientEntity)
215 ERR_PRINTF1(_L("Unexpected client entity"));
219 // Make sure base class implementation for ForcePromptL returns EFalse
220 TUint evaluatorInfo(0);
221 ASSERT_FALSE(policyEvaluator->Imp().CPolicyEvaluator::ForcePromptL(*iDecisionRecord, evaluatorInfo));
223 CPlugin<CDialogCreator>* dialogCreator
224 = pluginManager->CreateDialogCreatorL(policy->DialogCreator());
225 CleanupStack::PushL(dialogCreator);
226 dialogCreator->Imp().GetExtension(0, extensionPtr, NULL);
228 dialogCreator->Imp().PrepareDialog(
229 aRequest, *policy, fingerprints, clientEntity, dialogCreatorParams, iActiveWaiter->iStatus);
230 iActiveWaiter->WaitActive(KErrNone);
232 const CFingerprint* fingerprint(0);
233 CPolicy::TOptions selected;
234 dialogCreator->Imp().DisplayDialog(
235 selected, fingerprint, evaluatorInfo, iActiveWaiter->iStatus);
236 iActiveWaiter->WaitActive(KErrNone);
238 if (! iOom) INFO_PRINTF2(_L("User response: %S"), &OptionToString(selected));
239 if (! iInteractive && selected != iResponse) // don't check response in interactive mode
241 ERR_PRINTF3(_L("Expected user response = %S, actual user response = %S"),
242 &OptionToString(iResponse), &OptionToString(selected));
248 if (! iOom) INFO_PRINTF2(_L("Description for selected fingerprint: %S"), &fingerprint->Description());
250 CleanupStack::PopAndDestroy(3, policyEvaluator); // fingerprint, dialogcreator
254 if (! iOom) INFO_PRINTF2(_L("Policy %d does not require a prompt to be displayed"), policy->Flags());
256 CleanupStack::PopAndDestroy(2, pluginManager); // policyCache
260 void CTestPlugins::GetExpectedValuesL(const TDesC& aSection)
262 TExpectedValue* expected = new(ELeave) TExpectedValue();
263 CleanupStack::PushL(expected);
264 iExpectedValues.AppendL(expected);
265 CleanupStack::Pop(expected);
267 iConfig.GetHex(aSection, _L("expectedflags"), expected->iFlags);
268 iConfig.GetHex(aSection, _L("expectedmajorversion"), expected->iMajorVersion);
269 iConfig.GetHex(aSection, _L("expectedminorversion"), expected->iMinorVersion);
270 iConfig.GetBool(aSection, _L("expectedsilent"), expected->iSilent);
271 iConfig.GetHex(aSection, _L("expectedpolicyevaluator"), expected->iPolicyEvaluator);
274 if (iConfig.GetString(aSection, _L("cliententity"), clientEntity))
276 ASSERT_TRUE(clientEntity.Length() < expected->iClientEntity.MaxLength());
277 expected->iClientEntity.Copy(clientEntity);
281 TBool CTestPlugins::CheckPolicyL(const TExpectedValue& aExpected, const CPolicy* aPolicy)
283 Checks the correct policy was found for the request.
284 @param aPolicy The policy object returned by the policy cache.
285 @return ETrue if successful; otherwise, EFalse is returned.
288 TBool passed = ETrue;
290 if (aExpected.iFlags != aPolicy->Flags())
292 ERR_PRINTF3(_L("Expected flags = 0x%04x, actual flags 0x%04x"),
293 aExpected.iFlags, aPolicy->Flags());
297 if (aExpected.iMajorVersion != aPolicy->MajorVersion())
299 ERR_PRINTF3(_L("Expected major version = %d, actual major version = %d"),
300 aExpected.iMajorVersion, aPolicy->MajorVersion());
304 if (aExpected.iMinorVersion != aPolicy->MinorVersion())
306 ERR_PRINTF3(_L("Expected minor version = %d, actual minor version = %d"),
307 aExpected.iMinorVersion, aPolicy->MinorVersion());
311 if ((TUint)aExpected.iPolicyEvaluator != aPolicy->PolicyEvaluator().iUid)
313 ERR_PRINTF3(_L("Expected policy evaluator = %08x, actual policy evaluator = %08x"),
314 aExpected.iPolicyEvaluator, aPolicy->PolicyEvaluator().iUid)
318 TBool silent = ! aPolicy->PromptRequired();
321 if (! iOom) INFO_PRINTF1(_L("Policy never requires a prompt."));
324 if (silent != aExpected.iSilent)
326 ERR_PRINTF3(_L("Expected value for silent policy = %d, actual = %d"),
327 aExpected.iSilent, silent);
334 CTestSuite* CTestPlugins::CreateSuiteL(const TDesC& aName)
336 Creates the test suite for User Prompt Service ECOM plug-ins
337 @param aName - Suite name
342 AddAsyncTestCase(lTestSuite, _L("TestCacheL"), &CTestPlugins::TestCacheL);
343 AddAsyncTestCase(lTestSuite, _L("TestPluginManagerL"), &CTestPlugins::TestPluginManagerL);
344 AddAsyncTestCase(lTestSuite, _L("TestLengthsL"), &CTestPlugins::TestLengthsL);
348 CPromptRequest* CTestPlugins::GetRequestParamsLC(const TDesC& aSection, const TDesC8& aClientEntity)
350 Gets the data from the CPromptRequest from a section in the config file.
351 @param aSection The name of the section containing the prompt request data.
352 @return The new CPromptRequest object.
356 ASSERT_TRUE(iConfig.GetHex(aSection,_L("clientsid"), clientSid));
359 ASSERT_TRUE(iConfig.GetHex(aSection,_L("clientvid"), clientVid));
362 ASSERT_TRUE(iConfig.GetHex(aSection,_L("serversid"), serverSid));
365 ASSERT_TRUE(iConfig.GetHex(aSection,_L("serviceid"), serviceId));
368 ASSERT_TRUE(iConfig.GetString(aSection,_L("destination"), destination));
370 TBool securityResult;
371 ASSERT_TRUE(iConfig.GetBool(aSection,_L("securityresult"), securityResult));
374 ASSERT_TRUE(iConfig.GetString(aSection,_L("opaquedata"), opaqueData));
376 TThreadId myThreadId;
378 myThreadId = me.Id();
381 TProcessId myProcessId;
383 myProcessId = mep.Id();
387 destinationRb.CreateL(destination);
388 CleanupClosePushL(destinationRb);
391 opaqueDataRb.CreateL(opaqueData.Length() + aClientEntity.Length() + 64);
392 CleanupClosePushL(opaqueDataRb);
393 opaqueDataRb.Copy(opaqueData);
394 if (aClientEntity.Length())
396 opaqueDataRb.Append(_L8("<ce>"));
397 opaqueDataRb.Append(aClientEntity);
398 opaqueDataRb.Append(_L8("</ce>"));
402 CPromptRequest* req =
403 CPromptRequest::NewL(TUid::Uid(clientSid), TUid::Uid(clientVid), myThreadId, myProcessId,
404 TUid::Uid(serverSid), TUid::Uid(serviceId),
405 destinationRb, opaqueDataRb, securityResult);
407 CleanupStack::PopAndDestroy(&opaqueDataRb);
408 CleanupStack::PopAndDestroy(&destinationRb);
410 CleanupStack::PushL(req);
415 void CTestPlugins::TestLengthsL()
417 Tests length constrains in the CFingerprint and CClientEntity classes
420 TInt maxLen = KUpsMaxFingerprintLength + 1;
421 HBufC8* bigBuffer = HBufC8::NewMaxLC(maxLen);
424 _LIT8(K32Bytes, "01234567890123456789012345678901");
425 _LIT(KDescription, "description");
428 CClientEntity* ce(0);
432 if (! iOom) INFO_PRINTF1(_L("Create a fingerprint with an empty value - should pass"));
433 fp = CFingerprint::NewL(emptyBuf, KDescription);
436 if (! iOom) INFO_PRINTF1(_L("Create a fingerprint with value that's too long - should fail"));
437 TRAP(err, fp = CFingerprint::NewL(*bigBuffer, KDescription));
438 ASSERT_EQUALS(err, KErrUpsBadFingerprintLength);
440 if (! iOom) INFO_PRINTF1(_L("Create a fingerprint with 32 bytes of data - should pass"));
441 fp = CFingerprint::NewLC(K32Bytes, KDescription);
442 ASSERT_TRUE(fp->Fingerprint() == K32Bytes);
443 ASSERT_TRUE(fp->Description() == KDescription);
444 CleanupStack::PopAndDestroy(fp);
446 if (! iOom) INFO_PRINTF1(_L("Create a client entity with an empty value - should pass"));
447 ce = CClientEntity::NewLC(emptyBuf);
448 ASSERT_TRUE(ce->Name() == emptyBuf);
449 CleanupStack::PopAndDestroy(ce);
451 if (! iOom) INFO_PRINTF1(_L("Create a client entity with a name that's too long - should fail"));
452 TRAP(err, ce = CClientEntity::NewL(*bigBuffer));
453 ASSERT_EQUALS(err, KErrUpsBadClientEntityLength);
455 if (! iOom) INFO_PRINTF1(_L("Create a client entity with a 32 byte name - should pass"));
456 ce = CClientEntity::NewLC(K32Bytes);
457 ASSERT_TRUE(ce->Name() == K32Bytes);
458 CleanupStack::PopAndDestroy(ce);
460 CleanupStack::PopAndDestroy(bigBuffer);
463 void CTestPlugins::TestPluginManagerL()
465 Creates and destroy UPS plug-ins to test CPluginManager.
469 CPluginManager* pluginManager = CPluginManager::NewL();
470 CleanupStack::PushL(pluginManager);
472 CPlugin<CPolicyEvaluator>* policyEvaluator(0);
473 CPlugin<CDialogCreator>* dialogCreator(0);
475 ASSERT_TRUE(iConfig.GetHex(_L("pluginmanager"),_L("policyevaluatoruid"), uidVal));
476 TUid policyEvaluatorUid = TUid::Uid(uidVal);
478 ASSERT_TRUE(iConfig.GetHex(_L("pluginmanager"),_L("dialogcreatoruid"), uidVal));
479 TUid dialogCreatorUid = TUid::Uid(uidVal);
481 if (! iOom) INFO_PRINTF1(_L("Calling unload with no active plug-ins"));
482 pluginManager->Unload();
483 ASSERT_EQUALS(pluginManager->iPluginCount, 0);
485 if (! iOom) INFO_PRINTF1(_L("Calling unload after creating and destroying a plug-in"));
486 policyEvaluator = pluginManager->CreateEvaluatorL(policyEvaluatorUid);
487 ASSERT_EQUALS(pluginManager->iPluginCount, 1);
488 delete policyEvaluator;
489 ASSERT_EQUALS(pluginManager->iPluginCount, 0);
490 pluginManager->Unload();
491 ASSERT_EQUALS(pluginManager->iPluginCount, 0);
493 if (! iOom) INFO_PRINTF1(_L("Calling unload after deleting last plug-in"));
494 policyEvaluator = pluginManager->CreateEvaluatorL(policyEvaluatorUid);
495 ASSERT_EQUALS(pluginManager->iPluginCount, 1);
496 delete policyEvaluator;
497 ASSERT_EQUALS(pluginManager->iPluginCount, 0);
498 policyEvaluator = pluginManager->CreateEvaluatorL(policyEvaluatorUid);
499 ASSERT_EQUALS(pluginManager->iPluginCount, 1);
500 pluginManager->Unload();
501 ASSERT_EQUALS(pluginManager->iPluginCount, 1);
502 delete policyEvaluator;
503 ASSERT_EQUALS(pluginManager->iPluginCount, 0);
505 INFO_PRINTF1(_L("Policy evaluators and dialog creators"));
506 policyEvaluator = pluginManager->CreateEvaluatorL(policyEvaluatorUid);
507 ASSERT_EQUALS(pluginManager->iPluginCount, 1);
508 CleanupStack::PushL(policyEvaluator);
509 pluginManager->Unload();
510 ASSERT_EQUALS(pluginManager->iPluginCount, 1);
511 dialogCreator = pluginManager->CreateDialogCreatorL(dialogCreatorUid);
512 ASSERT_EQUALS(pluginManager->iPluginCount, 2);
513 CleanupStack::PushL(dialogCreator);
514 CleanupStack::PopAndDestroy(2, policyEvaluator); // dialogCreator
515 ASSERT_EQUALS(pluginManager->iPluginCount, 0);
517 CleanupStack::PopAndDestroy(pluginManager);
520 TBool CTestPlugins::DoOomRequestL(const TExpectedValue& aExpected, const CPromptRequest& aRequest)
525 TBool passed = EFalse;
528 TInt countBefore = 0;
529 for (TInt oomCount = 0; ; oomCount++)
531 INFO_PRINTF2(_L("\n ==== Number of memory allocations %d ===="), oomCount);
534 __UHEAP_SETFAIL(RHeap::EDeterministic, oomCount);
535 countBefore = User::CountAllocCells();
536 TRAPD(error, DoRequestL(aExpected, aRequest));// ----> This is the actual test that runs under OOM conditions.
537 countAfter = User::CountAllocCells();
540 ASSERT_TRUE(error == KErrNone || error == KErrNoMemory);
542 if (error == KErrNone)
545 INFO_PRINTF2(_L("OOM Status %d"),error);
546 INFO_PRINTF1(_L("Test outcome : Passed"));
551 if (countBefore != countAfter)
553 INFO_PRINTF2(_L("OOM Status %d"),error);
554 INFO_PRINTF2(_L("OOM Failed at %d"), oomCount);
558 INFO_PRINTF2(_L("OOM Failed Point status %d"), error);
560 INFO_PRINTF3(_L("Heap alloc count ok: %d final vs %d initial"), countAfter,countBefore);