os/persistentdata/persistentstorage/centralrepository/pccenrep/test/common_neg.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2008-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 //
    15 
    16 #ifdef CENREP_PC_TEST
    17 	#include <x86tool/centralrepository.h>
    18 #else
    19 	#include <centralrepository.h>
    20 #endif
    21 
    22 #include <e32test.h>
    23 #include <f32file.h>
    24 
    25 enum TestMode{
    26 	ETxt,
    27 	ECre,
    28 	EAutoCre,
    29 	EAutoTxt
    30 };
    31 
    32 //DEFINED IN THE TEST MAIN CPP
    33 extern void SetupEnv(const TDesC& aInFilePath,const TDesC& aOutFilePath,TUint aTestMode);
    34 extern RFs TheFs;
    35 extern RTest TheTest;
    36 
    37 using namespace NCentralRepositoryConstants;
    38 
    39 _LIT(KLongBuf16,"sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss\
    40 	ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss\
    41 	ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss\
    42 	ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss\
    43 	ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss\
    44 	ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss\
    45 	ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss\
    46 	ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss\
    47 	ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss\
    48 	ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss\
    49 	ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss\
    50 	ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss");
    51 
    52 
    53 ////////////////////////////////////////////////////////////////////////////////
    54 // Test macro and function.
    55 
    56 LOCAL_C void Check(TInt aValue, TInt aLine)
    57 	{
    58 	if(!aValue)
    59 		{
    60 		TheTest(EFalse, aLine);
    61 		}
    62 	}
    63 
    64 LOCAL_C void Check(TInt aValue, TInt aExpected, TInt aLine)
    65 	{
    66 	if(aValue != aExpected)
    67 		{
    68 		RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
    69 		TheTest(EFalse, aLine);
    70 		}
    71 	}
    72 
    73 #define TEST(arg) ::Check((arg), __LINE__)
    74 #define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
    75 
    76 ////////////////////////////////////////////////////////////////////////////////
    77 
    78 /**
    79 NEWL Negative Testing
    80 */
    81 void NewL(TUint aTestMode)
    82 	{
    83 	CRepository* repos=NULL;
    84 	//Negative testing to do with initialising the repository
    85 	
    86 	//-repository file not found
    87 	TUid nonexistentRepository={0x88888888};
    88 	TRAPD(err,repos=CRepository::NewL(nonexistentRepository));
    89 	TEST2(err, KErrNotFound);
    90 	
    91 	//-repository file corrupt
    92 	TUid corruptRepositoryUid;
    93 	if (aTestMode==EAutoCre)
    94 		corruptRepositoryUid=TUid::Uid(0x88888881);
    95 	else
    96 		corruptRepositoryUid=TUid::Uid(0x88888880);
    97 	TRAP(err,repos=CRepository::NewL(corruptRepositoryUid));
    98 	TEST2(err, KErrCorrupt);
    99 	
   100 	delete repos;
   101 	}
   102 
   103 
   104 void GetFunction(CRepository* aRepository)
   105 	{
   106 	TInt error;
   107 	TInt intVal;
   108 	TReal realVal;
   109 	TBuf<50> longStr;
   110 	
   111 	//GENERAL
   112 	//-Setting does not exist
   113 	error = aRepository->Get(100, intVal);
   114 	TEST2(error, KErrNotFound);
   115 	error = aRepository->Get(100, realVal);
   116 	TEST2(error, KErrNotFound);
   117 	error = aRepository->Get(100, longStr);
   118 	TEST2(error, KErrNotFound);
   119 	
   120 	//-Setting exist but not requested type(mismatch)
   121 	//INT
   122 	error = aRepository->Get(1, realVal);
   123 	TEST2(error, KErrArgument);
   124 	error = aRepository->Get(1, longStr);
   125 	TEST2(error, KErrArgument);
   126 	
   127 	//REAL
   128 	error = aRepository->Get(2, intVal);
   129 	TEST2(error, KErrArgument);
   130 	error = aRepository->Get(2, longStr);
   131 	TEST2(error, KErrArgument);
   132 	
   133 	//DESC8,DESC16
   134 	error = aRepository->Get(0x300, intVal);
   135 	TEST2(error, KErrArgument);
   136 	error = aRepository->Get(0x300, realVal);
   137 	TEST2(error, KErrArgument);
   138 	
   139 	//-The buffer allocated too small
   140 	TBuf8<4> short8BitStr;
   141 	error = aRepository->Get(11, short8BitStr);
   142 	TEST2(error, KErrOverflow);
   143 	//-The bufer allocated is ok
   144 	TBuf8<12> good8BitStr;
   145 	error = aRepository->Get(11, good8BitStr);
   146 	TEST2(error, KErrNone);
   147 	//-The buffer allocated is too big(not a problem)
   148 	TBuf8<50> long8BitStr;
   149 	error = aRepository->Get(11, long8BitStr);
   150 	TEST2(error, KErrNone);
   151 	//DESC8,DESC16(with actual length)
   152 	//-The buffer allocated too small
   153 	TBuf<4> shortStr;
   154 	error = aRepository->Get(0x300, shortStr);
   155 	TEST2(error, KErrOverflow);
   156 	//-The bufer allocated is ok
   157 	TBuf<11> goodStr;
   158 	error = aRepository->Get(0x300, goodStr);
   159 	TEST2(error, KErrNone);
   160 	//-The buffer allocated is too big(not a problem)
   161 	error = aRepository->Get(0x300, longStr);
   162 	TEST2(error, KErrNone);
   163 	}
   164 	
   165 void GetMeta(CRepository* aRepository)
   166 	{
   167 	//-Setting does not exist	
   168 	TUint32 meta;
   169 	TInt error;
   170 	error = aRepository->GetMeta(100, meta);
   171 	TEST2(error, KErrNotFound);
   172 	}
   173 
   174 void FindFunctionL(CRepository* aRepository)
   175 	{
   176 	RArray<TUint32> keyList;
   177 	RArray<TUint32> shortKeyList(1);
   178 	CleanupClosePushL(keyList);
   179 	CleanupClosePushL(shortKeyList);
   180 	
   181 	TInt error;
   182 	
   183 	//FindL
   184 	//-Partial setting list requested not found(based on partialid and mask)
   185 	error = aRepository->FindL(0x0000FFFF, 0x0000F000, keyList);
   186 	TEST2(error, KErrNotFound);
   187 	
   188 	//-purposely initially allocated a array with small size(force it to grow)
   189 	error = aRepository->FindL(0xFFFFFFFF, 0x0, shortKeyList);
   190 	TEST2(error, KErrNone);
   191 	TEST2(shortKeyList.Count(), 33);
   192 	
   193 	//FindEqL/FindNeqL
   194 	keyList.Reset();
   195 	error = aRepository->FindEqL(0xFFFFFFFF, 0, 199, keyList);
   196 	TEST2(error, KErrNotFound);
   197 
   198 	//GENERAL
   199 	//-Partial setting list requested not found(based on partialid and mask)
   200 	keyList.Reset();
   201 	error = aRepository->FindEqL(0x0000FFFF, 0x0000F000, 100, keyList);
   202 	TEST2(error, KErrNotFound);
   203 	keyList.Reset();
   204 	error = aRepository->FindNeqL(0x0000FFFF, 0x0000F000, 100, keyList);
   205 	TEST2(error, KErrNotFound);
   206 	
   207 	//-Partial setting list found but some setting not the TYPE as requested(ensure not returned)
   208 	//INT
   209 	keyList.Reset();
   210 	error = aRepository->FindEqL(0x00100000, 0xFFFFF0FF, 88, keyList);
   211 	TEST2(error, KErrNone);
   212 	TEST2(keyList.Count(), 2);
   213 	
   214 	keyList.Reset();
   215 	error = aRepository->FindNeqL(0x00100000, 0xFFFFF0FF, 88, keyList);
   216 	TEST2(error, KErrNone);
   217 	TEST2(keyList.Count(), 3);
   218 	
   219 	//REAL
   220 	keyList.Reset();
   221 	error = aRepository->FindEqL(0x00100000, 0xFFFFF0FF, 88.0, keyList);
   222 	TEST2(error, KErrNone);
   223 	TEST2(keyList.Count(), 2);
   224 	
   225 	keyList.Reset();
   226 	error = aRepository->FindNeqL(0x00100000, 0xFFFFF0FF, 88.0, keyList);
   227 	TEST2(error, KErrNone);
   228 	TEST2(keyList.Count(), 3);
   229 
   230 	_LIT(KString16, "88");
   231 	
   232 	keyList.Reset();
   233 	error = aRepository->FindEqL(0x00100000, 0xFFFFF0FF, KString16, keyList);
   234 	TEST2(error, KErrNone);
   235 	TEST2(keyList.Count(), 1);
   236 	
   237 	keyList.Reset();
   238 	error = aRepository->FindNeqL(0x00100000, 0xFFFFF0FF, KString16, keyList);
   239 	TEST2(error, KErrNone);
   240 	TEST2(keyList.Count(), 4);
   241 	
   242 	_LIT(KString8, "88");
   243 	
   244 	keyList.Reset();
   245 	error = aRepository->FindEqL(0x00100000, 0xFFFFF0FF, KString8, keyList);
   246 	TEST2(error, KErrNone);
   247 	TEST2(keyList.Count(), 1);
   248 	
   249 	keyList.Reset();
   250 	error = aRepository->FindNeqL(0x00100000, 0xFFFFF0FF, KString8, keyList);
   251 	TEST2(error, KErrNone);
   252 	TEST2(keyList.Count(), 4);
   253 	
   254 	//-A value specified greater than the max limit allowed KMaxBinaryLength(8 bit) and KMaxUnicodeStringLength(16 bit)
   255 	TBuf8<KMaxUnicodeStringLength*3> KLongBuf8;
   256 	KLongBuf8.Copy(KLongBuf16);
   257 	KLongBuf8.Append(KLongBuf16);
   258 
   259 	keyList.Reset();
   260 	error = aRepository->FindEqL(0xFFFFFFFF, 0, KLongBuf8, keyList);
   261 	TEST2(error, KErrNotFound);
   262 	keyList.Reset();
   263 	error = aRepository->FindNeqL(0xFFFFFFFF, 0, KLongBuf8, keyList);
   264 	TEST2(error, KErrNone);
   265 	TEST2(shortKeyList.Count(), 33);
   266 	
   267 	keyList.Reset();
   268 	error = aRepository->FindEqL(0xFFFFFFFF, 0, KLongBuf16, keyList);
   269 	TEST2(error, KErrNotFound);
   270 	keyList.Reset();
   271 	error = aRepository->FindNeqL(0xFFFFFFFF, 0, KLongBuf16, keyList);
   272 	TEST2(error, KErrNone);
   273 	TEST2(shortKeyList.Count(), 33);
   274 	
   275 	//-A value specified empty
   276 	_LIT8(KEmptyStr8, "");
   277 	keyList.Reset();
   278 	error = aRepository->FindEqL(0xFFFFFFFF, 0, KEmptyStr8, keyList);
   279 	TEST2(error, KErrNotFound);
   280 	keyList.Reset();
   281 	error = aRepository->FindNeqL(0xFFFFFFFF, 0, KEmptyStr8, keyList);
   282 	TEST2(error, KErrNone);
   283 	TEST2(shortKeyList.Count(), 33);
   284 	
   285 	_LIT16(KEmptyStr16, "");
   286 	keyList.Reset();
   287 	error = aRepository->FindEqL(0xFFFFFFFF, 0, KEmptyStr16, keyList);
   288 	TEST2(error, KErrNotFound);
   289 	keyList.Reset();
   290 	error = aRepository->FindNeqL(0xFFFFFFFF, 0, KEmptyStr16, keyList);
   291 	TEST2(error, KErrNone);
   292 	TEST2(shortKeyList.Count(), 33);
   293 	
   294 	CleanupStack::PopAndDestroy(2);
   295 	}
   296 
   297 void SetFunction(CRepository* aRepository)
   298 	{
   299 	//----------SET
   300 	//GENERAL:
   301 	//-Setting exist but not requested type(mismatch)
   302 	//-If setting does not exist, it creates one(also check the expected meta)
   303 	//INT
   304 	//REAL
   305 	//DESC8,DESC16
   306 	//-Try to set a setting that is greater than KMaxBinaryLength(8 bit) and KMaxUnicodeStringLength(16 bit)
   307 	//-Try to set a setting with empty string content(is this a problem)
   308 		
   309 	TInt intVal=0;
   310 	TReal realVal=0;
   311 	TUint32 metaVal=0;
   312 	
   313 	//INT
   314 	TInt err=aRepository->Set(0x2,intVal);
   315 	TEST2(err, KErrArgument);
   316 	err=aRepository->Set(0x888,888);
   317 	TEST2(err, KErrNone);
   318 	err=aRepository->Get(0x888,intVal);
   319 	TEST2(intVal, 888);
   320 	//will use default one
   321 	err=aRepository->GetMeta(0x888,metaVal);
   322 	TEST2(metaVal, 0x10);
   323 	
   324 	//REAL
   325 	err=aRepository->Set(0x1,realVal);
   326 	TEST2(err, KErrArgument);
   327 	err=aRepository->Set(0x333,333.33);
   328 	TEST2(err, KErrNone);
   329 	err=aRepository->Get(0x333,realVal);
   330 	TEST(realVal == 333.33);
   331 	//will use range one
   332 	err=aRepository->GetMeta(0x333,metaVal);
   333 	TEST2(metaVal, 0x20);
   334 	
   335 	//DESC8,DESC16
   336 	_LIT8(KBuf8,"full");
   337 	err=aRepository->Set(0x1,KBuf8());
   338 	TEST2(err, KErrArgument);
   339 	_LIT(KBuf16,"full");
   340 	err=aRepository->Set(0x1,KBuf16());
   341 	TEST2(err, KErrArgument);
   342 	err=aRepository->Set(0x111,KBuf16());
   343 	TEST2(err, KErrNone);
   344 	TBuf16<255> buffer16;
   345 	err=aRepository->Get(0x111,buffer16);
   346 	TEST2(err, KErrNone);
   347 	TEST2(buffer16.Compare(KBuf16()), 0);
   348 	err=aRepository->GetMeta(0x111,metaVal);
   349 	//will use range one
   350 	err=aRepository->GetMeta(0x111,metaVal);
   351 	TEST2(metaVal, 0x20);	
   352 	
   353 	//Set an existing string setting with string longer than limit(KMaxUnicodeStringLength)
   354 	TBuf8<KMaxUnicodeStringLength*3> KLongBuf8;
   355 	KLongBuf8.Copy(KLongBuf16());
   356 	KLongBuf8.Append(KLongBuf16());
   357 
   358 	//try it on existing string setting
   359 	err=aRepository->Set(0x222,KLongBuf16());
   360 	TEST2(err, KErrArgument);
   361 	err=aRepository->Set(0x222,KLongBuf8);
   362 	TEST2(err, KErrArgument);
   363 	
   364 	//set an empty string content
   365 	_LIT8(KEmptyString8,"");
   366 	err=aRepository->Set(0x444,KEmptyString8());
   367 	TEST2(err, KErrNone);
   368 	_LIT16(KEmptyString16,"");
   369 	err=aRepository->Set(0x555,KEmptyString16());
   370 	TEST2(err, KErrNone);
   371 	}
   372 
   373 void CreateFunction(CRepository* aRepository)
   374 	{
   375 	//----------CREATE
   376 	//GENERAL:
   377 	//-Setting already exist
   378 	//INT
   379 	//REAL
   380 	//DESC8,DESC16
   381 	//-Try to create a setting that is greater than KMaxBinaryLength(8 bit) and KMaxUnicodeStringLength(16 bit)
   382 	//-Try to set a setting with empty string content(is this a problem)
   383 	TInt intVal = 0;
   384 	TReal realVal = 0.0;
   385 
   386 	//INT
   387 	TInt err=aRepository->Create(1,intVal);
   388 	TEST2(err, KErrAlreadyExists);
   389 	//REAL
   390 	err=aRepository->Create(2,realVal);
   391 	TEST2(err, KErrAlreadyExists);
   392 	//DESC8,DESC16
   393 	_LIT8(KString8,"string");
   394 	_LIT16(KString16,"string");	
   395 	err=aRepository->Create(5,KString8);
   396 	TEST2(err, KErrAlreadyExists);
   397 	err=aRepository->Create(5,KString16);
   398 	TEST2(err, KErrAlreadyExists);		
   399 	
   400 	//empty string
   401 	_LIT8(KEmptyString8,"");
   402 	_LIT8(KEmptyString16,"");
   403 	err=aRepository->Create(0x1111,KEmptyString8());
   404 	TEST2(err, KErrNone); 
   405 	err=aRepository->Create(0x2222,KEmptyString16());
   406 	TEST2(err, KErrNone);
   407 	
   408 	//Create an existing string setting with string longer than limit(KMaxUnicodeStringLength)
   409 	TBuf8<KMaxUnicodeStringLength*3> KLongBuf8;
   410 	KLongBuf8.Copy(KLongBuf16());
   411 	KLongBuf8.Append(KLongBuf16());
   412 
   413 	err=aRepository->Create(0x3333,KLongBuf16());
   414 	TEST2(err, KErrArgument);
   415 	err=aRepository->Create(0x4444,KLongBuf8);
   416 	TEST2(err, KErrArgument);
   417 	}
   418 
   419 void DeleteFunction(CRepository* aRepository)
   420 	{
   421 	//----------DELETE SINGLE
   422 	//-Setting does not exist
   423 	//-Delete a setting and then recreate,see original meta data preserved(vary with default,range,single)
   424 	//-When deleted always verify setting does not exist
   425 	TInt err=aRepository->Delete(0x757);
   426 	TEST2(err, KErrNotFound);
   427 	
   428 	//delete and existing setting with single meta, then recreate it, expect same meta before deletion
   429 	TReal realVal;
   430 	TUint32 metaVal;
   431 	//this is the original setting meta
   432 	err=aRepository->GetMeta(2,metaVal);
   433 	TEST2(metaVal, 0xa);
   434 	err=aRepository->Delete(2);
   435 	TEST2(err, KErrNone);
   436 	err=aRepository->Get(2,realVal);
   437 	TEST2(err, KErrNotFound);
   438 	err=aRepository->Create(2,realVal);
   439 	TEST2(err, KErrNone);
   440 	TUint32 newMetaVal;
   441 	err=aRepository->GetMeta(2,newMetaVal);
   442 	TEST2(err, KErrNone);
   443 	TEST2(newMetaVal, metaVal);
   444 	
   445 	
   446 	//----------DELETE RANGE
   447 	//-Partial setting list requested not found(based on partialid and mask)
   448 	//-Check the aErrorKey returned with each of the negative testing.
   449 	TUint32 error;
   450 	err=aRepository->Delete(0x0F010000,0xFFFFF0FF,error);
   451 	TEST2(err, KErrNotFound);
   452 	//if not found, the error key here will be set to the partial ID
   453 	TEST2(error, 0x0F010000);
   454 
   455 	}
   456 
   457 void MoveFunction(CRepository* aRepository)
   458 	{
   459 	//-Partial setting list requested not found(based on partialid and mask)
   460 	//-Intended target setting already exist(say 1 out of X)
   461 	TUint32 errorId;	
   462 	TInt err=aRepository->Move(0x0F010000,0x0E010000,0xFFFFF0FF,errorId);
   463 	TEST2(err, KErrNotFound);
   464 	TEST2(errorId, 0x0F010000);
   465 
   466 	
   467 	err=aRepository->Move(0x03010000,0x02010000,0xFFFFF0FF,errorId);
   468 	TEST2(err, KErrAlreadyExists);
   469 	//errorId will be set to the first id of the target when error is encountered
   470 	TEST2(errorId, 0x02010100);
   471 	}
   472 
   473 /**
   474 */
   475 void NegativeTestingL(TUid aUid,const TDesC& aInFile,const TDesC& aOutFile,TUint aTestMode)
   476 	{
   477 	SetupEnv(aInFile,aOutFile,aTestMode);
   478 	
   479 	__UHEAP_MARK;
   480 	
   481 	//repository initialising negative testing
   482 	NewL(aTestMode);
   483 	
   484 	//ok repository but operation negative testing
   485 	CRepository* repository=CRepository::NewLC(aUid);
   486 
   487 	//these are all negative testing specifically each of the error code
   488 	GetFunction(repository);
   489 	GetMeta(repository);
   490 	FindFunctionL(repository);
   491 	SetFunction(repository);
   492 	CreateFunction(repository);
   493 	DeleteFunction(repository);
   494 	MoveFunction(repository);
   495 
   496 	CleanupStack::PopAndDestroy();	
   497 	
   498 	__UHEAP_MARKEND;
   499 	}
   500 
   501 TInt CopyFile(const TDesC& aSource, const TDesC& aTarget)
   502 	{
   503 	RFile file;
   504 	TInt ret=file.Open(TheFs,aSource,EFileRead);
   505 	if (ret!=KErrNone)
   506 		return ret;
   507 	TInt fileSize;
   508 	file.Size(fileSize);
   509 	HBufC8* buf=HBufC8::New(fileSize);
   510 	if (!buf)
   511 		{
   512 		file.Close();
   513 		return KErrNoMemory;
   514 		}
   515 	TPtr8 mod(buf->Des());
   516 	file.Read(mod);
   517 	file.Close();
   518 	ret=file.Replace(TheFs,aTarget,EFileWrite);
   519 	if (ret==KErrNone)
   520 		{
   521 		file.Write(*buf);
   522 		}
   523 	file.Close();
   524 	delete buf;
   525 	return ret;
   526 	}