persistentstorage/sql/TEST/t_sqlbur2.cpp
changeset 51 7d4490026038
child 55 44f437012c90
equal deleted inserted replaced
40:b8bdbc8f59c7 51:7d4490026038
       
     1 // Copyright (c) 2010 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 #include <e32test.h>
       
    17 #include <bautils.h>
       
    18 #include <sqldb.h>
       
    19 #include <e32math.h>
       
    20 #include "SqlBur.h"
       
    21 
       
    22 //CSqlSrvTestBurInterface - test implementation of the MSqlSrvBurInterface, implemented in the production code by the SQL server.
       
    23 class CSqlSrvTestBurInterface : public CBase, public MSqlSrvBurInterface
       
    24 	{
       
    25 	public:
       
    26 		static CSqlSrvTestBurInterface* New();
       
    27 		virtual ~CSqlSrvTestBurInterface();
       
    28 		virtual RFs& Fs();
       
    29 		virtual void GetBackUpListL(TSecureId aUid, TDriveNumber aDrive, RArray<HBufC*>& aFileList);
       
    30 		
       
    31 	private:
       
    32 		void Construct();
       
    33 		
       
    34 	private:
       
    35 		RFs iFs;
       
    36 		
       
    37 	};
       
    38 
       
    39 ///////////////////////////////////////////////////////////////////////////////////////
       
    40 
       
    41 RTest TheTest(_L("t_sqlbur2"));
       
    42 
       
    43 #ifdef _DEBUG
       
    44 static const TInt KBurstRate = 100;
       
    45 #endif
       
    46 
       
    47 TDriveNumber KTestDrive = EDriveC;
       
    48 _LIT(KTestDir, "c:\\test\\");
       
    49 CActiveScheduler* TheScheduler = NULL;
       
    50 CSqlSrvTestBurInterface* TheSqlSrvTestBurInterface = NULL;
       
    51 TInt TestModeSqlBurError = KErrNone;//The CSqlBurEventMonitor code will set the error here
       
    52 
       
    53 ///////////////////////////////////////////////////////////////////////////////////////
       
    54 
       
    55 void DestroyTestEnv()
       
    56 	{
       
    57 	delete TheSqlSrvTestBurInterface;
       
    58 	TheSqlSrvTestBurInterface = NULL;		
       
    59 	
       
    60 	delete TheScheduler;
       
    61 	TheScheduler = NULL;
       
    62 	
       
    63 	(void)RProperty::Delete(KSqlBurPropertyCategoryUid, KSqlBurBackupRestoreKey);  
       
    64 	}
       
    65 
       
    66 ///////////////////////////////////////////////////////////////////////////////////////
       
    67 ///////////////////////////////////////////////////////////////////////////////////////
       
    68 //Test macros and functions
       
    69 void Check(TInt aValue, TInt aLine)
       
    70 	{
       
    71 	if(!aValue)
       
    72 		{
       
    73 		DestroyTestEnv();
       
    74 		RDebug::Print(_L("*** Boolean expression evaluated to false.\r\n"));
       
    75 		TheTest(EFalse, aLine);
       
    76 		}
       
    77 	}
       
    78 void Check(TInt aValue, TInt aExpected, TInt aLine)
       
    79 	{
       
    80 	if(aValue != aExpected)
       
    81 		{
       
    82 		DestroyTestEnv();
       
    83 		RDebug::Print(_L("*** Expected error: %d, got: %d.\r\n"), aExpected, aValue);
       
    84 		TheTest(EFalse, aLine);
       
    85 		}
       
    86 	}
       
    87 #define TEST(arg) ::Check((arg), __LINE__)
       
    88 #define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
       
    89 
       
    90 ///////////////////////////////////////////////////////////////////////////////////////
       
    91 
       
    92 CSqlSrvTestBurInterface* CSqlSrvTestBurInterface::New()
       
    93 	{
       
    94 	CSqlSrvTestBurInterface* self = new CSqlSrvTestBurInterface;
       
    95 	TEST(self != NULL);
       
    96 	self->Construct();
       
    97 	return self;
       
    98 	}
       
    99 
       
   100 void CSqlSrvTestBurInterface::Construct()
       
   101 	{
       
   102 	TInt err = iFs.Connect();
       
   103 	TEST2(err, KErrNone);
       
   104 
       
   105 	err = iFs.MkDir(KTestDir);
       
   106 	TEST(err == KErrNone || err == KErrAlreadyExists);
       
   107 
       
   108 	err = iFs.CreatePrivatePath(KTestDrive);
       
   109 	TEST(err == KErrNone || err == KErrAlreadyExists);
       
   110 	}
       
   111 
       
   112 CSqlSrvTestBurInterface::~CSqlSrvTestBurInterface()
       
   113 	{
       
   114 	iFs.Close();
       
   115 	}
       
   116 
       
   117 RFs& CSqlSrvTestBurInterface::Fs()
       
   118 	{
       
   119 	return iFs;
       
   120 	}
       
   121 
       
   122 //No-op. Not needed in this test app.
       
   123 void CSqlSrvTestBurInterface::GetBackUpListL(TSecureId, TDriveNumber, RArray<HBufC*>&)
       
   124 	{
       
   125 	TEST(EFalse);
       
   126 	}
       
   127 
       
   128 ///////////////////////////////////////////////////////////////////////////////////////
       
   129 
       
   130 void CreateTestEnv()
       
   131     {
       
   132 	TheScheduler = new CActiveScheduler;
       
   133 	TEST(TheScheduler != NULL);
       
   134 	CActiveScheduler::Install(TheScheduler);
       
   135 	
       
   136 	TheSqlSrvTestBurInterface = CSqlSrvTestBurInterface::New();
       
   137 	TEST(TheSqlSrvTestBurInterface != NULL);
       
   138 	
       
   139 	TInt err = RProperty::Define(KSqlBurPropertyCategoryUid, KSqlBurBackupRestoreKey, 0);  
       
   140 	TEST(err == KErrNone || err == KErrAlreadyExists);
       
   141 	}
       
   142 
       
   143 ///////////////////////////////////////////////////////////////////////////////////////
       
   144 
       
   145 /**
       
   146 @SYMTestCaseID			PDS-SQL-UT-4233
       
   147 @SYMTestCaseDesc		CSqlBurEventMonitor object creation - OOM test
       
   148 						The test runs CSqlBurEventMonitor::NewL() in an OOM loop.
       
   149 @SYMTestActions			CSqlBurEventMonitor object creation - OOM test
       
   150 @SYMTestExpectedResults Test must not fail
       
   151 @SYMTestPriority		High
       
   152 */
       
   153 void SqlBurEventMonitorOomTest()
       
   154 	{
       
   155 	TInt err = KErrNoMemory;
       
   156 	TInt failingAllocationNo = 0;
       
   157 	TheTest.Printf(_L("Iteration:\r\n"));
       
   158 	while(err == KErrNoMemory)
       
   159 		{
       
   160 		TheTest.Printf(_L(" %d"), ++failingAllocationNo);
       
   161 		
       
   162 		TInt startProcessHandleCount;
       
   163 		TInt startThreadHandleCount;
       
   164 		RThread().HandleCount(startProcessHandleCount, startThreadHandleCount);
       
   165 		__UHEAP_MARK;
       
   166 		__UHEAP_SETBURSTFAIL(RAllocator::EBurstFailNext, failingAllocationNo, KBurstRate);
       
   167 
       
   168 		CSqlBurEventMonitor* monitor = NULL;
       
   169 		TRAP(err, monitor = CSqlBurEventMonitor::NewL(*TheSqlSrvTestBurInterface));
       
   170 		delete monitor;
       
   171 		
       
   172 		__UHEAP_RESET;
       
   173 		__UHEAP_MARKEND;
       
   174 		TInt endProcessHandleCount;
       
   175 		TInt endThreadHandleCount;
       
   176 		RThread().HandleCount(endProcessHandleCount, endThreadHandleCount);
       
   177 		
       
   178 		TEST2(startProcessHandleCount, endProcessHandleCount);
       
   179 		TEST2(startThreadHandleCount, endThreadHandleCount);
       
   180 		}
       
   181 	TEST2(err, KErrNone);
       
   182 	TheTest.Printf(_L("\r\n=== OOM Test succeeded at heap failure rate of %d ===\r\n"), failingAllocationNo);
       
   183 	}
       
   184 
       
   185 /**
       
   186 @SYMTestCaseID			PDS-SQL-UT-4234
       
   187 @SYMTestCaseDesc		CSqlBurEventMonitor functional test
       
   188 						The test sets the backup & restore property status and then checks
       
   189 						how the backup & restore property monitor (CSqlBurEventMonitor) reacts to the event.
       
   190 @SYMTestActions			CSqlBurEventMonitor functional test
       
   191 @SYMTestExpectedResults Test must not fail
       
   192 @SYMTestPriority		High
       
   193 */
       
   194 void SqlBurEventMonitorFunctionalTest()
       
   195 	{
       
   196 	CSqlBurEventMonitor* monitor = NULL;
       
   197 	TRAPD(err, monitor = CSqlBurEventMonitor::NewL(*TheSqlSrvTestBurInterface));
       
   198 	TEST2(err, KErrNone);
       
   199 	TEST(!monitor->ActiveBackupClient());
       
   200 	TEST(!monitor->SqlBurCallback());
       
   201 	//Set the property to conn::EBURBackupFull, conn::EBURBackupPartial, conn::EBURRestoreFull, conn::EBURRestorePartial, 
       
   202 	//then start the scheduler. CSqlBurEventMonitor::RunL() gets called and 
       
   203 	//CSqlBurCallback and CActiveBackupClient interfaces get created (if the interfaces do exist, they are destroyed first).
       
   204 	TInt burPropertyStatus[] = {conn::EBURBackupFull, conn::EBURBackupPartial, conn::EBURRestoreFull, conn::EBURRestorePartial};
       
   205 	for(TInt i=0;i<(sizeof(burPropertyStatus)/sizeof(burPropertyStatus[0]));++i)
       
   206 		{
       
   207 		err = RProperty::Set(KSqlBurPropertyCategoryUid, KSqlBurBackupRestoreKey, burPropertyStatus[i]);
       
   208 		TEST2(err, KErrNone);
       
   209 		TestModeSqlBurError = KErrNone;
       
   210 		if(i > 0)
       
   211 			{
       
   212 			__UHEAP_MARK;
       
   213 			}
       
   214 		CActiveScheduler::Start();
       
   215 		if(i > 0)
       
   216 			{
       
   217 			__UHEAP_MARKEND;
       
   218 			}
       
   219 		TEST2(TestModeSqlBurError, KErrNone);
       
   220 		TEST(monitor->ActiveBackupClient() != NULL);
       
   221 		TEST(monitor->SqlBurCallback() != NULL);
       
   222 		}
       
   223 	//Set the property to conn::EBURUnset, start the scheduler. CSqlBurEventMonitor::RunL() gets called 
       
   224 	//and CSqlBurCallback interface gets destroyed.
       
   225 	err = RProperty::Set(KSqlBurPropertyCategoryUid, KSqlBurBackupRestoreKey, conn::EBURUnset);
       
   226 	TEST2(err, KErrNone);
       
   227 	TestModeSqlBurError = KErrNone;
       
   228 	CActiveScheduler::Start();
       
   229 	TEST2(TestModeSqlBurError, KErrNone);
       
   230 	TEST(!monitor->ActiveBackupClient());
       
   231 	TEST(!monitor->SqlBurCallback());
       
   232 	//Set the property to conn::EBURNormal, start the scheduler. CSqlBurEventMonitor::RunL() gets called. 
       
   233 	//CSqlBurCallback interface has been destroyed alread. No memory deallocations should be made during this call.
       
   234 	err = RProperty::Set(KSqlBurPropertyCategoryUid, KSqlBurBackupRestoreKey, conn::EBURNormal);
       
   235 	TEST2(err, KErrNone);
       
   236 	__UHEAP_MARK;
       
   237 	TestModeSqlBurError = KErrNone;
       
   238 	CActiveScheduler::Start();
       
   239 	TEST2(TestModeSqlBurError, KErrNone);
       
   240 	TEST(!monitor->ActiveBackupClient());
       
   241 	TEST(!monitor->SqlBurCallback());
       
   242 	__UHEAP_MARKEND;
       
   243 	//Set the property, then delete it. CSqlBurEventMonitor::RunL() should get called, but the call should
       
   244 	//fail because the property does not exist.
       
   245 	err = RProperty::Set(KSqlBurPropertyCategoryUid, KSqlBurBackupRestoreKey, conn::EBURBackupFull);
       
   246 	TEST2(err, KErrNone);
       
   247 	err = RProperty::Delete(KSqlBurPropertyCategoryUid, KSqlBurBackupRestoreKey);  
       
   248 	TEST2(err, KErrNone);
       
   249 	__UHEAP_MARK;
       
   250 	TestModeSqlBurError = KErrNone;
       
   251 	CActiveScheduler::Start();
       
   252 	TEST2(TestModeSqlBurError, KErrNotFound);
       
   253 	TEST(!monitor->ActiveBackupClient());
       
   254 	TEST(!monitor->SqlBurCallback());
       
   255 	__UHEAP_MARKEND;
       
   256 	//Restore the property
       
   257 	err = RProperty::Define(KSqlBurPropertyCategoryUid, KSqlBurBackupRestoreKey, 0);  
       
   258 	TEST2(err, KErrNone);
       
   259 	//
       
   260 	delete monitor;
       
   261 	}
       
   262 
       
   263 /**
       
   264 @SYMTestCaseID			PDS-SQL-UT-4235
       
   265 @SYMTestCaseDesc		CSqlBurEventMonitor::RunL() - OOM test
       
   266 						The test sets the backup & restore property status and then checks
       
   267 						how the backup & restore property monitor (CSqlBurEventMonitor) reacts to the event.
       
   268 						The test is performed in an OOM loop.
       
   269 @SYMTestActions			CSqlBurEventMonitor::RunL() - OOM test
       
   270 @SYMTestExpectedResults Test must not fail
       
   271 @SYMTestPriority		High
       
   272 */
       
   273 void SqlBurEventMonitorRunOomTest()
       
   274 	{
       
   275 	CSqlBurEventMonitor* monitor = NULL;
       
   276 	TRAPD(err, monitor = CSqlBurEventMonitor::NewL(*TheSqlSrvTestBurInterface));
       
   277 	
       
   278 	err = KErrNoMemory;
       
   279 	TInt failingAllocationNo = 0;
       
   280 	TheTest.Printf(_L("Iteration:\r\n"));
       
   281 	while(err == KErrNoMemory)
       
   282 		{
       
   283 		TheTest.Printf(_L(" %d"), ++failingAllocationNo);
       
   284 		
       
   285 		TInt startProcessHandleCount;
       
   286 		TInt startThreadHandleCount;
       
   287 		RThread().HandleCount(startProcessHandleCount, startThreadHandleCount);
       
   288 		__UHEAP_MARK;
       
   289 		__UHEAP_SETBURSTFAIL(RAllocator::EBurstFailNext, failingAllocationNo, KBurstRate);
       
   290 
       
   291 		TEST(!monitor->ActiveBackupClient());
       
   292 		TEST(!monitor->SqlBurCallback());
       
   293 		//Set the property, start the scheduler. CSqlBurEventMonitor::RunL() gets called and CSqlBurCallback
       
   294 		//interface gets created.
       
   295 		err = RProperty::Set(KSqlBurPropertyCategoryUid, KSqlBurBackupRestoreKey, conn::EBURBackupFull);
       
   296 		if(err == KErrNone)
       
   297 			{
       
   298 			TestModeSqlBurError = KErrNone;
       
   299 			CActiveScheduler::Start();
       
   300 			err = TestModeSqlBurError;
       
   301 			if(err == KErrNone)
       
   302 				{
       
   303 				TEST(monitor->ActiveBackupClient() != NULL);
       
   304 				TEST(monitor->SqlBurCallback() != NULL);
       
   305 				//Destroy the SQL backup & restore callback
       
   306 				err = RProperty::Set(KSqlBurPropertyCategoryUid, KSqlBurBackupRestoreKey, conn::EBURNormal);
       
   307 				TestModeSqlBurError = KErrNone;
       
   308 				CActiveScheduler::Start();
       
   309 				err = TestModeSqlBurError;
       
   310 				if(err == KErrNone)
       
   311 					{
       
   312 					TEST(!monitor->ActiveBackupClient());
       
   313 					TEST(!monitor->SqlBurCallback());
       
   314 					}
       
   315 				}
       
   316 			}
       
   317 		
       
   318 		__UHEAP_RESET;
       
   319 		__UHEAP_MARKEND;
       
   320 		TInt endProcessHandleCount;
       
   321 		TInt endThreadHandleCount;
       
   322 		RThread().HandleCount(endProcessHandleCount, endThreadHandleCount);
       
   323 		
       
   324 		TEST2(startProcessHandleCount, endProcessHandleCount);
       
   325 		TEST2(startThreadHandleCount, endThreadHandleCount);
       
   326 		}
       
   327 	TEST2(err, KErrNone);
       
   328 	TheTest.Printf(_L("\r\n=== OOM Test succeeded at heap failure rate of %d ===\r\n"), failingAllocationNo);
       
   329 	delete monitor;
       
   330 	}
       
   331 
       
   332 void DoTestsL()
       
   333 	{
       
   334 	TheTest.Start(_L(" @SYMTestCaseID:PDS-SQL-UT-4233 CSqlBurEventMonitor object creation - OOM test"));
       
   335 	SqlBurEventMonitorOomTest();
       
   336 	TheTest.Next(_L(" @SYMTestCaseID:PDS-SQL-UT-4234 CSqlBurEventMonitor functional test"));
       
   337 	SqlBurEventMonitorFunctionalTest();
       
   338 	TheTest.Next(_L(" @SYMTestCaseID:PDS-SQL-UT-4235 CSqlBurEventMonitor::RunL() - OOM test"));
       
   339 	SqlBurEventMonitorRunOomTest();
       
   340 	}
       
   341 
       
   342 TInt E32Main()
       
   343 	{
       
   344 	TheTest.Title();
       
   345 
       
   346 	CTrapCleanup* tc = CTrapCleanup::New();
       
   347 	TheTest(tc != NULL);
       
   348 
       
   349 	__UHEAP_MARK;
       
   350 
       
   351 	CreateTestEnv();
       
   352 	TRAPD(err, DoTestsL());
       
   353 	DestroyTestEnv();
       
   354 	TEST2(err, KErrNone);
       
   355 
       
   356 	__UHEAP_MARKEND;
       
   357 
       
   358 	TheTest.End();
       
   359 	TheTest.Close();
       
   360 
       
   361 	delete tc;
       
   362 
       
   363 	User::Heap().Check();
       
   364 	return KErrNone;
       
   365 	}