os/persistentdata/persistentstorage/sql/TEST/t_sqlbadclient.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 // Copyright (c) 2006-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 <e32math.h>
    19 #include <sqldb.h>
    20 #include "SqlUtil.h"
    21 #include "SqlSrvStartup.h" //KSqlMajorVer, KSqlMinorVer, KSqlBuildVer
    22 
    23 ///////////////////////////////////////////////////////////////////////////////////////
    24 
    25 static RFs TheFs;
    26 RTest TheTest(_L("t_sqlbadclient test"));
    27 _LIT(KTestDir, "c:\\test\\");
    28 _LIT(KTestDbName1, "c:\\test\\t_sqlbadclient.db");
    29 _LIT(KTestDbName2, "c:[1111D1C1]t_sqlbadclient.db");
    30 
    31 #if defined __WINS__ || defined __WINSCW__
    32 const TInt KTestIterCount = 5000;
    33 #else
    34 const TInt KTestIterCount = 4000;
    35 #endif
    36 const TInt KMaxDesArgLen = 1000;
    37 enum TArgType 
    38 	{
    39 	EIntArgType, 
    40 	ETextArgType, 
    41 	EBinArgType, 
    42 	ELastArgType
    43 	};
    44 
    45 //////////////////////////////////////////////////////////////////////////////////////
    46 
    47 //If the SQL server crashes and the test receives KErrServerTerminated error, then the 
    48 //next set will contain the last:
    49 // - iteration number;
    50 // - handle type;
    51 // - function code;
    52 // - handle;
    53 // - IPC arguments values;
    54 struct TThreadData
    55 	{
    56 	TInt				iIteration;
    57 	TSqlSrvHandleType 	iHandleType;
    58 	TInt 				iFunction;
    59 	TInt 				iHandle;
    60 	TArgType 			iArgType[KMaxMessageArguments];
    61 	TInt 				iIntArg[KMaxMessageArguments];
    62 	TBuf<KMaxDesArgLen> iTextArg[KMaxMessageArguments];
    63 	TBuf8<KMaxDesArgLen> iBinArg[KMaxMessageArguments];
    64 	TInt64 				iSeed;
    65 	};
    66 //////////////////////////////////////////////////////////////////////////////////////
    67 
    68 _LIT(KPanicCategory, "SrvTerm");
    69 _LIT(KPanicCategory2, "InvArg");
    70 const TInt KPanicCode = 1111;
    71 const TInt KPanicCode2 = 2222;
    72 
    73 ///////////////////////////////////////////////////////////////////////////////////////
    74 
    75 //Deletes all created test files.
    76 void DeleteTestFiles()
    77 	{
    78 	RSqlDatabase::Delete(KTestDbName1);
    79 	}
    80 
    81 ///////////////////////////////////////////////////////////////////////////////////////
    82 ///////////////////////////////////////////////////////////////////////////////////////
    83 //Test macros and functions
    84 void Check1(TInt aValue, TInt aLine, TBool aPrintThreadName = EFalse)
    85 	{
    86 	if(!aValue)
    87 		{
    88 		DeleteTestFiles();
    89 		if(aPrintThreadName)
    90 			{
    91 			RThread th;
    92 			TName name = th.Name();
    93 			RDebug::Print(_L("*** Thread %S, Line %d\r\n"), &name, aLine);
    94 			}
    95 		else
    96 			{
    97 			RDebug::Print(_L("*** Line %d\r\n"), aLine);
    98 			}
    99 		TheTest(EFalse, aLine);
   100 		}
   101 	}
   102 void Check2(TInt aValue, TInt aExpected, TInt aLine, TBool aPrintThreadName = EFalse)
   103 	{
   104 	if(aValue != aExpected)
   105 		{
   106 		DeleteTestFiles();
   107 		if(aPrintThreadName)
   108 			{
   109 			RThread th;
   110 			TName name = th.Name();
   111 			RDebug::Print(_L("*** Thread %S, Line %d Expected error: %d, got: %d\r\n"), &name, aLine, aExpected, aValue);
   112 			}
   113 		else
   114 			{
   115 			RDebug::Print(_L("*** Line %d, Expected error: %d, got: %d\r\n"), aLine, aExpected, aValue);
   116 			}
   117 		TheTest(EFalse, aLine);
   118 		}
   119 	}
   120 #define TEST(arg) ::Check1((arg), __LINE__)
   121 #define TEST2(aValue, aExpected) ::Check2(aValue, aExpected, __LINE__)
   122 #define TTEST(arg) ::Check1((arg), __LINE__, ETrue)
   123 #define TTEST2(aValue, aExpected) ::Check2(aValue, aExpected, __LINE__, ETrue)
   124 
   125 ///////////////////////////////////////////////////////////////////////////////////////
   126 
   127 //Creates file session instance and the test directory
   128 void CreateTestEnv()
   129     {
   130 	TInt err = TheFs.Connect();
   131 	TEST2(err, KErrNone);
   132 
   133 	err = TheFs.MkDir(KTestDir);
   134 	TEST(err == KErrNone || err == KErrAlreadyExists);
   135 	}
   136 
   137 //Starts the SQL server process.
   138 TInt StartSqlServer()
   139 	{
   140 	const TUid KSqlSrvUid3 = {0x10281E17};//The same UID is in SqlSrv.mmp file
   141 	const TUidType serverUid(KNullUid, KNullUid, KSqlSrvUid3);
   142 	_LIT(KSqlSrvImg, "SqlSrv");//SQL server image name
   143 	RProcess server;
   144 	TInt err = server.Create(KSqlSrvImg, KNullDesC, serverUid);
   145 	if(err != KErrNone)
   146 		{
   147 		return err;
   148 		}
   149 	TRequestStatus stat;
   150 	server.Rendezvous(stat);
   151 	if(stat != KRequestPending)
   152 		{
   153 		server.Kill(0);		// abort startup
   154 		}
   155 	else
   156 		{
   157 		server.Resume();	// logon OK - start the server
   158 		}
   159 	User::WaitForRequest(stat);		// wait for start or death
   160 	// we can't use the 'exit reason' if the server panicked as this
   161 	// is the panic 'reason' and may be '0' which cannot be distinguished
   162 	// from KErrNone
   163 	err = (server.ExitType() == EExitPanic) ? KErrGeneral : stat.Int();
   164 	server.Close();
   165 	return err;
   166 	}
   167 
   168 //////////////////////////////////////////////////////////////////////////////////////////////////
   169 /////////////////////////////        RTestSqlDbSession          //////////////////////////////////
   170 //////////////////////////////////////////////////////////////////////////////////////////////////
   171 
   172 //Test client SQL session class
   173 class RTestSqlDbSession : public RSessionBase
   174 	{
   175 public:	
   176 	TInt Connect(const TVersion& aVersion);
   177 	void Close();
   178 	TInt SendReceive(TInt aFunction);
   179 	TInt SendReceive(TInt aFunction, const TIpcArgs& aArgs);
   180 
   181 private:
   182 	TInt DoCreateSession(const TVersion& aVersion);
   183 	};
   184 	
   185 TInt RTestSqlDbSession::Connect(const TVersion& aVersion)
   186 	{
   187 	TInt err = DoCreateSession(aVersion);
   188 	if(err == KErrNone)
   189 		{
   190 		TIpcArgs ipcArgs(KTestDbName1().Length(), &KTestDbName1(), 0, 0);
   191 		err = SendReceive(ESqlSrvDbOpen, ipcArgs);
   192 		}
   193 	if(err != KErrNone && err != KErrAlreadyExists)
   194 		{
   195 		Close();	
   196 		}
   197 	return err;
   198 	}
   199 
   200 void RTestSqlDbSession::Close()
   201 	{
   202 	if(Handle())
   203 		{
   204 		(void)SendReceive(ESqlSrvDbClose);
   205 		}
   206 	RSessionBase::Close();
   207 	}
   208 
   209 TInt RTestSqlDbSession::SendReceive(TInt aFunction)
   210 	{
   211 	return RSessionBase::SendReceive(aFunction);	
   212 	}
   213 	
   214 TInt RTestSqlDbSession::SendReceive(TInt aFunction, const TIpcArgs& aArgs)
   215 	{
   216 	return RSessionBase::SendReceive(aFunction, aArgs);	
   217 	}
   218 
   219 TInt RTestSqlDbSession::DoCreateSession(const TVersion& aVersion)
   220 	{
   221 	const TInt KTimesToRetryConnection = 2;
   222 	TInt retry = KTimesToRetryConnection;
   223 	_LIT(KSqlSrvName, "!SQL Server");//SqlDb server name
   224 	for(;;)
   225 		{
   226 		TInt err = CreateSession(KSqlSrvName, aVersion);
   227 		if(err != KErrNotFound && err != KErrServerTerminated)
   228 			{
   229 			return err;
   230 			}
   231 		if(--retry == 0)
   232 			{
   233 			return err;
   234 			}
   235 		err = ::StartSqlServer();
   236 		if(err != KErrNone && err != KErrAlreadyExists)
   237 			{
   238 			return err;
   239 			}
   240 		}
   241 	}
   242 	
   243 //////////////////////////////////////////////////////////////////////////////////////////////////
   244 //////////////////////////////////////////////////////////////////////////////////////////////////
   245 
   246 TInt SendReceive(RTestSqlDbSession aSession, TSqlSrvHandleType aHandleType, TInt aFunction, TInt aHandle, TIpcArgs& aArgs)
   247 	{
   248 	return aSession.SendReceive(::MakeMsgCode(static_cast <TSqlSrvFunction> (aFunction), aHandleType, aHandle), aArgs);
   249 	}
   250 
   251 TInt SendReceive(RTestSqlDbSession aSession, TInt aFunction, TIpcArgs& aArgs)
   252 	{
   253 	return aSession.SendReceive(aFunction, aArgs);
   254 	}
   255 
   256 void PrintIterationCount(TInt aIteration)
   257 	{
   258 	if((aIteration % 100) == 0)
   259 		{
   260 		TTime time;
   261 		time.HomeTime();
   262 		TDateTime dt = time.DateTime();
   263 		TBuf<16> tbuf;
   264 		tbuf.Format(_L("%02d:%02d:%02d.%06d"), dt.Hour(), dt.Minute(), dt.Second(), dt.MicroSecond());
   265 		RDebug::Print(_L("-----[%S] Test iterations: %d\r\n"), &tbuf, aIteration);
   266 		}
   267 	}
   268 
   269 //Worker thread function.
   270 //It behaves as a malicious client. Connects to the SQL server. In each test iteration generates some random values
   271 //for the function number, handle, handle type, IPC arguments. Then sends a command to the server using these
   272 //randomly generated values. If the server crashes and the thread function receives KErrServerTerminated error,
   273 //then the thread kills itself and the main thread will get KPanicCategory and KPanicCode as a reason for the
   274 //worker thread's death. The last set of randomly generated values will be stored in the memory, pointed by aData argument.
   275 TInt ThreadFunc1(void* aData)
   276 	{
   277 	__UHEAP_MARK;
   278 	
   279 	CTrapCleanup* tc = CTrapCleanup::New();
   280 	TTEST(tc != NULL);
   281 
   282 	TThreadData* p = static_cast <TThreadData*> (aData);
   283 	TTEST(p != NULL);
   284 	TThreadData& data = *p;
   285 
   286 	TVersion sqlSoftwareVersion(KSqlMajorVer, KSqlMinorVer, KSqlBuildVer);
   287 	RTestSqlDbSession sess;
   288 	TInt err = sess.Connect(sqlSoftwareVersion);
   289 	TTEST2(err, KErrNone);
   290 
   291 	while(++data.iIteration <= KTestIterCount)
   292 		{
   293 		PrintIterationCount(data.iIteration);
   294 		TIpcArgs args;
   295 		do
   296 			{
   297 			data.iFunction = Math::Rand(data.iSeed) % (ESqlSrvStreamClose + 1);//ESqlSrvStreamClose - the last server message number)
   298 			}
   299 		while(data.iFunction >= ESqlSrvResourceMark && data.iFunction <= ESqlSrvSetHeapFailure);
   300 		for(TInt i=0;i<KMaxMessageArguments;++i)
   301 			{
   302 			//Initialize arguments
   303 			data.iArgType[i] = static_cast <TArgType> (Math::Rand(data.iSeed) % ELastArgType);
   304 			switch(data.iArgType[i])
   305 				{
   306 				case EIntArgType:
   307 					data.iIntArg[i] = Math::Rand(data.iSeed) % 9711;
   308 					args.Set(i, data.iIntArg[i]);
   309 					break;
   310 				case ETextArgType:
   311 					{
   312 					TInt len = Math::Rand(data.iSeed) % KMaxDesArgLen;	
   313 					data.iTextArg[i].SetLength(len);
   314 					args.Set(i, &data.iTextArg[i]);
   315 					}
   316 					break;
   317 				case EBinArgType:
   318 					{
   319 					TInt len = Math::Rand(data.iSeed) % KMaxDesArgLen;	
   320 					data.iBinArg[i].SetLength(len);
   321 					args.Set(i, &data.iBinArg[i]);
   322 					}
   323 					break;
   324 				default:
   325 					User::Panic(KPanicCategory2, KPanicCode2);
   326 					break;
   327 				}
   328 			}
   329 		//Send arguments
   330 		User::SetJustInTime(EFalse);
   331 		TInt err = KErrNone;
   332 		if((Math::Rand(data.iSeed) % 5) == 0)	//Pass a handle (statement or stream)
   333 			{
   334 			data.iHandleType = (Math::Rand(data.iSeed) % 2) ? ESqlSrvStatementHandle : ESqlSrvStreamHandle;
   335 			data.iHandle = Math::Rand(data.iSeed) % 64;
   336 			err = ::SendReceive(sess, data.iHandleType, data.iFunction, data.iHandle, args);
   337 			}
   338 		else
   339 			{
   340 			err = ::SendReceive(sess, data.iFunction, args);
   341 			}
   342 		if(err == KErrServerTerminated)
   343 			{
   344 			User::Panic(KPanicCategory, KPanicCode);
   345 			}
   346 		User::SetJustInTime(ETrue);
   347 		}
   348 
   349 	sess.Close();
   350 
   351 	delete tc;	
   352 	
   353 	__UHEAP_MARKEND;
   354 	
   355 	return KErrNone;		
   356 	}
   357 
   358 /**
   359 @SYMTestCaseID			SYSLIB-SQL-CT-1769
   360 @SYMTestCaseDesc		In a loop, where the loop iterations are less than KTestIterCount (5000 at the moment), 
   361 						the test creates a public shared database and a worker thread, which will behave as 
   362 						malicious client. If the worker thread crashes the SQL server, then the worker thread
   363 						dies notifying the main thread about the SQL server crash. The main thread prints the 
   364 						values used in the last IPC call and crashes the test.
   365 @SYMTestPriority		High
   366 @SYMTestActions			SQL, Malicious client simulation test.
   367 @SYMTestExpectedResults Test must not fail
   368 @SYMREQ					REQ5792
   369                         REQ5793
   370 						REQ10405
   371 						REQ10407
   372 */	
   373 void BadClientTest()
   374 	{
   375 	TThreadData* p = new TThreadData;
   376 	TEST(p != NULL);
   377 	TThreadData& data = *p;
   378 	data.iFunction = 0;
   379 	TTime now;
   380 	now.UniversalTime();
   381 	data.iSeed = now.Int64();
   382 	
   383 	_LIT(KThreadName, "WorkThrd");
   384 	
   385 	for(data.iIteration=0;data.iIteration<KTestIterCount;++data.iIteration)
   386 		{
   387 		PrintIterationCount(data.iIteration);
   388 		//Create/open the test database
   389 		RSqlDatabase db;
   390 		TInt err = db.Create(KTestDbName1);
   391 		TEST(err == KErrNone || err == KErrAlreadyExists);
   392 		if(err == KErrNone)
   393 			{
   394 			err = db.Exec(_L8("CREATE TABLE A(Id INTEGER); INSERT INTO A(Id) VALUES(1); INSERT INTO A(Id) VALUES(2);"));	
   395 			TEST(err >= 0);
   396 			}
   397 		db.Close();
   398 		//Run the malicious client (one worker theread which will try to crash the SQL server)
   399 		RThread thread;
   400 		TEST2(thread.Create(KThreadName, &ThreadFunc1, 0x2000, 0x1000, 0x10000, &data, EOwnerProcess), KErrNone);
   401 		TRequestStatus status;
   402 		thread.Logon(status);
   403 		TEST2(status.Int(), KRequestPending);
   404 		thread.Resume();
   405 		User::WaitForRequest(status);
   406 		User::SetJustInTime(ETrue);	// enable debugger panic handling
   407 		if(thread.ExitType() == EExitPanic)
   408 			{
   409 			if(thread.ExitReason() == KPanicCode)
   410 				{
   411 				TheTest.Printf(_L("##Server terminated!\r\n"));
   412 				TheTest.Printf(_L("##Iteration=%d, Handle type(hex)=%X, Function(hex)=%X, Handle=%d\r\n"), data.iIteration, data.iHandleType, data.iFunction, data.iHandle);
   413 				for(TInt i=0;i<KMaxMessageArguments;++i)
   414 					{
   415 					switch(data.iArgType[i])
   416 						{
   417 						case EIntArgType:
   418 							TheTest.Printf(_L("##Arg %d, Integer, value=%d\r\n"), i, data.iIntArg[i]);
   419 							break;
   420 						case ETextArgType:
   421 							TheTest.Printf(_L("##Arg %d, Text,    length=%d\r\n"), i, data.iTextArg[i].Length());
   422 							break;
   423 						case EBinArgType:
   424 							TheTest.Printf(_L("##Arg %d, Binary,  length=%d\r\n"), i, data.iBinArg[i].Length());
   425 							break;
   426 						default:
   427 							TheTest.Printf(_L("##Arg %d, Invalid argument type: %d\r\n"), i, data.iArgType[i]);
   428 							break;
   429 						}
   430 					}
   431 				TEST(0);
   432 				}
   433 			}
   434 		thread.Close();
   435 		}
   436 	User::SetJustInTime(ETrue);	// enable debugger panic handling
   437 	delete p;
   438 	}
   439 
   440 RSqlSecurityPolicy CreateSecurityPolicy()
   441 	{
   442 	RSqlSecurityPolicy securityPolicy;
   443 	const TSecurityPolicy KDefaultPolicy(TSecurityPolicy::EAlwaysPass);
   444 	TInt err = securityPolicy.Create(KDefaultPolicy);
   445 	TEST2(err, KErrNone);
   446 	return securityPolicy;
   447 	}
   448 
   449 void DoBadNameTest(RSqlDatabase& aDb)
   450 	{
   451 	TFileName invalidFileName1;
   452 	invalidFileName1.SetLength(KMaxFileName);
   453 	//Initialise the first character so that it is not treated as an empty descriptor by the sever
   454 	invalidFileName1[0] = TChar(204); //0xCC
   455 	TFileName invalidFileName2;
   456 	invalidFileName2.SetLength(KMaxFileName);
   457 	invalidFileName2.Fill(TChar(204));//0xCC
   458 	
   459 	TInt err = aDb.Attach(invalidFileName1, invalidFileName2);
   460 	TheTest.Printf(_L("Attach, invalid database name-1, err=%d\r\n"), err);
   461 	TEST(err != KErrNone);
   462 	err = aDb.Attach(invalidFileName2, invalidFileName1);
   463 	TheTest.Printf(_L("Attach, invalid database name-2, err=%d\r\n"), err);
   464 	TEST(err != KErrNone);
   465 	err = aDb.Attach(KTestDbName2, invalidFileName2);
   466 	TheTest.Printf(_L("Attach, invalid database name-3, err=%d\r\n"), err);
   467 	TEST(err != KErrDied);
   468 	if(err == KErrNone)
   469 		{
   470 		err = aDb.Detach(invalidFileName2);
   471 		TheTest.Printf(_L("Detach, invalid database name-3, err=%d\r\n"), err);
   472 		TEST2(err, KErrNone);
   473 		}
   474 	err = aDb.Attach(KTestDbName2, invalidFileName1);
   475 	TheTest.Printf(_L("Attach, invalid database name-4, err=%d\r\n"), err);
   476 	TEST(err != KErrDied);
   477 	if(err == KErrNone)
   478 		{
   479 		err = aDb.Detach(invalidFileName1);
   480 		TheTest.Printf(_L("Detach, invalid database name-4, err=%d\r\n"), err);
   481 		TEST2(err, KErrNone);
   482 		}
   483 	
   484 	RSqlDatabase::TSize size;
   485 
   486 	err = aDb.Size(size, invalidFileName1);
   487 	TheTest.Printf(_L("Size, invalid database name-1, err=%d\r\n"), err);
   488 	TEST(err != KErrNone);
   489 	err = aDb.Size(size, invalidFileName2);
   490 	TheTest.Printf(_L("Size, invalid database name-2, err=%d\r\n"), err);
   491 	TEST(err != KErrNone);
   492 
   493 	err = aDb.Compact(RSqlDatabase::EMaxCompaction, invalidFileName1);
   494 	TheTest.Printf(_L("Compact, invalid database name-1, err=%d\r\n"), err);
   495 	TEST(err != KErrNone);
   496 	err = aDb.Compact(RSqlDatabase::EMaxCompaction, invalidFileName2);
   497 	TheTest.Printf(_L("Compact, invalid database name-2, err=%d\r\n"), err);
   498 	TEST(err != KErrNone);
   499 
   500 	RSqlStatement stmt;
   501 	err = stmt.Prepare(aDb, _L("SELECT * FROM A"));
   502 	TEST2(err, KErrNone);
   503 	
   504 	err = stmt.ColumnIndex(invalidFileName1);
   505 	TheTest.Printf(_L("ColumnIndex, invalid column name-1, err=%d\r\n"), err);
   506 	TEST(err != KErrNone);
   507 	err = stmt.ColumnIndex(invalidFileName2);
   508 	TheTest.Printf(_L("ColumnIndex, invalid column name-2, err=%d\r\n"), err);
   509 	TEST(err != KErrNone);
   510 	err = stmt.ParameterIndex(invalidFileName1);
   511 	TheTest.Printf(_L("ParameterIndex, invalid parameter name-1, err=%d\r\n"), err);
   512 	TEST(err != KErrNone);
   513 	err = stmt.ParameterIndex(invalidFileName2);
   514 	TheTest.Printf(_L("ParameterIndex, invalid parameter name-2, err=%d\r\n"), err);
   515 	TEST(err != KErrNone);
   516 
   517 	stmt.Close();
   518 	}
   519 
   520 /**
   521 @SYMTestCaseID			SYSLIB-SQL-UT-4048
   522 @SYMTestCaseDesc		Bad database names - robustness test.
   523 						The test defines file names with invalid content and attempts to use 
   524 						these file names with the SQL API. The test should not cause crashes 
   525 						in the SQL server.
   526 @SYMTestPriority		High
   527 @SYMTestActions			Bad database names - robustness test.
   528 @SYMTestExpectedResults Test must not fail
   529 @SYMREQ					REQ10405
   530                         REQ10407
   531 */
   532 void BadNameTest()
   533 	{
   534 	TFileName invalidFileName1;
   535 	invalidFileName1.SetLength(KMaxFileName);
   536 	TFileName invalidFileName2;
   537 	invalidFileName2.SetLength(KMaxFileName / 2);
   538 	
   539 	//Initialise the first character so that it is not treated as an empty descriptor by the sever
   540 	invalidFileName1[0] = TChar(204); //0xCC
   541 	invalidFileName2[0] = TChar(204); //0xCC
   542 	
   543 	(void)RSqlDatabase::Delete(KTestDbName1);
   544 	(void)RSqlDatabase::Delete(KTestDbName2);
   545 		
   546 	RSqlDatabase db;
   547 	TInt err = db.Create(invalidFileName1);
   548 	TEST(err != KErrNone);
   549 	err = db.Create(invalidFileName2);
   550 	TEST(err != KErrNone);
   551 
   552 	err = db.Open(invalidFileName1);
   553 	TEST(err != KErrNone);
   554 	err = db.Open(invalidFileName2);
   555 	TEST(err != KErrNone);
   556 
   557 	err = db.Create(KTestDbName1);
   558 	TEST2(err, KErrNone);
   559 	err = db.Exec(_L("CREATE TABLE A(I INTEGER)"));
   560 	TEST(err >= 0);
   561 	db.Close();
   562 	
   563 	RSqlSecurityPolicy sp = CreateSecurityPolicy();
   564 	err = db.Create(KTestDbName2, sp);
   565 	TEST2(err, KErrNone);
   566 	sp.Close();
   567 	err = db.Exec(_L("CREATE TABLE A(I INTEGER)"));
   568 	TEST(err >= 0);
   569 	db.Close();
   570 
   571 	TheTest.Printf(_L("Bad names test - public shared database\r\n"));
   572 	err = db.Open(KTestDbName1);	
   573 	TEST2(err, KErrNone);
   574 	DoBadNameTest(db);
   575 	db.Close();
   576 
   577 	TheTest.Printf(_L("Bad names test - public secure shared database\r\n"));
   578 	err = db.Open(KTestDbName2);	
   579 	TEST2(err, KErrNone);
   580 	DoBadNameTest(db);
   581 	db.Close();
   582 	
   583 	(void)RSqlDatabase::Delete(KTestDbName2);
   584 	(void)RSqlDatabase::Delete(KTestDbName1);
   585 	
   586 	err = RSqlDatabase::Copy(invalidFileName1, invalidFileName2);
   587 	TheTest.Printf(_L("Copy database, err=%d\r\n"), err);
   588 	TEST(err != KErrNone);
   589 	err = RSqlDatabase::Delete(invalidFileName1);
   590 	TheTest.Printf(_L("Delete database-1, err=%d\r\n"), err);
   591 	TEST(err != KErrNone);
   592 	err = RSqlDatabase::Delete(invalidFileName2);
   593 	TheTest.Printf(_L("Delete database-2, err=%d\r\n"), err);
   594 	TEST(err != KErrNone);
   595 	}
   596 
   597 /**
   598 @SYMTestCaseID          PDS-SQL-CT-4200
   599 @SYMTestCaseDesc        Invalid sql software version test.
   600 @SYMTestPriority        High
   601 @SYMTestActions			The test verifies that the SQL server checks that the software version of SQL sessions
   602 						to be created is less or equal to the current version of the server software. 
   603 						If that is not true then the SQL server does not create the session annd returns KErrNotSupported.
   604 @SYMTestExpectedResults Test must not fail
   605 @SYMDEF                 DEF145236
   606 */  
   607 void InvalidSoftwareVersionTest()
   608 	{
   609 	(void)RSqlDatabase::Delete(KTestDbName1);
   610 	RSqlDatabase db;
   611 	TInt err = db.Create(KTestDbName1);
   612 	TEST2(err, KErrNone);
   613 	db.Close();
   614 	
   615 	//Smaller version number
   616 	TVersion sqlSoftwareVersion1(1, 0, 0);
   617 	RTestSqlDbSession sess1;
   618 	err = sess1.Connect(sqlSoftwareVersion1);
   619 	sess1.Close();
   620 	TEST2(err, KErrNone);
   621 	
   622 	//Bigger version number 1
   623 	TVersion sqlSoftwareVersion2(1, 97, 3);
   624 	RTestSqlDbSession sess2;
   625 	err = sess2.Connect(sqlSoftwareVersion2);
   626 	TEST2(err, KErrNotSupported);
   627 	
   628 	//Bigger version number 2
   629 	TVersion sqlSoftwareVersion3(78, 0, 1);
   630 	RTestSqlDbSession sess3;
   631 	err = sess3.Connect(sqlSoftwareVersion3);
   632 	TEST2(err, KErrNotSupported);
   633 
   634 	//The current version number
   635 	TVersion sqlSoftwareVersion4(KSqlMajorVer, KSqlMinorVer, KSqlBuildVer);
   636 	RTestSqlDbSession sess4;
   637 	err = sess4.Connect(sqlSoftwareVersion4);
   638 	sess4.Close();
   639 	TEST2(err, KErrNone);
   640 	
   641 	(void)RSqlDatabase::Delete(KTestDbName1);
   642 	}
   643 
   644 void DoTests()
   645 	{
   646 	TheTest.Start(_L(" @SYMTestCaseID:SYSLIB-SQL-CT-1769 Bad client test "));
   647 	BadClientTest();
   648 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-4048 Bad names test"));
   649 	BadNameTest();
   650 	TheTest.Next(_L(" @SYMTestCaseID:PDS-SQL-CT-4200 Invalid software version test"));
   651 	InvalidSoftwareVersionTest();
   652 	}
   653 
   654 TInt E32Main()
   655 	{
   656 	TheTest.Title();
   657 	
   658 	CTrapCleanup* tc = CTrapCleanup::New();
   659 	
   660 	__UHEAP_MARK;
   661 	
   662 	DeleteTestFiles();
   663 	CreateTestEnv();
   664 	DoTests();
   665 	DeleteTestFiles();
   666 	TheFs.Close();
   667 
   668 	__UHEAP_MARKEND;
   669 	
   670 	TheTest.End();
   671 	TheTest.Close();
   672 	
   673 	delete tc;
   674 
   675 	User::Heap().Check();
   676 	return KErrNone;
   677 	}