os/kernelhwsrv/kerneltest/e32test/system/t_kucopy.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 // Copyright (c) 1999-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\system\t_kucopy.cpp
    15 // Test copying between kernel and user side.
    16 // 
    17 //
    18 
    19 #define __E32TEST_EXTENSION__
    20 
    21 #include <e32test.h>
    22 #include "d_kucopy.h"
    23 #include "../mmu/mmudetect.h"
    24 
    25 //#define QUICK
    26 
    27 _LIT(KLddFileName,"D_KUCOPY.LDD");
    28 _LIT(KLitKernExec,"KERN-EXEC");
    29 
    30 RTest test(_L("T_KUCOPY"));
    31 RKUCopy KU;
    32 TInt BufSize;
    33 TInt RandSize;
    34 TUint8* Buf1;
    35 TUint8* Buf2;
    36 TUint8* Random;
    37 TBool KernProt=EFalse;
    38 
    39 _LIT8(KTest8_1,"1");
    40 _LIT8(KTest8_2,"Test2");
    41 _LIT8(KTest8_3,"extensionality emptyset pairset separation powerset unionset infinity foundation replacement choice");
    42 _LIT8(KTest8_4,"And darkness and decay and the red death held illimitable dominion over all.");
    43 
    44 _LIT16(KTest16_1,"39");
    45 _LIT16(KTest16_2,"Test16");
    46 _LIT16(KTest16_3,"extensionality emptyset pairset separation powerset unionset infinity foundation replacement choice.");
    47 _LIT16(KTest16_4,"And darkness and decay and the red death held illimitable dominion over all.");
    48 
    49 static const TDesC8* const TestStrings8[]=
    50 	{
    51 	&KTest8_1,
    52 	&KTest8_2,
    53 	&KTest8_3,
    54 	&KTest8_4,
    55 	};
    56 
    57 static const TDesC16* const TestStrings16[]=
    58 	{
    59 	&KTest16_1,
    60 	&KTest16_2,
    61 	&KTest16_3,
    62 	&KTest16_4,
    63 	};
    64 
    65 #define NTESTS8		((TInt)(sizeof(TestStrings8)/sizeof(const TDesC8*)))
    66 #define NTESTS16	((TInt)(sizeof(TestStrings16)/sizeof(const TDesC16*)))
    67 
    68 enum TTest
    69 	{
    70 	EPut,
    71 	EGet,
    72 	EPut32,
    73 	EGet32,
    74 	ESet,
    75 	EDesPut8,
    76 	EDesGet8,
    77 	EDesInfo8,
    78 	EDesPut16,
    79 	EDesGet16,
    80 	EDesInfo16,
    81 	};
    82 
    83 void DoPutGetTest(TInt aSrcOffset, TInt aDestOffset, TInt aLen, TTest aTest)
    84 	{
    85 	// Use the first BufSize of random data as background, the rest as copy data.
    86 	// Use Buf1 as target
    87 //	test.Printf(_L("s=%4d d=%4d l=%4d t=%d\n"),aSrcOffset,aDestOffset,aLen,aTest);
    88 	switch (aTest)
    89 		{
    90 		case EPut:
    91 			Mem::Move(Buf1,Random,BufSize);
    92 			KU.Put(Buf1+aDestOffset,aSrcOffset+BufSize,aLen);
    93 			break;
    94 		case EGet:
    95 			KU.Get(Random+BufSize+aSrcOffset,aDestOffset,aLen);
    96 			KU.Read(Buf1);
    97 			break;
    98 		case EPut32:
    99 			Mem::Move(Buf1,Random,BufSize);
   100 			KU.Put32((TUint32*)(Buf1+aDestOffset),aSrcOffset+BufSize,aLen);
   101 			break;
   102 		case EGet32:
   103 			KU.Get32((const TUint32*)(Random+BufSize+aSrcOffset),aDestOffset,aLen);
   104 			KU.Read(Buf1);
   105 			break;
   106 		default:
   107 			User::Panic(_L("PutGetTest"),aTest);
   108 		}
   109 
   110 	// Should now have:
   111 	// Buf1[0...aDestOffset-1]=Random[0...aDestOffset-1]
   112 	// Buf1[aDestOffset...aDestOffset+aLen-1]=Random[BufSize+aSrcOffset...BufSize+aSrcOffset+aLen-1]
   113 	// Buf1[aDestOffset+aLen...BufSize-1]=Random[aDestOffset+aLen...BufSize-1]
   114 	test(Mem::Compare(Buf1,aDestOffset,Random,aDestOffset)==0);
   115 	test(Mem::Compare(Buf1+aDestOffset,aLen,Random+BufSize+aSrcOffset,aLen)==0);
   116 	test(Mem::Compare(Buf1+aDestOffset+aLen,BufSize-aDestOffset-aLen,Random+aDestOffset+aLen,BufSize-aDestOffset-aLen)==0);
   117 	}
   118 
   119 void DoSetTest(TInt aOffset, TInt aLen, TUint8 aValue)
   120 	{
   121 	Mem::Move(Buf1,Random,BufSize);
   122 	KU.Set(Buf1+aOffset,aLen,aValue);
   123 	test(Mem::Compare(Buf1,aOffset,Random,aOffset)==0);
   124 	TUint8* p=Buf1+aOffset;
   125 	TUint8* pE=p+aLen;
   126 	while (p<pE)
   127 		test(*p++==aValue);
   128 	test(Mem::Compare(Buf1+aOffset+aLen,BufSize-aOffset-aLen,Random+aOffset+aLen,BufSize-aOffset-aLen)==0);
   129 	}
   130 
   131 void TestDesGet8(const TDesC8& aSrc)
   132 	{
   133 	TPtr8 dest(Buf2,0,BufSize);
   134 	KU.DesGet(dest,aSrc);
   135 	test(dest==aSrc);
   136 	}
   137 
   138 void TestDesGet8()
   139 	{
   140 	TInt i;
   141 	for (i=0; i<NTESTS8; ++i)
   142 		{
   143 		const TDesC8* orig=TestStrings8[i];
   144 		TestDesGet8(*orig);	// test EBufC
   145 		TBuf8<256> buf=*orig;
   146 		TestDesGet8(buf);	// test EBuf
   147 		TPtrC8 ptrc(orig->Ptr(),orig->Length());
   148 		TestDesGet8(ptrc);	// test EPtrC
   149 		TPtr8 ptr((TUint8*)orig->Ptr(),orig->Length(),orig->Length());
   150 		TestDesGet8(ptr);	// test EPtr
   151 		HBufC8* pH=orig->Alloc();
   152 		test(pH!=NULL);
   153 		TPtr8 bufCptr(pH->Des());
   154 		TestDesGet8(bufCptr);	// test EBufCPtr
   155 		User::Free(pH);
   156 		}
   157 	}
   158 
   159 void TestDesPut8(TDesC8& aDest, const TDesC8* aExtraDest, const TDesC8& aSrc)
   160 	{
   161 	KU.DesPut((TDes8&)aDest,aSrc);
   162 	test(aDest==aSrc);
   163 	if (aExtraDest)
   164 		test(*aExtraDest==aSrc);	// for testing EBufCPtr
   165 	}
   166 
   167 void TestDesPut8()
   168 	{
   169 	TInt i;
   170 	for (i=0; i<NTESTS8; ++i)
   171 		{
   172 		const TDesC8* orig=TestStrings8[i];
   173 		TBuf8<256> buf;
   174 		TestDesPut8(buf,NULL,*orig);	// test EBuf
   175 		Mem::FillZ((TAny*)buf.Ptr(),256);
   176 		TPtr8 ptr((TUint8*)buf.Ptr(),0,256);
   177 		TestDesPut8(ptr,NULL,*orig);	// test EPtr
   178 		HBufC8* pH=HBufC8::New(256);
   179 		test(pH!=NULL);
   180 		TPtr8 bufCptr(pH->Des());
   181 		TestDesPut8(bufCptr,pH,*orig);	// test EBufCPtr
   182 		User::Free(pH);
   183 		}
   184 	}
   185 
   186 void TestDesInfo8(const TDesC8& aDes, TInt aLength, TInt aMaxLength, const TUint8* aPtr)
   187 	{
   188 	SDesInfo info;
   189 	KU.DesInfo(aDes,info);
   190 	test(aLength==info.iLength);
   191 	test(aMaxLength==info.iMaxLength);
   192 	test((TAny*)aPtr==info.iPtr);
   193 	}
   194 
   195 void TestDesInfo8()
   196 	{
   197 	TInt i;
   198 	for (i=0; i<NTESTS8; ++i)
   199 		{
   200 		const TDesC8* orig=TestStrings8[i];
   201 		TestDesInfo8(*orig,orig->Length(),-1,orig->Ptr());	// test EBufC
   202 		TBuf8<256> buf;
   203 		TestDesInfo8(buf,0,256,buf.Ptr());				// test EBuf
   204 		buf=*orig;
   205 		TestDesInfo8(buf,orig->Length(),256,buf.Ptr());	// test EBuf
   206 		TBuf8<203> buf2;
   207 		TestDesInfo8(buf2,0,203,buf2.Ptr());			// test EBuf
   208 		buf2=*orig;
   209 		TestDesInfo8(buf2,orig->Length(),203,buf2.Ptr());	// test EBuf
   210 		TPtrC8 ptrc(orig->Ptr(),orig->Length());
   211 		TestDesInfo8(ptrc,orig->Length(),-1,orig->Ptr());	// test EPtrC
   212 		TPtr8 ptr((TUint8*)orig->Ptr(),orig->Length(),orig->Length());
   213 		TestDesInfo8(ptr,orig->Length(),orig->Length(),orig->Ptr());	// test EPtr
   214 		HBufC8* pH=orig->Alloc();
   215 		test(pH!=NULL);
   216 		TPtr8 bufCptr(pH->Des());
   217 		TestDesInfo8(*pH,orig->Length(),-1,pH->Ptr());
   218 		TestDesInfo8(bufCptr,orig->Length(),User::AllocLen(pH)-sizeof(TDesC8),pH->Ptr());
   219 		User::Free(pH);
   220 		}
   221 	}
   222 
   223 void RunTestInThread(TThreadFunction aFn, TAny* aParameter, const TDesC* aPanicCat, TInt aExitCode, TInt aLine)
   224 	{
   225 	test.Printf(_L("Line %d\n"),aLine);
   226 	RThread t;
   227 	TInt r=t.Create(KNullDesC(),aFn,0x2000,NULL,aParameter);
   228 	test(r==KErrNone);
   229 	TRequestStatus s;
   230 	t.Logon(s);
   231 	t.Resume();
   232 	User::WaitForRequest(s);
   233 	if (aPanicCat)
   234 		{
   235 		test(t.ExitType()==EExitPanic);
   236 		test(t.ExitCategory()==*aPanicCat);
   237 		test(t.ExitReason()==aExitCode);
   238 		}
   239 	else
   240 		{
   241 		test(t.ExitType()==EExitKill);
   242 		test(t.ExitReason()==aExitCode);
   243 		}
   244 	CLOSE_AND_WAIT(t);
   245 	}
   246 
   247 TInt DesGet8Thread(TAny* aPtr)
   248 	{
   249 	TBuf8<256> dest;
   250 	KU.DesGet(dest, *(const TDesC8*)aPtr);
   251 	return 0;
   252 	}
   253 
   254 TInt DesPut8Thread(TAny* aPtr)
   255 	{
   256 	KU.DesPut(*(TDes8*)aPtr,KTest8_4);
   257 	return 0;
   258 	}
   259 
   260 TInt DesInfo8Thread(TAny* aPtr)
   261 	{
   262 	SDesInfo info;
   263 	KU.DesInfo(*(TDesC8*)aPtr,info);
   264 	return 0;
   265 	}
   266 
   267 void TestErrors()
   268 	{
   269 	TBool jit=User::JustInTime();
   270 	User::SetJustInTime(EFalse);
   271 	TUint8* Kern=KU.KernelBufferAddress();
   272 	TUint x=0xffffffff;
   273 	TBuf8<256> ubuf;
   274 	TPtrC8 uptrc(Buf1,100);
   275 	TPtrC8 kptrc(Kern,1);
   276 	TPtr8 kptr(Kern,10,256);
   277 	RunTestInThread(DesGet8Thread,&x,&KLitKernExec,EKUDesInfoInvalidType,__LINE__);
   278 	RunTestInThread(DesGet8Thread,&ubuf,NULL,KErrNone,__LINE__);
   279 	RunTestInThread(DesGet8Thread,&ubuf,NULL,KErrNone,__LINE__);
   280 	if (KernProt)
   281 		{
   282 		RunTestInThread(DesGet8Thread,Kern,&KLitKernExec,ECausedException,__LINE__);
   283 		RunTestInThread(DesGet8Thread,&kptrc,&KLitKernExec,ECausedException,__LINE__);
   284 		RunTestInThread(DesGet8Thread,&kptr,&KLitKernExec,ECausedException,__LINE__);
   285 		}
   286 	RunTestInThread(DesPut8Thread,&x,&KLitKernExec,EKUDesInfoInvalidType,__LINE__);
   287 	RunTestInThread(DesPut8Thread,&ubuf,NULL,KErrNone,__LINE__);
   288 	RunTestInThread(DesPut8Thread,&uptrc,&KLitKernExec,EKUDesSetLengthInvalidType,__LINE__);
   289 	if (KernProt)
   290 		{
   291 		RunTestInThread(DesPut8Thread,Kern,&KLitKernExec,ECausedException,__LINE__);
   292 		RunTestInThread(DesPut8Thread,&kptrc,&KLitKernExec,EKUDesSetLengthInvalidType,__LINE__);
   293 		RunTestInThread(DesPut8Thread,&kptr,&KLitKernExec,ECausedException,__LINE__);
   294 		}
   295 	RunTestInThread(DesInfo8Thread,&x,&KLitKernExec,EKUDesInfoInvalidType,__LINE__);
   296 	RunTestInThread(DesInfo8Thread,&ubuf,NULL,KErrNone,__LINE__);
   297 	RunTestInThread(DesInfo8Thread,&uptrc,NULL,KErrNone,__LINE__);
   298 	RunTestInThread(DesInfo8Thread,&kptrc,NULL,KErrNone,__LINE__);
   299 	RunTestInThread(DesInfo8Thread,&kptr,NULL,KErrNone,__LINE__);
   300 	if (KernProt)
   301 		{
   302 		RunTestInThread(DesInfo8Thread,Kern,&KLitKernExec,ECausedException,__LINE__);
   303 		}
   304 
   305 	User::SetJustInTime(jit);
   306 	}
   307 
   308 void RequestCompleteBadPointer(TUint aBadAddress)
   309 	{
   310 	TRequestStatus* badStatus = (TRequestStatus*)aBadAddress;
   311 	test.Printf(_L("Test Kern::RequestComplete to %08x\n"), aBadAddress);
   312 	KU.RequestComplete(badStatus);
   313 	test.Printf(_L("Test Kern::RequestComplete to %08x (in current thread)\n"), aBadAddress);
   314 	KU.RequestCompleteLocal(badStatus);
   315 
   316 	// Check the thread hasn't been signalled
   317 	TRequestStatus status;
   318 	RTimer timer;
   319 	test_KErrNone(timer.CreateLocal());
   320 	timer.After(status, 100000); // 100ms
   321 	test_Equal(KRequestPending, status.Int());
   322 	User::WaitForAnyRequest();
   323 	test_Equal(KErrNone, status.Int());
   324 	timer.Close();	
   325 	
   326 	test.Printf(_L("Test Kern::QueueRequestComplete to %08x\n"), aBadAddress);
   327 	TInt r = KU.QueueRequestComplete(badStatus);
   328 
   329 	// Kern::QueueRequestComplete will signal even if the pointer is bad, unless:
   330 	//  - the pointer is null
   331 	//  - the call to Setup returned an error
   332 	if (badStatus != NULL && r == KErrNone)
   333 		User::WaitForAnyRequest();
   334 	}
   335 
   336 void TestRequestCompleteTrapped()
   337 	{
   338 	TRequestStatus status;
   339 	test.Printf(_L("Test Kern::RequestComplete to %08x\n"), &status);
   340 	status = KRequestPending;
   341 	KU.RequestComplete(&status);
   342 	User::WaitForRequest(status);
   343 	test_Equal(KErrNone, status.Int());
   344 	
   345 	test.Printf(_L("Test Kern::RequestComplete to %08x (in current thread)\n"), &status);
   346 	status = KRequestPending;
   347 	KU.RequestCompleteLocal(&status);
   348 	User::WaitForRequest(status);
   349 	test_Equal(KErrNone, status.Int());
   350 
   351 	test.Printf(_L("Test Kern::QueueRequestComplete to %08x\n"), &status);
   352 	status = KRequestPending;
   353 	KU.QueueRequestComplete(&status);
   354 	User::WaitForRequest(status);
   355 	test_Equal(KErrNone, status.Int());
   356 
   357 	RequestCompleteBadPointer(0x00000000);
   358 	RequestCompleteBadPointer(0x00000001);
   359 	RequestCompleteBadPointer(0x00000004);
   360 	RequestCompleteBadPointer(0x00000008);
   361 	if (KernProt)
   362 		{
   363 		RequestCompleteBadPointer(0x80000000);
   364 		RequestCompleteBadPointer(0xfffffff0);
   365 		RequestCompleteBadPointer((TUint)KU.KernelBufferAddress());
   366 		}
   367 	}
   368 
   369 GLDEF_C TInt E32Main()
   370 	{
   371 	test.Title();
   372 	test.Start(_L("Load LDD"));
   373 	TInt r=User::LoadLogicalDevice(KLddFileName);
   374 	test(r==KErrNone || r==KErrAlreadyExists);
   375 	test.Next(_L("Open channel"));
   376 	r=KU.Open();
   377 	test(r==KErrNone);
   378 	test.Next(_L("Create chunk"));
   379 	RChunk c;
   380 	r=c.CreateDisconnectedLocal(0,0,0x100000);
   381 	test(r==KErrNone);
   382 	BufSize=KU.Length();
   383 	RandSize=KU.RandomLength();
   384 	test.Printf(_L("BufSize=%x, RandSize=%x\n"),BufSize,RandSize);
   385 	r=c.Commit(0,BufSize);
   386 	test(r==KErrNone);
   387 	r=c.Commit(0x10000,BufSize);
   388 	test(r==KErrNone);
   389 	r=c.Commit(0x20000,RandSize);
   390 	test(r==KErrNone);
   391 	Buf1=c.Base();
   392 	Buf2=c.Base()+0x10000;
   393 	Random=c.Base()+0x20000;
   394 	test.Printf(_L("Buf1 at %08x, Buf2 at %08x, Random at %08x\n"),Buf1,Buf2,Random);
   395 	Mem::Fill(Buf1,BufSize,0xb1);
   396 	Mem::Fill(Buf2,BufSize,0xb2);
   397 	Mem::Fill(Random,RandSize,0x8b);
   398 	KU.ReadRandom(Random);
   399 
   400 	KernProt=HaveDirectKernProt();
   401 
   402 #ifndef QUICK
   403 	TInt s;
   404 	TInt d;
   405 	TInt l;
   406 	test.Next(_L("Put/Get 1"));
   407 	for (l=1; l<300; l+=3)
   408 		{
   409 		test.Printf(_L("PG1: l=%d\n"),l);
   410 		for (s=0; s<=BufSize-l; s+=227)
   411 			{
   412 			for (d=0; d<=BufSize-l; d+=229)
   413 				{
   414 				DoPutGetTest(s,d,l,EPut);
   415 				DoPutGetTest(s,d,l,EGet);
   416 				}
   417 			}
   418 		}
   419 
   420 	test.Next(_L("Put/Get 2"));
   421 	for (l=1; l<300; l+=3)
   422 		{
   423 		test.Printf(_L("PG2: l=%d\n"),l);
   424 		for (s=BufSize-l; s>=0; s-=227)
   425 			{
   426 			for (d=BufSize-l; d>=0; d-=229)
   427 				{
   428 				DoPutGetTest(s,d,l,EPut);
   429 				DoPutGetTest(s,d,l,EGet);
   430 				}
   431 			}
   432 		}
   433 
   434 	test.Next(_L("Put32/Get32 1"));
   435 	for (l=4; l<300; l+=12)
   436 		{
   437 		test.Printf(_L("PG32,1: l=%d\n"),l);
   438 		for (s=0; s<=BufSize-l; s+=4*59)
   439 			{
   440 			for (d=0; d<=BufSize-l; d+=4*61)
   441 				{
   442 				DoPutGetTest(s,d,l,EPut32);
   443 				DoPutGetTest(s,d,l,EGet32);
   444 				}
   445 			}
   446 		}
   447 
   448 	test.Next(_L("Put32/Get32 2"));
   449 	for (l=4; l<300; l+=12)
   450 		{
   451 		test.Printf(_L("PG32,2: l=%d\n"),l);
   452 		for (s=BufSize-l; s>=0; s-=4*59)
   453 			{
   454 			for (d=BufSize-l; d>=0; d-=4*61)
   455 				{
   456 				DoPutGetTest(s,d,l,EPut32);
   457 				DoPutGetTest(s,d,l,EGet32);
   458 				}
   459 			}
   460 		}
   461 	test.Next(_L("Set"));
   462 	for (l=1; l<300; l+=3)
   463 		{
   464 		test.Printf(_L("Set: l=%d\n"),l);
   465 		for (s=0; s<=BufSize-l; s+=83)
   466 			{
   467 			DoSetTest(s,l,0x1b);
   468 			DoSetTest(s,l,0xaa);
   469 			}
   470 		for (s=BufSize-l; s>=0; s-=79)
   471 			{
   472 			DoSetTest(s,l,0x1b);
   473 			DoSetTest(s,l,0xaa);
   474 			}
   475 		}
   476 #endif
   477 	test.Next(_L("DesGet8"));
   478 	TestDesGet8();
   479 	test.Next(_L("DesPut8"));
   480 	TestDesPut8();
   481 	test.Next(_L("DesInfo8"));
   482 	TestDesInfo8();
   483 	test.Next(_L("Errors"));
   484 	TestErrors();
   485 	test.Next(_L("Test Kern::RequestComplete functions trapped"));
   486 	TestRequestCompleteTrapped();
   487 
   488 	c.Close();
   489 	KU.Close();
   490 	test.End();
   491 	return 0;
   492 	}
   493 
   494 
   495 
   496