Update contrib.
1 // Copyright (c) 1995-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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32test\mmu\t_chunk4.cpp
16 // Test disconnected chunks
20 // - Check Allocate/Commit/Decommit methods on local disconnected chunk and write/read
21 // access to both committed and uncommitted regions.
22 // - Check IPC that involves local disconnected chunk and verify results are as expected.
23 // Platforms/Drives/Compatibility:
25 // Assumptions/Requirement/Pre-requisites:
26 // Failures and causes:
27 // Base Port information:
31 #define __E32TEST_EXTENSION__
34 #include "mmudetect.h"
35 #include "../misc/prbs.h"
36 #include "d_memorytest.h"
39 RTest test(_L("T_CHUNK4"));
41 RMemoryTestLdd TestLdd;
45 class CChunk : public CBase
48 static CChunk* New(TInt aMaxSize);
52 TInt Commit(TInt anOffset, TInt aSize);
53 TInt Allocate(TInt aSize);
54 TInt Decommit(TInt anOffset, TInt aSize);
55 void CheckL(volatile TUint* aPtr);
56 TInt AddPages(TInt anOffset, TInt aSize);
57 TInt RemovePages(TInt anOffset, TInt aSize);
66 CChunk* CChunk::New(TInt aMaxSize)
68 CChunk* pC=new CChunk;
72 UserHal::PageSizeInBytes(p);
74 pC->iMaxSize=aMaxSize;
77 TInt r=pC->iChunk.CreateDisconnectedLocal(0,0,aMaxSize);
80 TUint8* pI=(TUint8*)User::Alloc(n);
104 void CChunk::CheckL(volatile TUint* aPtr)
110 TInt CChunk::Verify()
114 TUint8* base=iChunk.Base();
115 for (i=0; i<iNumPages; i++)
117 volatile TUint* pX=(volatile TUint*)base;
118 TInt r=TestLdd.ReadWriteMemory((TAny*)pX);
119 TUint8 info=iPageInfo[i];
120 if (info==0 && r==KErrNone)
122 if (info!=0 && r!=KErrNone)
123 return KErrAccessDenied;
130 for (j=0; j<iPageSize; j+=4)
132 if (*pX++!=Random(seed))
141 TInt CChunk::AddPages(TInt anOffset, TInt aSize)
143 TInt i=anOffset/iPageSize;
144 TInt n=aSize/iPageSize;
146 TUint* p=(TUint*)(iChunk.Base()+anOffset);
149 TUint8 s=(TUint8)Random(RndSeed);
157 for (j=0; j<iPageSize; j+=4)
165 TInt CChunk::RemovePages(TInt anOffset, TInt aSize)
167 TInt i=anOffset/iPageSize;
168 TInt n=aSize/iPageSize;
175 TInt CChunk::Commit(TInt anOffset, TInt aSize)
177 TInt r=iChunk.Commit(anOffset,aSize);
180 AddPages(anOffset,aSize);
185 TInt CChunk::Allocate(TInt aSize)
187 TInt r=iChunk.Allocate(aSize);
195 TInt CChunk::Decommit(TInt anOffset, TInt aSize)
197 TInt r=iChunk.Decommit(anOffset,aSize);
200 RemovePages(anOffset,aSize);
205 // Stuff to test remote writes
206 _LIT(KServerName, "Chunk4Test");
208 class CTestSession : public CSession2
212 virtual void ServiceL(const RMessage2& aMessage);
215 class CTestServer : public CServer2
219 virtual CSession2* NewSessionL(const TVersion& aVersion, const RMessage2& aMessage) const;
222 class RTestSession : public RSessionBase
225 enum {ETestIpc, ETestStop};
228 TInt TestRemoteWrite(TInt aLength, TInt anOffset1, TInt anOffset2, TInt anOffset3, TInt anOffset4);
229 TInt IpcWrite(TDes8* aRemoteDest, const TAny* aLocalSrc, TInt aOffset);
232 CTestSession::CTestSession()
236 void CTestSession::ServiceL(const RMessage2& aMessage)
238 switch (aMessage.Function())
240 case RTestSession::ETestIpc:
242 const TDesC8& localSrc = *(const TDesC8*)aMessage.Ptr1();
243 TInt offset = aMessage.Int2();
244 TInt r = aMessage.Write(0, localSrc, offset);
245 aMessage.Complete(r);
248 case RTestSession::ETestStop:
249 CActiveScheduler::Stop();
256 CTestServer::CTestServer()
261 CSession2* CTestServer::NewSessionL(const TVersion& /*aVersion*/, const RMessage2& /*aMessage*/) const
263 return new (ELeave) CTestSession;
266 TInt ServerThread(TAny*)
268 CActiveScheduler* pA = new CActiveScheduler;
269 CTestServer* pS = new CTestServer;
272 CActiveScheduler::Install(pA);
273 TInt r = pS->Start(KServerName);
276 RThread::Rendezvous(KErrNone);
277 CActiveScheduler::Start();
281 TInt RTestSession::Connect()
284 TInt r = t.Create(KServerName, ServerThread, 0x1000, 0x1000, 0x10000, NULL);
286 t.SetPriority(EPriorityMore);
289 test(s==KRequestPending);
291 User::WaitForRequest(s);
292 test(t.ExitType()==EExitPending);
295 r = CreateSession(KServerName, TVersion());
299 void RTestSession::Stop()
301 TInt r = SendReceive(ETestStop);
302 test(r==KErrServerTerminated);
306 TInt RTestSession::IpcWrite(TDes8* aRemoteDest, const TAny* aLocalSrc, TInt aOffset)
308 return SendReceive(ETestIpc, TIpcArgs(aRemoteDest, aLocalSrc, aOffset));
311 TInt RTestSession::TestRemoteWrite(TInt aLength, TInt anOffset1, TInt anOffset2, TInt anOffset3, TInt anOffset4)
313 test.Printf(_L("%x %x %x %x %x\n"),aLength,anOffset1,anOffset2,anOffset3,anOffset4);
315 TBool ptr=(anOffset1>=0);
319 TInt r=c.CreateDisconnectedLocal(0,0,0x800000);
321 TUint8* base=c.Base();
324 r=c.Commit(anOffset1,(TInt)sizeof(TPtr8));
326 pDes=(TDes8*)(base+anOffset1);
327 Mem::FillZ(pDes,(TInt)sizeof(TPtr8));
328 r=c.Commit(anOffset2,aLength);
330 pData=base+anOffset2;
331 Mem::FillZ(pData,aLength);
332 new(pDes) TPtr8(pData,0,aLength);
333 test(pDes->Length()==0);
334 test(pDes->MaxLength()==aLength);
335 test(pDes->Ptr()==pData);
339 TInt len=(TInt)sizeof(TDes8)+aLength;
340 r=c.Commit(anOffset2,len);
342 pDes=(TDes8*)(base+anOffset2);
343 Mem::FillZ(pDes,len);
344 pData=base+anOffset2+(TInt)sizeof(TDes8);
346 ((TInt*)pDes)[1]=aLength;
347 test(pDes->Length()==0);
348 test(pDes->MaxLength()==aLength);
349 test(pDes->Ptr()==pData);
351 TInt slen=aLength-anOffset3;
352 TUint8* pS=(TUint8*)User::Alloc(aLength);
354 Mem::FillZ(pS,aLength);
355 TPtrC8 src(pS+anOffset3,slen);
357 for (i=anOffset3; i<aLength; i++)
358 pS[i]=(TUint8)Random(RndSeed);
360 c.Decommit(anOffset4,0x1000);
361 r = IpcWrite(pDes, &src, anOffset3);
364 TPtrC8 tsrc(pS,aLength);
370 test.Printf(_L("Return value %d\n"),r);
374 GLDEF_C TInt E32Main()
376 RndSeed[0]=0xddb3d743;
382 test.Printf(_L("This test requires an MMU\n"));
385 test.Start(_L("Testing Disconnected Chunks"));
387 test.Printf(_L("Load test LDD\n"));
388 test(TestLdd.Open()==KErrNone);
390 CChunk* pC=CChunk::New(0x800000);
393 test.Printf(_L("Free RAM %08x\n"),free);
394 test(pC->Verify()==KErrNone);
395 test.Printf(_L("Commit 0+0x1000\n"));
396 test(pC->Commit(0,0x1000)==KErrNone);
397 test(pC->Verify()==KErrNone);
398 test.Printf(_L("Commit 0+0x1000 (again)\n"));
399 test(pC->Commit(0,0x1000)==KErrAlreadyExists);
400 test(pC->Verify()==KErrNone);
401 test.Printf(_L("Commit 0x3000+0x1000\n"));
402 test(pC->Commit(0x3000,0x1000)==KErrNone);
403 test(pC->Verify()==KErrNone);
404 test.Printf(_L("Commit 0x2000+0x3000 (overlaps previous)\n"));
405 test(pC->Commit(0x2000,0x3000)==KErrAlreadyExists);
406 test(pC->Verify()==KErrNone);
407 test.Printf(_L("Allocate 0x5000\n"));
408 test(pC->Allocate(0x5000)==0x4000);
409 test(pC->Verify()==KErrNone);
410 test.Printf(_L("Allocate 0x1000\n"));
411 test(pC->Allocate(0x1000)==0x1000);
412 test(pC->Verify()==KErrNone);
413 test.Printf(_L("Decommit 0+0x4000\n"));
414 test(pC->Decommit(0,0x4000)==KErrNone);
415 test(pC->Verify()==KErrNone);
416 test.Printf(_L("Decommit 0+0x4000 (again)\n"));
417 test(pC->Decommit(0,0x4000)==KErrNone);
418 test(pC->Verify()==KErrNone);
419 test.Printf(_L("Allocate 0x4000\n"));
420 test(pC->Allocate(0x4000)==0x0000);
421 test(pC->Verify()==KErrNone);
422 test.Printf(_L("Decommit 0+0x10000\n"));
423 test(pC->Decommit(0,0x10000)==KErrNone);
424 test(pC->Verify()==KErrNone);
425 test.Printf(_L("Check Free RAM\n"));
426 test(FreeRam()==free);
428 test.Printf(_L("Commit 0x700000+0x10000\n"));
429 test(pC->Commit(0x700000,0x10000)==KErrNone);
430 test(pC->Verify()==KErrNone);
431 test.Printf(_L("Allocate 0x4000\n"));
432 test(pC->Allocate(0x4000)==0x0000);
433 test(pC->Verify()==KErrNone);
434 test.Printf(_L("Decommit 0+0x800000\n"));
435 test(pC->Decommit(0,0x800000)==KErrNone);
436 test(pC->Verify()==KErrNone);
437 test.Printf(_L("Check Free RAM\n"));
438 test(FreeRam()==free);
442 // Test decommiting from a chunk which has no memory commited
443 // in the first megabyte. On the moving memory model, such
444 // chunks have a non-zero iHomeRegionOffset value and has been
445 // the cause of defects (DEF121857)
446 test.Printf(_L("Create new chunk\n"));
447 pC=CChunk::New(0x800000);
449 test.Printf(_L("Commit 0x100000+0x3000\n"));
450 test(pC->Commit(0x100000,0x3000)==KErrNone);
451 test(pC->Verify()==KErrNone);
452 test.Printf(_L("Decommit 0+0x101000\n"));
453 test(pC->Decommit(0,0x101000)==KErrNone);
454 test(pC->Verify()==KErrNone);
455 test.Printf(_L("Decommit 0x101000+0x1000\n"));
456 test(pC->Decommit(0x101000,0x1000)==KErrNone);
457 test(pC->Verify()==KErrNone);
458 test.Printf(_L("Decommit 0x100000+0x100000\n"));
459 test(pC->Decommit(0x100000,0x100000)==KErrNone);
460 test(pC->Verify()==KErrNone);
463 test.Next(_L("Testing RThread::Write to disconnected chunks"));
465 TInt r = ts.Connect();
467 test(ts.TestRemoteWrite(64,0,0x3000,0,-1)==KErrNone);
468 test(ts.TestRemoteWrite(64,0xffc,0x8000,0,-1)==KErrNone);
469 test(ts.TestRemoteWrite(256,0xffc,0x7f80,0,-1)==KErrNone);
470 test(ts.TestRemoteWrite(256,0xffc,0x7f80,128,-1)==KErrNone);
471 test(ts.TestRemoteWrite(0x10000,0xffc,0x707f80,0,-1)==KErrNone);
472 test(ts.TestRemoteWrite(0x10000,0xffc,0x707f80,0x2000,-1)==KErrNone);
473 test(ts.TestRemoteWrite(64,-1,0x3000,0,-1)==KErrNone);
474 test(ts.TestRemoteWrite(0x10000,-1,0xfff00,0x2000,-1)==KErrNone);
475 test(ts.TestRemoteWrite(256,0xffc,0x7f80,16,0)==KErrBadDescriptor);
476 test(ts.TestRemoteWrite(256,0xffc,0x7f80,16,0x1000)==KErrBadDescriptor);
477 test(ts.TestRemoteWrite(256,0xffc,0x7f80,16,0x7000)==KErrBadDescriptor);
478 test(ts.TestRemoteWrite(256,0xffc,0x7f80,16,0x8000)==KErrBadDescriptor);
479 test(ts.TestRemoteWrite(0x10000,0xffc,0x707f80,0x2000,0x710000)==KErrBadDescriptor);
480 test(ts.TestRemoteWrite(0x10000,-1,0xfff00,0x1000,0x100000)==KErrBadDescriptor);