sl@0: /* sl@0: * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * Test program exercises the skeleton UPS server API. sl@0: * See individual test functions for more information. sl@0: * sl@0: */ sl@0: sl@0: sl@0: /** sl@0: @file sl@0: */ sl@0: sl@0: #include sl@0: #include sl@0: #include "rtestwrapper.h" sl@0: sl@0: #include sl@0: #include sl@0: #include "f32file.h" sl@0: sl@0: using namespace UserPromptService; sl@0: sl@0: /** Top-level test object renders stages and confirms conditions. */ sl@0: static RTestWrapper test(_L("UPSTEST")); sl@0: sl@0: /** sl@0: This session handle is defined at the file level so each individual test sl@0: does not have to connect to the server. sl@0: */ sl@0: static RUpsSession sTestSession; sl@0: /** sl@0: This subsession handle is defined at the file level so each individual test sl@0: does not have to connect to the server and create a subssesion. sl@0: */ sl@0: static RUpsSubsession sTestSubsession; sl@0: sl@0: // -------- open / close session -------- sl@0: sl@0: static void TestOpenCloseSession() sl@0: /** sl@0: Open and close a connection to the UPS server. sl@0: */ sl@0: { sl@0: test.Start(_L("TestOpenCloseSession")); sl@0: sl@0: RUpsSession s; sl@0: TInt r = s.Connect(); sl@0: test(r == KErrNone); sl@0: sl@0: r = s.ShareAuto(); sl@0: test(r == KErrNone); sl@0: sl@0: s.Close(); sl@0: sl@0: test.End(); sl@0: } sl@0: sl@0: // -------- open / close subsession -------- sl@0: sl@0: static void TestOpenCloseSubsession() sl@0: /** sl@0: Open and close a subsession on the UPS server. sl@0: */ sl@0: { sl@0: test.Start(_L("TestOpenCloseSubsession")); sl@0: sl@0: RUpsSubsession ss; sl@0: RThread thd; sl@0: TInt r = ss.Initialise(sTestSession, thd); sl@0: test(r == KErrNone); sl@0: ss.Close(); sl@0: sl@0: test.End(); sl@0: } sl@0: sl@0: // -------- Authorise succeed, cancel -------- sl@0: _LIT8(KOpaqueData, "fred"); sl@0: static void TestAuthoriseL() sl@0: /** sl@0: Launch an asynchronous Authorise request on a UPS subsession. sl@0: Let it complete normally, and also cancel it. sl@0: */ sl@0: { sl@0: test.Start(_L("TestAuthorise")); sl@0: sl@0: TServiceId serviceId = {43}; sl@0: TRequestStatus rs; sl@0: sl@0: sl@0: test.Next(_L("complete normally - no opaque data\n")); sl@0: TUpsDecision dec = EUpsDecYes; sl@0: sTestSubsession.Authorise(EFalse, serviceId, KNullDesC, dec, rs); sl@0: test(dec == EUpsDecYes); // not changed yet sl@0: User::WaitForRequest(rs); sl@0: test(rs == KErrNone); sl@0: test(dec == EUpsDecNo); sl@0: sl@0: test.Next(_L("cancel - empty opaque data\n")); sl@0: dec = EUpsDecYes; // ensure changed sl@0: sTestSubsession.Authorise(EFalse, serviceId, KNullDesC, KNullDesC8, dec, rs); sl@0: test(dec == EUpsDecYes); // not changed yet sl@0: test.Printf(_L("About to cancel - current status is %d"), rs.Int()); sl@0: sTestSubsession.CancelPrompt(); sl@0: User::WaitForRequest(rs); sl@0: test(rs == KErrCancel); sl@0: test(dec == EUpsDecYes); // not changed sl@0: sl@0: test.Next(_L("Opaque data\n")); sl@0: dec = EUpsDecYes; // ensure changed sl@0: sTestSubsession.Authorise(EFalse, serviceId, _L("opaque data test"), KOpaqueData, dec, rs); sl@0: test(dec == EUpsDecYes); // not changed yet sl@0: User::WaitForRequest(rs); sl@0: test(rs == KErrNone); sl@0: test(dec == EUpsDecNo); sl@0: sl@0: sl@0: test.Next(_L("cancel when no outstanding - harmless\n")); sl@0: sTestSubsession.CancelPrompt(); sl@0: sl@0: test.Next(_L("cancel/close when sub session never created - harmless\n")); sl@0: RUpsSubsession uninitialisedCancel; sl@0: uninitialisedCancel.CancelPrompt(); sl@0: uninitialisedCancel.Close(); sl@0: sl@0: test.End(); sl@0: } sl@0: sl@0: static void TestFlurryL() sl@0: /** sl@0: Launch multiple requests sl@0: */ sl@0: { sl@0: test.Start(_L("TestFlurry")); sl@0: sl@0: RThread thd; sl@0: RUpsSubsession testSubsession1; sl@0: testSubsession1.Initialise(sTestSession, thd); sl@0: RUpsSubsession testSubsession2; sl@0: testSubsession2.Initialise(sTestSession, thd); sl@0: RUpsSubsession testSubsession3; sl@0: testSubsession3.Initialise(sTestSession, thd); sl@0: RUpsSubsession testSubsession4; sl@0: testSubsession4.Initialise(sTestSession, thd); sl@0: RUpsSubsession testSubsession5; sl@0: testSubsession5.Initialise(sTestSession, thd); sl@0: RUpsSubsession testSubsession6; sl@0: testSubsession6.Initialise(sTestSession, thd); sl@0: sl@0: TServiceId serviceId = {43}; sl@0: TRequestStatus rs1; sl@0: TRequestStatus rs2; sl@0: TRequestStatus rs3; sl@0: TRequestStatus rs4; sl@0: TRequestStatus rs5; sl@0: TRequestStatus rs6; sl@0: sl@0: // complete normally - no opaque data sl@0: TUpsDecision dec1 = EUpsDecYes; sl@0: TUpsDecision dec2 = EUpsDecYes; sl@0: TUpsDecision dec3 = EUpsDecYes; sl@0: TUpsDecision dec4 = EUpsDecYes; sl@0: TUpsDecision dec5 = EUpsDecYes; sl@0: TUpsDecision dec6 = EUpsDecYes; sl@0: testSubsession1.Authorise(EFalse, serviceId, _L("req1"), dec1, rs1); sl@0: testSubsession2.Authorise(EFalse, serviceId, _L("reqX"), dec2, rs2); sl@0: testSubsession3.Authorise(EFalse, serviceId, _L("req2"), dec3, rs3); sl@0: testSubsession4.Authorise(EFalse, serviceId, _L("reqX"), dec4, rs4); sl@0: testSubsession5.Authorise(EFalse, serviceId, _L("req3"), dec5, rs5); sl@0: testSubsession6.Authorise(EFalse, serviceId, _L("req4"), dec6, rs6); sl@0: #if 0 sl@0: // Will change immediately in non-interactive testing sl@0: test(dec1 == EUpsDecYes); // not changed yet sl@0: test(dec2 == EUpsDecYes); // not changed yet sl@0: test(dec3 == EUpsDecYes); // not changed yet sl@0: test(dec4 == EUpsDecYes); // not changed yet sl@0: test(dec5 == EUpsDecYes); // not changed yet sl@0: test(dec6 == EUpsDecYes); // not changed yet sl@0: #endif sl@0: User::After(1000); sl@0: sl@0: User::WaitForRequest(rs1); sl@0: sl@0: User::WaitForRequest(rs2); sl@0: User::WaitForRequest(rs4); sl@0: sl@0: User::WaitForRequest(rs3); sl@0: User::WaitForRequest(rs5); sl@0: User::WaitForRequest(rs6); sl@0: test(rs1 == KErrNone); sl@0: test(rs2 == KErrNone); sl@0: test(rs3 == KErrNone); sl@0: test(rs4 == KErrNone); sl@0: test(rs5 == KErrNone); sl@0: test(rs6 == KErrNone); sl@0: test(dec1 == EUpsDecNo); sl@0: test(dec2 == EUpsDecNo); sl@0: test(dec3 == EUpsDecNo); sl@0: test(dec4 == EUpsDecNo); sl@0: test(dec5 == EUpsDecNo); sl@0: test(dec6 == EUpsDecNo); sl@0: sl@0: test.End(); sl@0: } sl@0: sl@0: // -------- RUpsSubsession -------- sl@0: _LIT(KSayYes,"SayYes"); sl@0: static void TestRUpsSubsession() sl@0: /** sl@0: Attempt query with server checks passed and without sl@0: */ sl@0: { sl@0: // sl@0: // Tests for RUpsSubsession sl@0: // sl@0: RThread thd; sl@0: sl@0: test.Start(_L("Testing RUpsSubsession")); sl@0: RUpsSubsession clientSubsession; sl@0: TInt r = clientSubsession.Initialise(sTestSession, thd); sl@0: test(r == KErrNone); sl@0: sl@0: test.Next(_L("Query with server checks passed")); sl@0: TServiceId serviceId = {42}; sl@0: TUpsDecision dec = EUpsDecNo; sl@0: TRequestStatus rs; sl@0: // Query saying our checks were ok, expect to get decision set to EUpsDecYes sl@0: clientSubsession.Authorise(ETrue, serviceId, _L("Destination"), _L8("Opaque data"), dec, rs); sl@0: User::WaitForRequest(rs); sl@0: test(rs == KErrNone); sl@0: test(dec == EUpsDecYes); sl@0: sl@0: sl@0: sl@0: test.Next(_L("Try closing client subsession before it is really created")); sl@0: clientSubsession.Close(); sl@0: sl@0: test.Next(_L("Re-\"create\" client subsession")); sl@0: r = clientSubsession.Initialise(sTestSession, thd); sl@0: test(r == KErrNone); sl@0: sl@0: test.Next(_L("Query with server checks failed, ie query UPS, expect fail")); sl@0: // Query saying our checks failed, should talk to UPS and change decision to EUpsDecNo sl@0: dec = EUpsDecYes; sl@0: clientSubsession.Authorise(EFalse, serviceId, KNullDesC, dec, rs); sl@0: User::WaitForRequest(rs); sl@0: test(rs == KErrNone); sl@0: test(dec == EUpsDecNo); sl@0: sl@0: sl@0: test.Next(_L("Query with server checks failed, ie query UPS, special destination, expect yes")); sl@0: // Query saying our checks failed, should talk to UPS and change decision to EUpsDecNo sl@0: dec = EUpsDecYes; sl@0: clientSubsession.Authorise(EFalse, serviceId, KSayYes(), dec, rs); sl@0: User::WaitForRequest(rs); sl@0: test(rs == KErrNone); sl@0: test(dec == EUpsDecYes); sl@0: sl@0: sl@0: clientSubsession.Close(); sl@0: test.End(); sl@0: } sl@0: sl@0: TInt ThreadFunction(TAny *) sl@0: { sl@0: return KErrNone; sl@0: } sl@0: sl@0: static void TestRUpsSubsessionDeathL() sl@0: { sl@0: RThread thd; sl@0: TRequestStatus thdStatus; sl@0: User::LeaveIfError(thd.Create(_L("MyThread"), ThreadFunction, 4096, 4096, 4096, 0, EOwnerThread)); sl@0: // thd.SetHandle(666); sl@0: thd.Rendezvous(thdStatus); sl@0: thd.Kill(KErrAbort); sl@0: User::WaitForRequest(thdStatus); sl@0: sl@0: test.Start(_L("Testing RUpsSubsession")); sl@0: RUpsSubsession clientSubsession; sl@0: TInt r = clientSubsession.Initialise(sTestSession, thd); sl@0: test(r == KErrNone); sl@0: sl@0: test.Next(_L("Query with dead thread id")); sl@0: TServiceId serviceId = {43}; sl@0: TUpsDecision dec = EUpsDecYes; sl@0: TRequestStatus rs; sl@0: thd.Close(); sl@0: clientSubsession.Authorise(EFalse, serviceId, _L("req1"), dec, rs); sl@0: User::WaitForRequest(rs); sl@0: test(rs == KErrNone); sl@0: test(dec == EUpsDecNo); sl@0: sl@0: sl@0: clientSubsession.Close(); sl@0: test.End(); sl@0: } sl@0: sl@0: static void TestRUpsManagementL() sl@0: /** sl@0: Attempt to delete database sl@0: */ sl@0: { sl@0: test.Start(_L("Testing RUpsManagement")); sl@0: RThread thd; sl@0: TRequestStatus rs; sl@0: sl@0: // Create filter sl@0: TServiceId serviceId = {43}; sl@0: CDecisionFilter *filter = CDecisionFilter::NewLC(); sl@0: filter->SetClientSid(thd.SecureId(), EEqual); sl@0: filter->SetServerSid(thd.SecureId(), EEqual); sl@0: filter->SetServiceId(serviceId, EEqual); sl@0: sl@0: RUpsSubsession clientSubsession; sl@0: TInt r = clientSubsession.Initialise(sTestSession, thd); sl@0: test(r == KErrNone); sl@0: sl@0: test.Next(_L("Open management session")); sl@0: RUpsManagement mngmnt; sl@0: r = mngmnt.Connect(); sl@0: test(r == KErrNone); sl@0: User::LeaveIfError(r); sl@0: sl@0: test.Next(_L("View create - then delete DB")); sl@0: mngmnt.CreateView(*filter, rs); sl@0: sl@0: test.Next(_L("Delete database")); sl@0: TRAP(r, mngmnt.DeleteDatabaseL()); sl@0: test(r == KErrNone); sl@0: sl@0: test.Next(_L("Now see what view create completed with....")); sl@0: User::WaitForRequest(rs); sl@0: test(rs.Int() == KErrAbort); sl@0: sl@0: test.Next(_L("Add entry to new database")); sl@0: TUpsDecision dec = EUpsDecYes; sl@0: clientSubsession.Authorise(EFalse, serviceId, _L("DB delete 1"), _L8("Opaque data"), dec, rs); sl@0: User::WaitForRequest(rs); sl@0: test(rs == KErrNone); sl@0: test(dec == EUpsDecNo); sl@0: sl@0: dec = EUpsDecYes; sl@0: clientSubsession.Authorise(EFalse, serviceId, _L("DB delete 2"), _L8("Opaque data"), dec, rs); sl@0: User::WaitForRequest(rs); sl@0: test(rs == KErrNone); sl@0: test(dec == EUpsDecNo); sl@0: sl@0: sl@0: test.Next(_L("View create - immediate cancel")); sl@0: mngmnt.CreateView(*filter, rs); sl@0: mngmnt.CancelAndCloseView(); sl@0: User::WaitForRequest(rs); sl@0: test(rs.Int() == KErrCancel); sl@0: sl@0: mngmnt.UpdateDecision(TUint32(-23), ETrue, rs); sl@0: mngmnt.CancelUpdateDecision(); sl@0: User::WaitForRequest(rs); sl@0: test(rs.Int() == KErrCancel); sl@0: sl@0: mngmnt.UpdateDecision(TUint32(-23), ETrue, rs); sl@0: User::WaitForRequest(rs); sl@0: test(rs.Int() == KErrNotFound); sl@0: sl@0: test.Next(_L("View create - when busy")); sl@0: TRequestStatus rs2; sl@0: mngmnt.CreateView(*filter, rs); sl@0: mngmnt.CreateView(*filter, rs2); sl@0: User::WaitForRequest(rs2); sl@0: test(rs2.Int() == KErrServerBusy); sl@0: User::WaitForRequest(rs); sl@0: test(rs.Int() == KErrNone); sl@0: mngmnt.CancelAndCloseView(); sl@0: sl@0: test.Next(_L("View create - iterate through it")); sl@0: mngmnt.CreateView(*filter, rs); sl@0: sl@0: User::WaitForRequest(rs); sl@0: test(rs.Int() == KErrNone); sl@0: sl@0: CleanupStack::PopAndDestroy(filter); sl@0: sl@0: CDecisionRecord *record = 0; sl@0: r = KErrNone; sl@0: TInt recordCount = 0; sl@0: while(r == KErrNone) sl@0: { sl@0: TRAP(r, record = mngmnt.NextMatchL()); sl@0: if(record == 0) sl@0: { sl@0: break; sl@0: } sl@0: test(r == KErrNone); sl@0: if(r == KErrNone) sl@0: { sl@0: ++recordCount; sl@0: CleanupStack::PushL(record); sl@0: CDecisionFilter *exactFilter = CDecisionFilter::NewLC(record->iClientSid, sl@0: record->iEvaluatorId, sl@0: record->iServiceId, sl@0: record->iServerSid, sl@0: record->iFingerprint, sl@0: record->iClientEntity, sl@0: record->iMajorPolicyVersion); sl@0: sl@0: sl@0: mngmnt.UpdateDecision(record->iRecordId, ETrue, rs); sl@0: User::WaitForRequest(rs); sl@0: test(rs.Int() == KErrNone); sl@0: TRAP(r, mngmnt.RemoveDecisionsL(*exactFilter)); sl@0: test(r == KErrNone); sl@0: sl@0: CleanupStack::PopAndDestroy(exactFilter); sl@0: CleanupStack::PopAndDestroy(record); sl@0: } sl@0: sl@0: }; sl@0: test(recordCount == 2); sl@0: sl@0: TRAP(r, record = mngmnt.NextMatchL()); sl@0: test((r == KErrNone) && (record == 0)); sl@0: sl@0: mngmnt.CancelAndCloseView(); sl@0: sl@0: test.Next(_L("Close management session and clientSubsession")); sl@0: mngmnt.Close(); sl@0: clientSubsession.Close(); sl@0: sl@0: test.End(); sl@0: } sl@0: sl@0: void TestSwiObserverSecurityL() sl@0: { sl@0: test.Start(_L("Testing swi observer functions do not work from here...")); sl@0: sl@0: TInt r; sl@0: sl@0: RUpsManagement session; sl@0: User::LeaveIfError(session.Connect()); sl@0: CleanupClosePushL(session); sl@0: sl@0: TUid ourSid; sl@0: ourSid.iUid = 0x10283559; sl@0: TRAP(r, session.DeleteDecisionsForExeL(ourSid)); sl@0: test(r == KErrPermissionDenied); sl@0: sl@0: TRAP(r, session.NotifyPluginsMayHaveChangedL()); sl@0: test(r == KErrPermissionDenied); sl@0: sl@0: TRequestStatus rs; sl@0: session.NotifyPolicyFilesChanged(rs); sl@0: User::WaitForRequest(rs); sl@0: sl@0: test(rs.Int() == KErrPermissionDenied); sl@0: sl@0: session.CancelNotifyPolicyFilesChanged(); sl@0: sl@0: CleanupStack::PopAndDestroy(&session); sl@0: sl@0: test.End(); sl@0: } sl@0: sl@0: // -------- entrypoint -------- sl@0: sl@0: sl@0: void MainL() sl@0: { sl@0: test.Title(_L("c:\\upstest.log")); sl@0: test.Start(_L(" @SYMTestCaseID:SEC-UPS-0001 Testing RUpsSubsession ")); sl@0: sl@0: RFs fs; sl@0: User::LeaveIfError(fs.Connect()); sl@0: CleanupClosePushL(fs); sl@0: sl@0: TBuf<21> notifierConfig(_L("!:\\upsrefnotifier.txt")); sl@0: notifierConfig[0] = fs.GetSystemDriveChar(); sl@0: sl@0: TBuf<35> database(_L("!:\\Private\\10283558\\database\\ups.db")); sl@0: database[0] = fs.GetSystemDriveChar(); sl@0: sl@0: TInt lineLength = User::CommandLineLength(); sl@0: switch(lineLength) sl@0: { sl@0: default: sl@0: case 2: sl@0: (void) fs.Delete(database); sl@0: // Fall through to also delete notifier config file sl@0: case 1: sl@0: (void) fs.Delete(notifierConfig); sl@0: break; sl@0: case 0: sl@0: { sl@0: // No args so run in silent mode sl@0: (void) fs.Delete(database); sl@0: (void) fs.Delete(notifierConfig); sl@0: RFile file; sl@0: User::LeaveIfError(file.Create(fs, notifierConfig, EFileShareExclusive | EFileWrite)); sl@0: User::LeaveIfError(file.Write(_L8("Never"))); sl@0: file.Close(); sl@0: break; sl@0: } sl@0: } sl@0: sl@0: //RThread ourThread; sl@0: //ourThread.SetPriority(EPriorityMore); sl@0: //ourThread.Close(); sl@0: // User::SetProcessCritical(User::ESystemCritical); sl@0: // User::SetCritical(User::ESystemCritical); sl@0: TestOpenCloseSession(); sl@0: sl@0: sl@0: TInt r = sTestSession.Connect(); sl@0: test(r == KErrNone); sl@0: User::LeaveIfError(r); sl@0: sl@0: TestRUpsSubsessionDeathL(); sl@0: sl@0: TestOpenCloseSubsession(); sl@0: sl@0: TestSwiObserverSecurityL(); sl@0: sl@0: RThread thd; sl@0: r = sTestSubsession.Initialise(sTestSession, thd); sl@0: test(r == KErrNone); sl@0: User::LeaveIfError(r); sl@0: sl@0: TestFlurryL(); sl@0: sl@0: TestAuthoriseL(); sl@0: sl@0: sTestSubsession.Close(); sl@0: sl@0: TestRUpsSubsession(); sl@0: sl@0: TestRUpsManagementL(); sl@0: sl@0: sTestSession.ShutdownServer(); sl@0: sl@0: // Close top level session (low level session was closed by sl@0: // ShutdownServer, but we still need to do the RUpsSession sl@0: // cleanup). sl@0: sTestSession.Close(); sl@0: sl@0: (void) fs.Delete(notifierConfig); sl@0: CleanupStack::PopAndDestroy(&fs); sl@0: sl@0: test.End(); sl@0: test.Close(); sl@0: } sl@0: sl@0: void PanicIfError(TInt r) sl@0: { sl@0: if(r != KErrNone) sl@0: { sl@0: User::Panic(_L("upstest failed: "), r); sl@0: } sl@0: } sl@0: sl@0: sl@0: TInt E32Main() sl@0: /** sl@0: Executable entrypoint establishes connection with UPS server sl@0: and then invokes tests for each functional area. sl@0: sl@0: @return Symbian OS error code where KErrNone indicates sl@0: success and any other value indicates failure. sl@0: */ sl@0: { sl@0: // disable lazy DLL unloading so kernel heap balances at end sl@0: RLoader l; sl@0: PanicIfError(l.Connect()); sl@0: PanicIfError(l.CancelLazyDllUnload()); sl@0: l.Close(); sl@0: sl@0: __UHEAP_MARK; sl@0: //__KHEAP_MARK; sl@0: sl@0: // allocating a cleanup stack also installs it sl@0: CTrapCleanup* tc = CTrapCleanup::New(); sl@0: if (tc == 0) sl@0: return KErrNoMemory; sl@0: sl@0: TRAPD(err, MainL()); sl@0: if(err != KErrNone) sl@0: { sl@0: User::Panic(_L("upstest failed: "), err); sl@0: } sl@0: delete tc; sl@0: sl@0: //__KHEAP_MARKEND; sl@0: __UHEAP_MARKEND; sl@0: sl@0: sl@0: return KErrNone; sl@0: } sl@0: