os/security/authorisation/userpromptservice/examples/integration/tmsgapp/tmsgapp.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     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".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description: 
    15 * Example Code
    16 *
    17 */
    18 
    19 
    20 #include "tmsgapp.h"
    21 #include <e32debug.h>
    22 #include <f32file.h>
    23 
    24 // UPS client library is only included to allow the test application
    25 // to reset its decision records.
    26 #include <ups/upsclient.h>	
    27 
    28 _LIT(KTo, "+442071541000");
    29 _LIT_SECURE_ID(KMySecureId, 0x01000003);
    30 
    31 // CAlarm ********************************************************************
    32 inline CAlarm::CAlarm() :CTimer(-1) {CActiveScheduler::Add(this);}
    33 inline void CAlarm::ConstructL() {CTimer::ConstructL();}
    34 
    35 void CAlarm::RunL() 
    36 	{
    37 	iObserver->WakeupL();
    38 	}
    39 
    40 void CAlarm::After(MAlarmObserver* aObserver, TTimeIntervalMicroSeconds32 aInterval)
    41 	{
    42 	iObserver = aObserver;
    43 	CTimer::After(aInterval);
    44 	}
    45 
    46 // CSendMessages *************************************************************
    47 CSendMessages::CSendMessages() : CActive(EPriorityStandard) 
    48 	{
    49 	CActiveScheduler::Add(this);
    50 	}
    51 
    52 CSendMessages::~CSendMessages() 
    53 	{
    54 	Deque();
    55 	iMsgCon.Close();
    56 	}
    57 
    58 CSendMessages* CSendMessages::NewLC()
    59 	{
    60 	CSendMessages* self = new(ELeave) CSendMessages();
    61 	CleanupStack::PushL(self);
    62 	self->ConstructL();
    63 	return self;
    64 	}
    65 
    66 void CSendMessages::ConstructL()
    67 	{
    68 	iAlarm.ConstructL();
    69 	User::LeaveIfError(iMsgCon.Connect());
    70 	TRequestStatus* status = &iStatus;
    71 	*status = KRequestPending;
    72 	SetActive();
    73 	User::RequestComplete(status, KErrNone);
    74 	}
    75 
    76 TInt CSendMessages::RunError(TInt /* aError */)
    77 	{
    78 	CActiveScheduler::Stop(); // Non-recoverable error
    79 	return KErrNone;
    80 	}
    81 
    82 void CSendMessages::DoCancel()
    83 	{
    84 	iMsgCon.CancelSendMsg();	
    85 	iAlarm.Cancel();
    86 	}
    87 
    88 void CSendMessages::WakeupL()
    89 /**
    90  * Timeout the pending message.
    91  */
    92 	{
    93 	iMsgCon.CancelSendMsg();
    94 	}
    95 
    96 void CSendMessages::RunL()
    97 /**
    98  * Send a series of test messages and cancellations that should trigger
    99  * user prompts.
   100  * All test cases timeout after KDefaultTimeout to enable automated tests
   101  */
   102 	{
   103 	TInt err = iStatus.Int();
   104 	if (iStatus != KErrNone && iStatus != KErrCancel && iStatus != KErrPermissionDenied) 
   105 		{
   106 		User::Leave(err);
   107 		}
   108 	iAlarm.Cancel();
   109 	
   110 	RDebug::Printf("Test %d completed with error %d", iTestNum, 0);
   111 		
   112 	++iTestNum;	// move on to next test case	
   113 	switch (iTestNum)
   114 		{
   115 		case 1:
   116 			{
   117 			// Send message and wait for user prompt to complete			
   118 			_LIT(KBody, "Hello");
   119 			iMsgCon.SendMsg(KTo, KBody, iStatus);
   120 			SetActive();
   121 			iAlarm.After(this, KDefaultTimeout);
   122 			break;
   123 			}
   124 		case 2:
   125 			{
   126 			// Send a message and cancel straight away, should be no user prompt
   127 			_LIT(KBody, "Hello - Cancel immediate");
   128 			iMsgCon.SendMsg(KTo, KBody, iStatus);
   129 			SetActive();
   130 			iAlarm.After(this, 1);
   131 			break;
   132 			}
   133 		case 3:
   134 			{
   135 			// Send a message and attempt to cancel whilst the user prompt is being displayed.
   136 			_LIT(KBody, "Hello - Cancel 5 secs");
   137 			iMsgCon.SendMsg(KTo, KBody, iStatus);
   138 			SetActive();
   139 			iAlarm.After(this, 5 * 1000 * 1000);
   140 			break;
   141 			}
   142 		case 4:
   143 			{
   144 			// Force the message server to exit
   145 			iMsgCon.Close();
   146 			_LIT(KInfo, "Waiting for messageserver to exit");
   147 			User::InfoPrint(KInfo);
   148 			User::After(5 * 1000 * 1000);	
   149 			User::LeaveIfError(iMsgCon.Connect());	// reconnect
   150 
   151 			// Send a message to the new server instance
   152 			_LIT(KBody, "Hello again");
   153 			iMsgCon.SendMsg(KTo, KBody, iStatus);
   154 			SetActive();
   155 			iAlarm.After(this, KDefaultTimeout);
   156 			break;
   157 			}
   158 		default:
   159 			// All done
   160 			CActiveScheduler::Stop();
   161 			break;
   162 		}	
   163 	}
   164 
   165 void DeleteOldDecisionsL()
   166 /**
   167  * Delete all decision records for this test application
   168  */
   169 	{
   170 	UserPromptService::RUpsManagement m;
   171 	CleanupClosePushL(m);
   172 	User::LeaveIfError(m.Connect());
   173 	UserPromptService::CDecisionFilter* f = UserPromptService::CDecisionFilter::NewLC();
   174 	f->SetClientSid(KMySecureId, UserPromptService::EEqual);
   175 	m.RemoveDecisionsL(*f);
   176 	CleanupStack::PopAndDestroy(2, &m); // m, f
   177 	}
   178 
   179 void MainL()
   180 	{
   181 	// Create active scheduler, reset UPS and start tests
   182 	CActiveScheduler* s=new(ELeave) CActiveScheduler;	
   183 	CleanupStack::PushL(s);
   184 	CActiveScheduler::Install(s);
   185 	DeleteOldDecisionsL();	// reset decision database
   186 	CSendMessages* test = CSendMessages::NewLC();
   187 	s->Start();
   188 	CleanupStack::PopAndDestroy(2, s); // s, test
   189 	
   190 	// Add log file for automated test environment
   191 	RFs fs;
   192 	User::LeaveIfError(fs.Connect());
   193 	CleanupClosePushL(fs);
   194 	RFile log;
   195 	CleanupClosePushL(log);
   196 	User::LeaveIfError(log.Replace(fs, _L("c:\\tmsgapp.log"), EFileShareAny|EFileWrite));
   197 	User::LeaveIfError(log.Write(_L8("\n\n0 tests failed out of 1\n")));
   198 	CleanupStack::PopAndDestroy(2, &fs);
   199 	}
   200 
   201 GLDEF_C TInt E32Main()
   202 	{
   203 	CTrapCleanup* cleanup = CTrapCleanup::New();
   204 	if(cleanup == NULL)
   205 		{
   206 		return KErrNoMemory;
   207 		}
   208 	TRAP_IGNORE(MainL());
   209 	delete cleanup;
   210 	return KErrNone;
   211 	}
   212