os/graphics/windowing/windowserver/test/tauto/TBUFFERSECURITY.CPP
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2006-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 "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 // DEF075471 buffer security test
    15 // 
    16 //
    17 
    18 /**
    19  @file
    20  @test
    21  @internalComponent - Internal Symbian test code
    22 */
    23 
    24 #include "TBUFFERSECURITY.H"
    25 #include "graphics/windowserverconstants.h"
    26 
    27 //Set this #define in buffersecurity.h to add extra logging to this test case (useful when debugging a test fail)
    28 //#define _TBUFS_LOGGING
    29 
    30 //Set this #define in buffersecurity.h to initiate a long running soak test, this should be done periodically
    31 //#define _TBUFS_TEST_SOAK_TEST
    32 
    33 
    34 
    35 LOCAL_C TInt TestWsThreadFunc(TAny* aPtr)
    36 	{
    37 	TTestThreadData* data = (TTestThreadData*)aPtr;
    38 	RWsSession ws;
    39 
    40 	TInt err=ws.Connect();
    41 	if (err!=KErrNone)
    42 		return(err);
    43 	ws.TestWrite(ws.WsHandle(), data->iOpCode, data->iData, data->iDataLength);
    44 	ws.Flush();
    45 	ws.Close();
    46 
    47 	return(KErrNone);
    48 	}
    49 
    50 LOCAL_C TInt TestAnimDllThreadFunc(TInt aInt, TAny* /*aPtr*/)
    51 	{
    52 	TBool useTestWrite = (TBool)aInt;
    53 
    54 	RWsSession ws;
    55 	TInt err=ws.Connect();
    56 	if (err != KErrNone)
    57 		return (err);
    58 	if (useTestWrite)
    59 		{
    60 		TUint32 data[2];
    61 		data[0] = 200;
    62 		data[1] = 4;
    63 		ws.TestWrite(ws.WsHandle(), EWsClOpCreateAnimDll, REINTERPRET_CAST(TUint8*,data), 8);
    64 		ws.Flush();
    65 		}
    66 	else
    67 		{
    68 		RAnimDll animDll(ws);
    69 		const TPtrC	filename(REINTERPRET_CAST(TUint16*,4),200);
    70 		animDll.Load(filename);
    71 		animDll.Close();
    72 		}
    73 	ws.Close();
    74 
    75 	return(KErrNone);
    76 	}
    77 
    78 TInt RTestIpcSession::Connect()
    79 	{
    80 	TVersion v(KWservMajorVersionNumber,KWservMinorVersionNumber,KWservBuildVersionNumber);
    81 	TInt err = CreateSession(KWSERVServerName,v);
    82 	if (err == KErrNone)
    83 		{
    84 		err=iWsHandle=SendReceive(EWservMessInit,TIpcArgs());
    85 		}
    86 	return err;
    87 	}
    88 
    89 TInt RTestIpcSession::SendBadBuffer()
    90 	{
    91 	TIpcArgs ipcArgs;
    92 	TUint32 iData[2];
    93 	iData[0] = 200;
    94 	iData[1] = 4;
    95 	ipcArgs.Set(KBufferMessageSlot,&iData);
    96 	return SendReceive(EWservMessCommandBuffer,ipcArgs);
    97 	}
    98 
    99 LOCAL_C TInt TestIpcThreadFunc(TInt /*aInt*/, TAny* /*aPtr*/)
   100 	{
   101 	RTestIpcSession server;
   102 
   103 	TInt handle=server.Connect();
   104 	if (handle >= KErrNone)
   105 		server.SendBadBuffer();
   106 
   107 	return(KErrNone);
   108 	}
   109 
   110 
   111 CTBufferSecurity::CTBufferSecurity(CTestStep* aStep):
   112 	CTWsGraphicsBase(aStep)
   113 	{
   114 	}
   115 
   116 CTBufferSecurity::~CTBufferSecurity()
   117 	{
   118 	}
   119 
   120 void CTBufferSecurity::ConstructL()
   121 	{
   122 	RProperty securityTesting;
   123 	TInt err=securityTesting.Attach(KUidWServSecurityTesting,EWServSecTestBufferSecurity);
   124 	User::LeaveIfError(err);
   125 	err=securityTesting.Define(KUidWServSecurityTesting,EWServSecTestBufferSecurity,RProperty::EInt,KAllowAllPolicy,KWriteDeviceDataMgmtPolicy);
   126 	if (err!=KErrAlreadyExists)
   127     	User::LeaveIfError(err);
   128 	TInt value = ETrue;
   129 	err = securityTesting.Set(value);
   130 	User::LeaveIfError(err);
   131 	}
   132 
   133 void CTBufferSecurity::TestWsBufferL(TInt aOpCode, TUint aDataFill, TBool aEightBit)
   134 	{
   135 	// create a new thread in which to test each of the TWsClientOpCode values
   136 	// so if the thread is panicked the test will not fail.
   137 	RThread testThread;
   138 	TTestThreadData data;
   139 	TRequestStatus status;
   140 	TUint8 storeData[KTestDataMax];
   141 	TTestDataStore store;
   142 	store.any=storeData;
   143 
   144 	//collect initial values
   145 	for (TInt count = 0; count < KTestDataMax; count++)
   146 		storeData[count] = 0;
   147 	switch (aOpCode)
   148 		{
   149 		case EWsClOpHeapSetFail:
   150 			store.heapSetFail->type = RAllocator::ENone;
   151 			break;
   152 		case EWsClOpSetPointerCursorArea:
   153 			store.cursorArea->area = TheClient->iWs.PointerCursorArea();
   154 			break;
   155 		case EWsClOpRawEvent:
   156 			//skip switch off (would need a timer to turn it back on again)
   157 			if (aDataFill == TRawEvent::ESwitchOff)
   158 				aDataFill = TRawEvent::ENone;
   159 			break;
   160 #if defined(__WINS__)
   161 		case EWsClOpSimulateXyInput:
   162 			*store.xyInputType = EXYInputMouse;
   163 			break;
   164 #endif
   165 		}
   166 
   167 	data.iOpCode = aOpCode;
   168 	if (aEightBit)
   169 		{
   170 		for (TInt count = 0; count < KTestDataMax; count++)
   171 			data.iData[count] = (TUint8)aDataFill;
   172 		}
   173 	else
   174 		{
   175 		for (TInt count = 0; count < KTestDataMax32; count++)
   176 			data.iData32[count] = aDataFill;
   177 		}
   178 	data.iDataLength = KTestDataMax;
   179 	_LIT(KThreadNameFormat,"BufSecTestWsThread-%d-%d");
   180 	HBufC* threadName = HBufC::NewLC(KThreadNameFormat().Size() + 32);
   181 	TPtr threadNamePtr(threadName->Des());
   182 	threadNamePtr.Format(KThreadNameFormat(), aOpCode, aDataFill);
   183 	TInt err = testThread.Create(threadNamePtr, TestWsThreadFunc,KDefaultStackSize,KPanicThreadHeapSize,KPanicThreadHeapSize,(TAny*)&data,EOwnerThread);
   184 
   185 	if (err != KErrNone)
   186 		{
   187 		User::After(100000);
   188 		err = testThread.Create(threadNamePtr,TestWsThreadFunc,KDefaultStackSize,KPanicThreadHeapSize,KPanicThreadHeapSize,(TAny*)&data,EOwnerThread);
   189 		}
   190 	CleanupStack::PopAndDestroy(threadName);
   191 	testThread.Logon(status);
   192 	User::SetJustInTime(EFalse);
   193 	testThread.Resume();
   194 	User::WaitForRequest(status);
   195 	User::SetJustInTime(ETrue);
   196 #ifdef _TBUFS_LOGGING
   197 	TLogMessageText logMessageText;
   198 	TBufSStartLogText("TestWsBufferL");
   199 	logMessageText.Format(_L(" OpCode(%d), ExitReason: %d"),aOpCode,testThread.ExitReason());
   200 	TBufSLogFormat(logMessageText);
   201 #endif
   202 
   203 	testThread.Close();
   204 	//reset some values to sensible ones
   205 	switch (aOpCode)
   206 		{
   207 		case EWsClOpHeapSetFail:
   208 		case EWsClOpSetPointerCursorArea:
   209 #if defined(__WINS__)
   210 		case EWsClOpSimulateXyInput:
   211 #endif
   212 			RThread resetThread;
   213 			TheClient->iWs.TestWrite(TheClient->iWs.WsHandle(), data.iOpCode, storeData, data.iDataLength);
   214 			TheClient->iWs.Flush();
   215 			break;
   216 		}
   217 	}
   218 
   219 void CTBufferSecurity::TestBadStringAnimDllL()
   220 	{
   221 	TEST(iTest->TestPanicL(&TestAnimDllThreadFunc,3,EFalse,NULL,KLitKernExec));
   222 	}
   223 
   224 void CTBufferSecurity::TestBadStringL()
   225 	{
   226 	TEST(iTest->TestWsPanicL(&TestAnimDllThreadFunc,EWservPanicBufferPtr,ETrue,NULL));
   227 	}
   228 
   229 void CTBufferSecurity::TestBadIpcL()
   230 	{
   231 	TEST(iTest->TestWsPanicL(&TestIpcThreadFunc,EWservPanicDescriptor,1));
   232 	}
   233 
   234 void CTBufferSecurity::RunTestCaseL(TInt /*aCurTestCase*/)
   235 	{
   236 	TInt ii;
   237 	((CTBufferSecurityStep*)iStep)->SetTestStepID(KUnknownSYMTestCaseIDName);
   238 	switch(++iTest->iState)
   239 		{
   240 		case 1:
   241 			((CTBufferSecurityStep*)iStep)->SetTestStepID(_L("GRAPHICS-WSERV-0022"));
   242 			_LIT(KBuffSecTestBadIpcL,"TestBadIpcL");
   243 			iTest->LogSubTest(KBuffSecTestBadIpcL);
   244 			TestBadIpcL();
   245 			break;
   246 		case 2:
   247 /**
   248 @SYMTestCaseID		GRAPHICS-WSERV-0549
   249 */
   250 			((CTBufferSecurityStep*)iStep)->SetTestStepID(_L("GRAPHICS-WSERV-0549"));
   251 			_LIT(KBuffSecTestBadStringLRAnimDll,"TestBadStringL use RAnimDll");
   252 			iTest->LogSubTest(KBuffSecTestBadStringLRAnimDll);
   253 			TestBadStringAnimDllL();
   254 			break;
   255 		case 3:
   256 /**
   257 @SYMTestCaseID		GRAPHICS-WSERV-0550
   258 */
   259 			((CTBufferSecurityStep*)iStep)->SetTestStepID(_L("GRAPHICS-WSERV-0550"));
   260 			_LIT(KBuffSecTestBadStringLTestWrite,"TestBadStringL use TestWrite");
   261 			iTest->LogSubTest(KBuffSecTestBadStringLTestWrite);
   262 			TestBadStringL();
   263 			break;
   264 #ifdef _TBUFS_TEST_SOAK_TEST
   265 		case 4:
   266 /**
   267 @SYMTestCaseID		GRAPHICS-WSERV-0551
   268 */
   269 			((CTBufferSecurityStep*)iStep)->SetTestStepID(_L("GRAPHICS-WSERV-0551"));
   270 			_LIT(KBuffSecTestWsBufferLSoak8bit,"TestWsBufferL Soak 8bit");
   271 			iTest->LogSubTest(KBuffSecTestWsBufferLSoak8bit);
   272 			for(ii = EWsClOpDisconnect; ii < EWsClOpLastEnumValue; ii++)
   273 				{
   274 				TUint8 soak8=0;//gets set as KMaxTUint8 on first iteration
   275 				do
   276 					{
   277 					--soak8;
   278 					TestWsBufferL(ii,soak8);
   279 					}
   280 				while (soak8>0);
   281 				}
   282 			break;
   283 		case 5:
   284 /**
   285 @SYMTestCaseID		GRAPHICS-WSERV-0552
   286 */
   287 			((CTBufferSecurityStep*)iStep)->SetTestStepID(_L("GRAPHICS-WSERV-0552"));
   288 			_LIT(KBuffSecTestWsBufferLSoak32bit,"TestWsBufferL Soak 32bit");
   289 			iTest->LogSubTest(KBuffSecTestWsBufferLSoak32bit);
   290 			for(ii = EWsClOpDisconnect; ii < EWsClOpLastEnumValue; ii++)
   291 				{
   292 				TUint32 soak32=0;//gets set as KMaxTUint8 on first iteration
   293 				do
   294 					{
   295 					--soak32;
   296 					TestWsBufferL(ii,soak32,EFalse);
   297 					}
   298 				while (soak32>0);
   299 				}
   300 			break;
   301 #else
   302 		case 4:
   303 /**
   304 @SYMTestCaseID		GRAPHICS-WSERV-0553
   305 */
   306 			((CTBufferSecurityStep*)iStep)->SetTestStepID(_L("GRAPHICS-WSERV-0553"));
   307 			_LIT(KBuffSecTestWsBufferL0,"TestWsBufferL 0");
   308 			iTest->LogSubTest(KBuffSecTestWsBufferL0);
   309 			for(ii = EWsClOpDisconnect; ii < EWsClOpLastEnumValue; ii++)
   310 				{
   311 				TestWsBufferL(ii,0);
   312 				}
   313 			break;
   314 		case 5:
   315 /**
   316 @SYMTestCaseID		GRAPHICS-WSERV-0554
   317 */
   318 			((CTBufferSecurityStep*)iStep)->SetTestStepID(_L("GRAPHICS-WSERV-0554"));
   319 			_LIT(KBuffSecTestWsBufferL5,"TestWsBufferL 5");
   320 			iTest->LogSubTest(KBuffSecTestWsBufferL5);
   321 			for(ii = EWsClOpDisconnect; ii < EWsClOpLastEnumValue; ii++)
   322 				{
   323 				TestWsBufferL(ii,5);
   324 				}
   325 			break;
   326 		case 6:
   327 /**
   328 @SYMTestCaseID		GRAPHICS-WSERV-0555
   329 */
   330 			((CTBufferSecurityStep*)iStep)->SetTestStepID(_L("GRAPHICS-WSERV-0555"));
   331 			_LIT(KBuffSecTestWsBufferL32,"TestWsBufferL 32");
   332 			iTest->LogSubTest(KBuffSecTestWsBufferL32);
   333 			for(ii = EWsClOpDisconnect; ii < EWsClOpLastEnumValue; ii++)
   334 				{
   335 				TestWsBufferL(ii,32);
   336 				}
   337 			break;
   338 		case 7:
   339 /**
   340 @SYMTestCaseID		GRAPHICS-WSERV-0556
   341 */
   342 			((CTBufferSecurityStep*)iStep)->SetTestStepID(_L("GRAPHICS-WSERV-0556"));
   343 			_LIT(KBuffSecTestWsBufferL64,"TestWsBufferL 64");
   344 			iTest->LogSubTest(KBuffSecTestWsBufferL64);
   345 			for(ii = EWsClOpDisconnect; ii < EWsClOpLastEnumValue; ii++)
   346 				{
   347 				TestWsBufferL(ii,64);
   348 				}
   349 			break;
   350 		case 8:
   351 /**
   352 @SYMTestCaseID		GRAPHICS-WSERV-0557
   353 */
   354 			((CTBufferSecurityStep*)iStep)->SetTestStepID(_L("GRAPHICS-WSERV-0557"));
   355 			_LIT(KBuffSecTestWsBufferL128,"TestWsBufferL 128");
   356 			iTest->LogSubTest(KBuffSecTestWsBufferL128);
   357 			for(ii = EWsClOpDisconnect; ii < EWsClOpLastEnumValue; ii++)
   358 				{
   359 				TestWsBufferL(ii,128);
   360 				}
   361 			break;
   362 		case 9:
   363 /**
   364 @SYMTestCaseID		GRAPHICS-WSERV-0558
   365 */
   366 			((CTBufferSecurityStep*)iStep)->SetTestStepID(_L("GRAPHICS-WSERV-0558"));
   367 			_LIT(KBuffSecTestWsBufferL255,"TestWsBufferL 255");
   368 			iTest->LogSubTest(KBuffSecTestWsBufferL255);
   369 			for(ii = EWsClOpDisconnect; ii < EWsClOpLastEnumValue; ii++)
   370 				{
   371 				TestWsBufferL(ii,255);
   372 				}
   373 			break;
   374 		case 10:
   375 /**
   376 @SYMTestCaseID		GRAPHICS-WSERV-0559
   377 */
   378 			((CTBufferSecurityStep*)iStep)->SetTestStepID(_L("GRAPHICS-WSERV-0559"));
   379 			_LIT(KBuffSecTestWsBufferL325,"TestWsBufferL 32-5");
   380 			iTest->LogSubTest(KBuffSecTestWsBufferL325);
   381 			for(ii = EWsClOpDisconnect; ii < EWsClOpLastEnumValue; ii++)
   382 				{
   383 				TestWsBufferL(ii,5,EFalse);
   384 				}
   385 			break;
   386 		case 11:
   387 /**
   388 @SYMTestCaseID		GRAPHICS-WSERV-0560
   389 */
   390 			((CTBufferSecurityStep*)iStep)->SetTestStepID(_L("GRAPHICS-WSERV-0560"));
   391 			_LIT(KBuffSecTestWsBufferL32128,"TestWsBufferL 32-128");
   392 			iTest->LogSubTest(KBuffSecTestWsBufferL32128);
   393 			for(ii = EWsClOpDisconnect; ii < EWsClOpLastEnumValue; ii++)
   394 				{
   395 				TestWsBufferL(ii,128,EFalse);
   396 				}
   397 			break;
   398 		case 12:
   399 /**
   400 @SYMTestCaseID		GRAPHICS-WSERV-0561
   401 */
   402 			((CTBufferSecurityStep*)iStep)->SetTestStepID(_L("GRAPHICS-WSERV-0561"));
   403 			_LIT(KBuffSecTestWsBufferL32512,"TestWsBufferL 32-512");
   404 			iTest->LogSubTest(KBuffSecTestWsBufferL32512);
   405 			for(ii = EWsClOpDisconnect; ii < EWsClOpLastEnumValue; ii++)
   406 				{
   407 				TestWsBufferL(ii,512,EFalse);
   408 				}
   409 			break;
   410 		case 13:
   411 /**
   412 @SYMTestCaseID		GRAPHICS-WSERV-0562
   413 */
   414 			((CTBufferSecurityStep*)iStep)->SetTestStepID(_L("GRAPHICS-WSERV-0562"));
   415 			_LIT(KBuffSecTestWsBufferL320x01234567,"TestWsBufferL 32-0x01234567");
   416 			iTest->LogSubTest(KBuffSecTestWsBufferL320x01234567);
   417 			for(ii = EWsClOpDisconnect; ii < EWsClOpLastEnumValue; ii++)
   418 				{
   419 				TestWsBufferL(ii,0x01234567,EFalse);
   420 				}
   421 			break;
   422 		case 14:
   423 /**
   424 @SYMTestCaseID		GRAPHICS-WSERV-0563
   425 */
   426 			((CTBufferSecurityStep*)iStep)->SetTestStepID(_L("GRAPHICS-WSERV-0563"));
   427 			_LIT(KBuffSecTestWsBufferL320x89abcdef,"TestWsBufferL 32-0x89abcdef");
   428 			iTest->LogSubTest(KBuffSecTestWsBufferL320x89abcdef);
   429 			for(ii = EWsClOpDisconnect; ii < EWsClOpLastEnumValue; ii++)
   430 				{
   431 				TestWsBufferL(ii,0x89abcdef,EFalse);
   432 				}
   433 			break;
   434 		case 15:
   435 /**
   436 @SYMTestCaseID		GRAPHICS-WSERV-0564
   437 */
   438 			((CTBufferSecurityStep*)iStep)->SetTestStepID(_L("GRAPHICS-WSERV-0564"));
   439 			_LIT(KBuffSecTestWsBufferL320xCCCCCCCC,"TestWsBufferL 32-0xCCCCCCCC");
   440 			iTest->LogSubTest(KBuffSecTestWsBufferL320xCCCCCCCC);
   441 			for(ii = EWsClOpDisconnect; ii < EWsClOpLastEnumValue; ii++)
   442 				{
   443 				TestWsBufferL(ii,0xCCCCCCCC,EFalse);
   444 				}
   445 			break;
   446 #endif
   447 		default:
   448 			((CTBufferSecurityStep*)iStep)->SetTestStepID(KNotATestSYMTestCaseIDName);
   449 			((CTBufferSecurityStep*)iStep)->CloseTMSGraphicsStep();
   450 			TInt err = RProperty::Set(KUidWServSecurityTesting,EWServSecTestBufferSecurity,EFalse);
   451 			User::LeaveIfError(err);
   452 			TestComplete();
   453 		}
   454 	((CTBufferSecurityStep*)iStep)->RecordTestResultL();
   455 	}
   456 
   457 __WS_CONSTRUCT_STEP__(BufferSecurity)