os/persistentdata/persistentstorage/dbms/pcdbms/tdbms/src/t_defect.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2004-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 #include <d32dbms.h>
    17 #include <e32math.h>
    18 #include <e32test.h>
    19 #include <f32file.h>
    20 #include <s32buf.h>
    21 
    22 #include "crccheck.h"
    23 
    24 #undef __UHEAP_MARK
    25 #undef __UHEAP_MARKEND
    26 #define __UHEAP_MARK
    27 #define __UHEAP_MARKEND
    28 
    29 LOCAL_D TDBMS_CRCChecks TheCrcChecker;
    30 
    31 #ifndef __linux__ //No CRC test on LINUX
    32 #ifdef __TOOLS2__
    33 const TPtrC	KCrcRecord=_L("\\epoc32\\winscw\\c\\dbms-tst\\T_DEFECT.CRC");
    34 #else
    35 const TPtrC	KCrcRecord=_L("C:\\dbms-tst\\T_DEFECT.CRC");
    36 #endif
    37 #endif
    38 
    39 
    40 LOCAL_D RTest 			TheTest (_L ("t_defect.exe"));
    41 LOCAL_D CTrapCleanup*	TheTrapCleanup = NULL;
    42 #ifndef __TOOLS2__
    43 LOCAL_D RSemaphore 		TheWaitToStartSem;
    44 LOCAL_D RSemaphore 		TheWaitForThreadsReadySem;
    45 #endif
    46 LOCAL_D RFs 			TheFs;
    47 LOCAL_D RDbs			TheDbs1, TheDbs2;
    48 LOCAL_D RDbNamedDatabase TheDb1, TheDb2;
    49 
    50 _LIT (KName,			"ConnectTestThread_");
    51 _LIT (KStart,			"Starting thread %x.\n");
    52 _LIT (KConnect,			"Thread %x: Waiting to connect...\n");
    53 _LIT (KConSuccess,		"Thread %x: Connection succeeded.\n");
    54 _LIT (KConFailed,		"Thread %x: Connection failed. Error %d.\n");
    55 _LIT (KStatus,			"Status of thread %x is %d.\n");
    56  
    57  
    58 _LIT(KTable, 			"TABLE");
    59 _LIT(KColName,			"Fld");
    60 _LIT(KCol2Name, 		"Fld2");
    61 
    62 #ifndef __TOOLS2__
    63 const TPtrC KDbName=_L("C:\\dbms-tst\\TESTDB22.DB");
    64 #else
    65 const TPtrC KDbName=_L(".\\dbms-tst\\TESTDB22.DB");
    66 #endif
    67 
    68 _LIT(KSQLInsert1, 		"INSERT INTO TABLE (Fld, Fld2) VALUES ('ACDC\\','BLAH')");
    69 _LIT(KSQLInsert2, 		"INSERT INTO TABLE   (Fld) VALUES ('ABCDEFGH')");
    70 _LIT(KSQLInsert3, 		"INSERT INTO TABLE   (Fld) VALUES ('A?CDEFGH')");
    71 _LIT(KSQLInsert4, 		"INSERT INTO TABLE   (Fld) VALUES ('A?*?CDEFGH')");
    72 _LIT(KSQLInsert5, 		"INSERT INTO TABLE   (Fld) VALUES ('A*CDEFGH')");
    73 _LIT(KSQLInsert6, 		"INSERT INTO TABLE   (Fld, Fld2) VALUES ('ADCDEFGHIJKLMNOPQRSTUVWXYZ123456789ACDEFGHIJKLMNOPQRSTUVWXYZ123456789ACDEFGHIJKLMNOPQRSTUVWXYZ123456789ACDEFGHIJKLMNOPQRSTUVWXYZ123456789ACDEFGHIJKLMNOPQRSTUVWXYZ123456789ACDEFGHIJKLMNOPQRSTUVWXYZ123456789ACDEFGHIJKLMNOPQRSTUVWXYZ123456789ACDEFGHIJKLMNOP','ADCB')");
    74 _LIT(KSQLInsert7, 		"INSERT INTO TABLE   (Fld) VALUES ('XZD\\FZX')");
    75 
    76 _LIT(KSqlRequestGranularity, "SELECT Fld FROM test WHERE (Fld LIKE '1' AND Fld LIKE '2') AND Fld LIKE '3'  AND Fld LIKE '4'  AND Fld LIKE '5'  AND Fld LIKE '6'");
    77 
    78 _LIT(KText16Name, 		"text16");
    79 _LIT(KTableName, 		"test");
    80 _LIT(KIndexName, 		"test_index");
    81 _LIT(KMaxStringFormat, 	"%0256d");
    82 
    83 void TestCleanup()
    84 	{
    85 	TheDb2.Close();
    86 	TheDb1.Close();
    87 #ifndef __TOOLS2__
    88 	TheDbs2.Close();
    89 	TheDbs1.Close();
    90 #endif
    91 	(void)TheFs.Delete(KDbName);
    92 	TheFs.Close();
    93 	TheCrcChecker.GenerateCrcL(KDbName);
    94 	}
    95 
    96 //-----------------------------------------------------------------------------
    97 //
    98 // Test macros and functions.
    99 //
   100 //-----------------------------------------------------------------------------
   101 // If (!aValue) then the test will be panicked, the test data files will be 
   102 // deleted.
   103 LOCAL_C void Check(TInt aValue, TInt aLine)
   104 	{
   105 	if(!aValue)
   106 		{
   107 		TestCleanup();
   108 		TheTest(EFalse, aLine);
   109 		}
   110 	}
   111 
   112 
   113 // If (aValue != aExpected) then the test will be panicked, the test data files
   114 // will be deleted.
   115 LOCAL_C void Check(TInt aValue, TInt aExpected, TInt aLine)
   116 	{
   117 	if(aValue != aExpected)
   118 		{
   119 		RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
   120 		TestCleanup();
   121 		TheTest(EFalse, aLine);
   122 		}
   123 	}
   124 		
   125 	
   126 //Use these to test conditions.
   127 #define TEST(arg)                ::Check((arg), __LINE__)
   128 #define TEST2(aValue, aExpected) ::Check((aValue), (aExpected), __LINE__)
   129 //-----------------------------------------------------------------------------
   130 //-----------------------------------------------------------------------------
   131 
   132 struct TTest
   133 	{
   134 	const TText* result;
   135 	const TText* query;
   136 	};
   137 		
   138 const TTest KQuery[]=
   139 	{
   140 	{_S("ACDC\\"),_S("SELECT Fld FROM TABLE WHERE  Fld LIKE 'ACDC\\' AND Fld2 LIKE '*BL*'")},
   141 	{_S("A*CDEFGH"),_S("SELECT * FROM TABLE WHERE  Fld LIKE '*A\\*C*' ESCAPE '\\'")},
   142 	{_S("A?CDEFGH"),_S("SELECT * FROM TABLE WHERE  Fld LIKE '*A\\?C*' ESCAPE '\\'")},
   143 	{_S("A?*?CDEFGH"),_S("SELECT * FROM TABLE WHERE  Fld LIKE '*A\\?\\*\\?C*' ESCAPE '\\'")},
   144 	{_S("ADCDEFGHIJKLMNOPQRSTUVWXYZ123456789ACDEFGHIJKLMNOPQRSTUVWXYZ123456789ACDEFGHIJKLMNOPQRSTUVWXYZ123456789ACDEFGHIJKLMNOPQRSTUVWXYZ123456789ACDEFGHIJKLMNOPQRSTUVWXYZ123456789ACDEFGHIJKLMNOPQRSTUVWXYZ123456789ACDEFGHIJKLMNOPQRSTUVWXYZ123456789ACDEFGHIJKLMNOP"),_S("SELECT * FROM TABLE WHERE  Fld LIKE '*1234*'")},	
   145 	{_S("BLAH"),_S("SELECT Fld2 FROM TABLE WHERE  Fld LIKE '*AC*' AND Fld2 LIKE '?LA?'")},
   146 	{_S("BLAH"),_S("SELECT Fld2 FROM TABLE WHERE  Fld LIKE 'NOTINTABLE' OR Fld2 LIKE '?LA?'")},
   147 	{_S("ADCDEFGHIJKLMNOPQRSTUVWXYZ123456789ACDEFGHIJKLMNOPQRSTUVWXYZ123456789ACDEFGHIJKLMNOPQRSTUVWXYZ123456789ACDEFGHIJKLMNOPQRSTUVWXYZ123456789ACDEFGHIJKLMNOPQRSTUVWXYZ123456789ACDEFGHIJKLMNOPQRSTUVWXYZ123456789ACDEFGHIJKLMNOPQRSTUVWXYZ123456789ACDEFGHIJKLMNOP"),_S("SELECT * FROM TABLE WHERE  Fld LIKE '*ADC*' AND Fld2 LIKE 'ADC?'")},	
   148 	{_S("A*CDEFGH"),_S("SELECT Fld FROM TABLE WHERE Fld LIKE '*\\*C*' ESCAPE '\\'")},
   149 	{_S("XZD\\FZX"),_S("SELECT Fld FROM TABLE WHERE Fld LIKE '*D\\\\*' ESCAPE '\\'")}
   150 	};
   151 
   152 const TTest KBadQuery[]=
   153 	{
   154 	{_S(""),_S("SELECT * FROM TABLE WHERE  Fld LIKE 'A?C' ESCAPE '\\'")},
   155 	{_S(""),_S("SELECT * FROM TABLE WHERE  Fld LIKE 'A*C' ESCAPE '\\'")},
   156 	{_S(""),_S("SELECT * FROM TABLE WHERE  Fld LIKE '?A\\?C' ESCAPE '\\'")},
   157 	{_S(""),_S("SELECT * FROM TABLE WHERE  Fld LIKE '?A\\?C?' ESCAPE '\\'")},
   158 	{_S(""),_S("SELECT * FROM TABLE WHERE  Fld LIKE '*A\\??\\?C*' ESCAPE '\\'")},
   159 	{_S(""),_S("SELECT * FROM TABLE WHERE  Fld LIKE '*A*\\*C*' ESCAPE '\\'")},
   160 	{_S(""),_S("SELECT * FROM TABLE WHERE  Fld LIKE 'ABC' ESCAPE '\\'")},
   161 	{_S(""),_S("SELECT * FROM TABLE WHERE  Fld LIKE 'ABC*' ESCAPE '\\'")},
   162 	{_S(""),_S("SELECT * FROM TABLE WHERE  Fld LIKE '*ABC' ESCAPE '\\'")}
   163 	};	
   164 
   165 const TInt KNumQueries = 10;
   166 const TInt KNumBadQueries = 9;
   167 const TInt KThreadCount	= 3;
   168 const TInt KOneSecond 	= 1000000;
   169 const TInt KDbNameLen   = 255;
   170 const TInt KMaxColName = 32;
   171 
   172 #ifndef __TOOLS2__
   173 static void DoDbmsConnectThreadSubFunctionL (TInt aThreadNumber)
   174 	{
   175 	// The session	
   176 	RDbs TheDbSession;
   177 	CleanupClosePushL (TheDbSession);
   178 
   179 	RDebug::Print (KStart (), aThreadNumber);
   180 	RDebug::Print (KConnect (), aThreadNumber);
   181 	
   182  	// Signal the main thread to continue
   183  	TheWaitForThreadsReadySem.Signal (1);
   184 
   185 	// Wait until we are signalled
   186 	TheWaitToStartSem.Wait ();	
   187 
   188 	// Connect to Dbms
   189 	TInt r = TheDbSession.Connect ();
   190 
   191 	if (r == KErrNone)
   192 		{
   193 		RDebug::Print (KConSuccess (), aThreadNumber);
   194 		TheDbSession.Close ();
   195 		}
   196 	else
   197 		{
   198 		RDebug::Print (KConFailed (), aThreadNumber, r);
   199 		User::Leave (r);
   200 		}
   201 
   202 	CleanupStack::PopAndDestroy (1); // session
   203 	}
   204 
   205 static TInt DoDbmsConnectThread (TAny* aThreadNumber)
   206 	{
   207 	__UHEAP_MARK;
   208 
   209 	CTrapCleanup* trapCleanup = CTrapCleanup::New ();
   210 	__ASSERT_ALWAYS (trapCleanup!=NULL, User::Invariant ());
   211 
   212 	TInt* threadNumber = static_cast <TInt*> (aThreadNumber);
   213 	TRAPD (err, DoDbmsConnectThreadSubFunctionL (*threadNumber));
   214 	
   215 	delete trapCleanup;
   216 	
   217 	__UHEAP_MARKEND;
   218 
   219 	return err;		
   220 	}
   221 #endif
   222 
   223 #ifndef TOOLS2_VARIANT
   224 /**
   225 @SYMTestCaseID          SYSLIB-DBMS-CT-0644
   226 @SYMTestCaseDesc        Test for defect no 44697
   227 @SYMTestPriority        Medium
   228 @SYMTestActions         Test for defect fixes
   229 @SYMTestExpectedResults Test must not fail
   230 @SYMREQ                 REQ0000
   231 */
   232 LOCAL_C void Defect_DEF44697L ()
   233 	{
   234 	TheTest.Next (_L (" @SYMTestCaseID:SYSLIB-DBMS-CT-0644 Defect_DEF44697L "));
   235 
   236 	__UHEAP_MARK;
   237 	
   238   	// find out the number of open handles
   239 	TInt startProcessHandleCount;
   240 	TInt startThreadHandleCount;
   241 	RThread ().HandleCount (startProcessHandleCount, startThreadHandleCount);
   242 
   243 	/////////////////////
   244 	// The Test Begins...
   245 	
   246  	// Create semaphores
   247  	::CleanupClosePushL (::TheWaitForThreadsReadySem);
   248  	User::LeaveIfError (::TheWaitForThreadsReadySem.CreateLocal (0));
   249 
   250  	::CleanupClosePushL (::TheWaitToStartSem);
   251  	User::LeaveIfError (::TheWaitToStartSem.CreateLocal (0));
   252 
   253  	// Create the threads.
   254  	RThread createTestThread_1;
   255  	RThread createTestThread_2;
   256  	RThread createTestThread_3;
   257  	
   258  	TBuf<100> thread_name;
   259 	TInt KThreadNumber1 = 1;
   260 	TInt KThreadNumber2 = 2;
   261 	TInt KThreadNumber3 = 3;
   262  	
   263  	// Create the first test thread______________________________
   264 	thread_name = KName ();
   265 	thread_name.AppendNum (KThreadNumber1);
   266  		
   267  	User::LeaveIfError (
   268  		createTestThread_1.Create (thread_name, 
   269 								   (TThreadFunction) DoDbmsConnectThread,
   270 								   KDefaultStackSize,
   271 								   KMinHeapSize,
   272 								   KMinHeapSize,
   273 								   &KThreadNumber1));
   274  	
   275  	// Default priority of Main thread is EPriorityNormal								   
   276  	createTestThread_1.SetPriority(EPriorityMore);
   277  	
   278  	TheTest.Printf (_L ("%S thread started\n"), &thread_name);
   279 	 	
   280  	// Request notification when the thread dies.
   281  	TRequestStatus threadStatus_1;
   282  	createTestThread_1.Logon (threadStatus_1);
   283 
   284 	//___________________________________________________________
   285 
   286  	// Create the second test thread______________________________
   287 	thread_name = KName ();
   288 	thread_name.AppendNum (KThreadNumber2);
   289  		
   290  	User::LeaveIfError (
   291  		createTestThread_2.Create (thread_name, 
   292 								   (TThreadFunction) DoDbmsConnectThread,
   293 								   KDefaultStackSize,
   294 								   KMinHeapSize,
   295 								   KMinHeapSize,
   296 								   &KThreadNumber2));
   297  					
   298   	// Default priority of Main thread is EPriorityNormal								   
   299  	createTestThread_2.SetPriority(EPriorityMore);
   300 				   
   301  	TheTest.Printf (_L ("%S thread started\n"), &thread_name);
   302 	 	
   303  	// Request notification when the tread dies.
   304  	TRequestStatus threadStatus_2;
   305  	createTestThread_2.Logon (threadStatus_2);
   306 
   307 	//___________________________________________________________
   308 
   309  	// Create the third test thread______________________________
   310 	thread_name = KName ();
   311 	thread_name.AppendNum (KThreadNumber3);
   312  		
   313  	User::LeaveIfError (
   314  		createTestThread_3.Create (thread_name, 
   315 								   (TThreadFunction) DoDbmsConnectThread,
   316 								   KDefaultStackSize,
   317 								   KMinHeapSize,
   318 								   KMinHeapSize,
   319 								   &KThreadNumber3));
   320  		
   321   	// Default priority of Main thread is EPriorityNormal								   
   322  	createTestThread_3.SetPriority(EPriorityMore);
   323 							   
   324  	TheTest.Printf (_L ("%S thread started\n"), &thread_name);
   325 	 	
   326  	// Request notification when the tread dies.
   327  	TRequestStatus threadStatus_3;
   328  	createTestThread_3.Logon (threadStatus_3);
   329 
   330 	//___________________________________________________________
   331 
   332 	TheTest (threadStatus_1.Int () == KRequestPending);
   333 	TheTest (threadStatus_2.Int () == KRequestPending);
   334 	TheTest (threadStatus_3.Int () == KRequestPending);
   335 
   336  	// Make threads eligible for execution
   337  	createTestThread_1.Resume ();
   338  	createTestThread_2.Resume ();
   339  	createTestThread_3.Resume ();
   340 
   341 	// The way this works is that the main thread blocks until all
   342 	// the test threads are ready (semaphore 1) and then signals them
   343 	// (semaphore 2).
   344 	//
   345 	// 1: Main thread Waits for ALL test threads to become ready.
   346 	// 2: Main thread Signals ALL test threads to run.
   347 	//
   348 	// 1: Test thread Signals Main thread
   349 	// 2: Test thread Waits for Main thread
   350 	//
   351 	// There is still a slight race condition between the 
   352 	// test thread signalling (semaphore 1) and then waiting 
   353 	// (semaphore 2) which is why we use both higher priority test
   354 	// threads and a timer.
   355 	//
   356 	// The problems come with the way Time slicing works due to
   357 	// other threads of higher priority being run.
   358 	//
   359 	// Higher priority: Ensures the test thread runs before the 
   360 	// the main thread.
   361 	//
   362 	// Timer: Safeguards when multiple core processors are being used.
   363 	//
   364 	// The Higher priority fixes the problem on single core processors
   365 	// and multiple cores processors (SMP) where each core can run a
   366 	// thread.
   367 	// 
   368 	// It should also ensure that if the system is so busy that it 
   369 	// affects the test thread execution, the test thread will still
   370 	// get to the Wait state before the Main thread can Signal.
   371 	// 
   372 	// However, on multiple cores the Main thread may run at the same 
   373 	// time as the test thread, so we need to make sure that when the
   374 	// test thread Signals it can acheive its Wait state before the 
   375 	// Main thread Signals. For example, if the timer has elapsed on the
   376 	// Main thread and the sytem is busy, the test thread should still
   377 	// run before the Main thread due to it higher priority.
   378 	//
   379 	// We also have to think about things like priority inheritance
   380 	// where a thread that has a handle on a Mutex inherits the same 
   381 	// priority as a thread Waiting on it. This shouldn't happen for 
   382 	// Semaphores as there is no one handle, i.e. no critical section.
   383 	//
   384 	// This higher priority inheritance will take affect when a low 
   385 	// priority thread that has a handle on the Mutex blocks because of
   386 	// another medium priority running thread. So in effect a high 
   387 	// priority thread Waiting on this Mutex is also blocked.
   388 	//
   389 	// It is also worth noting that on EKA1 emulator, scheduling is
   390 	// performed by windows. On EKA2 emulator scheduling is performed
   391 	// by Symbian so that it is the same as hardware.
   392 	
   393 	TheWaitForThreadsReadySem.Wait();			
   394 	TheWaitForThreadsReadySem.Wait();			
   395 	TheWaitForThreadsReadySem.Wait();			
   396 	
   397   	// Sleep for a while to allow threads to block on the semaphore
   398   	User::After (KOneSecond<<2); // 4 seconds
   399 	
   400  	// Signal all the threads to continue
   401  	TheWaitToStartSem.Signal (KThreadCount);
   402  	
   403  	// Wait for all threads to complete, don't care on the order.
   404 	User::WaitForRequest (threadStatus_1);
   405 	User::WaitForRequest (threadStatus_2);
   406 	User::WaitForRequest (threadStatus_3);
   407 	
   408 	TheTest.Printf (KStatus, KThreadNumber1, threadStatus_1.Int ());
   409 	TheTest.Printf (KStatus, KThreadNumber2, threadStatus_2.Int ());
   410 	TheTest.Printf (KStatus, KThreadNumber3, threadStatus_3.Int ());
   411 	
   412 	TheTest (threadStatus_1.Int () == KErrNone);
   413 	TheTest (threadStatus_2.Int () == KErrNone);
   414 	TheTest (threadStatus_3.Int () == KErrNone);
   415 	
   416 	CleanupStack::PopAndDestroy (&::TheWaitToStartSem);
   417 	CleanupStack::PopAndDestroy (&::TheWaitForThreadsReadySem);
   418 	
   419 	// The Test Ends...
   420 	/////////////////////
   421 
   422 	// check that no handles have leaked
   423 	TInt endProcessHandleCount;
   424 	TInt endThreadHandleCount;
   425 	RThread ().HandleCount (endProcessHandleCount, endThreadHandleCount);
   426 
   427 	TheTest (startThreadHandleCount == endThreadHandleCount);
   428 
   429 	__UHEAP_MARKEND;
   430 	}
   431 #endif // !TOOLS2_VARIANT
   432 	
   433 	
   434 	
   435 // Test for LIKE Predicate for EDbColLongText16
   436 LOCAL_C void LikePredicateDbColLongText16TestL()
   437  	{
   438  	TheTest.Next (_L ("LikePredicateDbColLongText16TestL"));
   439  	//Creating database
   440 
   441 	RFs fsSession;
   442     User::LeaveIfError(fsSession.Connect());
   443     CleanupClosePushL(fsSession);
   444 	RDbNamedDatabase database;
   445 	User::LeaveIfError(database.Replace(fsSession, KDbName));
   446 	CleanupClosePushL(database);
   447 	
   448 	//Create table
   449 
   450 	CDbColSet* columns= CDbColSet::NewLC();
   451 			
   452 	TDbCol name(KColName,EDbColLongText16,KDbNameLen);
   453 	name.iAttributes = TDbCol::ENotNull;
   454 	
   455 	TDbCol name2(KCol2Name,EDbColLongText16,KDbNameLen);
   456 
   457 	columns->AddL(name);
   458 	columns->AddL(name2);
   459    	User::LeaveIfError (database.CreateTable (KTable, *columns));
   460 	CleanupStack::PopAndDestroy(); // columns
   461     
   462     // Insert values into table
   463     TInt error =	database.Execute(KSQLInsert1);
   464     TheTest(error>=0);
   465     error =database.Execute(KSQLInsert2);
   466     TheTest(error>=0);
   467     error =database.Execute(KSQLInsert3);
   468     TheTest(error>=0);
   469 	error =	database.Execute(KSQLInsert4);
   470 	TheTest(error>=0);
   471 	error =	database.Execute(KSQLInsert5);
   472 	TheTest(error>=0);
   473 	error = database.Execute(KSQLInsert6);
   474 	TheTest(error>=0);
   475 	error = database.Execute(KSQLInsert7);
   476 	TheTest(error>=0);
   477 	
   478 		
   479 	TheTest.Next(_L("Test for valid LIKE predicate queries"));
   480 	
   481 	
   482 	for(TInt i =0;i<KNumQueries;++i)
   483 		{
   484 		RDebug::Print(_L("Executing statement: %s \n"),(KQuery[i].query));
   485 		RDbView view;
   486 		view.Prepare(database, TDbQuery(TPtrC(KQuery[i].query), EDbCompareFolded), view.EReadOnly);
   487 		view.EvaluateAll();
   488 		view.FirstL();      
   489 		typedef TBuf<256> TScriptLine;
   490 		TInt count =0;
   491 		while (view.AtRow())
   492 			{
   493 			view.GetL();
   494 			count++;
   495 			RDbColReadStream rd;
   496 			rd.OpenLC(view,1);
   497 			TScriptLine text;
   498 			rd.ReadL(text,view.ColLength(1));
   499 			CleanupStack::PopAndDestroy();
   500 			RDebug::Print(_L("Expected result: %s Actual Result: %S\n"),(KQuery[i].result),&text);
   501 			TInt err = text.Compare(TPtrC(KQuery[i].result));
   502 			TheTest(err ==0);
   503 			view.NextL();
   504 			}
   505 		view.Close();		
   506 		}
   507 	
   508 	
   509 	// test for illegal statements, check they return KErrArgument
   510 	TheTest.Next(_L("Test that illegal queries return KErrArgument"));	
   511 	
   512 		
   513 	for(TInt j =0;j<KNumBadQueries;++j)
   514 		{
   515 		RDebug::Print(_L("Executing illegal statement: %s \n"),(KBadQuery[j].query));
   516 		RDbView view;
   517 		TInt prepErr = view.Prepare(database, TDbQuery(TPtrC(KBadQuery[j].query), EDbCompareFolded), view.EReadOnly);
   518 		TheTest(prepErr==KErrArgument);
   519 		view.Close();		
   520 		}
   521         
   522     CleanupStack::PopAndDestroy(&database); // database
   523     CleanupStack::PopAndDestroy(&fsSession); // fsSession
   524  	}
   525  	
   526  	
   527  	
   528  //  for LIKE Predicate for EDbColLongText8	
   529 LOCAL_C void LikePredicateDbColLongText8TestL()
   530  	{
   531 	TheTest.Next (_L ("LikePredicate DbColLongText8 TestL"));
   532 
   533  	//Creating database
   534 
   535 	RFs      fsSession;
   536     User::LeaveIfError(fsSession.Connect());
   537     CleanupClosePushL(fsSession);
   538 	RDbNamedDatabase database;
   539 	User::LeaveIfError(database.Replace(fsSession, KDbName));
   540 	CleanupClosePushL(database);
   541 		
   542 	//Create table
   543 
   544 	CDbColSet* columns= CDbColSet::NewLC();
   545 			
   546 	TDbCol name(KColName,EDbColLongText8,KDbNameLen);
   547 	name.iAttributes = TDbCol::ENotNull;
   548 		
   549 	TDbCol name2(KCol2Name,EDbColLongText8,KDbNameLen);
   550 
   551 	columns->AddL(name);
   552 	columns->AddL(name2);
   553    	
   554 	User::LeaveIfError (database.CreateTable (KTable, *columns));
   555 	CleanupStack::PopAndDestroy(); // columns
   556     
   557 	// Insert values into the table    
   558  	TInt error =	database.Execute(KSQLInsert1);
   559     TheTest(error>=0);
   560     error =database.Execute(KSQLInsert2);
   561     TheTest(error>=0);
   562     error =database.Execute(KSQLInsert3);
   563     TheTest(error>=0);
   564 	error =	database.Execute(KSQLInsert4);
   565 	TheTest(error>=0);
   566 	error =	database.Execute(KSQLInsert5);
   567 	TheTest(error>=0);
   568 	error = database.Execute(KSQLInsert6);
   569 	TheTest(error>=0);
   570 	error = database.Execute(KSQLInsert7);
   571 	TheTest(error>=0);
   572 			
   573 	TheTest.Next(_L("Test for valid LIKE predicate queries"));
   574 	
   575 		
   576 	for(TInt i =0;i<KNumQueries;++i)
   577 		{
   578 		RDebug::Print(_L("Executing statement: %s \n"),(KQuery[i].query));
   579 		RDbView view;
   580 		TInt prepErr = view.Prepare(database, TDbQuery(TPtrC(KQuery[i].query), EDbCompareFolded), view.EReadOnly);
   581 		TheTest(prepErr>=0);
   582 		TInt evErr = view.EvaluateAll();
   583 		TheTest(evErr==0);
   584 		TBuf8<256> result;
   585 		TBuf8<256> colname;
   586 		result.Copy(TPtrC(KQuery[i].result));
   587 		view.FirstL(); 
   588 		
   589 		while (view.AtRow())
   590 			{
   591 			view.GetL();
   592 			RDbColReadStream rd;
   593 			rd.OpenLC(view,1);
   594 			rd.ReadL(colname,view.ColLength(1));
   595 			CleanupStack::PopAndDestroy();
   596 			RDebug::Print(_L("Expected result: %S Actual Result: %S\n"),&result,&colname);
   597 			TInt err = colname.CompareF(result);
   598 			TheTest(err ==0);
   599 				
   600 			view.NextL();
   601 			}
   602 					
   603 		view.Close();		
   604 		}
   605 		
   606 		// test for illegal statements, check they return KErrArgument
   607 	TheTest.Next(_L("Test that illegal queries return KErrArgument"));	
   608 	
   609 	
   610 		
   611 	for(TInt j =0;j<KNumBadQueries;++j)
   612 		{
   613 		RDebug::Print(_L("Executing illegal statement: %s \n"),(KBadQuery[j].query));	
   614 		RDbView view;
   615 		TInt prepErr = view.Prepare(database, TDbQuery(TPtrC(KBadQuery[j].query), EDbCompareFolded), view.EReadOnly);
   616 		TheTest(prepErr==KErrArgument);
   617 	
   618 		view.Close();		
   619 		}
   620 	       
   621    	CleanupStack::PopAndDestroy(&database); // database
   622     CleanupStack::PopAndDestroy(&fsSession); // fsSession
   623  	}
   624 
   625 
   626 
   627 // Test for LIKE Predicate for EDbColText
   628 LOCAL_C void LikePredicateDbColTextTestL()
   629  	{
   630  	TheTest.Next (_L ("LikePredicate DbColText TestL"));
   631  	//Creating database
   632 	
   633 	RFs      fsSession;
   634     User::LeaveIfError(fsSession.Connect());
   635     CleanupClosePushL(fsSession);
   636 	RDbNamedDatabase database;
   637 	User::LeaveIfError(database.Replace(fsSession, KDbName));
   638 	CleanupClosePushL(database);
   639 	
   640 	//Create table
   641 
   642 	CDbColSet* columns= CDbColSet::NewLC();
   643 			
   644 	TDbCol name(KColName,EDbColText,KDbNameLen);
   645 	name.iAttributes = TDbCol::ENotNull;
   646 	
   647 	TDbCol name2(KCol2Name,EDbColText,KDbNameLen);
   648 
   649 	columns->AddL(name);
   650 	columns->AddL(name2);
   651 		
   652 	User::LeaveIfError (database.CreateTable (KTable, *columns));
   653 	CleanupStack::PopAndDestroy(); // columns
   654    
   655  	// Insert values into the table
   656     TInt error =	database.Execute(KSQLInsert1);
   657     TheTest(error>=0);
   658     error =database.Execute(KSQLInsert2);
   659     TheTest(error>=0);
   660     error =database.Execute(KSQLInsert3);
   661     TheTest(error>=0);
   662 	error =	database.Execute(KSQLInsert4);
   663 	TheTest(error>=0);
   664 	error =	database.Execute(KSQLInsert5);
   665 	TheTest(error>=0);
   666 	error = database.Execute(KSQLInsert6);
   667 	TheTest(error>=0);
   668 	error = database.Execute(KSQLInsert7);
   669 	TheTest(error>=0);
   670 		
   671 
   672 	TheTest.Next(_L("Test for valid LIKE predicate queries"));
   673 	
   674 		
   675 	for(TInt i =0;i<KNumQueries;++i)
   676 		{
   677 		RDebug::Print(_L("Executing statement: %s \n"),(KQuery[i].query));
   678 		RDbView view;
   679 		TInt prepErr = view.Prepare(database, TDbQuery(TPtrC(KQuery[i].query), EDbCompareFolded), view.EReadOnly);
   680 		if(TPtrC(KQuery[i].result).Length() == 0)
   681 			{
   682 			TheTest(prepErr != KErrNone);
   683 			continue;	
   684 			}
   685 				
   686 		view.EvaluateAll();
   687 		TBufC<256> colname;
   688 		TBufC<256> res;
   689 		view.FirstL();      
   690 		while (view.AtRow())
   691 			{
   692 			view.GetL();
   693 			colname = view.ColDes(1);
   694 			res= KQuery[i].result;
   695 			RDebug::Print(_L("Expected result: %s Actual Result: %S\n"),(KQuery[i].result),&colname);
   696 			TInt err =	colname.Compare(TPtrC(KQuery[i].result));	
   697 			TheTest(err ==0);
   698 			view.NextL();
   699 			}
   700 		view.Close();		
   701 		}
   702 	
   703 	// test for illegal statements, check they return KErrArgument
   704 	TheTest.Next(_L("Test that illegal queries return KErrArgument"));	
   705 	
   706 	for(TInt j =0;j<KNumBadQueries;++j)
   707 		{
   708 		RDebug::Print(_L("Executing illegal statement: %s \n"),(KBadQuery[j].query));
   709 		RDbView view;
   710 		TInt prepErr = view.Prepare(database, TDbQuery(TPtrC(KBadQuery[j].query), EDbCompareFolded), view.EReadOnly);
   711 		TheTest(prepErr==KErrArgument);
   712 		view.Close();		
   713 		}
   714         
   715     CleanupStack::PopAndDestroy(&database); // database
   716     CleanupStack::PopAndDestroy(&fsSession); // fsSession
   717     }
   718  	
   719  	
   720  	
   721  /**
   722 @SYMTestCaseID 			SYSLIB-DBMS-UT-1592
   723 @SYMTestCaseDesc     	Testing limited-ESCAPE-clause
   724 @SYMTestPriority     	High
   725 @SYMTestActions      	Execute DBMS query with ESCAPE-clause
   726 @SYMTestExpectedResults The test should not fail.
   727 @SYMDEF INC076370 
   728 */ 	
   729 LOCAL_C void Defect_INC076370 ()
   730 	{
   731 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-DBMS-UT-1592 Defect INC076370 "));
   732 	LikePredicateDbColTextTestL(); //EDbColText
   733 	LikePredicateDbColLongText16TestL(); //EDbColLongText16
   734 	LikePredicateDbColLongText8TestL();	//EDbColLongText8	
   735 	}
   736 	
   737  /**
   738 @SYMTestCaseID 			SYSLIB-DBMS-UT-1667
   739 @SYMTestCaseDesc     	Testing RdbRowSet::DeleteL() with EDbColLongText16 type columns
   740 @SYMTestPriority     	High
   741 @SYMTestActions      	Create a table with a EDbColLongText16 type column and then use
   742 						RdbRowSet::DeleteL() to delete the current row.
   743 @SYMTestExpectedResults The test should not fail.
   744 @SYMDEF INC083027
   745 */ 	
   746 LOCAL_C void Defect_INC083027 ()
   747 	{
   748 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-DBMS-UT-1667 Defect INC083027 "));
   749 	RFs fs;
   750 	CleanupClosePushL(fs);
   751 	User::LeaveIfError(fs.Connect());
   752 
   753 	// create database
   754 	RDbNamedDatabase database;
   755 	CleanupClosePushL(database);
   756 	User::LeaveIfError(database.Replace(fs, _L("c:\\test.db")));
   757 
   758 	CDbColSet* columns = CDbColSet::NewLC();
   759 	const TInt maxTextLength = 256;
   760 	TDbCol text16Col(KText16Name, EDbColLongText16, maxTextLength);
   761 	columns->AddL(text16Col);
   762 
   763 	TBuf<KMaxColName> targetColName;
   764 	targetColName = KText16Name;
   765 	
   766 	// create table
   767 	User::LeaveIfError(database.CreateTable(KTableName, *columns));
   768 	
   769 	//create index
   770 	CDbKey* key = CDbKey::NewLC();
   771 	TInt keyLength = 122;
   772 	TDbKeyCol keyCol(targetColName, keyLength);
   773 	key->AddL(keyCol);
   774 	User::LeaveIfError(database.CreateIndex(KIndexName, KTableName, *key));
   775 	CleanupStack::PopAndDestroy(2);		// key and columns
   776 	
   777 	//insert rows
   778 	HBufC* sqlQueryBuf = HBufC::NewLC(512);
   779 	TPtr sqlQuery(sqlQueryBuf->Des());
   780 	_LIT(KSQLInsertFormat, "SELECT %S FROM %S");
   781 	sqlQuery.Format(KSQLInsertFormat, &targetColName, &KTableName);
   782 	
   783 	RDbView insertview;
   784 	User::LeaveIfError(insertview.Prepare(database, TDbQuery(sqlQuery), RDbView::EInsertOnly));
   785 	
   786 	HBufC* tmpBuf = HBufC::NewLC(maxTextLength);
   787 	TPtr maxString(tmpBuf->Des());
   788 	maxString.Format(KMaxStringFormat, 0);
   789 	insertview.InsertL();
   790 	insertview.SetColL(1, maxString);
   791 	insertview.PutL();
   792 	insertview.Close();
   793 	
   794 	//delete the row
   795 	RDbView deleteview;
   796 	User::LeaveIfError(deleteview.Prepare(database, TDbQuery(sqlQuery), RDbView::EUpdatable));
   797 	User::LeaveIfError(deleteview.EvaluateAll());
   798 
   799 	while (deleteview.NextL())
   800 		{
   801 		deleteview.GetL();
   802 		TRAPD(err , deleteview.DeleteL());
   803 		TheTest(err==KErrNone);
   804 		}
   805 	deleteview.Close();
   806 	
   807 	CleanupStack::PopAndDestroy(2);  // tmpBuf, sqlQueryBuf
   808     CleanupStack::PopAndDestroy(&database); // database
   809     CleanupStack::PopAndDestroy(&fs); // fs
   810 	}
   811 
   812  /**
   813 @SYMTestCaseID          SYSLIB-DBMS-UT-1894
   814 @SYMTestCaseDesc        Testing memory handling in CSqlMultiNode::Concatenate() 
   815 @SYMTestPriority        Medium
   816 @SYMTestActions         Execute a special request to a database which will trigger CSqlMultiNode::Concatenate(), and the size of one of the SQL nodes will be divisible by the CSqlMultiNode granularity
   817 @SYMTestExpectedResults The test should not fail or panic.
   818 @SYMDEF INC093657
   819 */        
   820 LOCAL_C void Defect_INC093657L ()
   821    {
   822    TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-DBMS-UT-1894 Defect INC093657 "));
   823    RFs fs;
   824    CleanupClosePushL(fs);
   825    User::LeaveIfError(fs.Connect());
   826 
   827    // create database
   828    RDbNamedDatabase database;
   829    CleanupClosePushL(database);
   830    User::LeaveIfError(database.Replace(fs, _L("c:\\test.db")));
   831    
   832    CDbColSet* columns = CDbColSet::NewLC();
   833    const TInt maxTextLength = 256;
   834    TDbCol column(KColName, EDbColLongText16, maxTextLength);
   835    columns->AddL(column);
   836    
   837    // create table
   838    User::LeaveIfError(database.CreateTable(KTableName, *columns));
   839    CleanupStack::PopAndDestroy();  // columns
   840    
   841    //execute a pointless request that is intended to detect subtle memory corruptions in CSqlMultiNode::Concatenate
   842    RDbView view;   
   843    TInt err = view.Prepare(database, TDbQuery(KSqlRequestGranularity));   
   844    
   845    TheTest(err==KErrNone);   
   846    
   847    view.Close();
   848    database.Destroy();
   849    
   850    CleanupStack::PopAndDestroy(&database); // database
   851    CleanupStack::PopAndDestroy(&fs); // fs
   852    }
   853 
   854 /**
   855 @SYMTestCaseID			SYSLIB-DBMS-UT-3467
   856 @SYMTestCaseDesc		Test for DEF105615 "DBMS, CDbColSet::operator[](TDbColNo) operator may access an invalid memory".
   857 						The test creates a table with 3 coluumns and a multi-column key (3 columns). The column
   858 						names length is such that when RDbRowSet::ColSetL() is called for retrieving the column
   859 						names, the CDbColSet array data member will make just a single memory allocation, where
   860 						all TDbCol elements will be stored. Then the test repeats 100 times, the following statements:
   861 							<retrieve a colset>;
   862 							<create a copy of colset's last TDbCol element using TDbCol's copy constructor>;
   863 							<create a copy of colset's last TDbCol element using TDbCol's "=" operator>;
   864 						If the test uses the compiler generated TDbCol's copy constructor and "=" operator,
   865 						the test crashes at some iteration, because an invalid memory region is accessed and
   866 						the crash is: KERN-EXEC 3.
   867 						The same test is repeated for TDbKeyCol's copy constructor and "=" operator.
   868 @SYMTestPriority		High
   869 @SYMTestActions			Test for DEF105615 "DBMS, CDbColSet::operator[](TDbColNo) operator may access an invalid memory".
   870 @SYMTestExpectedResults The test must not fail
   871 @SYMDEF					DEF105615
   872 */
   873 void DEF105615()
   874 	{
   875    	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-DBMS-UT-3467 DEF105615 DBMS, CDbColSet::operator[](TDbColNo) operator may access an invalid memory"));
   876 	RFs fs;
   877 	TInt err = fs.Connect();
   878 	TheTest(err == KErrNone);
   879 	
   880 	RDbNamedDatabase db;
   881 	err = db.Replace(fs, KDbName);
   882 	TheTest(err == KErrNone);
   883 
   884 	const TInt KColCnt = 3;
   885 
   886 	err = db.Execute(_L("CREATE TABLE A(A1234567890 INTEGER, B1234567890 INTEGER, C12345 INTEGER)"));
   887 	TheTest(err == KErrNone);
   888 	err = db.Execute(_L("CREATE INDEX I1 ON A(A1234567890, B1234567890, C12345)"));
   889 	TheTest(err == KErrNone);
   890 	
   891 	RDbTable tbl;
   892 	err = tbl.Open(db, _L("A"));
   893 	TheTest(err == KErrNone);
   894 
   895 	//It is very hard to reproduce the problem, because the memory region after the memory, occupied by 
   896 	//CDbColSet's array, may be valid. That is the reason the test is repeated in a loop KTestCnt times, 
   897 	//where every  ColSetL() call will allocate a new block of memory for its array and at some point TDbCol's 
   898 	//copy constructor and "=" operator may try to access an invalid memory area, if the compiler generated
   899 	//ones are used.
   900 	const TInt KTestCnt = 100;
   901 	TInt i;
   902 	CDbColSet* colset[KTestCnt];
   903 	for(i=0;i<KTestCnt;++i)
   904 		{
   905 		TRAP(err, colset[i] = tbl.ColSetL());
   906 		TheTest(err == KErrNone);
   907 		TDbCol lastCol = (*colset[i])[KColCnt];		//copy constructor
   908 		lastCol = (*colset[i])[KColCnt];			//"=" operator
   909 		}
   910 	for(i=0;i<KTestCnt;++i)
   911 		{
   912 		delete colset[i];
   913 		}
   914 	
   915 	tbl.Close();
   916 
   917 	//The same test is repeated for TDbKeyCol's copy constructor and "=" operator
   918 	CDbKey* key[KTestCnt];	
   919 	for(i=0;i<KTestCnt;++i)
   920 		{
   921 		TRAP(err, key[i] = db.KeyL(_L("I1"), _L("A")));
   922 		TheTest(err == KErrNone);
   923 		TDbKeyCol lastKeyCol = (*key[i])[KColCnt - 1];	//copy constructor
   924 		lastKeyCol = (*key[i])[KColCnt - 1];			//"=" operator
   925 		}
   926 	for(i=0;i<KTestCnt;++i)
   927 		{
   928 		delete key[i];
   929 		}
   930 
   931 	db.Close();
   932 	err = fs.Delete(KDbName);
   933 	TheTest(err == KErrNone);
   934 	fs.Close();
   935 	}
   936    
   937 /**
   938 @SYMTestCaseID			SYSLIB-DBMS-UT-3469
   939 @SYMTestCaseDesc		Test for DEF105615 "DBMS, CDbColSet::operator[](TDbColNo) operator may access an invalid memory".
   940 						The test creates TDbCol and TDbKeyCol objects, creates their copies using copy constructors
   941 						and "=" operators and checks that the copies were constructed correctly,
   942 @SYMTestPriority		High
   943 @SYMTestActions			Test for DEF105615 "DBMS, CDbColSet::operator[](TDbColNo) operator may access an invalid memory".
   944 @SYMTestExpectedResults The test must not fail
   945 @SYMDEF					DEF105615
   946 */
   947 void DEF105615_2()
   948 	{
   949    	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-DBMS-UT-3469 DEF105615 DBMS, CDbColSet::operator[](TDbColNo) operator may access an invalid memory - 2 "));
   950    	
   951    	const TDbColType KColType = EDbColText16;
   952    	const TInt KMaxColLen = 73;
   953    	const TUint KColAttributes = TDbCol::ENotNull;
   954    	_LIT(KColName, "Name");
   955    	
   956    	TDbCol srcDbCol(KColName, EDbColText16, KMaxColLen);
   957    	srcDbCol.iAttributes = KColAttributes;
   958 
   959 	//TDbCol - copy constructor   	
   960    	TDbCol dbColCopy1(srcDbCol);
   961    	TheTest(dbColCopy1.iType == srcDbCol.iType && dbColCopy1.iType == KColType);
   962    	TheTest(dbColCopy1.iMaxLength == srcDbCol.iMaxLength && dbColCopy1.iMaxLength == KMaxColLen);
   963    	TheTest(dbColCopy1.iAttributes == srcDbCol.iAttributes && dbColCopy1.iAttributes == KColAttributes);
   964    	TheTest(dbColCopy1.iName == srcDbCol.iName && dbColCopy1.iName == KColName);
   965 
   966 	//TDbCol - "=" operator
   967    	TDbCol dbColCopy2;
   968    	dbColCopy2 = srcDbCol;
   969    	TheTest(dbColCopy2.iType == srcDbCol.iType && dbColCopy2.iType == KColType);
   970    	TheTest(dbColCopy2.iMaxLength == srcDbCol.iMaxLength && dbColCopy2.iMaxLength == KMaxColLen);
   971    	TheTest(dbColCopy2.iAttributes == srcDbCol.iAttributes && dbColCopy2.iAttributes == KColAttributes);
   972    	TheTest(dbColCopy2.iName == srcDbCol.iName && dbColCopy2.iName == KColName);
   973 
   974 	//TDbCol - self assignment
   975 	srcDbCol = srcDbCol;
   976    	TheTest(srcDbCol.iType == KColType);
   977    	TheTest(srcDbCol.iMaxLength == KMaxColLen);
   978    	TheTest(srcDbCol.iAttributes == KColAttributes);
   979    	TheTest(srcDbCol.iName == KColName);
   980 
   981    	const TInt KKeyLen = 29;
   982    	const TDbKeyCol::TOrder KKeyOrder = TDbKeyCol::EDesc;
   983    	_LIT(KKeyName, "Name22");
   984 
   985    	TDbKeyCol srcDbKeyCol(KKeyName, KKeyLen, KKeyOrder);
   986    	
   987 	//TDbKeyCol - copy constructor   	
   988    	TDbKeyCol dbKeyColCopy1(srcDbKeyCol);
   989    	TheTest(dbKeyColCopy1.iOrder == srcDbKeyCol.iOrder && dbKeyColCopy1.iOrder == KKeyOrder);
   990    	TheTest(dbKeyColCopy1.iLength == srcDbKeyCol.iLength && dbKeyColCopy1.iLength == KKeyLen);
   991    	TheTest(dbKeyColCopy1.iName == srcDbKeyCol.iName && dbKeyColCopy1.iName == KKeyName);
   992    	
   993 	//TDbKeyCol - "=" operator
   994    	TDbKeyCol dbKeyColCopy2;
   995    	dbKeyColCopy2 = srcDbKeyCol;
   996    	TheTest(dbKeyColCopy2.iOrder == srcDbKeyCol.iOrder && dbKeyColCopy2.iOrder == KKeyOrder);
   997    	TheTest(dbKeyColCopy2.iLength == srcDbKeyCol.iLength && dbKeyColCopy2.iLength == KKeyLen);
   998    	TheTest(dbKeyColCopy2.iName == srcDbKeyCol.iName && dbKeyColCopy2.iName == KKeyName);
   999 
  1000 	//TDbKeyCol - self assignment
  1001 	srcDbKeyCol = srcDbKeyCol;
  1002    	TheTest(srcDbKeyCol.iOrder == KKeyOrder);
  1003    	TheTest(srcDbKeyCol.iLength == KKeyLen);
  1004    	TheTest(srcDbKeyCol.iName == KKeyName);
  1005    	}
  1006    
  1007 /**
  1008 @SYMTestCaseID          SYSLIB-DBMS-UT-3413
  1009 @SYMTestCaseDesc        Testing that "incremental update" operations running in one connection does not 
  1010 						interfere with database operations executed from a second connection
  1011 @SYMTestPriority        High
  1012 @SYMTestActions         Create a test database with one table and insert some records there (> 100).
  1013 						Create 2 database connections.
  1014 						Open that database from connection 1 and execute an incremental update operation
  1015 						in a transaction. At the same time try to open and close the same table from 
  1016 						connection 2, mixing these operations with the RDbUpdate::Next() calls from 
  1017 						connection 1. So the call pattern should be:
  1018 						@code
  1019 						RDbUpdate dbUpdate;
  1020 						....
  1021 						while((err = dbUpdate.Next()) > 0)		//from "Conenction 1"
  1022 							{
  1023 							RDbTable tbl;
  1024 							err = tbl.Open(TheDb2, _L("A"));	//from "Conenction 2"
  1025 							...
  1026 							}
  1027 						@endcode						
  1028 @SYMTestExpectedResults The test should not fail or panic.
  1029 @SYMDEF INC101720
  1030 */
  1031 void Defect_INC101720()
  1032 	{
  1033     TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-DBMS-UT-3413 "));
  1034 	//Create the test database
  1035 	TInt err = TheDb1.Replace(TheFs, KDbName);
  1036 	TEST2(err, KErrNone);
  1037 	TheDb1.Close();
  1038 	TheCrcChecker.GenerateCrcL(KDbName);
  1039 
  1040 	//Establish the first database connection
  1041 #ifndef __TOOLS2__
  1042 	err = TheDbs1.Connect();
  1043 	TEST2(err, KErrNone);
  1044 #endif
  1045 	err = TheDb1.Open(TheDbs1, KDbName);
  1046 	TEST2(err, KErrNone);
  1047 	//Create a test table and fill the table with enough test records (> 100)
  1048 	err = TheDb1.Execute(_L("CREATE TABLE A(Id COUNTER, Id2 INTEGER, Name LONG VARCHAR)"));	
  1049 	TEST2(err, KErrNone);
  1050 	const TInt KTestRecCount = 200;
  1051 	err = TheDb1.Begin();	
  1052 	TEST2(err, KErrNone);
  1053 	for(TInt i=0;i<KTestRecCount;++i)
  1054 		{
  1055 		_LIT(KSqlFmtStr, "INSERT INTO A(Id2, Name) VALUES(%d, 'TestNameString')");
  1056 		TBuf<100> sql;
  1057 		// We can't use this because Math::Random() may return different numbers on 
  1058 		// different platform, which will lead to different output that result in 
  1059 		// CRC test failing.
  1060 		// TUint32 id = Math::Random() % KTestRecCount;
  1061 		TUint32 id = (i^0x55555555) % KTestRecCount;
  1062 
  1063 		sql.Format(KSqlFmtStr, id + 1);
  1064 		err = TheDb1.Execute(sql);
  1065 		TEST2(err, 1);
  1066 		}
  1067 	err = TheDb1.Commit();	
  1068 	TEST2(err, KErrNone);
  1069 	//Establish a second connection with the same test database
  1070 #ifndef __TOOLS2__
  1071 	err = TheDbs2.Connect();
  1072 	TEST2(err, KErrNone);
  1073 #endif
  1074 	err = TheDb2.Open(TheDbs2, KDbName);
  1075 	TEST2(err, KErrNone);
  1076 	//The test: Conenction 1 - "incremental update" operation.
  1077 	//			Connection 2 - "open table/close table" operations mixed with the incremental Next-s.
  1078 	//Expectation: The test must not fail.
  1079 	err = TheDb1.Begin();
  1080 	TEST2(err, KErrNone);
  1081 	RDbUpdate dbUpdate;
  1082 	err = dbUpdate.Execute(TheDb1, _L("UPDATE A SET Name = 'ModifiedNameString' WHERE Id2 > 10"));
  1083 	TEST2(err, KErrNone);
  1084 	TInt step = 0;
  1085 	while((err = dbUpdate.Next()) > 0)
  1086 		{
  1087 		++step;
  1088 		RDbTable tbl;
  1089 		err = tbl.Open(TheDb2, _L("A"));
  1090 		TEST2(err, KErrNone);
  1091 		tbl.Close();
  1092 		}
  1093 	TEST(step > 1);//just to be sure that the test executes dbUpdate.Next() more than once
  1094 	TEST2(err, KErrNone);
  1095 	dbUpdate.Close();
  1096 	err = TheDb1.Commit();
  1097 	TEST2(err, KErrNone);
  1098 	//Cleanup
  1099 	TheDb2.Close();
  1100 #ifndef __TOOLS2__
  1101 	TheDbs2.Close();
  1102 #endif
  1103 	TheDb1.Close();
  1104 #ifndef __TOOLS2__
  1105 	TheDbs1.Close();
  1106 #endif
  1107 	TheCrcChecker.GenerateCrcL(KDbName);
  1108 	}
  1109 
  1110 /**
  1111 @SYMTestCaseID          SYSLIB-DBMS-UT-3484
  1112 @SYMTestCaseDesc        DBMS Hindi collation doesn't work on long text fields.  
  1113 @SYMTestPriority        Medium
  1114 @SYMTestActions         This test is to check that DBMS correctly sorts columns using Collation, when 
  1115 						the columns are of type EDbColLongText16.  Previous implementations split the 
  1116 						strings to be compared into chunks, however this could cause it to be sorted 
  1117 						incorrectly if it was split on a combining or accent character.  This fault
  1118 						occurs on the default locale as well as Hindi.  Test steps:
  1119 						* Create a database table and adds several unicode strings to EDbColLongText16 
  1120 						  column in table.  One set of strings have an ascii character followed by 
  1121 						  an accent (e + ') and the other set have the combined equivilant ascii 
  1122 						  character (è).  These should have the same sort order,however if are split 
  1123 						  then will compare differently.
  1124 						* Sort the columns using EDbCompareCollated
  1125 						* Check that the columns were sorted in the correct order
  1126 @SYMTestExpectedResults The columns should get sorted into ascending order correctly
  1127 @SYMDEF INC107268
  1128 */
  1129 void Defect_INC107268L()
  1130 	{
  1131 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-DBMS-UT-3484 Defect INC107268 - DBMS Hindi collation doesn't work on long text fields"));
  1132      
  1133      // some unicode characters
  1134      const TChar Ka(0x0061); 		// a    
  1135      const TChar Kb(0x0062); 		// b
  1136      const TChar Ke(0x0065);			// e
  1137      const TChar Kgrave(0x0060); 	// ' (grave)
  1138      const TChar Kegrave(0x00e8);  	// e with grave
  1139      const TChar K1(0x0031);			// 1
  1140      const TChar K2(0x0032);			// 2
  1141  
  1142   	// the maximum characters in a EDbColLongText16 string before dbms stops storing
  1143   	// the string inline, and we need to read it from a stream (see TBlobKey).
  1144   	const TInt KInlineLimit = 127;
  1145   	
  1146   	// maximum number of characters buffered in TBlobKey when string stored out of line.
  1147   	// (see TBlobKey::ETruncSize which is in bytes)
  1148   	const TInt KTruncLimit = 16; 
  1149  
  1150   	const TInt KMaxStringSize = 256;
  1151   	
  1152   	TBuf<KMaxStringSize> inLineBoundryA;
  1153   	TBuf<KMaxStringSize> inLineBoundryB;
  1154   	TBuf<KMaxStringSize> truncBoundryA;
  1155   	TBuf<KMaxStringSize> truncBoundryB;
  1156   	TBuf<KMaxStringSize> padding;
  1157      
  1158      // this string will be stored inline.  It should sort to be < stringB
  1159      inLineBoundryA.Fill(Ka, KInlineLimit-2);
  1160      inLineBoundryA.Append(Kegrave);
  1161      inLineBoundryA.Append(K1);
  1162      
  1163      // this string is just over the break point, so *is* truncated. 
  1164      // this is expected to get sorted incorrecly as combining character is split off (negative test case)
  1165      inLineBoundryB.Fill(Ka, KInlineLimit-2);
  1166      inLineBoundryB.Append(Ke);
  1167      inLineBoundryB.Append(Kgrave);
  1168      inLineBoundryB.Append(K2);
  1169          
  1170      padding.Fill(Kb, KInlineLimit);
  1171      
  1172      // this string is longger that KInlineLimit so is stored out of line
  1173      truncBoundryA.Fill(Kb, KTruncLimit-2);
  1174      truncBoundryA.Append(Kegrave);
  1175      truncBoundryA.Append(K1);
  1176      truncBoundryA.Append(padding);
  1177      
  1178      // this string has combining characters that fall on boundry of ETruncSize value (32 bytes)
  1179      truncBoundryB.Fill(Kb, KTruncLimit-2);
  1180      truncBoundryB.Append(Ke);
  1181      truncBoundryB.Append(Kgrave);
  1182      truncBoundryB.Append(K2);
  1183      truncBoundryB.Append(padding);
  1184      
  1185      
  1186      // e and '(grave) characters seperately
  1187      TBuf<3> e_grave; 
  1188      e_grave.Append( Ke );
  1189      e_grave.Append( Kgrave );    
  1190      
  1191      // e with grave character - this should sort the same as e_grave
  1192      TBuf<3> egrave; 
  1193      egrave.Append( Kegrave );
  1194      
  1195      TBuf<1> nullString;
  1196      
  1197  	e_grave.Append(K2); // make e_grave sort second
  1198      egrave.Append(K1);  // make egrave sort first
  1199      
  1200      // Check with database
  1201      _LIT(KPosLmLandmarkTable, "lmt_landmark");
  1202      _LIT(KPosLmLandmarkIdCol, "lmc_lmid");
  1203      _LIT(KPosLmNameCol, "lmc_name");
  1204          
  1205      TInt err = TheDb1.Replace( TheFs, KDbName );
  1206      TEST2 (err, KErrNone);
  1207  	CleanupClosePushL(TheDb1);
  1208          
  1209      CDbColSet* columns = CDbColSet::NewLC();
  1210      TDbCol idField( KPosLmLandmarkIdCol, EDbColUint32 );
  1211      idField.iAttributes |= TDbCol::EAutoIncrement;
  1212      columns->AddL( idField );
  1213      columns->AddL( TDbCol( KPosLmNameCol, EDbColLongText16 ) ); // Works with EDbColText16.  Defect only for EDbColLongText16.
  1214      
  1215      err = TheDb1.CreateTable( KPosLmLandmarkTable, *columns );
  1216      TEST2 (err, KErrNone);   
  1217      CleanupStack::PopAndDestroy(columns);
  1218      
  1219      RDbTable table;
  1220      err = table.Open( TheDb1, KPosLmLandmarkTable );
  1221      TEST2 (err, KErrNone);
  1222      CleanupClosePushL(table);
  1223  	
  1224  	// add rows to table
  1225    	table.InsertL();
  1226      table.SetColL( 2, egrave); // row 0 - sorted 8th
  1227      table.PutL();
  1228  
  1229  	table.InsertL();
  1230      table.SetColL( 2, e_grave ); // row 1 - sorted 9th
  1231      table.PutL();
  1232     
  1233      table.InsertL();
  1234      table.SetColL( 2, inLineBoundryA ); // row 2 - sorted 3rd (incorrectly - negative test case)
  1235      table.PutL();
  1236      
  1237      table.InsertL();
  1238      table.SetColL( 2, inLineBoundryB ); // row 3 - sorted 2nd (incorrectly - negative test case)
  1239      table.PutL();
  1240      
  1241      table.InsertL();
  1242      table.SetColL( 2, nullString ); // row 4 - sorted 1st
  1243      table.PutL();
  1244    
  1245    	table.InsertL();
  1246      table.SetColL( 2, truncBoundryB ); // row 5 - sorted 5th
  1247      table.PutL();
  1248      
  1249      table.InsertL();
  1250      table.SetColL( 2, truncBoundryA ); // row 6 - sorted 4th
  1251      table.PutL();
  1252      
  1253  
  1254      CleanupStack::PopAndDestroy(); // table.close()
  1255  
  1256  	// do an sql select with Order By to sort columns
  1257      _LIT(KPosLmSqlSelectOrderByString, "SELECT %S, %S FROM %S ORDER BY %S");
  1258      TBuf<200> sql;
  1259      sql.Format( KPosLmSqlSelectOrderByString, 
  1260                  &KPosLmLandmarkIdCol,
  1261                  &KPosLmNameCol,
  1262                  &KPosLmLandmarkTable,
  1263                  &KPosLmNameCol);
  1264      
  1265      RDbView view;
  1266      CleanupClosePushL(view);
  1267      err = view.Prepare( TheDb1, TDbQuery( sql, EDbCompareCollated ) );
  1268      TEST2 (err, KErrNone);
  1269      err = view.EvaluateAll();
  1270      TEST2 (err, KErrNone);
  1271          
  1272      // Now check that view is ordered correctly  
  1273      const TUint32 ExpectedOrder[] = {4,3,2,6,5,0,1};
  1274      TInt x = 0;
  1275      while (view.NextL())
  1276          {
  1277          view.GetL();
  1278          TEST2(view.ColUint32(1), ExpectedOrder[x]); // check we got the expected order
  1279          x++;          
  1280          }
  1281      TEST2(x, 7); // check we got the right number of values
  1282      CleanupStack::PopAndDestroy(2); // TheDb1.Close(); view.Close()
  1283 	TheCrcChecker.GenerateCrcL(KDbName);
  1284 
  1285  	}
  1286  
  1287    
  1288 LOCAL_C void DoTestsL ()
  1289 	{
  1290 	__UHEAP_MARK;
  1291 	CleanupClosePushL(TheFs);
  1292 	Defect_INC076370();
  1293 	Defect_INC083027();
  1294 
  1295 //	Defect_DEF44697L ();  // Use of three RThreads, not possible for tools2.
  1296 
  1297 	Defect_INC093657L(); 
  1298 	Defect_INC101720(); 
  1299 // These two tests don't work on 9.3.
  1300 	DEF105615();
  1301 	DEF105615_2();
  1302 
  1303 	Defect_INC107268L();
  1304 	CleanupStack::PopAndDestroy(); // TheFs.Close()
  1305 	__UHEAP_MARKEND;
  1306 	}
  1307 
  1308 GLDEF_C TInt E32Main ()
  1309 	{
  1310 	__UHEAP_MARK;
  1311 	TheTest.Title ();
  1312 	TheTest.Start (_L ("Verify Defect Fixes"));
  1313 	
  1314 	TheTrapCleanup = CTrapCleanup::New ();
  1315 	__ASSERT_ALWAYS (TheTrapCleanup!=NULL, User::Invariant ());
  1316 
  1317 	TInt err = TheFs.Connect();
  1318 	TheTest(err == KErrNone);
  1319 	
  1320 	TRAP (err,DoTestsL ());
  1321 	TheTest (err==KErrNone);
  1322 
  1323 	//Wait some time, because DBMS server won't be destroyed right after the last DBMS session
  1324 	//being clossed.
  1325 	TheTest.Printf(_L("Wait DBMS server shutdown...\n"));
  1326 	User::After(7000000);
  1327 
  1328 #ifndef __linux__
  1329 #ifndef __TOOLS2__
  1330 	TRAPD(lc, err = TheCrcChecker.DumpCrcRecordsL(KCrcRecord));
  1331 	TheTest(err==KErrNone);
  1332 	TheTest(lc==KErrNone);
  1333 #else
  1334 	TRAPD(lc, err = TheCrcChecker.ValidateCrcRecordsL(KCrcRecord));
  1335 	TPtrC errmsg;
  1336 	TheCrcChecker.ErrorReportL(err, errmsg);
  1337 	RDebug::Print(errmsg);
  1338 	TheTest(err==KErrNone || err==TDBMS_CRCChecks::ECrcCheckOk);
  1339 #endif
  1340 #endif
  1341 
  1342 	delete TheTrapCleanup;
  1343 
  1344 	TheTest.End ();
  1345 	TheTest.Close ();
  1346 
  1347 	__UHEAP_MARKEND;
  1348 	return (0);
  1349 	}