phonebookengines_old/contactsmodel/tsrc/t_dbnametest.cpp
changeset 40 b46a585f6909
equal deleted inserted replaced
37:fd64c38c277d 40:b46a585f6909
       
     1 // Copyright (c) 2007-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 /**
       
    17 @SYMTestCaseID      PIM-APPENG-CNTM-CNTDB-106281-0001
       
    18 @SYMTestCaseDesc    The purpose of this test is to verify the expected behaviour
       
    19                     of the CContactDatabase public APIs involving invalid database
       
    20                     file paths (eg. string length exceeds 190 characters,
       
    21                     and string containing path characters)
       
    22 @SYMTestActions     Passing an empty string, a string with more than 190 characters,
       
    23                     and a string containing path into CContactDatabase's public APIs and
       
    24                     verify if the expected error code is returned
       
    25 @SYMTestExpectedResults     Expected all test cases to pass.
       
    26 @SYMDEF                     DEF106281
       
    27 @SYMTestType                UT
       
    28 @SYMTestPriority            Low
       
    29 */
       
    30 
       
    31 #include <cntdb.h>
       
    32 #include "CCntIpcCodes.h"
       
    33 #include "t_dbnametest.h"
       
    34 
       
    35 //===========================================================
       
    36 // CDbNameTest Class
       
    37 //===========================================================
       
    38 CDbNameTest::~CDbNameTest()
       
    39     {
       
    40     iLongDbName.Close();
       
    41     iInvalidDbName.Close();
       
    42 
       
    43     if (iActiveSchedulerWait)
       
    44         {
       
    45         if (iActiveSchedulerWait->IsStarted())
       
    46             {
       
    47             iActiveSchedulerWait->AsyncStop();
       
    48             }
       
    49         delete iActiveSchedulerWait;
       
    50         }
       
    51 
       
    52     iTest->Close();
       
    53     delete iTest;
       
    54     }
       
    55 
       
    56 CDbNameTest::CDbNameTest()
       
    57     : CActive(EPriorityStandard)
       
    58     {
       
    59     CActiveScheduler::Add(this);
       
    60     }
       
    61 
       
    62 CDbNameTest* CDbNameTest::NewLC()
       
    63     {
       
    64     CDbNameTest* self = new (ELeave) CDbNameTest();
       
    65     CleanupStack::PushL(self);
       
    66     self->ConstructL();
       
    67     return self;
       
    68     }
       
    69 
       
    70 void CDbNameTest::ConstructL()
       
    71     {
       
    72     iTest = new(ELeave) RTest(_L("T_DBNameTest"));
       
    73 
       
    74     // Create invalid DB Name (with wildcard & path characters)
       
    75     iInvalidDbName.Create(_L("c:**"));
       
    76     }
       
    77 
       
    78 void CDbNameTest::RunTestsL()
       
    79     {
       
    80     iTest->Start(_L(" ----------- T_DBNameTest ---------- "));
       
    81 	
       
    82 	RArray<TInt> errArray;
       
    83 	CleanupClosePushL(errArray);
       
    84 	
       
    85 	//MaxLength	
       
    86 	errArray.AppendL(KErrNone);  //Create
       
    87 	errArray.AppendL(KErrNone);     //Open
       
    88 	errArray.AppendL(KErrNone); //Replace
       
    89 	errArray.AppendL(KErrNone);    //Delete
       
    90 	errArray.AppendL(KErrNone);    //Exists
       
    91 	SetAndTestFileNameL(EMaxLength, errArray);
       
    92 	
       
    93 	//ELessThanMaxLength
       
    94 	errArray.Reset();	
       
    95 	errArray.AppendL(KErrNone);  //Create
       
    96 	errArray.AppendL(KErrNone); //Open
       
    97 	errArray.AppendL(KErrNone); //Replace
       
    98 	errArray.AppendL(KErrNone); //Delete
       
    99 	errArray.AppendL(KErrNone); //Exists
       
   100 	SetAndTestFileNameL(ELessThanMaxLength, errArray);
       
   101 
       
   102 	//ENoDriveName
       
   103 	errArray.Reset();	
       
   104 	errArray.AppendL(KErrBadName); //Create
       
   105 	errArray.AppendL(KErrBadName); //Open
       
   106 	errArray.AppendL(KErrBadName); //Replace
       
   107 	errArray.AppendL(KErrBadName); //Delete
       
   108 	errArray.AppendL(KErrBadName); //Exists
       
   109 	SetAndTestFileNameL(ENoDriveName, errArray);
       
   110 		
       
   111 	//EInvalidFileName
       
   112 	errArray.Reset();
       
   113 	errArray.AppendL(KErrBadName); //Create
       
   114 	errArray.AppendL(KErrBadName); //Open
       
   115 	errArray.AppendL(KErrBadName); //Replace
       
   116 	errArray.AppendL(KErrBadName); //Delete
       
   117 	errArray.AppendL(KErrBadName); //Exists
       
   118 	SetAndTestFileNameL(EInvalidFileName, errArray);
       
   119 	
       
   120 	//ENullFileName	
       
   121 	errArray.Reset();
       
   122 	errArray.AppendL(KErrBadName);  //Create
       
   123 	errArray.AppendL(KErrBadName);  //Open
       
   124 	errArray.AppendL(KErrBadName);  //Replace
       
   125 	errArray.AppendL(KErrNotFound); //Delete (expects no default database in the private directory) 
       
   126 	errArray.AppendL(KErrNone);     //Exists
       
   127 	//deleting default database if exists.
       
   128 	TRAP_IGNORE(CContactDatabase::DeleteDatabaseL(KNullDesC));
       
   129 	SetAndTestFileNameL(ENullFileName, errArray);
       
   130 	
       
   131 	//EMoreThanMaxLength
       
   132 	errArray.Reset();
       
   133 	errArray.AppendL(KErrArgument); //Create
       
   134 	errArray.AppendL(KErrArgument); //Open
       
   135 	errArray.AppendL(KErrArgument); //Replace
       
   136 	errArray.AppendL(KErrArgument); //Delete
       
   137 	errArray.AppendL(KErrArgument); //Exists
       
   138 	SetAndTestFileNameL(EMoreThanMaxLength, errArray);
       
   139    
       
   140     CleanupStack::PopAndDestroy(); //Releasing memory used by errArray.
       
   141     
       
   142     //EMoreThanMaxLength above creates a filename with length more than allowed length, Test Asyncopendatabasetest
       
   143     //requires such a file name, so it should be run after above test.
       
   144   
       
   145     AsyncOpenDatabaseTest();
       
   146 
       
   147     (*iTest) (iFailedTests == 0);
       
   148 
       
   149     iTest->End();
       
   150     }
       
   151 
       
   152 
       
   153 void CDbNameTest::AsyncOpenDatabaseTest()
       
   154     {
       
   155     // open database with long path name
       
   156     iTest->Next(_L("TEST 1: CContactDatabase::Open() with long file name"));
       
   157 
       
   158     iCurrName.Set(iLongDbName);
       
   159 
       
   160     SetActive();
       
   161     TRequestStatus* status = &iStatus;
       
   162     iOpenOp = CContactDatabase::Open(iLongDbName, *status);
       
   163 
       
   164     iActiveSchedulerWait = new (ELeave) CActiveSchedulerWait();
       
   165     iActiveSchedulerWait->Start();
       
   166     }
       
   167 
       
   168 void CDbNameTest::AsyncOpenDatabaseNullTestL()
       
   169     {
       
   170     // open database with NULL path name
       
   171     iTest->Next(_L("TEST 2: CContactDatabase::Open() with NULL file name."));
       
   172 
       
   173     iCurrName.Set(KNullDesC);
       
   174 
       
   175     SetActive();
       
   176     TRequestStatus* status = &iStatus;
       
   177     iOpenOp = CContactDatabase::Open(KNullDesC, *status);
       
   178     }
       
   179 
       
   180 void CDbNameTest::AsyncOpenDatabaseInvalidTest()
       
   181     {
       
   182     // open database with invalid path name
       
   183     iTest->Next(_L("TEST 3: CContactDatabase::Open() with file name containing path or wildcard characters."));
       
   184 
       
   185     iCurrName.Set(iInvalidDbName);
       
   186 
       
   187     SetActive();
       
   188     TRequestStatus* status = &iStatus;
       
   189     iOpenOp = CContactDatabase::Open(iInvalidDbName, *status);
       
   190     }
       
   191 
       
   192 void CDbNameTest::RunL()
       
   193     {
       
   194     TInt err = iStatus.Int();
       
   195 
       
   196     if (iOpenOp)
       
   197         {
       
   198         if (err == KErrNone)
       
   199             {
       
   200             // if the database has been successfully opened, close it
       
   201             CContactDatabase* database = iOpenOp->TakeDatabase();
       
   202             if (database)
       
   203                 {
       
   204                 delete database;
       
   205                 }
       
   206             }
       
   207         delete iOpenOp;
       
   208         iOpenOp = NULL;
       
   209         }
       
   210 
       
   211     if (iLongDbName == iCurrName)
       
   212         {
       
   213         if (err != KErrArgument)
       
   214             {
       
   215             iFailedTests++;
       
   216             iTest->Printf(_L("-> FAILED - invalid error code returned-- %d.\n"), err);
       
   217             }
       
   218         
       
   219         // Proceed to test 2
       
   220         AsyncOpenDatabaseNullTestL();
       
   221         }
       
   222     else if (iCurrName.Length() == 0)
       
   223         {
       
   224         // Asyc Open treats a NULL database path as the default database.
       
   225         // Therefore, both KErrNone, (meaning a default database exists), and 
       
   226         // KErrNotFound, (meaning the default database not yet exist), are acceptable
       
   227         // error codes.
       
   228         if (err != KErrNone && err != KErrNotFound)
       
   229             {
       
   230             iFailedTests++;
       
   231             iTest->Printf(_L("-> FAILED - invalid error code returned-- %d.\n"), err);
       
   232             }
       
   233 
       
   234         // Process to last test
       
   235         AsyncOpenDatabaseInvalidTest();
       
   236         }
       
   237     else
       
   238         {
       
   239         if (err != KErrBadName)
       
   240             {
       
   241             iFailedTests++;
       
   242             iTest->Printf(_L("-> FAILED - invalid error code returned-- %d.\n"), err);
       
   243             }
       
   244 
       
   245         iActiveSchedulerWait->AsyncStop();
       
   246         }
       
   247     }
       
   248 
       
   249 
       
   250 /**
       
   251 @SYMTestCaseID      		PIM-DBNAMETEST-0001
       
   252 @SYMTestCaseDesc    		Attempts to test the behaviour of Contactdatabse API's
       
   253 							while Opening, Deleting, Creating, Replacing a filename of different types. 
       
   254 @SYMTestActions     		1)Create a filename, it can be of different types.
       
   255 							2)Try to perform Create,Open,Delete,Replace on that filename.
       
   256 							3)Check if it returns a correct errorcode.						
       
   257 @SYMTestExpectedResults     Expected all test cases to pass.
       
   258 @SYMDEF                     DEF114180
       
   259 @SYMTestType                UT
       
   260 @SYMTestPriority            Low
       
   261 */
       
   262     
       
   263 void CDbNameTest::SetAndTestFileNameL(TFileNameType aType, RArray<TInt>& aErrArray)
       
   264 	{
       
   265 	_LIT16(KDrive,"c:");
       
   266 	
       
   267 	TBool driveName = ETrue;
       
   268 	TBool invalidFileName = EFalse;
       
   269 	TBool createFilename = ETrue;
       
   270 	TBool nullFilename = EFalse;
       
   271 	TInt nameLength = 0;
       
   272 	
       
   273 	
       
   274 	switch (aType)
       
   275 		{
       
   276 		case ENoDriveName:
       
   277 			{
       
   278 			iTest->Next(_L("\n*****Operations without Drive name.*****"));
       
   279 			nameLength = 50;
       
   280 			driveName = EFalse;	
       
   281 			}
       
   282 		break;
       
   283 		
       
   284 		case EMoreThanMaxLength:
       
   285 			{
       
   286 			iTest->Next(_L("\n*****Operations with Filename more than maximum allowed length.*****"));
       
   287 			nameLength = 200;	
       
   288 			}
       
   289 		break;
       
   290 		
       
   291 		case EMaxLength:
       
   292 			{
       
   293 			iTest->Next(_L("\n*****Operations with Filename having maximum allowed length.*****"));
       
   294 			nameLength = 190;	
       
   295 			}
       
   296 		break;
       
   297 		
       
   298 		case ELessThanMaxLength:
       
   299 			{
       
   300 			iTest->Next(_L("\n*****Operations with Filename length less than maximum allowed length.*****"));
       
   301 			nameLength = 100;	
       
   302 			}
       
   303 		break;
       
   304 		
       
   305 		case  EInvalidFileName:
       
   306 			{
       
   307 			iTest->Next(_L("\n*****Operations with Invalid Filename.*****"));
       
   308 			invalidFileName = ETrue;
       
   309 			createFilename = EFalse;
       
   310 			driveName = EFalse;
       
   311 			nameLength = 5;	
       
   312 			}
       
   313 		break;
       
   314 		
       
   315 		case ENullFileName:
       
   316 			{
       
   317 			iTest->Next(_L("\n*****Operations with NULL Filename.*****"));			
       
   318 			nullFilename = ETrue;
       
   319 			createFilename = EFalse;
       
   320 			driveName = EFalse;	
       
   321 			}
       
   322 		break;	
       
   323 		
       
   324 		default:
       
   325 		User::Leave(KErrArgument);
       
   326 		
       
   327 		}
       
   328 	
       
   329 	iLongDbName.Close();
       
   330 	iLongDbName.CreateL(nameLength);
       
   331 	
       
   332 	if(driveName)
       
   333 		{
       
   334 		iLongDbName.Append(KDrive);	
       
   335 		}
       
   336 	
       
   337 	if(invalidFileName)
       
   338 		{
       
   339 		iLongDbName = iInvalidDbName;	
       
   340 		}
       
   341 		
       
   342 	if(nullFilename)
       
   343 		{
       
   344 		iLongDbName.Append(KNullDesC);	
       
   345 		}
       
   346 		
       
   347 	if(createFilename)
       
   348 		{
       
   349 	    for (TInt i = 1; i < iLongDbName.MaxLength()-2; i++)
       
   350 	        {
       
   351 	        TChar ch('a');
       
   352 	        ch += i % 26;
       
   353 	        iLongDbName.Append(ch);
       
   354 	        }		
       
   355 		}
       
   356 
       
   357 	TInt typeOfTests = 5;
       
   358  	for(TInt loop = 0;loop < typeOfTests ; ++loop)
       
   359 	 	{
       
   360 	 	
       
   361 	 	CContactDatabase* db = NULL;
       
   362 	 	
       
   363 	 	TInt err(0);
       
   364 	 	
       
   365 	 	TInt typeOfError(0);
       
   366 	 	
       
   367 	 	switch(loop)
       
   368 	 	{
       
   369 	 		case 0:   // CREATE DATABASE TEST
       
   370 	 	 		{
       
   371 		 		iTest->Printf(_L("TEST : CContactDatabase::CreateL() ."));
       
   372 		    	
       
   373 		    	TRAP(err, db = CContactDatabase::CreateL(iLongDbName));
       
   374 		    	
       
   375 		    	if (err == aErrArray[0])
       
   376 			    	{
       
   377 			    	typeOfError = 1;					
       
   378 			    	}
       
   379 		    	else if(err == KErrAlreadyExists || err == KErrBadName || err == KErrDiskFull || err == KErrArgument)
       
   380 			    	{
       
   381 			     	typeOfError = 2;	
       
   382 			    	}
       
   383 				} 
       
   384 	     	break;
       
   385 	 
       
   386 	 		case 1:	  // OPEN DATABASE TEST
       
   387 		 		{
       
   388 		 		iTest->Printf(_L("TEST : CContactDatabase::OpenL()."));
       
   389 		 		
       
   390 		    	TRAP(err, db = CContactDatabase::OpenL(iLongDbName));
       
   391 		    	
       
   392 		    	if (err == aErrArray[1])
       
   393 			    	{
       
   394 			    	typeOfError = 1;					
       
   395 			    	}
       
   396 		    	else if(err == KErrNotFound || err == KErrLocked || err == KErrBadName || err == KErrDiskFull || err == KErrArgument)
       
   397 			    	{
       
   398 			     	typeOfError = 2;	
       
   399 			    	}		    	
       
   400 				}    	
       
   401 	 		break;
       
   402 	 		
       
   403 	 		case 2:  // REPLACE DATABASE TEST
       
   404 		 		{
       
   405 		 		iTest->Printf(_L("TEST : CContactDatabase::ReplaceL()()."));
       
   406 		 		
       
   407 		    	TRAP(err, db = CContactDatabase::ReplaceL(iLongDbName));
       
   408 		    	
       
   409 		    	if (err == aErrArray[2])
       
   410 			    	{
       
   411 			    	typeOfError = 1;					
       
   412 			    	}
       
   413 		    	else if(err == KErrInUse || err == KErrBadName || err == KErrDiskFull || err == KErrArgument)
       
   414 			    	{
       
   415 			     	typeOfError = 2;	
       
   416 			    	}		    	
       
   417 				}  		
       
   418 	 		break;
       
   419 	 		
       
   420 	 		case 3:  // DELETE DATABASE TEST
       
   421 	 	 		{
       
   422 		 		iTest->Printf(_L("TEST : CContactDatabase::DeleteDatabaseL()."));
       
   423 		 		
       
   424 		    	TRAP(err, CContactDatabase::DeleteDatabaseL(iLongDbName));
       
   425 		    	
       
   426 		    	if (err == aErrArray[3])
       
   427 			    	{
       
   428 			    	typeOfError = 1;					
       
   429 			    	}
       
   430 		    	else if(err == KErrNotFound || err == KErrBadName || err == KErrInUse || err == KErrArgument)
       
   431 			    	{
       
   432 			     	typeOfError = 2;	
       
   433 			    	}		    	
       
   434 				} 		
       
   435 	 		break;
       
   436 	 		
       
   437 	 		case 4:  // DATABASE EXISTS TEST
       
   438 	 	 		{
       
   439 		 		iTest->Printf(_L("TEST : CContactDatabase::ContactDatabaseExistsL()."));
       
   440 		 		
       
   441 		    	TRAP(err, CContactDatabase::ContactDatabaseExistsL(iLongDbName));
       
   442 		    	
       
   443 		    	if (err == aErrArray[4])
       
   444 			    	{
       
   445 			    	typeOfError = 1;					
       
   446 			    	}
       
   447 		    	else if(err == KErrNotReady || err == KErrBadName || err == KErrNotFound || err == KErrArgument || err == KErrCorrupt)
       
   448 			    	{
       
   449 			     	typeOfError = 2;	
       
   450 			    	}		    	
       
   451 				} 		
       
   452 	 		break;
       
   453 	 		
       
   454 	 		
       
   455 	 		default:
       
   456 	 			User::Leave(KErrArgument);
       
   457 	 		break;	 		
       
   458 	 		
       
   459 	 	}
       
   460 	 	
       
   461 	 	if(typeOfError == 1)
       
   462 		 	{
       
   463 			iTest->Printf(_L("Correct Error Code Returned: %d"), err);
       
   464 		 	}
       
   465 	 	else if(typeOfError == 2)
       
   466 		 	{
       
   467 			iTest->Printf(_L("Incorrect, but one of the possible error codes returned: %d"), err);
       
   468 		 	}
       
   469 	 	else
       
   470 		 	{
       
   471 			iTest->Printf(_L("ERROR:- Test Failed, Unexpected Error Code returned: %d"), err);
       
   472 		 	}
       
   473  	 
       
   474 	    if (db)
       
   475 	        {
       
   476 	        delete db;
       
   477 	        }
       
   478 	  	}  	
       
   479 				
       
   480 	}
       
   481 
       
   482 
       
   483 LOCAL_C void DoTestsL()
       
   484     {
       
   485     CDbNameTest* test = CDbNameTest::NewLC();
       
   486     test->RunTestsL();
       
   487     CleanupStack::PopAndDestroy(test);
       
   488     }
       
   489 
       
   490 GLDEF_C TInt E32Main()
       
   491     {
       
   492     // Init
       
   493     CTrapCleanup* cleanupStack = CTrapCleanup::New();
       
   494     if (!cleanupStack)
       
   495         {
       
   496         return KErrNoMemory;
       
   497         }
       
   498 
       
   499     CActiveScheduler* activeScheduler = new CActiveScheduler;
       
   500     if (!activeScheduler)
       
   501         {
       
   502         return KErrNoMemory;
       
   503         }
       
   504     CActiveScheduler::Install(activeScheduler);
       
   505 
       
   506     // Run the tests
       
   507     __UHEAP_MARK;
       
   508     TRAPD(err, DoTestsL());
       
   509     __UHEAP_MARKEND;
       
   510 
       
   511     // Cleanup
       
   512     delete activeScheduler;
       
   513     delete cleanupStack;
       
   514     return err;
       
   515     }
       
   516