os/persistentdata/persistentstorage/sql/TEST/t_sqlbadclient.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/persistentdata/persistentstorage/sql/TEST/t_sqlbadclient.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,677 @@
     1.4 +// Copyright (c) 2006-2010 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +//
    1.18 +
    1.19 +#include <e32test.h>
    1.20 +#include <bautils.h>
    1.21 +#include <e32math.h>
    1.22 +#include <sqldb.h>
    1.23 +#include "SqlUtil.h"
    1.24 +#include "SqlSrvStartup.h" //KSqlMajorVer, KSqlMinorVer, KSqlBuildVer
    1.25 +
    1.26 +///////////////////////////////////////////////////////////////////////////////////////
    1.27 +
    1.28 +static RFs TheFs;
    1.29 +RTest TheTest(_L("t_sqlbadclient test"));
    1.30 +_LIT(KTestDir, "c:\\test\\");
    1.31 +_LIT(KTestDbName1, "c:\\test\\t_sqlbadclient.db");
    1.32 +_LIT(KTestDbName2, "c:[1111D1C1]t_sqlbadclient.db");
    1.33 +
    1.34 +#if defined __WINS__ || defined __WINSCW__
    1.35 +const TInt KTestIterCount = 5000;
    1.36 +#else
    1.37 +const TInt KTestIterCount = 4000;
    1.38 +#endif
    1.39 +const TInt KMaxDesArgLen = 1000;
    1.40 +enum TArgType 
    1.41 +	{
    1.42 +	EIntArgType, 
    1.43 +	ETextArgType, 
    1.44 +	EBinArgType, 
    1.45 +	ELastArgType
    1.46 +	};
    1.47 +
    1.48 +//////////////////////////////////////////////////////////////////////////////////////
    1.49 +
    1.50 +//If the SQL server crashes and the test receives KErrServerTerminated error, then the 
    1.51 +//next set will contain the last:
    1.52 +// - iteration number;
    1.53 +// - handle type;
    1.54 +// - function code;
    1.55 +// - handle;
    1.56 +// - IPC arguments values;
    1.57 +struct TThreadData
    1.58 +	{
    1.59 +	TInt				iIteration;
    1.60 +	TSqlSrvHandleType 	iHandleType;
    1.61 +	TInt 				iFunction;
    1.62 +	TInt 				iHandle;
    1.63 +	TArgType 			iArgType[KMaxMessageArguments];
    1.64 +	TInt 				iIntArg[KMaxMessageArguments];
    1.65 +	TBuf<KMaxDesArgLen> iTextArg[KMaxMessageArguments];
    1.66 +	TBuf8<KMaxDesArgLen> iBinArg[KMaxMessageArguments];
    1.67 +	TInt64 				iSeed;
    1.68 +	};
    1.69 +//////////////////////////////////////////////////////////////////////////////////////
    1.70 +
    1.71 +_LIT(KPanicCategory, "SrvTerm");
    1.72 +_LIT(KPanicCategory2, "InvArg");
    1.73 +const TInt KPanicCode = 1111;
    1.74 +const TInt KPanicCode2 = 2222;
    1.75 +
    1.76 +///////////////////////////////////////////////////////////////////////////////////////
    1.77 +
    1.78 +//Deletes all created test files.
    1.79 +void DeleteTestFiles()
    1.80 +	{
    1.81 +	RSqlDatabase::Delete(KTestDbName1);
    1.82 +	}
    1.83 +
    1.84 +///////////////////////////////////////////////////////////////////////////////////////
    1.85 +///////////////////////////////////////////////////////////////////////////////////////
    1.86 +//Test macros and functions
    1.87 +void Check1(TInt aValue, TInt aLine, TBool aPrintThreadName = EFalse)
    1.88 +	{
    1.89 +	if(!aValue)
    1.90 +		{
    1.91 +		DeleteTestFiles();
    1.92 +		if(aPrintThreadName)
    1.93 +			{
    1.94 +			RThread th;
    1.95 +			TName name = th.Name();
    1.96 +			RDebug::Print(_L("*** Thread %S, Line %d\r\n"), &name, aLine);
    1.97 +			}
    1.98 +		else
    1.99 +			{
   1.100 +			RDebug::Print(_L("*** Line %d\r\n"), aLine);
   1.101 +			}
   1.102 +		TheTest(EFalse, aLine);
   1.103 +		}
   1.104 +	}
   1.105 +void Check2(TInt aValue, TInt aExpected, TInt aLine, TBool aPrintThreadName = EFalse)
   1.106 +	{
   1.107 +	if(aValue != aExpected)
   1.108 +		{
   1.109 +		DeleteTestFiles();
   1.110 +		if(aPrintThreadName)
   1.111 +			{
   1.112 +			RThread th;
   1.113 +			TName name = th.Name();
   1.114 +			RDebug::Print(_L("*** Thread %S, Line %d Expected error: %d, got: %d\r\n"), &name, aLine, aExpected, aValue);
   1.115 +			}
   1.116 +		else
   1.117 +			{
   1.118 +			RDebug::Print(_L("*** Line %d, Expected error: %d, got: %d\r\n"), aLine, aExpected, aValue);
   1.119 +			}
   1.120 +		TheTest(EFalse, aLine);
   1.121 +		}
   1.122 +	}
   1.123 +#define TEST(arg) ::Check1((arg), __LINE__)
   1.124 +#define TEST2(aValue, aExpected) ::Check2(aValue, aExpected, __LINE__)
   1.125 +#define TTEST(arg) ::Check1((arg), __LINE__, ETrue)
   1.126 +#define TTEST2(aValue, aExpected) ::Check2(aValue, aExpected, __LINE__, ETrue)
   1.127 +
   1.128 +///////////////////////////////////////////////////////////////////////////////////////
   1.129 +
   1.130 +//Creates file session instance and the test directory
   1.131 +void CreateTestEnv()
   1.132 +    {
   1.133 +	TInt err = TheFs.Connect();
   1.134 +	TEST2(err, KErrNone);
   1.135 +
   1.136 +	err = TheFs.MkDir(KTestDir);
   1.137 +	TEST(err == KErrNone || err == KErrAlreadyExists);
   1.138 +	}
   1.139 +
   1.140 +//Starts the SQL server process.
   1.141 +TInt StartSqlServer()
   1.142 +	{
   1.143 +	const TUid KSqlSrvUid3 = {0x10281E17};//The same UID is in SqlSrv.mmp file
   1.144 +	const TUidType serverUid(KNullUid, KNullUid, KSqlSrvUid3);
   1.145 +	_LIT(KSqlSrvImg, "SqlSrv");//SQL server image name
   1.146 +	RProcess server;
   1.147 +	TInt err = server.Create(KSqlSrvImg, KNullDesC, serverUid);
   1.148 +	if(err != KErrNone)
   1.149 +		{
   1.150 +		return err;
   1.151 +		}
   1.152 +	TRequestStatus stat;
   1.153 +	server.Rendezvous(stat);
   1.154 +	if(stat != KRequestPending)
   1.155 +		{
   1.156 +		server.Kill(0);		// abort startup
   1.157 +		}
   1.158 +	else
   1.159 +		{
   1.160 +		server.Resume();	// logon OK - start the server
   1.161 +		}
   1.162 +	User::WaitForRequest(stat);		// wait for start or death
   1.163 +	// we can't use the 'exit reason' if the server panicked as this
   1.164 +	// is the panic 'reason' and may be '0' which cannot be distinguished
   1.165 +	// from KErrNone
   1.166 +	err = (server.ExitType() == EExitPanic) ? KErrGeneral : stat.Int();
   1.167 +	server.Close();
   1.168 +	return err;
   1.169 +	}
   1.170 +
   1.171 +//////////////////////////////////////////////////////////////////////////////////////////////////
   1.172 +/////////////////////////////        RTestSqlDbSession          //////////////////////////////////
   1.173 +//////////////////////////////////////////////////////////////////////////////////////////////////
   1.174 +
   1.175 +//Test client SQL session class
   1.176 +class RTestSqlDbSession : public RSessionBase
   1.177 +	{
   1.178 +public:	
   1.179 +	TInt Connect(const TVersion& aVersion);
   1.180 +	void Close();
   1.181 +	TInt SendReceive(TInt aFunction);
   1.182 +	TInt SendReceive(TInt aFunction, const TIpcArgs& aArgs);
   1.183 +
   1.184 +private:
   1.185 +	TInt DoCreateSession(const TVersion& aVersion);
   1.186 +	};
   1.187 +	
   1.188 +TInt RTestSqlDbSession::Connect(const TVersion& aVersion)
   1.189 +	{
   1.190 +	TInt err = DoCreateSession(aVersion);
   1.191 +	if(err == KErrNone)
   1.192 +		{
   1.193 +		TIpcArgs ipcArgs(KTestDbName1().Length(), &KTestDbName1(), 0, 0);
   1.194 +		err = SendReceive(ESqlSrvDbOpen, ipcArgs);
   1.195 +		}
   1.196 +	if(err != KErrNone && err != KErrAlreadyExists)
   1.197 +		{
   1.198 +		Close();	
   1.199 +		}
   1.200 +	return err;
   1.201 +	}
   1.202 +
   1.203 +void RTestSqlDbSession::Close()
   1.204 +	{
   1.205 +	if(Handle())
   1.206 +		{
   1.207 +		(void)SendReceive(ESqlSrvDbClose);
   1.208 +		}
   1.209 +	RSessionBase::Close();
   1.210 +	}
   1.211 +
   1.212 +TInt RTestSqlDbSession::SendReceive(TInt aFunction)
   1.213 +	{
   1.214 +	return RSessionBase::SendReceive(aFunction);	
   1.215 +	}
   1.216 +	
   1.217 +TInt RTestSqlDbSession::SendReceive(TInt aFunction, const TIpcArgs& aArgs)
   1.218 +	{
   1.219 +	return RSessionBase::SendReceive(aFunction, aArgs);	
   1.220 +	}
   1.221 +
   1.222 +TInt RTestSqlDbSession::DoCreateSession(const TVersion& aVersion)
   1.223 +	{
   1.224 +	const TInt KTimesToRetryConnection = 2;
   1.225 +	TInt retry = KTimesToRetryConnection;
   1.226 +	_LIT(KSqlSrvName, "!SQL Server");//SqlDb server name
   1.227 +	for(;;)
   1.228 +		{
   1.229 +		TInt err = CreateSession(KSqlSrvName, aVersion);
   1.230 +		if(err != KErrNotFound && err != KErrServerTerminated)
   1.231 +			{
   1.232 +			return err;
   1.233 +			}
   1.234 +		if(--retry == 0)
   1.235 +			{
   1.236 +			return err;
   1.237 +			}
   1.238 +		err = ::StartSqlServer();
   1.239 +		if(err != KErrNone && err != KErrAlreadyExists)
   1.240 +			{
   1.241 +			return err;
   1.242 +			}
   1.243 +		}
   1.244 +	}
   1.245 +	
   1.246 +//////////////////////////////////////////////////////////////////////////////////////////////////
   1.247 +//////////////////////////////////////////////////////////////////////////////////////////////////
   1.248 +
   1.249 +TInt SendReceive(RTestSqlDbSession aSession, TSqlSrvHandleType aHandleType, TInt aFunction, TInt aHandle, TIpcArgs& aArgs)
   1.250 +	{
   1.251 +	return aSession.SendReceive(::MakeMsgCode(static_cast <TSqlSrvFunction> (aFunction), aHandleType, aHandle), aArgs);
   1.252 +	}
   1.253 +
   1.254 +TInt SendReceive(RTestSqlDbSession aSession, TInt aFunction, TIpcArgs& aArgs)
   1.255 +	{
   1.256 +	return aSession.SendReceive(aFunction, aArgs);
   1.257 +	}
   1.258 +
   1.259 +void PrintIterationCount(TInt aIteration)
   1.260 +	{
   1.261 +	if((aIteration % 100) == 0)
   1.262 +		{
   1.263 +		TTime time;
   1.264 +		time.HomeTime();
   1.265 +		TDateTime dt = time.DateTime();
   1.266 +		TBuf<16> tbuf;
   1.267 +		tbuf.Format(_L("%02d:%02d:%02d.%06d"), dt.Hour(), dt.Minute(), dt.Second(), dt.MicroSecond());
   1.268 +		RDebug::Print(_L("-----[%S] Test iterations: %d\r\n"), &tbuf, aIteration);
   1.269 +		}
   1.270 +	}
   1.271 +
   1.272 +//Worker thread function.
   1.273 +//It behaves as a malicious client. Connects to the SQL server. In each test iteration generates some random values
   1.274 +//for the function number, handle, handle type, IPC arguments. Then sends a command to the server using these
   1.275 +//randomly generated values. If the server crashes and the thread function receives KErrServerTerminated error,
   1.276 +//then the thread kills itself and the main thread will get KPanicCategory and KPanicCode as a reason for the
   1.277 +//worker thread's death. The last set of randomly generated values will be stored in the memory, pointed by aData argument.
   1.278 +TInt ThreadFunc1(void* aData)
   1.279 +	{
   1.280 +	__UHEAP_MARK;
   1.281 +	
   1.282 +	CTrapCleanup* tc = CTrapCleanup::New();
   1.283 +	TTEST(tc != NULL);
   1.284 +
   1.285 +	TThreadData* p = static_cast <TThreadData*> (aData);
   1.286 +	TTEST(p != NULL);
   1.287 +	TThreadData& data = *p;
   1.288 +
   1.289 +	TVersion sqlSoftwareVersion(KSqlMajorVer, KSqlMinorVer, KSqlBuildVer);
   1.290 +	RTestSqlDbSession sess;
   1.291 +	TInt err = sess.Connect(sqlSoftwareVersion);
   1.292 +	TTEST2(err, KErrNone);
   1.293 +
   1.294 +	while(++data.iIteration <= KTestIterCount)
   1.295 +		{
   1.296 +		PrintIterationCount(data.iIteration);
   1.297 +		TIpcArgs args;
   1.298 +		do
   1.299 +			{
   1.300 +			data.iFunction = Math::Rand(data.iSeed) % (ESqlSrvStreamClose + 1);//ESqlSrvStreamClose - the last server message number)
   1.301 +			}
   1.302 +		while(data.iFunction >= ESqlSrvResourceMark && data.iFunction <= ESqlSrvSetHeapFailure);
   1.303 +		for(TInt i=0;i<KMaxMessageArguments;++i)
   1.304 +			{
   1.305 +			//Initialize arguments
   1.306 +			data.iArgType[i] = static_cast <TArgType> (Math::Rand(data.iSeed) % ELastArgType);
   1.307 +			switch(data.iArgType[i])
   1.308 +				{
   1.309 +				case EIntArgType:
   1.310 +					data.iIntArg[i] = Math::Rand(data.iSeed) % 9711;
   1.311 +					args.Set(i, data.iIntArg[i]);
   1.312 +					break;
   1.313 +				case ETextArgType:
   1.314 +					{
   1.315 +					TInt len = Math::Rand(data.iSeed) % KMaxDesArgLen;	
   1.316 +					data.iTextArg[i].SetLength(len);
   1.317 +					args.Set(i, &data.iTextArg[i]);
   1.318 +					}
   1.319 +					break;
   1.320 +				case EBinArgType:
   1.321 +					{
   1.322 +					TInt len = Math::Rand(data.iSeed) % KMaxDesArgLen;	
   1.323 +					data.iBinArg[i].SetLength(len);
   1.324 +					args.Set(i, &data.iBinArg[i]);
   1.325 +					}
   1.326 +					break;
   1.327 +				default:
   1.328 +					User::Panic(KPanicCategory2, KPanicCode2);
   1.329 +					break;
   1.330 +				}
   1.331 +			}
   1.332 +		//Send arguments
   1.333 +		User::SetJustInTime(EFalse);
   1.334 +		TInt err = KErrNone;
   1.335 +		if((Math::Rand(data.iSeed) % 5) == 0)	//Pass a handle (statement or stream)
   1.336 +			{
   1.337 +			data.iHandleType = (Math::Rand(data.iSeed) % 2) ? ESqlSrvStatementHandle : ESqlSrvStreamHandle;
   1.338 +			data.iHandle = Math::Rand(data.iSeed) % 64;
   1.339 +			err = ::SendReceive(sess, data.iHandleType, data.iFunction, data.iHandle, args);
   1.340 +			}
   1.341 +		else
   1.342 +			{
   1.343 +			err = ::SendReceive(sess, data.iFunction, args);
   1.344 +			}
   1.345 +		if(err == KErrServerTerminated)
   1.346 +			{
   1.347 +			User::Panic(KPanicCategory, KPanicCode);
   1.348 +			}
   1.349 +		User::SetJustInTime(ETrue);
   1.350 +		}
   1.351 +
   1.352 +	sess.Close();
   1.353 +
   1.354 +	delete tc;	
   1.355 +	
   1.356 +	__UHEAP_MARKEND;
   1.357 +	
   1.358 +	return KErrNone;		
   1.359 +	}
   1.360 +
   1.361 +/**
   1.362 +@SYMTestCaseID			SYSLIB-SQL-CT-1769
   1.363 +@SYMTestCaseDesc		In a loop, where the loop iterations are less than KTestIterCount (5000 at the moment), 
   1.364 +						the test creates a public shared database and a worker thread, which will behave as 
   1.365 +						malicious client. If the worker thread crashes the SQL server, then the worker thread
   1.366 +						dies notifying the main thread about the SQL server crash. The main thread prints the 
   1.367 +						values used in the last IPC call and crashes the test.
   1.368 +@SYMTestPriority		High
   1.369 +@SYMTestActions			SQL, Malicious client simulation test.
   1.370 +@SYMTestExpectedResults Test must not fail
   1.371 +@SYMREQ					REQ5792
   1.372 +                        REQ5793
   1.373 +						REQ10405
   1.374 +						REQ10407
   1.375 +*/	
   1.376 +void BadClientTest()
   1.377 +	{
   1.378 +	TThreadData* p = new TThreadData;
   1.379 +	TEST(p != NULL);
   1.380 +	TThreadData& data = *p;
   1.381 +	data.iFunction = 0;
   1.382 +	TTime now;
   1.383 +	now.UniversalTime();
   1.384 +	data.iSeed = now.Int64();
   1.385 +	
   1.386 +	_LIT(KThreadName, "WorkThrd");
   1.387 +	
   1.388 +	for(data.iIteration=0;data.iIteration<KTestIterCount;++data.iIteration)
   1.389 +		{
   1.390 +		PrintIterationCount(data.iIteration);
   1.391 +		//Create/open the test database
   1.392 +		RSqlDatabase db;
   1.393 +		TInt err = db.Create(KTestDbName1);
   1.394 +		TEST(err == KErrNone || err == KErrAlreadyExists);
   1.395 +		if(err == KErrNone)
   1.396 +			{
   1.397 +			err = db.Exec(_L8("CREATE TABLE A(Id INTEGER); INSERT INTO A(Id) VALUES(1); INSERT INTO A(Id) VALUES(2);"));	
   1.398 +			TEST(err >= 0);
   1.399 +			}
   1.400 +		db.Close();
   1.401 +		//Run the malicious client (one worker theread which will try to crash the SQL server)
   1.402 +		RThread thread;
   1.403 +		TEST2(thread.Create(KThreadName, &ThreadFunc1, 0x2000, 0x1000, 0x10000, &data, EOwnerProcess), KErrNone);
   1.404 +		TRequestStatus status;
   1.405 +		thread.Logon(status);
   1.406 +		TEST2(status.Int(), KRequestPending);
   1.407 +		thread.Resume();
   1.408 +		User::WaitForRequest(status);
   1.409 +		User::SetJustInTime(ETrue);	// enable debugger panic handling
   1.410 +		if(thread.ExitType() == EExitPanic)
   1.411 +			{
   1.412 +			if(thread.ExitReason() == KPanicCode)
   1.413 +				{
   1.414 +				TheTest.Printf(_L("##Server terminated!\r\n"));
   1.415 +				TheTest.Printf(_L("##Iteration=%d, Handle type(hex)=%X, Function(hex)=%X, Handle=%d\r\n"), data.iIteration, data.iHandleType, data.iFunction, data.iHandle);
   1.416 +				for(TInt i=0;i<KMaxMessageArguments;++i)
   1.417 +					{
   1.418 +					switch(data.iArgType[i])
   1.419 +						{
   1.420 +						case EIntArgType:
   1.421 +							TheTest.Printf(_L("##Arg %d, Integer, value=%d\r\n"), i, data.iIntArg[i]);
   1.422 +							break;
   1.423 +						case ETextArgType:
   1.424 +							TheTest.Printf(_L("##Arg %d, Text,    length=%d\r\n"), i, data.iTextArg[i].Length());
   1.425 +							break;
   1.426 +						case EBinArgType:
   1.427 +							TheTest.Printf(_L("##Arg %d, Binary,  length=%d\r\n"), i, data.iBinArg[i].Length());
   1.428 +							break;
   1.429 +						default:
   1.430 +							TheTest.Printf(_L("##Arg %d, Invalid argument type: %d\r\n"), i, data.iArgType[i]);
   1.431 +							break;
   1.432 +						}
   1.433 +					}
   1.434 +				TEST(0);
   1.435 +				}
   1.436 +			}
   1.437 +		thread.Close();
   1.438 +		}
   1.439 +	User::SetJustInTime(ETrue);	// enable debugger panic handling
   1.440 +	delete p;
   1.441 +	}
   1.442 +
   1.443 +RSqlSecurityPolicy CreateSecurityPolicy()
   1.444 +	{
   1.445 +	RSqlSecurityPolicy securityPolicy;
   1.446 +	const TSecurityPolicy KDefaultPolicy(TSecurityPolicy::EAlwaysPass);
   1.447 +	TInt err = securityPolicy.Create(KDefaultPolicy);
   1.448 +	TEST2(err, KErrNone);
   1.449 +	return securityPolicy;
   1.450 +	}
   1.451 +
   1.452 +void DoBadNameTest(RSqlDatabase& aDb)
   1.453 +	{
   1.454 +	TFileName invalidFileName1;
   1.455 +	invalidFileName1.SetLength(KMaxFileName);
   1.456 +	//Initialise the first character so that it is not treated as an empty descriptor by the sever
   1.457 +	invalidFileName1[0] = TChar(204); //0xCC
   1.458 +	TFileName invalidFileName2;
   1.459 +	invalidFileName2.SetLength(KMaxFileName);
   1.460 +	invalidFileName2.Fill(TChar(204));//0xCC
   1.461 +	
   1.462 +	TInt err = aDb.Attach(invalidFileName1, invalidFileName2);
   1.463 +	TheTest.Printf(_L("Attach, invalid database name-1, err=%d\r\n"), err);
   1.464 +	TEST(err != KErrNone);
   1.465 +	err = aDb.Attach(invalidFileName2, invalidFileName1);
   1.466 +	TheTest.Printf(_L("Attach, invalid database name-2, err=%d\r\n"), err);
   1.467 +	TEST(err != KErrNone);
   1.468 +	err = aDb.Attach(KTestDbName2, invalidFileName2);
   1.469 +	TheTest.Printf(_L("Attach, invalid database name-3, err=%d\r\n"), err);
   1.470 +	TEST(err != KErrDied);
   1.471 +	if(err == KErrNone)
   1.472 +		{
   1.473 +		err = aDb.Detach(invalidFileName2);
   1.474 +		TheTest.Printf(_L("Detach, invalid database name-3, err=%d\r\n"), err);
   1.475 +		TEST2(err, KErrNone);
   1.476 +		}
   1.477 +	err = aDb.Attach(KTestDbName2, invalidFileName1);
   1.478 +	TheTest.Printf(_L("Attach, invalid database name-4, err=%d\r\n"), err);
   1.479 +	TEST(err != KErrDied);
   1.480 +	if(err == KErrNone)
   1.481 +		{
   1.482 +		err = aDb.Detach(invalidFileName1);
   1.483 +		TheTest.Printf(_L("Detach, invalid database name-4, err=%d\r\n"), err);
   1.484 +		TEST2(err, KErrNone);
   1.485 +		}
   1.486 +	
   1.487 +	RSqlDatabase::TSize size;
   1.488 +
   1.489 +	err = aDb.Size(size, invalidFileName1);
   1.490 +	TheTest.Printf(_L("Size, invalid database name-1, err=%d\r\n"), err);
   1.491 +	TEST(err != KErrNone);
   1.492 +	err = aDb.Size(size, invalidFileName2);
   1.493 +	TheTest.Printf(_L("Size, invalid database name-2, err=%d\r\n"), err);
   1.494 +	TEST(err != KErrNone);
   1.495 +
   1.496 +	err = aDb.Compact(RSqlDatabase::EMaxCompaction, invalidFileName1);
   1.497 +	TheTest.Printf(_L("Compact, invalid database name-1, err=%d\r\n"), err);
   1.498 +	TEST(err != KErrNone);
   1.499 +	err = aDb.Compact(RSqlDatabase::EMaxCompaction, invalidFileName2);
   1.500 +	TheTest.Printf(_L("Compact, invalid database name-2, err=%d\r\n"), err);
   1.501 +	TEST(err != KErrNone);
   1.502 +
   1.503 +	RSqlStatement stmt;
   1.504 +	err = stmt.Prepare(aDb, _L("SELECT * FROM A"));
   1.505 +	TEST2(err, KErrNone);
   1.506 +	
   1.507 +	err = stmt.ColumnIndex(invalidFileName1);
   1.508 +	TheTest.Printf(_L("ColumnIndex, invalid column name-1, err=%d\r\n"), err);
   1.509 +	TEST(err != KErrNone);
   1.510 +	err = stmt.ColumnIndex(invalidFileName2);
   1.511 +	TheTest.Printf(_L("ColumnIndex, invalid column name-2, err=%d\r\n"), err);
   1.512 +	TEST(err != KErrNone);
   1.513 +	err = stmt.ParameterIndex(invalidFileName1);
   1.514 +	TheTest.Printf(_L("ParameterIndex, invalid parameter name-1, err=%d\r\n"), err);
   1.515 +	TEST(err != KErrNone);
   1.516 +	err = stmt.ParameterIndex(invalidFileName2);
   1.517 +	TheTest.Printf(_L("ParameterIndex, invalid parameter name-2, err=%d\r\n"), err);
   1.518 +	TEST(err != KErrNone);
   1.519 +
   1.520 +	stmt.Close();
   1.521 +	}
   1.522 +
   1.523 +/**
   1.524 +@SYMTestCaseID			SYSLIB-SQL-UT-4048
   1.525 +@SYMTestCaseDesc		Bad database names - robustness test.
   1.526 +						The test defines file names with invalid content and attempts to use 
   1.527 +						these file names with the SQL API. The test should not cause crashes 
   1.528 +						in the SQL server.
   1.529 +@SYMTestPriority		High
   1.530 +@SYMTestActions			Bad database names - robustness test.
   1.531 +@SYMTestExpectedResults Test must not fail
   1.532 +@SYMREQ					REQ10405
   1.533 +                        REQ10407
   1.534 +*/
   1.535 +void BadNameTest()
   1.536 +	{
   1.537 +	TFileName invalidFileName1;
   1.538 +	invalidFileName1.SetLength(KMaxFileName);
   1.539 +	TFileName invalidFileName2;
   1.540 +	invalidFileName2.SetLength(KMaxFileName / 2);
   1.541 +	
   1.542 +	//Initialise the first character so that it is not treated as an empty descriptor by the sever
   1.543 +	invalidFileName1[0] = TChar(204); //0xCC
   1.544 +	invalidFileName2[0] = TChar(204); //0xCC
   1.545 +	
   1.546 +	(void)RSqlDatabase::Delete(KTestDbName1);
   1.547 +	(void)RSqlDatabase::Delete(KTestDbName2);
   1.548 +		
   1.549 +	RSqlDatabase db;
   1.550 +	TInt err = db.Create(invalidFileName1);
   1.551 +	TEST(err != KErrNone);
   1.552 +	err = db.Create(invalidFileName2);
   1.553 +	TEST(err != KErrNone);
   1.554 +
   1.555 +	err = db.Open(invalidFileName1);
   1.556 +	TEST(err != KErrNone);
   1.557 +	err = db.Open(invalidFileName2);
   1.558 +	TEST(err != KErrNone);
   1.559 +
   1.560 +	err = db.Create(KTestDbName1);
   1.561 +	TEST2(err, KErrNone);
   1.562 +	err = db.Exec(_L("CREATE TABLE A(I INTEGER)"));
   1.563 +	TEST(err >= 0);
   1.564 +	db.Close();
   1.565 +	
   1.566 +	RSqlSecurityPolicy sp = CreateSecurityPolicy();
   1.567 +	err = db.Create(KTestDbName2, sp);
   1.568 +	TEST2(err, KErrNone);
   1.569 +	sp.Close();
   1.570 +	err = db.Exec(_L("CREATE TABLE A(I INTEGER)"));
   1.571 +	TEST(err >= 0);
   1.572 +	db.Close();
   1.573 +
   1.574 +	TheTest.Printf(_L("Bad names test - public shared database\r\n"));
   1.575 +	err = db.Open(KTestDbName1);	
   1.576 +	TEST2(err, KErrNone);
   1.577 +	DoBadNameTest(db);
   1.578 +	db.Close();
   1.579 +
   1.580 +	TheTest.Printf(_L("Bad names test - public secure shared database\r\n"));
   1.581 +	err = db.Open(KTestDbName2);	
   1.582 +	TEST2(err, KErrNone);
   1.583 +	DoBadNameTest(db);
   1.584 +	db.Close();
   1.585 +	
   1.586 +	(void)RSqlDatabase::Delete(KTestDbName2);
   1.587 +	(void)RSqlDatabase::Delete(KTestDbName1);
   1.588 +	
   1.589 +	err = RSqlDatabase::Copy(invalidFileName1, invalidFileName2);
   1.590 +	TheTest.Printf(_L("Copy database, err=%d\r\n"), err);
   1.591 +	TEST(err != KErrNone);
   1.592 +	err = RSqlDatabase::Delete(invalidFileName1);
   1.593 +	TheTest.Printf(_L("Delete database-1, err=%d\r\n"), err);
   1.594 +	TEST(err != KErrNone);
   1.595 +	err = RSqlDatabase::Delete(invalidFileName2);
   1.596 +	TheTest.Printf(_L("Delete database-2, err=%d\r\n"), err);
   1.597 +	TEST(err != KErrNone);
   1.598 +	}
   1.599 +
   1.600 +/**
   1.601 +@SYMTestCaseID          PDS-SQL-CT-4200
   1.602 +@SYMTestCaseDesc        Invalid sql software version test.
   1.603 +@SYMTestPriority        High
   1.604 +@SYMTestActions			The test verifies that the SQL server checks that the software version of SQL sessions
   1.605 +						to be created is less or equal to the current version of the server software. 
   1.606 +						If that is not true then the SQL server does not create the session annd returns KErrNotSupported.
   1.607 +@SYMTestExpectedResults Test must not fail
   1.608 +@SYMDEF                 DEF145236
   1.609 +*/  
   1.610 +void InvalidSoftwareVersionTest()
   1.611 +	{
   1.612 +	(void)RSqlDatabase::Delete(KTestDbName1);
   1.613 +	RSqlDatabase db;
   1.614 +	TInt err = db.Create(KTestDbName1);
   1.615 +	TEST2(err, KErrNone);
   1.616 +	db.Close();
   1.617 +	
   1.618 +	//Smaller version number
   1.619 +	TVersion sqlSoftwareVersion1(1, 0, 0);
   1.620 +	RTestSqlDbSession sess1;
   1.621 +	err = sess1.Connect(sqlSoftwareVersion1);
   1.622 +	sess1.Close();
   1.623 +	TEST2(err, KErrNone);
   1.624 +	
   1.625 +	//Bigger version number 1
   1.626 +	TVersion sqlSoftwareVersion2(1, 97, 3);
   1.627 +	RTestSqlDbSession sess2;
   1.628 +	err = sess2.Connect(sqlSoftwareVersion2);
   1.629 +	TEST2(err, KErrNotSupported);
   1.630 +	
   1.631 +	//Bigger version number 2
   1.632 +	TVersion sqlSoftwareVersion3(78, 0, 1);
   1.633 +	RTestSqlDbSession sess3;
   1.634 +	err = sess3.Connect(sqlSoftwareVersion3);
   1.635 +	TEST2(err, KErrNotSupported);
   1.636 +
   1.637 +	//The current version number
   1.638 +	TVersion sqlSoftwareVersion4(KSqlMajorVer, KSqlMinorVer, KSqlBuildVer);
   1.639 +	RTestSqlDbSession sess4;
   1.640 +	err = sess4.Connect(sqlSoftwareVersion4);
   1.641 +	sess4.Close();
   1.642 +	TEST2(err, KErrNone);
   1.643 +	
   1.644 +	(void)RSqlDatabase::Delete(KTestDbName1);
   1.645 +	}
   1.646 +
   1.647 +void DoTests()
   1.648 +	{
   1.649 +	TheTest.Start(_L(" @SYMTestCaseID:SYSLIB-SQL-CT-1769 Bad client test "));
   1.650 +	BadClientTest();
   1.651 +	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-4048 Bad names test"));
   1.652 +	BadNameTest();
   1.653 +	TheTest.Next(_L(" @SYMTestCaseID:PDS-SQL-CT-4200 Invalid software version test"));
   1.654 +	InvalidSoftwareVersionTest();
   1.655 +	}
   1.656 +
   1.657 +TInt E32Main()
   1.658 +	{
   1.659 +	TheTest.Title();
   1.660 +	
   1.661 +	CTrapCleanup* tc = CTrapCleanup::New();
   1.662 +	
   1.663 +	__UHEAP_MARK;
   1.664 +	
   1.665 +	DeleteTestFiles();
   1.666 +	CreateTestEnv();
   1.667 +	DoTests();
   1.668 +	DeleteTestFiles();
   1.669 +	TheFs.Close();
   1.670 +
   1.671 +	__UHEAP_MARKEND;
   1.672 +	
   1.673 +	TheTest.End();
   1.674 +	TheTest.Close();
   1.675 +	
   1.676 +	delete tc;
   1.677 +
   1.678 +	User::Heap().Check();
   1.679 +	return KErrNone;
   1.680 +	}