kerneltest/e32test/system/t_regram.cpp
changeset 0 a41df078684a
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 1997-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_regram.cpp
       
    15 // 
       
    16 //
       
    17 
       
    18 #include <e32test.h>
       
    19 #include <e32svr.h>
       
    20 
       
    21 //
       
    22 // This test takes forever to run and destroys the internal Ram drive.
       
    23 //
       
    24 
       
    25 LOCAL_D RTest test(_L("T_REGRAM"));
       
    26 
       
    27 void deleteReg()
       
    28 //
       
    29 // Delete everything from the registry
       
    30 //
       
    31 	{
       
    32 	TUid uid;
       
    33 	TRegistryIter riter;
       
    34 	riter.Reset();
       
    35 	while (riter.Next(uid)==KErrNone)
       
    36 		{
       
    37 		TRegistryCategory cat(uid);
       
    38 		cat.DeleteAllItems();
       
    39 		}
       
    40 	}
       
    41 
       
    42 TInt fillReg(const TDesC8 &aDes, TInt aEntries)
       
    43 //
       
    44 // Fill the registry with big nasty monsters
       
    45 // returns the number of entries made.
       
    46 //
       
    47 	{
       
    48 	TBuf8<0x100> buf=_L8("");
       
    49 	TInt i;
       
    50 	for (i=0; i<8; i++)
       
    51 		buf.Append(_L8("Big_ nasty_ monster_ chasing_ me"));
       
    52 	
       
    53 	TUid uid;
       
    54 	uid=TUid::Uid(0x12345678);
       
    55 	TRegistryCategory cat(uid);
       
    56 	i=0;
       
    57 	TBuf8<0x20> item;
       
    58 	while (i<aEntries)
       
    59 		{
       
    60 		item.Format(_L8("%S %08x"),&aDes,i);
       
    61 		TInt r=cat.SetItem(item, buf);
       
    62 		if (r!=KErrNone)
       
    63 			break;
       
    64 		i++;
       
    65 		}
       
    66 	return i;
       
    67 	}
       
    68 
       
    69 TInt shrinkReg(const TDesC8 &aDes, TInt aEntries)
       
    70 //
       
    71 // Delete aEntries entries from the registry
       
    72 // returns number of entries deleted.
       
    73 //
       
    74 	{
       
    75 
       
    76 
       
    77 	TUid uid;
       
    78 	uid=TUid::Uid(0x12345678);
       
    79 	TRegistryCategory cat(uid);
       
    80 	TBuf8<0x20> item;
       
    81 	TInt i=aEntries-1;
       
    82 	while (i>=0)
       
    83 		{
       
    84 		item.Format(_L8("%S %08x"),&aDes,i);
       
    85 		TInt r=cat.DeleteItem(item);
       
    86 		if (r!=KErrNone)
       
    87 			break;
       
    88 		i--;
       
    89 		}
       
    90 	return aEntries-i-1;
       
    91 	}
       
    92 
       
    93 TInt testReg(const TDesC8 &aDes, TInt aEntries)
       
    94 //
       
    95 // Test the first aEntries entries are set correctly
       
    96 //
       
    97 	{
       
    98 	TBuf8<0x100> buf=_L8("");
       
    99 	TInt i;
       
   100 	for (i=0; i<8; i++)
       
   101 		buf.Append(_L8("Big_ nasty_ monster_ chasing_ me"));
       
   102 	
       
   103 	TUid uid;
       
   104 	uid=TUid::Uid(0x12345678);
       
   105 	TRegistryCategory cat(uid);
       
   106 
       
   107 	TBuf8<0x20> item;
       
   108 	TBuf8<0x100> res;
       
   109 	i=0;
       
   110 	while (i<aEntries)
       
   111 		{
       
   112 		item.Format(_L8("%S %08x"),&aDes,i);
       
   113 		if ((i&0x1f)==0)
       
   114 			test.Printf(_L("."));
       
   115 		res=cat.Item(item, _L8("Gone gone gone"));
       
   116 		test(buf==res);
       
   117 		i++;
       
   118 		}
       
   119 	test.Printf(_L("\n"));
       
   120 	return i;
       
   121 	}
       
   122 
       
   123 void test1()
       
   124 //
       
   125 // Test growing and shrinking the registry
       
   126 //
       
   127 	{
       
   128 
       
   129 	test.Start(_L("Cleanout the registry"));
       
   130 	deleteReg();
       
   131 
       
   132 	test.Next(_L("Grow the registry a bit"));
       
   133 	TInt n=fillReg(_L8("Run Away"),40);
       
   134 	test.Printf(_L("Made %d entries\n"), n);
       
   135 	test.Next(_L("Test the content of the registry"));
       
   136 	TInt m=testReg(_L8("Run Away"),n);
       
   137 	test(n==m);
       
   138 	test.Next(_L("Shrink it a bit"));
       
   139 	m=shrinkReg(_L8("Run Away"),n);
       
   140 	test.Printf(_L("Deleted %d entries\n"), m);
       
   141 	test(n==m);
       
   142 
       
   143 	test.Next(_L("Fill the registry with guff"));
       
   144 	test.Printf(_L("\tTHIS TEST TAKES AGES\n"));
       
   145 	n=fillReg(_L8("Run Away"),0x7fffffff);
       
   146 	test.Printf(_L("Made %d entries\n"), n);
       
   147 	test.Next(_L("Test the content of the registry"));
       
   148 	m=testReg(_L8("Run Away"),n);
       
   149 	test.Next(_L("Shrink the registry"));
       
   150 	m=shrinkReg(_L8("Run Away"),n);
       
   151 	test.Printf(_L("Deleted %d entries\n"), m);
       
   152 	test(n==m);
       
   153 
       
   154 	test.Next(_L("Grow a bigish registry"));
       
   155 	n=fillReg(_L8("Lunge"),40);
       
   156 	test.Next(_L("Save the registry"));
       
   157     TUint8* bigPtr=new TUint8[0x8000];
       
   158 	TPtr8 big(bigPtr, 0x8000);
       
   159 	TInt bigsize;
       
   160 	TInt r=User::MachineConfiguration(big,bigsize);
       
   161 	test(r==KErrNone);
       
   162 
       
   163 	test.Next(_L("Clear it"));
       
   164 	deleteReg();
       
   165 
       
   166 	test.Next(_L("Grow a little registry"));
       
   167 	m=fillReg(_L8("Groat"), 10);
       
   168 	test.Next(_L("Save it"));
       
   169     TUint8* littlePtr=new TUint8[0x8000];
       
   170 	TPtr8 little(littlePtr, 0x8000);
       
   171 	TInt littlesize;
       
   172 	r=User::MachineConfiguration(little,littlesize);
       
   173 	test(r==KErrNone);
       
   174 
       
   175 	test.Next(_L("Set to big registry"));
       
   176 	r=User::SetMachineConfiguration(big);
       
   177 	test(r==KErrNone);
       
   178 	test.Next(_L("Test it"));
       
   179 	TInt i=testReg(_L8("Lunge"), n);
       
   180 	test(n==i);
       
   181 
       
   182 	test.Next(_L("Set to little registry"));
       
   183 	r=User::SetMachineConfiguration(little);
       
   184 	test(r==KErrNone);
       
   185 	test.Next(_L("Test it"));
       
   186 	i=testReg(_L8("Groat"), m);
       
   187 	test(i==m);
       
   188 	
       
   189 	test.Next(_L("Set to big registry again"));
       
   190 	deleteReg();
       
   191 	r=User::SetMachineConfiguration(big);
       
   192 	test(r==KErrNone);
       
   193 	test.Next(_L("Test it"));
       
   194 	i=testReg(_L8("Lunge"), n);
       
   195 	test(n==i);
       
   196 
       
   197 	delete bigPtr;
       
   198 	delete littlePtr;
       
   199 	test.End();
       
   200 	}
       
   201 
       
   202 void fillRamDrive(TBusLocalDrive &aRamDrive)
       
   203 //
       
   204 // Fill the ram drive with data
       
   205 //
       
   206 	{
       
   207 
       
   208 	TLocalDriveCapsV2 info;
       
   209 	TPckg<TLocalDriveCapsV2> infoPckg(info);
       
   210 	test(aRamDrive.Caps(infoPckg)==KErrNone);
       
   211 	TInt size=info.iSize.Low();
       
   212 	TInt i;
       
   213 	TPckgBuf<TInt> dataBuf;
       
   214 	TInt &data=dataBuf();
       
   215 	for (i=0; i<size; i+=4)
       
   216 		{
       
   217 		data=i;
       
   218 		aRamDrive.Write(i, dataBuf);
       
   219 		}
       
   220 	}
       
   221 
       
   222 void testRamDrive(TBusLocalDrive &aRamDrive)
       
   223 //
       
   224 // Test the data is still OK
       
   225 //
       
   226 	{
       
   227 
       
   228 	TLocalDriveCapsV2 info;
       
   229 	TPckg<TLocalDriveCapsV2> infoPckg(info);
       
   230 	test(aRamDrive.Caps(infoPckg)==KErrNone);
       
   231 	TInt size=info.iSize.Low();
       
   232 	TInt i;
       
   233 	TPckgBuf<TInt> dataBuf;
       
   234 	TInt &data=dataBuf();
       
   235 	for (i=0; i<size; i+=4)
       
   236 		{
       
   237 		aRamDrive.Read(i, sizeof(TInt), dataBuf);
       
   238 		if (i&0x3ff==0)
       
   239 			test.Printf(_L("."));
       
   240 	//	test.Printf(_L("%08x "), data);
       
   241 		test(data==i);
       
   242 		}
       
   243 	test.Printf(_L("\n"));
       
   244 	}
       
   245 
       
   246 void testGrow()
       
   247 	{
       
   248 
       
   249 	test.Start(_L("Grow the registry a bit"));
       
   250 	TInt n=fillReg(_L8("Run Away"),20);
       
   251 	test(n==20);
       
   252 	test.Next(_L("Test the content of the registry"));
       
   253 	testReg(_L8("Run Away"),n);
       
   254 	test(n==20);
       
   255 	test.End();
       
   256 	}
       
   257 
       
   258 void setRamDriveSize(TBusLocalDrive aRamDrive, TInt aSize)
       
   259 	{
       
   260 
       
   261 	TLocalDriveCapsV2 info;
       
   262 	TPckg<TLocalDriveCapsV2> infoPckg(info);
       
   263 	test(aRamDrive.Caps(infoPckg)==KErrNone);
       
   264 	TInt oldsize=info.iSize.Low();
       
   265 	if (aSize>oldsize)
       
   266 		test(aRamDrive.Enlarge(aSize-oldsize)==KErrNone);
       
   267 	else
       
   268 		test(aRamDrive.ReduceSize(0,oldsize-aSize)==KErrNone);
       
   269 	}
       
   270 
       
   271 GLDEF_C TInt E32Main(void)
       
   272 //
       
   273 // Test the Ram Drive and the Registry
       
   274 //
       
   275 	{
       
   276 
       
   277 	test.Title();
       
   278 
       
   279 	test.Start(_L("Connect to the Local Drive"));
       
   280 	TBool changedFlag;
       
   281 	TBusLocalDrive ramDrive;
       
   282 	TInt r=ramDrive.Connect(0,changedFlag);
       
   283 	test(r==KErrNone);
       
   284 
       
   285 	test.Next(_L("Test testing the ram drive"));
       
   286 	fillRamDrive(ramDrive);
       
   287 	testRamDrive(ramDrive);
       
   288 
       
   289 	test.Next(_L("Simple grow test"));
       
   290 	fillRamDrive(ramDrive);
       
   291 	testGrow();
       
   292 	testRamDrive(ramDrive);	
       
   293 	test.Next(_L("Shrink"));
       
   294 	deleteReg();
       
   295 	testRamDrive(ramDrive);
       
   296 
       
   297 	{
       
   298 	deleteReg();
       
   299 	test.Next(_L("Grow the registry a bit"));
       
   300 	TInt n=fillReg(_L8("Run Away"),40);
       
   301 	test.Next(_L("Test the content of the registry"));
       
   302 	TInt m=testReg(_L8("Run Away"),n);
       
   303 	test(n==m);
       
   304 	test.Next(_L("Shrink it a bit"));
       
   305 	m=shrinkReg(_L8("Run Away"),n);
       
   306 	test(n==m);
       
   307 	test.Next(_L("test ram drive"));
       
   308 	testRamDrive(ramDrive);
       
   309 	}
       
   310 
       
   311 /*	test.Next(_L("Run the tests with the current ram drive size"));
       
   312 	fillRamDrive(ramDrive);
       
   313 	test1();
       
   314 	testRamDrive(ramDrive);*/
       
   315 
       
   316 	test.Next(_L("Run the tests with no Ram Drive"));
       
   317 	setRamDriveSize(ramDrive, 0);
       
   318 	test1();
       
   319 
       
   320 	test.Next(_L("Run the tests with small ram drive"));
       
   321 	setRamDriveSize(ramDrive, 0x3000);
       
   322 	fillRamDrive(ramDrive);
       
   323 	test1();
       
   324 	testRamDrive(ramDrive);
       
   325 
       
   326 	test.Next(_L("Clear the registry"));
       
   327 	deleteReg();
       
   328 
       
   329 /*	test.Next(_L("Run the tests with a big ram drive"));
       
   330 	r=KErrGeneral;
       
   331 	while (r==KErrNone)
       
   332 		r=ramDrive.Enlarge(0x1000);
       
   333 	test.Printf(_L("%d"), r);
       
   334 //	test(r==KErrDiskFull);
       
   335 	r=ramDrive.ReduceSize(0, 0x2000);
       
   336 	test(r==KErrNone);
       
   337 	fillRamDrive(ramDrive);
       
   338 	test1();
       
   339 	testRamDrive(ramDrive);*/
       
   340 
       
   341 	ramDrive.Disconnect();
       
   342 
       
   343 	test.End();
       
   344 	return(KErrNone);
       
   345 	}
       
   346