os/security/authorisation/userpromptservice/server/test/upstest/upstestobsifoom.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.
15 * Test program exercises the swi observer UPS API.
16 * See individual test functions for more information.
26 #include <e32ldr_private.h>
27 #include "rtestwrapper.h"
29 #include <ups/upsclient.h>
32 using namespace UserPromptService;
34 /** Top-level test object renders stages and confirms conditions. */
35 static RTestWrapper test(_L("UPSTESTOBSIFOOM"));
37 static RUpsManagement sMngmntSession;
39 void PopulateDatabaseL()
41 //test.Start(_L("Populate database"));
43 User::LeaveIfError(session.Connect());
44 CleanupClosePushL(session);
47 RUpsSubsession clientSubsession;
48 TInt r = clientSubsession.Initialise(session, thd);
50 CleanupClosePushL(clientSubsession);
53 destination.CreateL(100);
54 CleanupClosePushL(destination);
56 for(TInt i=0 ; i<10; ++i)
58 TServiceId serviceId = {42};
59 if( i & 1) serviceId.iUid = 43;
61 destination.AppendFormat(_L("destination %x"), i);
63 TUpsDecision dec = EUpsDecNo;
65 clientSubsession.Authorise(EFalse, serviceId, destination, _L8("Opaque data"), dec, rs);
66 User::WaitForRequest(rs);
67 test((rs == KErrNone) || (rs == KErrNoMemory));
70 if(serviceId.iUid == 42)
72 test(dec == EUpsDecYes);
76 test(dec == EUpsDecNo);
81 CleanupStack::PopAndDestroy(&destination);
82 CleanupStack::PopAndDestroy(&clientSubsession);
83 CleanupStack::PopAndDestroy(&session);
89 NONSHARABLE_CLASS(CRequest) : public CActive
92 static CRequest *NewL(RUpsSession &aSession, CTestSwiIf &aParent, TInt aId, TUpsDecision aExpected);
95 CRequest(CTestSwiIf &aParent, TUpsDecision aExpected);
96 void ConstructL(RUpsSession &aSession, TInt aId);
99 virtual void DoCancel();
100 virtual TInt RunError(TInt aError);
104 RUpsSubsession iSubSession;
107 TUpsDecision iExpected;
111 NONSHARABLE_CLASS(CTestSwiIf) : public CActive
114 static CTestSwiIf *NewL();
135 virtual void DoCancel();
136 virtual TInt RunError(TInt aError);
142 RUpsSession iUpsSession;
143 RUpsManagement iManagementSession;
148 CTestSwiIf *CTestSwiIf::NewL()
150 CTestSwiIf *self = new(ELeave) CTestSwiIf;
151 CleanupStack::PushL(self);
153 CleanupStack::Pop(self);
157 CTestSwiIf::CTestSwiIf()
158 : CActive(CActive::EPriorityStandard), iState(EPrePolicyChange)
160 CActiveScheduler::Add(this);
163 _LIT(KCheckIfFailed, "z:\\private\\10283558\\policies\\ups_102836c3_0000002b_checkiffailed.rsc");
164 _LIT(KResourceFileDirC, "c:\\private\\10283558\\policies\\");
165 _LIT(KResourceFileOnC, "c:\\private\\10283558\\policies\\ups_102836c3_0000002b.rsc");
167 void CTestSwiIf::ConstructL()
169 User::LeaveIfError(iFs.Connect());
172 User::LeaveIfError(iUpsSession.Connect());
173 User::LeaveIfError(iManagementSession.Connect());
176 TRequestStatus *rs = &iStatus;
177 *rs = KRequestPending;
178 User::RequestComplete(rs, KErrNone);
183 CTestSwiIf::~CTestSwiIf()
186 iManagementSession.Close();
191 TInt CTestSwiIf::Result()
196 void CTestSwiIf::IncUsers()
201 void CTestSwiIf::DecUsers()
205 ((iState == ETestingComplete) || (iResult != KErrNone)))
207 CActiveScheduler::Stop();
210 void CTestSwiIf::RunL()
212 User::LeaveIfError(iStatus.Int());
215 case EPrePolicyChange:
219 (void)CRequest::NewL(iUpsSession, *this, 8, EUpsDecYes);
220 (void)CRequest::NewL(iUpsSession, *this, 4, EUpsDecYes);
221 (void)CRequest::NewL(iUpsSession, *this, 5, EUpsDecNo);
222 (void)CRequest::NewL(iUpsSession, *this, 2, EUpsDecYes);
223 (void)CRequest::NewL(iUpsSession, *this, 7, EUpsDecNo);
225 (void)iFs.MkDirAll(KResourceFileDirC);
227 CFileMan *fileman = CFileMan::NewL(iFs);
228 CleanupStack::PushL(fileman);
229 TInt r = fileman->Copy(KCheckIfFailed, KResourceFileOnC);
230 User::LeaveIfError(r);
231 CleanupStack::PopAndDestroy(fileman);
234 iManagementSession.NotifyPolicyFilesChanged(rs);
235 iManagementSession.CancelNotifyPolicyFilesChanged();
236 User::WaitForRequest(rs);
237 if(rs.Int() != KErrCancel) User::Leave(rs.Int());
239 iState = EPostPolicyChange;
240 iManagementSession.NotifyPolicyFilesChanged(iStatus);
244 case EPostPolicyChange:
245 // Notify complete, do some more queries
246 (void)CRequest::NewL(iUpsSession, *this, 2, EUpsDecYes);
247 (void)CRequest::NewL(iUpsSession, *this, 8, EUpsDecYes);
248 (void)CRequest::NewL(iUpsSession, *this, 5, EUpsDecYes);
249 (void)CRequest::NewL(iUpsSession, *this, 4, EUpsDecYes);
250 (void)CRequest::NewL(iUpsSession, *this, 3, EUpsDecYes);
253 User::LeaveIfError(iFs.Delete(KResourceFileOnC));
255 iState = ERevertPolicyChange;
256 iManagementSession.NotifyPolicyFilesChanged(iStatus);
260 case ERevertPolicyChange:
261 iState = ETestingComplete;
264 CActiveScheduler::Stop();
268 case ETestingComplete:
273 void CTestSwiIf::DoCancel()
277 case EPrePolicyChange:
279 TRequestStatus *rs = &iStatus;
280 if(*rs == KRequestPending)
282 User::RequestComplete(rs, KErrCancel);
287 case EPostPolicyChange:
288 iManagementSession.CancelNotifyPolicyFilesChanged();
291 case ERevertPolicyChange:
292 iManagementSession.CancelNotifyPolicyFilesChanged();
295 case ETestingComplete:
298 ASSERT(0); // Unknown state
303 TInt CTestSwiIf::RunError(TInt aError)
308 CActiveScheduler::Stop();
313 CRequest *CRequest::NewL(RUpsSession &aSession, CTestSwiIf &aParent, TInt aId, TUpsDecision aExpected)
315 CRequest *self = new(ELeave) CRequest(aParent, aExpected);
316 CleanupStack::PushL(self);
317 self->ConstructL(aSession, aId);
318 CleanupStack::Pop(self);
322 CRequest::CRequest(CTestSwiIf &aParent, TUpsDecision aExpected)
323 : CActive(CActive::EPriorityStandard-1),
327 CActiveScheduler::Add(this);
331 void CRequest::ConstructL(RUpsSession &aSession, TInt aId)
334 User::LeaveIfError(iSubSession.Initialise(aSession, thd));
335 TServiceId serviceId = {42};
336 if( aId & 1) serviceId.iUid = 43;
338 destination.CreateL(100);
339 CleanupClosePushL(destination);
340 destination.AppendFormat(_L("destination %x"), aId);
343 iSubSession.Authorise(EFalse, serviceId, destination, _L8("Opaque data"), iDec, iStatus);
346 CleanupStack::PopAndDestroy(&destination);
349 CRequest::~CRequest()
355 void CRequest::RunL()
357 test((iStatus.Int() == KErrNone) || (iStatus.Int() == KErrNoMemory));
358 if(iStatus.Int() == KErrNone)
360 // Some OOM situations appear to cause us to return NO.
361 test((iDec == iExpected) || (iDec == EUpsDecNo));
366 void CRequest::DoCancel()
370 TInt CRequest::RunError(TInt aError)
372 User::Panic(_L("CRequest::RunError"), aError);
373 /*lint -unreachable*/
379 void TestSwiObserverL()
381 RUpsManagement session;
382 User::LeaveIfError(session.Connect());
383 CleanupClosePushL(session);
385 session.NotifyPluginsMayHaveChangedL();
388 session.NotifyPolicyFilesChanged(rs);
389 User::WaitForRequest(rs);
391 test((rs.Int() == KErrNone) || (rs.Int() == KErrNoMemory));
392 if(rs.Int() == KErrNone)
394 session.CancelNotifyPolicyFilesChanged();
397 TSecureId ourSid(0x10283559);
398 session.DeleteDecisionsForExeL(ourSid);
401 CleanupStack::PopAndDestroy(&session);
405 // -------- entrypoint --------
408 // We need to increase the priority of the thread running the test code to make sure
409 // that asynchronous ups management calls, for example CreateView() , won't finish
410 // before following synchronous cancellation or termination calls, for example:
411 // DeleteDatabaseL() or CancelAndCloseView().
413 TThreadPriority currentPri = thread.Priority();
414 currentPri = (TThreadPriority)((TInt)currentPri+10);
415 thread.SetPriority(currentPri);
417 CActiveScheduler *scheduler = new(ELeave) CActiveScheduler;
418 CActiveScheduler::Install(scheduler);
419 CleanupStack::PushL(scheduler);
421 test.Title(_L("c:\\upstestobsifoom.log"));
422 test.Start(_L(" @SYMTestCaseID:SEC-UPS-OBSIF_OOM-0001 Testing RUpsSession SWI observer IF "));
425 User::LeaveIfError(fs.Connect());
426 CleanupClosePushL(fs);
428 User::LeaveIfError(sMngmntSession.Connect());
429 sMngmntSession.ShutdownServer();
430 sMngmntSession.Close();
432 TBuf<21> notifierConfig(_L("!:\\upsrefnotifier.txt"));
433 notifierConfig[0] = fs.GetSystemDriveChar();
435 TInt lineLength = User::CommandLineLength();
439 // fall through - extra command line arguments are ignored
441 // 2 char arg - Delete DB and run interactive
442 (void) fs.Delete(_L("c:\\Private\\10283558\\database\\ups.db"));
443 // Fall through to also delete notifier config file
445 // 1 char arg - Run interactive, without deleting DB
446 (void) fs.Delete(notifierConfig);
450 // No args - delete DB and run in silent mode
451 (void) fs.Delete(_L("c:\\Private\\10283558\\database\\ups.db"));
453 (void) fs.Delete(notifierConfig);
455 User::LeaveIfError(file.Create(fs, notifierConfig, EFileShareExclusive | EFileWrite));
456 User::LeaveIfError(file.Write(_L8("Always")));
462 User::LeaveIfError(sMngmntSession.Connect());
463 CleanupClosePushL(sMngmntSession);
467 TInt errTmp = KErrNone;
469 TInt passingRuns = 0;
470 static const TInt maxRun = 1000;
471 static const TInt passThreshold = 5;
473 for(run=1; run<=maxRun; ++run)
475 test.Printf(_L("\n\nOBSIF OOM -- Run %d\n"), run);
477 // Make sure the C: policy is deleted.
478 (void)fs.Delete(KResourceFileOnC);
480 // Make sure server is not holding cached values for the C: policy
481 // We could stop/restart the server, but that is very slow...
483 sMngmntSession.NotifyPolicyFilesChanged(rs);
484 User::WaitForRequest(rs);
486 err = sMngmntSession.SetServerHeapFail(run);
487 if(err == KErrNoMemory)
489 // Reinitialisation failed
490 test.Printf(_L("\tReinitialisation failed\n"));
491 sMngmntSession.ResetServerHeapFail();
496 // Failed to set heap fail, maybe the previous loop crashed the server??
497 test.Printf(_L("Failed to set heap fail with error code %d"), err);
505 TRAP(err, t = CTestSwiIf::NewL());
508 CActiveScheduler::Start();
519 TRAP(err, TestSwiObserverL());
523 // Clear the heap fail
524 test.Printf(_L("Reseting heap failure\n"));
525 errTmp = sMngmntSession.ResetServerHeapFail();
526 if((err == KErrServerTerminated) || (errTmp == KErrServerTerminated))
528 test.Printf(_L("\tUPS server died"));
532 if((err == KErrNone) && (errTmp != KErrNone))
547 if(passingRuns > passThreshold) break;
555 sMngmntSession.ShutdownServer();
557 // Close top level session (low level session was closed by
558 // ShutdownServer, but we still need to do the RUpsManagement
560 CleanupStack::PopAndDestroy(&sMngmntSession);
562 (void) fs.Delete(notifierConfig);
563 CleanupStack::PopAndDestroy(&fs);
568 CleanupStack::PopAndDestroy(scheduler);
571 void PanicIfError(TInt r)
575 User::Panic(_L("upstest failed: "), r);
582 Executable entrypoint establishes connection with UPS server
583 and then invokes tests for each functional area.
585 @return Symbian OS error code where KErrNone indicates
586 success and any other value indicates failure.
589 // disable lazy DLL unloading so kernel heap balances at end
591 PanicIfError(l.Connect());
592 PanicIfError(l.CancelLazyDllUnload());
598 // allocating a cleanup stack also installs it
599 CTrapCleanup* tc = CTrapCleanup::New();
607 User::Panic(_L("upstest failed: "), err);