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