os/kernelhwsrv/kerneltest/e32test/power/t_power.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of the License "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // e32test\power\t_power.cpp
    15 // Overview:
    16 // Test power down and wakeup event notification
    17 // API Information:
    18 // Power::PowerDown(), Power::RequestWakeupEventNotification()
    19 // Details:
    20 // - Arm a wakeup timer, enable wakeup events, enter standby mode, get woken up.
    21 // - Test RequestWakeupEventNotification(): arm a timer, request notification,
    22 // wait for timer to expire, verify event has been notified, issue another 
    23 // notification request, disable wakeup events, arm another timer, verify 
    24 // wakeup event has not been notified, cancel the notification request and
    25 // close the timer.
    26 // - Verify that a slave process panics because it hasn't and capability.
    27 // - Confirm the number of open handles and pending requests are as expected.
    28 // Platforms/Drives/Compatibility:
    29 // All.
    30 // Assumptions/Requirement/Pre-requisites:
    31 // Failures and causes:
    32 // Base Port information:
    33 // 
    34 //
    35 
    36 #define __E32TEST_EXTENSION__
    37 
    38 #include <e32power.h>
    39 #include <e32test.h>
    40 #include <e32kpan.h>
    41 #include <f32file.h>
    42 #include <e32ldr.h>
    43 #include <e32ldr_private.h>
    44 
    45 LOCAL_D RTest test(_L(" T_POWER "));
    46 
    47 void SetAbsoluteTimeout(RTimer& aTimer, TUint aUs, TRequestStatus& aStatus)
    48 	{
    49 	TTime wakeup;
    50 	wakeup.HomeTime();
    51 	wakeup += TTimeIntervalMicroSeconds(aUs);
    52 	aTimer.At(aStatus, wakeup);
    53 	}
    54 
    55 void PowerTests()
    56 	{
    57 	test.Next(_L("test PowerDown()"));
    58 
    59 	TInt r = Power::PowerDown();
    60 	test (r == KErrNotReady);
    61 
    62 	for (int i = 0; i < 4; ++i)
    63 		{
    64 		test.Printf(_L(" %d "), i);
    65 		// Arm an absolute timer wakeup event after 5 sec
    66 		TRequestStatus absstatus;
    67 		RTimer abstimer;
    68 		r = abstimer.CreateLocal();
    69 		test (r == KErrNone);
    70 		SetAbsoluteTimeout(abstimer, 5000000, absstatus); // 5 sec
    71 		// Go to standby 
    72 		r = Power::EnableWakeupEvents(EPwStandby);
    73 		test (r == KErrNone);
    74 		r = Power::PowerDown();
    75 		test (r == KErrNone);
    76 		User::WaitForRequest(absstatus);
    77 		abstimer.Close();
    78 		}
    79 	test.Printf(_L(" OK\n"));
    80 
    81 	test.Next(_L("test RequestWakeupEventNotification()"));
    82 
    83 		{
    84 		TInt r = Power::EnableWakeupEvents(EPwActive);
    85 		test (r == KErrArgument);
    86 
    87 		// Request wakup event notification and enable wakeup events
    88 		TRequestStatus status;
    89 		Power::RequestWakeupEventNotification(status);
    90 		test(status.Int() == KRequestPending);
    91 		r = Power::EnableWakeupEvents(EPwStandby);
    92 		test (r == KErrNone);
    93 		// Arm an absolute timer wakeup event
    94 		TRequestStatus absstatus;
    95 		RTimer abstimer;
    96 		r = abstimer.CreateLocal();
    97 		test (r == KErrNone);
    98 		SetAbsoluteTimeout(abstimer, 100000, absstatus); // 100ms
    99 		// Wait for the timer
   100 		User::WaitForRequest(absstatus);
   101 		test(absstatus.Int() == KErrNone);
   102 		// Wakup event has to be already notified
   103 		test(status.Int() == KErrNone);
   104 		User::WaitForRequest(status);	// collect it
   105 		// Issue another notification request
   106 		Power::RequestWakeupEventNotification(status);
   107 		test(status.Int() == KRequestPending);
   108 		// Disable wakeup events
   109 		Power::DisableWakeupEvents();
   110 		// Arm another absolute timer wakeup event
   111 		SetAbsoluteTimeout(abstimer, 100000, absstatus); // 100ms
   112 		// Wait for the timer
   113 		User::WaitForRequest(absstatus);
   114 		test(absstatus.Int() == KErrNone);
   115 		// Wakeup event has not to be notified
   116 		test(status.Int() == KRequestPending);
   117 		// Cancel the notification request
   118 		Power::CancelWakeupEventNotification();
   119 		test(status.Int() == KErrCancel);
   120 		User::WaitForRequest(status);	// collect it
   121 		// Cancel again just for fun ...
   122 		Power::CancelWakeupEventNotification();
   123 		test(status.Int() == KErrCancel);
   124 
   125 		abstimer.Close();
   126 		}
   127 	}
   128 
   129 _LIT(KSecuritySlavePath, "t_power_slave.exe");
   130 
   131 void ExecSlave(TUint aArg)
   132 	{
   133 	RProcess proc;
   134 	TInt r = proc.Create(KSecuritySlavePath, TPtrC((TUint16*) &aArg, sizeof(aArg)/sizeof(TUint16)));
   135 	test(r == KErrNone);
   136 	TRequestStatus status;
   137 	proc.Logon(status);
   138 	proc.Resume();
   139 	User::WaitForRequest(status);
   140 	// The slave must panic
   141 	test_Equal(EExitPanic, proc.ExitType());
   142 	test_Equal(EPlatformSecurityTrap, proc.ExitReason());
   143 	CLOSE_AND_WAIT(proc);
   144 	}
   145 
   146 GLDEF_C TInt E32Main()
   147 	{
   148 	test.Title();
   149 	test.Start(_L("Testing"));
   150 
   151 	// Turn off evil lazy dll unloading
   152 	RLoader l;
   153 	test(l.Connect()==KErrNone);
   154 	test(l.CancelLazyDllUnload()==KErrNone);
   155 	l.Close();
   156 
   157 	//
   158 	// Perform the number of iterations specifed by the command line argument.
   159 	//
   160 	// If no arguments - perform two iterations
   161 	//
   162 	TInt iter = 2;
   163 	TInt len = User::CommandLineLength();
   164 	if (len)
   165 		{
   166 		// Copy the command line in a buffer
   167 		HBufC* hb = HBufC::NewMax(len);
   168 		test(hb != NULL);
   169 		TPtr cmd((TUint16*) hb->Ptr(), len);
   170 		User::CommandLine(cmd);
   171 		// Extract the number of iterations
   172 		TLex l(cmd);
   173 		TInt i;
   174 		TInt r = l.Val(i);
   175 		if (r == KErrNone)
   176 			iter = i;
   177 		else
   178 			// strange command - silently ignore
   179 			{} 
   180 		delete hb;
   181 		}
   182 
   183 	test.Printf(_L("Go for %d iterations\n"), iter);
   184 
   185 	while (iter--)
   186 		{
   187 		// Remember the number of open handles. Just for a sanity check ....
   188 		TInt start_thc, start_phc;
   189 		RThread().HandleCount(start_phc, start_thc);
   190 
   191 		PowerTests();
   192 
   193 		test.Start(_L("test platform security"));
   194 		// The slave process must panic because it hasn't any capability 
   195 		if(!PlatSec::IsCapabilityEnforced(ECapabilityPowerMgmt))
   196 			test.Printf(_L("TESTS NOT RUN - PowerMgmt capability isn't enforced on system"));
   197 		else
   198 			{
   199 			test.Next(_L("PowerDown()"));
   200 			ExecSlave(0);
   201 			test.Next(_L("EnableWakeupEvents()"));
   202 			ExecSlave(1);
   203 			test.Next(_L("DisableWakeupEvents()"));
   204 			ExecSlave(2);
   205 			test.Next(_L("RequestWakeupEventNotification()"));
   206 			ExecSlave(3);
   207 			test.Next(_L("CancelWakeupEventNotification()"));
   208 			ExecSlave(4);
   209 			}
   210 		test.End();
   211 
   212 		// Sanity check for open handles
   213 		TInt end_thc, end_phc;
   214 		RThread().HandleCount(end_phc, end_thc);
   215 		test(start_thc == end_thc);
   216 		test(start_phc == end_phc);
   217 			// and also for pending requests ...
   218 		test(RThread().RequestCount() == 0);
   219 		}
   220 
   221 	test.End();
   222 
   223 	return KErrNone;
   224 	}