os/persistentdata/persistentstorage/dbms/tdbms/t_dbfail.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 1998-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 <s32file.h>
    19 #include <e32test.h>
    20 #include "t_dbfail.h"
    21 
    22 _LIT(KTestTitle,"t_dbfail: DBMS Failure mode test");
    23 GLDEF_D RTest test(KTestTitle);
    24 GLDEF_D RDbs TheDbs;
    25 GLDEF_D RDbNamedDatabase TheDatabase;
    26 GLDEF_D TClientHeap KClientHeap;
    27 GLDEF_D TServerHeap KServerHeap;
    28 
    29 
    30 LOCAL_D CTrapCleanup* TheTrapCleanup;
    31 LOCAL_D RFs TheFs;
    32 LOCAL_D RDbView TheView;
    33 LOCAL_D RDbTable TheTable;
    34 LOCAL_D RDbRowSet::TAccess Access;
    35 LOCAL_D CDbColSet* TheColSet;
    36 LOCAL_D CDbKey* TheKey;
    37 LOCAL_D const TDesC* TheSql;
    38 LOCAL_D TBuf<64> TheFormat;
    39 
    40 const TInt KTestCleanupStack=0x20;
    41 _LIT(KTestDatabase,"C:\\DBMS-TST\\t_fail.db");
    42 _LIT(TableName,"Table1");
    43 _LIT(TableName2,"Table_two");
    44 _LIT(TableNameX,"Bad Table Name");
    45 _LIT(IndexName,"Index1");
    46 _LIT(IndexName2,"Index2");
    47 _LIT(Column1,"column_one");
    48 _LIT(Column1Fold,"COLUMN_ONE");
    49 _LIT(Column2,"column_2");
    50 _LIT(Column2X,"column_2%");
    51 _LIT(SimpleSql,"select * from Table1");
    52 _LIT(UpdateSql,"update Table1 SET column_2='hello'");
    53 
    54 //const TPtrC ComplexSql(_S("select * from Table1 where column_one<0 and not column_one is null or column_2 not like '*fred*' and column_2>'m' order by column_one desc"));
    55 const TPtrC ComplexSql[]=
    56 	{
    57 	_S("select * from Table1 where column_one<0 and column_one is null"),
    58 	_S("select * from Table1 where column_one<0 and (column_one is null and column_2 like '')"),
    59 	_S("select * from Table1 where (column_one<0 and column_one is null) and column_2 like ''"),
    60 	_S("select * from Table1 where (column_one<0 and column_one is null) and (column_2 like '' and column_one>0)"),
    61 	_S("select * from Table1 where (column_one<0 and column_one is null) and (column_2 like '' and column_one>0 and column_one is null)"),
    62 	_S("select * from Table1 where (column_one<0 and column_one is null and column_one = 10) and (column_2 like '' and column_one>0 and column_one is null)"),
    63 	_S("select * from Table1 where column_one<0 and column_one is null and column_one = 10 and column_2 like '' and column_one>0 and column_one is null")
    64 	};
    65 
    66 struct SSqlErrors
    67 	{
    68 	const TText* iSql;
    69 	TInt iError;
    70 	};
    71 
    72 LOCAL_D SSqlErrors const BadSql[]=
    73 	{
    74 		{_S("sponge"),KErrArgument},
    75 		{_S("select * from widget"),KErrNotFound},
    76 		{_S("select * from Table1 where x = 0"),KErrNotFound},
    77 		{_S("select * from Table1 where x 0 like"),KErrArgument},
    78 		{_S("select * from Table1 where column_2>'a' and column_one<'z'"),KErrGeneral},
    79 		{_S("select from Table1"),KErrArgument},
    80 		{_S("select x, from Table1"),KErrArgument},
    81 		{_S("select x from Table1"),KErrNotFound},
    82 		{_S("select column_2 column_one from Table1"),KErrArgument},
    83 		{_S("select * from Table1 order by x"),KErrNotFound},
    84 		{_S("select * from Table1 order column_one"),KErrArgument},
    85 		{_S("select * from Table1 order by column_one down"),KErrArgument}
    86 	};
    87 
    88 GLDEF_C void Connect()
    89 	{
    90 	TInt r=TheDbs.Connect();
    91 	test (r==KErrNone);
    92 	TheDbs.ResourceMark();
    93 	}
    94 
    95 GLDEF_C void Disconnect()
    96 	{
    97 	TheDbs.ResourceCheck();
    98 	TheDbs.Close();
    99 	}
   100 
   101 
   102 //SYMBIAN_REMOVE_TRIVIAL_ENCRYPTION version
   103 LOCAL_C void DbCreateL()
   104 	{
   105 	User::LeaveIfError(TheDatabase.Replace(TheFs,KTestDatabase,TheFormat));
   106 	}
   107 
   108 //SYMBIAN_REMOVE_TRIVIAL_ENCRYPTION version
   109 LOCAL_C void DbOpenL()
   110 	{
   111 	User::LeaveIfError(TheDatabase.Open(TheFs,KTestDatabase,TheFormat));
   112 	CleanupClosePushL(TheDatabase);
   113 	delete TheDatabase.TableNamesL();	// force a schema load
   114 	CleanupStack::Pop();
   115 	}
   116 
   117 //SYMBIAN_REMOVE_TRIVIAL_ENCRYPTION version
   118 LOCAL_C void DbShareL()
   119 	{
   120 	User::LeaveIfError(TheDatabase.Open(TheDbs,KTestDatabase,TheFormat));
   121 	CleanupClosePushL(TheDatabase);
   122 	delete TheDatabase.TableNamesL();	// force a schema load
   123 	CleanupStack::Pop();
   124 	}
   125 
   126 
   127 /**
   128 @SYMTestCaseID          SYSLIB-DBMS-CT-0612
   129 @SYMTestCaseDesc        Database validity test
   130 @SYMTestPriority        Medium
   131 @SYMTestActions         Tests for opening and closing of database
   132 @SYMTestExpectedResults Test must not fail
   133 @SYMREQ                 REQ0000
   134 */
   135 LOCAL_C void TestOpenL()
   136 	{
   137 	_LIT(KFileNotFound,"not a database");
   138 	TInt r=TheDatabase.Open(TheFs,KFileNotFound);
   139 	test (r==KErrNotFound || r==KErrPathNotFound);
   140 //
   141 	_LIT(KPathNotFound,"C:\\not a path\\database.db");
   142 	r=TheDatabase.Open(TheFs,KPathNotFound);
   143 	test (r==KErrPathNotFound);
   144 //
   145 	_LIT(KNotFormat,"not.a.dbx");
   146 	r=TheDatabase.Open(TheFs,KFileNotFound,KNotFormat);
   147 	test (r==KErrNotFound || r==KErrPathNotFound);
   148 //
   149 	DbCreateL();
   150 	TheDatabase.Close();
   151 	r=TheDatabase.Open(TheFs,KTestDatabase,TUid::Uid(0x01234567).Name());
   152 	test (r==KErrNone); // New code has no loadable drivers, it is irrelevant to expect error here
   153 	TheDatabase.Close(); // We have added it here because previous statement does not return error anymore
   154 //
   155 	RFile f;
   156 	r=f.Replace(TheFs,KTestDatabase,EFileWrite);
   157 	test (r==KErrNone);
   158 	TCheckedUid type(KDirectFileStoreLayoutUid);
   159 	r=f.Write(type.Des());
   160 	test (r==KErrNone);
   161 	f.Close();
   162 	r=TheDatabase.Open(TheFs,KTestDatabase);
   163 	test (r==KErrNotSupported);
   164 //
   165 	_LIT(KDefaultFormat,"epoc");
   166 	r=TheDatabase.Open(TheFs,KTestDatabase,KDefaultFormat);
   167 	test (r==KErrNotSupported); // We expect not supported db here
   168 	}
   169 
   170 class TClient : public TContext
   171 	{
   172 public:
   173 	TClient() {}
   174 private:
   175 	void OpenDbL() const
   176 		{DbOpenL();}
   177 	};
   178 class TServer : public TContext
   179 	{
   180 public:
   181 	TServer() {}
   182 private:
   183 	void OpenDbL() const
   184 		{DbShareL();}
   185 	};
   186 
   187 const TClient KClient;
   188 const TServer KServer;
   189 
   190 void TFail::Test(const THeapFail& aHeap,const TContext* aContext)
   191 	{
   192 	TInt ii;
   193 	TInt errCode;
   194 	for (ii=1;;++ii)
   195 		{
   196 		if (aContext)
   197 			{
   198 			TRAP(errCode, aContext->OpenDbL());
   199 			if(errCode != KErrNone)
   200 				return;
   201 			}
   202 		aHeap.Fail(ii);
   203 		aHeap.Mark();
   204 		TRAPD(r,RunL());
   205 		aHeap.Reset();
   206 		if (r==KErrNone)
   207 			break;
   208 		test(r==KErrNoMemory);
   209 		if (aContext)
   210 			TheDatabase.Close();
   211 		aHeap.Check();
   212 		}
   213 	End();
   214 	if (aContext)
   215 		TheDatabase.Close();
   216 	aHeap.Check();
   217 	}
   218 
   219 class TFailCreateDatabase : public TFail
   220 	{
   221 	void RunL()
   222 		{DbCreateL();}
   223 	void End()
   224 		{TheDatabase.Close();}
   225 	};
   226 
   227 class TFailOpenDatabase : public TFail
   228 	{
   229 	void RunL()
   230 		{DbOpenL();}
   231 	void End()
   232 		{TheDatabase.Close();}
   233 	};
   234 
   235 class TFailShareDatabase : public TFail
   236 	{
   237 	void RunL()
   238 		{DbShareL();}
   239 	void End()
   240 		{TheDatabase.Close();}
   241 	};
   242 
   243 /**
   244 @SYMTestCaseID          SYSLIB-DBMS-CT-0613
   245 @SYMTestCaseDesc        Tests for allocation failures on creating a database
   246 @SYMTestPriority        Medium
   247 @SYMTestActions         Tests for allocation failure for differently sourced databases
   248 @SYMTestExpectedResults Test must not fail
   249 @SYMREQ                 REQ0000
   250 */
   251 LOCAL_C void OriginsL()
   252 	{
   253 	test.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0613 Allocation failures on Creating a database "));
   254 	TFailCreateDatabase t1;
   255 	t1.Test(KClientHeap);
   256 //
   257 	test.Next(_L("Fail to create existing database"));
   258 	TUint att;
   259 	TInt r=TheFs.Att(KTestDatabase,att);
   260 	test (r==KErrNone);
   261 	r=TheDatabase.Create(TheFs,KTestDatabase,TheFormat);
   262 	test (r==KErrAlreadyExists);
   263 	r=TheFs.Att(KTestDatabase,att);
   264 	test (r==KErrNone);
   265 //
   266 	test.Next(_L("Allocation failures on Open"));
   267 	TFailOpenDatabase t2;
   268 	t2.Test(KClientHeap);
   269 //
   270 	test.Next(_L("Allocation failures on 1st Share"));
   271 	Connect();
   272 	TFailShareDatabase t3;
   273 	t3.Test(KClientHeap);
   274 	t3.Test(KServerHeap);
   275 //
   276 	test.Next(_L("Allocation failures on 2nd Share"));
   277 	DbShareL();
   278 	RDbNamedDatabase temp=TheDatabase;TheDatabase=RDbNamedDatabase();
   279 	t3.Test(KClientHeap);
   280 	t3.Test(KServerHeap);
   281 	temp.Close();
   282 	Disconnect();
   283 	test.End();
   284 	}
   285 
   286 
   287 class TFailCreateTable : public TFail
   288 	{
   289 	void RunL()
   290 		{User::LeaveIfError(TheDatabase.CreateTable(TableName,*TheColSet));}
   291 	};
   292 
   293 class TFailAlterTable : public TFail
   294 	{
   295 	void RunL()
   296 		{User::LeaveIfError(TheDatabase.AlterTable(TableName,*TheColSet));}
   297 	};
   298 
   299 class TFailDropTable : public TFail
   300 	{
   301 	void RunL()
   302 		{User::LeaveIfError(TheDatabase.DropTable(TableName));}
   303 	};
   304 
   305 class TFailCreateIndex : public TFail
   306 	{
   307 	void RunL()
   308 		{User::LeaveIfError(TheDatabase.CreateIndex(IndexName,TableName,*TheKey));}
   309 	};
   310 
   311 class TFailDropIndex : public TFail
   312 	{
   313 	void RunL()
   314 		{User::LeaveIfError(TheDatabase.DropIndex(IndexName,TableName));}
   315 	};
   316 
   317 class TFailGetObject : public TFail
   318 	{
   319 protected:
   320 	void End()
   321 		{delete iObject;}
   322 protected:
   323 	CBase* iObject;
   324 	};
   325 
   326 class TFailDatabaseTables : public TFailGetObject
   327 	{
   328 	void RunL()
   329 		{iObject=TheDatabase.TableNamesL();}
   330 	};
   331 
   332 class TFailDatabaseColSet : public TFailGetObject
   333 	{
   334 	void RunL()
   335 		{iObject=TheDatabase.ColSetL(TableName);}
   336 	};
   337 
   338 class TFailDatabaseIndexes : public TFailGetObject
   339 	{
   340 	void RunL()
   341 		{iObject=TheDatabase.IndexNamesL(TableName);}
   342 	};
   343 
   344 class TFailDatabaseKeys : public TFailGetObject
   345 	{
   346 	void RunL()
   347 		{iObject=TheDatabase.KeyL(IndexName,TableName);}
   348 	};
   349 
   350 const TInt KRowCount=60;
   351 
   352 LOCAL_C void WriteTableL()
   353 	{
   354 	DbOpenL();
   355 	TInt r=TheTable.Open(TheDatabase,TableName);
   356 	test (r==KErrNone);
   357 	TheDatabase.Begin();
   358 	for (TInt ii=0;ii<KRowCount;++ii)
   359 		{
   360 		TheTable.InsertL();
   361 		TheTable.SetColL(1,TUint((ii*17)%KRowCount));
   362 		TheTable.PutL();
   363 		}
   364 	r=TheDatabase.Commit();
   365 	test (r==KErrNone);
   366 	TheTable.Close();
   367 	TheDatabase.Close();
   368 	}
   369 
   370 LOCAL_C void DatabaseL()
   371 	{
   372 	test.Start(_L("Adding and dropping tables"));
   373 	DbCreateL();
   374 // ensure the database locking list has been allocated
   375 	TheDatabase.Begin();
   376 	TheDatabase.Commit();
   377 //
   378 	CDbColSet *col=CDbColSet::NewLC();
   379 //
   380 	test.Next(_L("Empty Column Set"));
   381 	__UHEAP_MARK;
   382 	test(TheDatabase.CreateTable(TableName,*col)!=KErrNone);
   383 	__UHEAP_MARKEND;
   384 //
   385 	test.Next(_L("Invalid table name"));
   386 	col->AddL(TDbCol(Column1,EDbColInt32));
   387 	__UHEAP_MARK;
   388 	test(TheDatabase.CreateTable(TableNameX,*col)!=KErrNone);
   389 	__UHEAP_MARKEND;
   390 //
   391 	test.Next(_L("Invalid column name"));
   392 	col->AddL(TDbCol(Column2X,EDbColBit));
   393 	__UHEAP_MARK;
   394 	test(TheDatabase.CreateTable(TableName,*col)!=KErrNone);
   395 	__UHEAP_MARKEND;
   396 //
   397 	test.Next(_L("Duplicate column name"));
   398 	col->Remove(Column2X);
   399 	col->AddL(TDbCol(Column1Fold,EDbColBit));
   400 	__UHEAP_MARK;
   401 	test(TheDatabase.CreateTable(TableName,*col)!=KErrNone);
   402 	__UHEAP_MARKEND;
   403 //
   404 	test.Next(_L("Invalid column type"));
   405 	col->Remove(Column1);
   406 	col->AddL(TDbCol(Column2,TDbColType(-1)));
   407 	__UHEAP_MARK;
   408 	test(TheDatabase.CreateTable(TableName,*col)!=KErrNone);
   409 	__UHEAP_MARKEND;
   410 //
   411 	test.Next(_L("Invalid maximum length"));
   412 	col->Remove(Column2);
   413 	col->AddL(TDbCol(Column2,EDbColInt32,0));
   414 	__UHEAP_MARK;
   415 	test(TheDatabase.CreateTable(TableName,*col)!=KErrNone);
   416 	__UHEAP_MARKEND;
   417 //
   418 	test.Next(_L("Invalid attributes"));
   419 	col->Remove(Column2);
   420 	TDbCol cc(Column2,EDbColInt32);
   421 	cc.iAttributes=13;
   422 	col->AddL(cc);
   423 	__UHEAP_MARK;
   424 	test(TheDatabase.CreateTable(TableName,*col)!=KErrNone);
   425 	__UHEAP_MARKEND;
   426 //
   427 	test.Next(_L("Adding/dropping a table name twice"));
   428 	col->Remove(Column2);
   429 	col->AddL(TDbCol(Column2,EDbColText8));
   430 	__UHEAP_MARK;
   431 	test(TheDatabase.CreateTable(TableName,*col)==KErrNone);
   432 	test(TheDatabase.CreateTable(TableName,*col)==KErrAlreadyExists);
   433 	test(TheDatabase.DropTable(TableNameX)!=KErrNone);
   434 	test(TheDatabase.DropTable(TableName)==KErrNone);
   435 	test(TheDatabase.DropTable(TableName)==KErrNotFound);
   436 	__UHEAP_MARKEND;
   437 //
   438 	test.Next(_L("Adding and dropping indexes"));
   439 	test(TheDatabase.CreateTable(TableName,*col)==KErrNone);
   440 	TheDatabase.Close();
   441 	CDbKey *key=CDbKey::NewLC();
   442 	__UHEAP_MARK;
   443 	DbOpenL();
   444 	test(TheDatabase.CreateIndex(IndexName,TableName,*key)!=KErrNone);
   445 	TheDatabase.Close();
   446 	__UHEAP_MARKEND;
   447 	key->AddL(Column2X());
   448 	__UHEAP_MARK;
   449 	DbOpenL();
   450 	test(TheDatabase.CreateIndex(IndexName,TableName,*key)!=KErrNone);
   451 	TheDatabase.Close();
   452 	__UHEAP_MARKEND;
   453 	key->Clear();
   454 	key->AddL(Column1());
   455 	__UHEAP_MARK;
   456 	DbOpenL();
   457 	test(TheDatabase.CreateIndex(TableNameX,TableName,*key)!=KErrNone);
   458 	TheDatabase.Close();
   459 	__UHEAP_CHECK(0);
   460 	DbOpenL();
   461 	test(TheDatabase.CreateIndex(IndexName,TableNameX,*key)!=KErrNone);
   462 	TheDatabase.Close();
   463 	__UHEAP_MARKEND;
   464 	__UHEAP_MARK;
   465 	DbOpenL();
   466 	test(TheDatabase.CreateIndex(IndexName,TableName,*key)==KErrNone);
   467 	test(TheDatabase.CreateIndex(IndexName,TableName,*key)==KErrAlreadyExists);
   468 	test(TheDatabase.DropIndex(TableNameX,TableName)!=KErrNone);
   469 	test(TheDatabase.DropIndex(IndexName,TableNameX)!=KErrNone);
   470 	test(TheDatabase.DropIndex(IndexName,TableName)==KErrNone);
   471 	test(TheDatabase.DropIndex(IndexName,TableName)==KErrNotFound);
   472 	test(TheDatabase.DropTable(TableName)==KErrNone);
   473 	test(TheDatabase.DropIndex(IndexName,TableName)==KErrNotFound);
   474 	TheDatabase.Close();
   475 	__UHEAP_MARKEND;
   476 //
   477 	test.Next(_L("Allocation failure during DDL"));
   478 	TFailCreateTable fct;
   479 	TFailAlterTable fat;
   480 	TFailDropTable fdt;
   481 	TFailCreateIndex fci;
   482 	TFailDropIndex fdi;
   483 	TheColSet=CDbColSet::NewL();
   484 	TheColSet->AddL(TDbCol(Column1,EDbColUint16));
   485 	TheKey=CDbKey::NewL();
   486 	TheKey->AddL(Column1());
   487 	fct.Test(KClientHeap,KClient);
   488 	WriteTableL();
   489 	TheColSet->AddL(TDbCol(Column2,EDbColText));
   490 	fat.Test(KClientHeap,KClient);
   491 	fci.Test(KClientHeap,KClient);
   492 	fdi.Test(KClientHeap,KClient);
   493 	fdt.Test(KClientHeap,KClient);
   494 //
   495 	test.Next(_L("Allocation failure during server DDL"));
   496 	Connect();
   497 	TheColSet->Remove(Column2);
   498 	fct.Test(KClientHeap,KServer);
   499 	WriteTableL();
   500 	TheColSet->AddL(TDbCol(Column2,EDbColText));
   501 	fat.Test(KClientHeap,KServer);
   502 	fci.Test(KClientHeap,KServer);
   503 	fdi.Test(KClientHeap,KServer);
   504 	fdt.Test(KClientHeap,KServer);
   505 //
   506 	TheColSet->Remove(Column2);
   507 	fct.Test(KServerHeap,KServer);
   508 	WriteTableL();
   509 	TheColSet->AddL(TDbCol(Column2,EDbColText));
   510 	fat.Test(KServerHeap,KServer);
   511 	fci.Test(KServerHeap,KServer);
   512 	fdi.Test(KServerHeap,KServer);
   513 	fdt.Test(KServerHeap,KServer);
   514 	Disconnect();
   515 //
   516 	delete TheColSet;
   517 	delete TheKey;
   518 
   519 //
   520 	test.Next(_L("Allocation failure on schema enquiry"));
   521 	DbCreateL();
   522 	test(TheDatabase.CreateTable(TableName,*col)==KErrNone);
   523 	test(TheDatabase.CreateIndex(IndexName,TableName,*key)==KErrNone);
   524 	CleanupStack::PopAndDestroy(2);	// columns set and key
   525 	TheDatabase.Close();
   526 	TFailDatabaseTables t4;
   527 	TFailDatabaseColSet t5;
   528 	TFailDatabaseIndexes t6;
   529 	TFailDatabaseKeys t7;
   530 	t4.Test(KClientHeap,KClient);
   531 	t5.Test(KClientHeap,KClient);
   532 	t6.Test(KClientHeap,KClient);
   533 	t7.Test(KClientHeap,KClient);
   534 //
   535 	test.Next(_L("Allocation failure on server schema enquiry"));
   536 	Connect();
   537 	t4.Test(KClientHeap,KServer);
   538 	t4.Test(KServerHeap,KServer);
   539 	t5.Test(KClientHeap,KServer);
   540 	t5.Test(KServerHeap,KServer);
   541 	t6.Test(KClientHeap,KServer);
   542 	t6.Test(KServerHeap,KServer);
   543 	t7.Test(KClientHeap,KServer);
   544 	t7.Test(KServerHeap,KServer);
   545 	Disconnect();
   546 	test.End();
   547 	}
   548 
   549 class TFailOpenTable : public TFail
   550 	{
   551 	void RunL()
   552 		{User::LeaveIfError(TheTable.Open(TheDatabase,TableName,Access));}
   553 	void End()
   554 		{TheTable.Close();}
   555 	};
   556 
   557 /**
   558 @SYMTestCaseID          SYSLIB-DBMS-CT-0614
   559 @SYMTestCaseDesc        Tests for allocation failure on opening and closing of database
   560 @SYMTestPriority        Medium
   561 @SYMTestActions         Tests for opening and closing of database
   562 @SYMTestExpectedResults Test must not fail
   563 @SYMREQ                 REQ0000
   564 */
   565 LOCAL_C void TestTableL(const THeapFail& aHeap,const TContext& aContext)
   566 	{
   567 	test.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0614 Allocation failure on Open "));
   568 	TFailOpenTable fot;
   569 	Access=RDbRowSet::EUpdatable;
   570 	fot.Test(aHeap,aContext);
   571 	Access=RDbRowSet::EReadOnly;
   572 	fot.Test(aHeap,aContext);
   573 	Access=RDbRowSet::EInsertOnly;
   574 	fot.Test(aHeap,aContext);
   575 //
   576 	test.Next(_L("Open invalid table"));
   577 	aContext.OpenDbL();
   578 	__UHEAP_MARK;
   579 	TInt r=TheTable.Open(TheDatabase,TableNameX);
   580 	test (r!=KErrNone);
   581 	__UHEAP_MARKEND;
   582 //
   583 	test.Next(_L("Set invalid index"));
   584 	r=TheTable.Open(TheDatabase,TableName);
   585 	test (r==KErrNone);
   586 	__UHEAP_MARK;
   587 	r=TheTable.SetIndex(IndexName2);
   588 	test (r!=KErrNone);
   589 	__UHEAP_MARKEND;
   590 //
   591 	test.Next(_L("Allocation failure on 2nd Open"));
   592 	RDbTable table(TheTable);
   593 	Access=RDbRowSet::EUpdatable;
   594 	fot.Test(aHeap);
   595 	Access=RDbRowSet::EReadOnly;
   596 	fot.Test(aHeap);
   597 	Access=RDbRowSet::EInsertOnly;
   598 	fot.Test(aHeap);
   599 	table.Close();
   600 	TheDatabase.Close();
   601 	test.End();
   602 	}
   603 
   604 LOCAL_C void TestTableDDL(const TContext& aContext)
   605 	{
   606 	test.Start(_L("DDL while open"));
   607 	aContext.OpenDbL();
   608 	TInt r=TheTable.Open(TheDatabase,TableName);
   609 	test (r==KErrNone);
   610 	CDbColSet* set=CDbColSet::NewLC();
   611 	set->AddL(TDbCol(Column1,EDbColText));
   612 	r=TheDatabase.CreateTable(TableName2,*set);
   613 	test (r==KErrNone);
   614 	TRAP(r,TheTable.CountL(TheTable.EQuick));
   615 	test (r==KErrNone);
   616 	TheTable.Close();
   617 	r=TheTable.Open(TheDatabase,TableName2);
   618 	test (r==KErrNone);
   619 //
   620 	set->AddL(TDbCol(Column2,EDbColUint32));
   621 	r=TheDatabase.AlterTable(TableName2,*set);
   622 	test (r==KErrNone);
   623 	CleanupStack::PopAndDestroy();		// set
   624 	TRAP(r,TheTable.CountL(TheTable.EQuick));
   625 	test (r==KErrDisconnected);
   626 	TheTable.Reset();
   627 	TRAP(r,TheTable.CountL(TheTable.EQuick));
   628 	test (r==KErrDisconnected);
   629 	TheTable.Close();
   630 	r=TheTable.Open(TheDatabase,TableName2);
   631 	test (r==KErrNone);
   632 //
   633 	CDbKey* key=CDbKey::NewLC();
   634 	key->AddL(Column2());
   635 	r=TheDatabase.CreateIndex(IndexName2,TableName,*key);
   636 	test (r==KErrNone);
   637 	TRAP(r,TheTable.CountL(TheTable.EQuick));
   638 	test (r==KErrNone);
   639 	r=TheDatabase.DropIndex(IndexName2,TableName);
   640 	test (r==KErrNone);
   641 	TRAP(r,TheTable.CountL(TheTable.EQuick));
   642 	test (r==KErrNone);
   643 //
   644 	r=TheDatabase.CreateIndex(IndexName,TableName2,*key);
   645 	test (r==KErrNone);
   646 	CleanupStack::PopAndDestroy();	// key
   647 	TRAP(r,TheTable.CountL(TheTable.EQuick));
   648 	test (r==KErrDisconnected);
   649 	TheTable.Close();
   650 	r=TheTable.Open(TheDatabase,TableName2);
   651 	test (r==KErrNone);
   652 //
   653 	r=TheDatabase.DropIndex(IndexName,TableName2);
   654 	test (r==KErrNone);
   655 	TRAP(r,TheTable.CountL(TheTable.EQuick));
   656 	test (r==KErrDisconnected);
   657 	TheTable.Close();
   658 	r=TheTable.Open(TheDatabase,TableName2);
   659 	test (r==KErrNone);
   660 //
   661 	r=TheDatabase.DropTable(TableName2);
   662 	test (r==KErrNone);
   663 	TRAP(r,TheTable.CountL(TheTable.EQuick));
   664 	test (r==KErrDisconnected);
   665 	TheTable.Close();
   666 	TheDatabase.Close();
   667 	test.End();
   668 	}
   669 
   670 LOCAL_C void TableL()
   671 	{
   672 	test.Start(_L("Testing Client-side"));
   673 	TestTableL(KClientHeap,KClient);
   674 	TestTableDDL(KClient);
   675 	test.Next(_L("Testing Client-Server"));
   676 	Connect();
   677 	TestTableL(KClientHeap,KServer);
   678 	TestTableL(KServerHeap,KServer);
   679 	TestTableDDL(KServer);
   680 	Disconnect();
   681 	test.End();
   682 	}
   683 
   684 class TFailExecuteSQL : public TFail
   685 	{
   686 	void RunL()
   687 		{User::LeaveIfError(TheDatabase.Execute(*TheSql));}
   688 	void End()
   689 		{}
   690 	};
   691 
   692 class TFailPrepareView : public TFail
   693 	{
   694 	void RunL()
   695 		{User::LeaveIfError(TheView.Prepare(TheDatabase,*TheSql,Access));}
   696 	void End()
   697 		{TheView.Close();}
   698 	};
   699 
   700 /**
   701 @SYMTestCaseID          SYSLIB-DBMS-CT-0615
   702 @SYMTestCaseDesc        Tests for allocation failure on prepare
   703 @SYMTestPriority        Medium
   704 @SYMTestActions         Tests for error on updating a row set data
   705 @SYMTestExpectedResults Test must not fail
   706 @SYMREQ                 REQ0000
   707 */
   708 LOCAL_C void TestView(const THeapFail& aHeap,const TContext& aContext)
   709 	{
   710 	test.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0615 Allocation failure on Prepare "));
   711 	TFailPrepareView fpv;
   712 	TheSql=&SimpleSql;
   713 	Access=RDbRowSet::EUpdatable;
   714 	fpv.Test(aHeap,aContext);
   715 	Access=RDbRowSet::EReadOnly;
   716 	fpv.Test(aHeap,aContext);
   717 	Access=RDbRowSet::EInsertOnly;
   718 	fpv.Test(aHeap,aContext);
   719 //
   720 	test.Next(_L("Allocation failure on Prepare (complex SQL)"));
   721 	for (TUint ii=0;ii<sizeof(ComplexSql)/sizeof(ComplexSql[0]);++ii)
   722 		{
   723 		TheSql=&ComplexSql[ii];
   724 		Access=RDbRowSet::EUpdatable;
   725 		fpv.Test(aHeap,aContext);
   726 		}
   727 	test.End();
   728 	}
   729 
   730 /**
   731 @SYMTestCaseID          SYSLIB-DBMS-CT-0616
   732 @SYMTestCaseDesc        Bad SQL query test
   733 @SYMTestPriority        Medium
   734 @SYMTestActions         Test for bad query
   735 @SYMTestExpectedResults Test must not fail
   736 @SYMREQ                 REQ0000
   737 */
   738 LOCAL_C void TestSQLL(const TContext& aContext)
   739 	{
   740 	test.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0616 Bad SQL "));
   741 	aContext.OpenDbL();
   742 	for (TUint ii=0;ii<sizeof(BadSql)/sizeof(BadSql[0]);++ii)
   743 		test(TheView.Prepare(TheDatabase,TPtrC(BadSql[ii].iSql))==BadSql[ii].iError);
   744 	TheDatabase.Close();
   745 	test.End();
   746 	}
   747 
   748 /**
   749 @SYMTestCaseID          SYSLIB-DBMS-CT-0617
   750 @SYMTestCaseDesc        Tests for updation of an SQL statement
   751 @SYMTestPriority        Medium
   752 @SYMTestActions         Tests for update SQL statement
   753 @SYMTestExpectedResults Test must not fail
   754 @SYMREQ                 REQ0000
   755 */
   756 LOCAL_C void TestUpdateSQL(const THeapFail& aHeap,const TContext& aContext)
   757 	{
   758 	test.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0617 Test for UPDATE SQL statement "));
   759 	TFailExecuteSQL fsql;
   760 	TheSql=&UpdateSql;
   761 	fsql.Test(aHeap,aContext);
   762 	test.End();
   763 	}
   764 
   765 LOCAL_C void ViewL()
   766 	{
   767 	test.Start(_L("Client side"));
   768 	TestView(KClientHeap,KClient);
   769 	TestSQLL(KClient);
   770 	test.Next(_L("Client-Server"));
   771 	Connect();
   772 	TestView(KClientHeap,KServer);
   773 	TestView(KServerHeap,KServer);
   774 	TestSQLL(KServer);
   775 	TestUpdateSQL(KClientHeap,KClient);
   776 	TestUpdateSQL(KServerHeap,KClient);
   777 	Disconnect();
   778 	test.End();
   779 	}
   780 
   781 //TFailIncrementalUpdate implements the base class' virtual methods - RunL() and End().
   782 //TFailIncrementalUpdate::RunL() is called by the base class' Test() method, which simulates
   783 //OOM failures and checks the behaviour of the "incremental update" statement used by RunL().
   784 class TFailIncrementalUpdate : public TFail
   785 	{
   786 	virtual void RunL()
   787 		{
   788 		RDbUpdate dbUpdate;
   789 		CleanupClosePushL(dbUpdate);
   790 		User::LeaveIfError(dbUpdate.Execute(TheDatabase, _L("UPDATE A SET Name = 'ModifiedNameString' WHERE Id2 > 10")));
   791 		TInt step = 0;
   792 		for(TInt err=1;err>0;++step)
   793 			{
   794 			err = dbUpdate.Next();
   795 			User::LeaveIfError(err);
   796 			}
   797 		test(step > 1);//just to be sure that the test executes dbUpdate.Next() more than once
   798 		CleanupStack::PopAndDestroy(&dbUpdate);
   799 		}
   800 	virtual void End()
   801 		{
   802 		TheDatabase.Close();
   803 		}
   804 	};
   805 
   806 /**
   807 @SYMTestCaseID          SYSLIB-DBMS-UT-3414
   808 @SYMTestCaseDesc        "Incremental update" operations - OOM test.
   809 @SYMTestPriority        High
   810 @SYMTestActions         Create a test database with one table and insert some records there (> 100).
   811 						Run an "incremental update" operation in OOM loop.
   812 @SYMTestExpectedResults The test should not fail or panic.
   813 @SYMDEF INC101720
   814 */
   815 LOCAL_C void IncrementalUpdateTest(const THeapFail& aHeap)
   816 	{
   817 	//Create a test shared database with a table
   818 	TheDatabase.Close();
   819 	TheDbs.Close();
   820 	TInt err = TheDbs.Connect();
   821 	test(err == KErrNone);
   822 	err = TheDatabase.Replace(TheFs, KTestDatabase);
   823 	test(err == KErrNone);
   824 	TheDatabase.Close();
   825 	err = TheDatabase.Open(TheDbs, KTestDatabase);
   826 	test(err == KErrNone);
   827 	//Create a test table and fill the table with enough test records (> 100)
   828 	err = TheDatabase.Execute(_L("CREATE TABLE A(Id COUNTER, Id2 INTEGER, Name LONG VARCHAR)"));
   829 	test(err == KErrNone);
   830 	const TInt KTestRecCount = 110;
   831 	err = TheDatabase.Begin();
   832 	test(err == KErrNone);
   833 	for(TInt i=0;i<KTestRecCount;++i)
   834 		{
   835 		_LIT(KSqlFmtStr, "INSERT INTO A(Id2, Name) VALUES(%d, 'TestNameString')");
   836 		TBuf<100> sql;
   837 		TUint32 id = Math::Random() % KTestRecCount;
   838 		sql.Format(KSqlFmtStr, id + 1);
   839 		err = TheDatabase.Execute(sql);
   840 		test(err == 1);
   841 		}
   842 	err = TheDatabase.Commit();
   843 	test(err == KErrNone);
   844 	//The OOM test
   845 	TFailIncrementalUpdate testObj;
   846 	testObj.Test(aHeap);
   847 	//Cleanup
   848 	TheDatabase.Close();
   849 	TheDbs.Close();
   850 	}
   851 
   852 //
   853 // Testing the DBMS for failure modes
   854 //
   855 LOCAL_C void doMainL()
   856 	{
   857 	test.Start(_L("Class RDbNamedDatabase"));
   858 	__UHEAP_MARK;
   859 	OriginsL();
   860 	__UHEAP_CHECK(0);
   861 	__UHEAP_MARK;
   862 	Origins2();
   863 	__UHEAP_CHECK(0);
   864 	test.Next(_L("Class RDbDatabase"));
   865 	DatabaseL();
   866 	__UHEAP_CHECK(0);
   867 	test.Next(_L("Class RDbTable"));
   868 	TableL();
   869 	__UHEAP_CHECK(0);
   870 	test.Next(_L("Class RDbView"));
   871 	ViewL();
   872 	__UHEAP_MARKEND;
   873 	test.End();
   874 	}
   875 
   876 //
   877 // Prepare the test directory.
   878 //
   879 LOCAL_C void setupTestDirectory()
   880     {
   881 	TInt r=TheFs.Connect();
   882 	test(r==KErrNone);
   883 //
   884 	r=TheFs.MkDir(KTestDatabase);
   885 	test(r==KErrNone || r==KErrAlreadyExists);
   886 	}
   887 
   888 //
   889 // Initialise the cleanup stack.
   890 //
   891 LOCAL_C void setupCleanup()
   892     {
   893 	TheTrapCleanup=CTrapCleanup::New();
   894 	test(TheTrapCleanup!=NULL);
   895 	TRAPD(r,\
   896 		{\
   897 		for (TInt i=KTestCleanupStack;i>0;i--)\
   898 			CleanupStack::PushL((TAny*)0);\
   899 		CleanupStack::Pop(KTestCleanupStack);\
   900 		});
   901 	test(r==KErrNone);
   902 	}
   903 
   904 LOCAL_C void DeleteDataFile(const TDesC& aFullName)
   905 	{
   906 	RFs fsSession;
   907 	TInt err = fsSession.Connect();
   908 	if(err == KErrNone)
   909 		{
   910 		TEntry entry;
   911 		if(fsSession.Entry(aFullName, entry) == KErrNone)
   912 			{
   913 			RDebug::Print(_L("Deleting \"%S\" file.\n"), &aFullName);
   914 			err = fsSession.SetAtt(aFullName, 0, KEntryAttReadOnly);
   915 			if(err != KErrNone)
   916 				{
   917 				RDebug::Print(_L("Error %d changing \"%S\" file attributes.\n"), err, &aFullName);
   918 				}
   919 			err = fsSession.Delete(aFullName);
   920 			if(err != KErrNone)
   921 				{
   922 				RDebug::Print(_L("Error %d deleting \"%S\" file.\n"), err, &aFullName);
   923 				}
   924 			}
   925 		fsSession.Close();
   926 		}
   927 	else
   928 		{
   929 		RDebug::Print(_L("Error %d connecting file session. File: %S.\n"), err, &aFullName);
   930 		}
   931 	}
   932 
   933 GLDEF_C TInt E32Main()
   934 	{
   935 	test.Title();
   936 	setupTestDirectory();
   937 	setupCleanup();
   938 	__UHEAP_MARK;
   939 //
   940 	test.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0612 Locating a database "));
   941 	TRAPD(r,TestOpenL());
   942 	test(r==KErrNone);
   943 	PrepareDbFmtString();
   944 	TRAP(r,TestOpen2());
   945 	test(r==KErrNone);
   946 	test.Next(_L("Standard database"));
   947 	TRAP(r,doMainL());
   948 	test(r==KErrNone);
   949 	test.Next(_L("Secure database"));
   950 	TRAP(r,doMainL());
   951 	test(r==KErrNone);
   952 	test.Next(_L("ISAM database"));
   953 	TheFormat=_S("epoc[12345678]");
   954 	TRAP(r,OriginsL());
   955 	test(r==KErrNone);
   956 	TRAP(r,Origins2());
   957 	test(r==KErrNone);
   958 	test.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-UT-3414 \"Incremental update\" - client test "));
   959 	IncrementalUpdateTest(KClientHeap);
   960 	test.End();
   961 	test.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-UT-3414 \"Incremental update\" - client-server test "));
   962 	IncrementalUpdateTest(KServerHeap);
   963 	test.End();
   964 
   965 	::DeleteDataFile(KTestDatabase);		// clean up data file used by this test - must be done before call to End() - DEF047652
   966 	test.End();
   967 //
   968 	__UHEAP_MARKEND;
   969 
   970 	delete TheTrapCleanup;
   971 	TheFs.Close();
   972 	test.Close();
   973 	return 0;
   974 	}