First public contribution.
     1 // Copyright (c) 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".
 
     8 // Initial Contributors:
 
     9 // Nokia Corporation - initial contribution.
 
    16 #include "t_sqlcmdlineutil.h"
 
    18 static void GetCmdLine(RTest& aTest, const TDesC& aTestName, TDes& aCmdLine)
 
    20 	User::CommandLine(aCmdLine);
 
    22 	if(aCmdLine.Length() == 0)
 
    24 		aTest.Printf(_L("Usage: %S [ [/enc=<16/8>] /drv=<drive letter>:] [/page=<512/1024/2048/4096/8192/16384/32768>] ] [/cache=<number>] [/hlimit=<Kb>]\r\n"), &aTestName);
 
    27 	aCmdLine.Append(TChar('/'));
 
    30 static void ExtractCmdLineParams(TDes& aCmdLine,  RArray<TPtrC>& aPrmNames, RArray<TPtrC>& aPrmValues)
 
    35 	enum TState{EWaitPrmStart, EReadPrmName, EReadPrmValue};
 
    36 	TState state = EWaitPrmStart;
 
    41 	aCmdLine.Append(TChar('/'));
 
    43 	for(TInt i=0;i<aCmdLine.Length();++i)
 
    48 				if(aCmdLine[i] == TChar('/'))
 
    56 				if(aCmdLine[i] == TChar('='))
 
    58 					TPtr p = aCmdLine.MidTPtr(startPos, i - startPos);
 
    63 					state = EReadPrmValue;
 
    67 				if(aCmdLine[i] == TChar('/'))
 
    69 					TPtr p = aCmdLine.MidTPtr(startPos, i - startPos);
 
    73 					aPrmNames.Append(prmName);
 
    74 					aPrmValues.Append(prmValue);
 
    86 static void ExtractParamNamesAndValues(const RArray<TPtrC>& aPrmNames, const RArray<TPtrC>& aPrmValues, TCmdLineParams& aCmdLineParams)
 
    88 	__ASSERT_ALWAYS(aPrmNames.Count() == aPrmValues.Count(), User::Invariant());
 
    90 	aCmdLineParams.SetDefaults();
 
    92 	for(TInt i=0;i<aPrmNames.Count();++i)
 
    94 		if(aPrmNames[i].CompareF(_L("enc")) == 0)
 
    96 			TLex lex(aPrmValues[i]);
 
    98 			TInt err = lex.Val(enc);
 
   103 					aCmdLineParams.iDbEncoding = TCmdLineParams::EDbUtf8;
 
   107 					aCmdLineParams.iDbEncoding = TCmdLineParams::EDbUtf16;
 
   111 		else if(aPrmNames[i].CompareF(_L("drv")) == 0)
 
   113 			if(aPrmValues[i].Length() == 2 && aPrmValues[i][1] == TChar(':'))
 
   115 				TChar ch(aPrmValues[i][0]);
 
   117 				if(ch >= TChar('a') && ch <= TChar('z'))
 
   118 					aCmdLineParams.iDriveName.Copy(aPrmValues[i]);
 
   121 		else if(aPrmNames[i].CompareF(_L("page")) == 0)
 
   123 			TLex lex(aPrmValues[i]);
 
   125 			TInt err = lex.Val(pageSize);
 
   126 			if(err == KErrNone && (pageSize == 512 || pageSize == 1024 || pageSize == 2048 ||
 
   127 			   pageSize == 4096 || pageSize == 8192 || pageSize == 16384 || pageSize == 32768))
 
   129 				aCmdLineParams.iPageSize = pageSize;
 
   132 		else if(aPrmNames[i].CompareF(_L("cache")) == 0)
 
   134 			TLex lex(aPrmValues[i]);
 
   136 			TInt err = lex.Val(cacheSize);
 
   137 			if(err == KErrNone && (cacheSize > 0 && cacheSize < 1000000000))
 
   139 				aCmdLineParams.iCacheSize = cacheSize;
 
   142 		else if(aPrmNames[i].CompareF(_L("hlimit")) == 0)
 
   144 			TLex lex(aPrmValues[i]);
 
   145 			TInt softHeapLimit = 0;
 
   146 			TInt err = lex.Val(softHeapLimit);
 
   147 			if(err == KErrNone && (softHeapLimit >= 0 && softHeapLimit < 1000000000))
 
   149 				aCmdLineParams.iSoftHeapLimitKb = softHeapLimit;
 
   155 static void PrepareSqlConfigString(RTest& aTest, const TCmdLineParams& aCmdLineParams, TDes8& aConfigStr)
 
   159 	if(aCmdLineParams.iDbEncoding == TCmdLineParams::EDbUtf8)
 
   161 		aTest.Printf(_L("--PRM--Database Encoding: UTF8\r\n"));
 
   162 		aConfigStr.Append(_L8("encoding=\"UTF-8\";"));
 
   166 		aTest.Printf(_L("--PRM--Database Encoding: UTF16\r\n"));
 
   167 		aConfigStr.Append(_L8("encoding=\"UTF-16\";"));
 
   170 	aTest.Printf(_L("--PRM--Database page size: %d\r\n"), aCmdLineParams.iPageSize);
 
   171 	TBuf8<20> pageSizeBuf;
 
   172 	pageSizeBuf.Format(_L8("page_size=%d;"), aCmdLineParams.iPageSize);
 
   173 	aConfigStr.Append(pageSizeBuf);
 
   175 	aTest.Printf(_L("--PRM--Database cache size: %d\r\n"), aCmdLineParams.iCacheSize);
 
   176 	TBuf8<20> cacheSizeBuf;
 
   177 	cacheSizeBuf.Format(_L8("cache_size=%d;"), aCmdLineParams.iCacheSize);
 
   178 	aConfigStr.Append(cacheSizeBuf);
 
   180 	aTest.Printf(_L("--PRM--Database drive: %S\r\n"), &aCmdLineParams.iDriveName);
 
   182 	if(aCmdLineParams.iSoftHeapLimitKb > 0)
 
   184 		aTest.Printf(_L("--PRM--Soft heap limit: %d Kb\r\n"), aCmdLineParams.iSoftHeapLimitKb);
 
   188 		aTest.Printf(_L("--PRM--Soft heap limit: default\r\n"));
 
   192 #ifdef SQL_SOFT_HEAP_LIMIT_TEST	
 
   194 static TInt KillProcess(const TDesC& aProcessName)
 
   197 	TBuf<64> pattern(aProcessName);
 
   198 	TInt length = pattern.Length();
 
   200 	TFindProcess procFinder(pattern);
 
   202 	while (procFinder.Next(name) == KErrNone)
 
   204 		if (name.Length() > length)
 
   205 			{//If found name is a string containing aProcessName string.
 
   206 			TChar c(name[length]);
 
   207 			if (c.IsAlphaDigit() ||
 
   211 				// If the found name is other valid application name
 
   212 				// starting with aProcessName string.
 
   217 		if (proc.Open(name) == KErrNone)
 
   226 _LIT(KSqlSrvName, "sqlsrv.exe");
 
   227 _LIT(KSqlSrvConfigFile, "c:\\test\\t_sqlserver.cfg");
 
   229 static void ReplaceConfigFile(const TDesC16& aConfig)
 
   232 	TInt err = fs.Connect();
 
   233 	__ASSERT_ALWAYS(err == KErrNone, User::Invariant());
 
   235 	(void)KillProcess(KSqlSrvName);
 
   237 	(void)fs.MkDirAll(KSqlSrvConfigFile);
 
   238 	(void)fs.Delete(KSqlSrvConfigFile);
 
   241 	err = file.Create(fs, KSqlSrvConfigFile, EFileRead | EFileWrite);
 
   242 	__ASSERT_ALWAYS(err == KErrNone, User::Invariant());
 
   244 	TPtrC8 p((const TUint8*)aConfig.Ptr(), aConfig.Length() * sizeof(TUint16));
 
   247 	__ASSERT_ALWAYS(err == KErrNone, User::Invariant());
 
   252 static void DeleteConfigFile()
 
   255 	TInt err = fs.Connect();
 
   256 	__ASSERT_ALWAYS(err == KErrNone, User::Invariant());
 
   258 	(void)KillProcess(KSqlSrvName);
 
   260 	(void)fs.MkDirAll(KSqlSrvConfigFile);
 
   261 	(void)fs.Delete(KSqlSrvConfigFile);
 
   266 #endif //SQL_SOFT_HEAP_LIMIT_TEST	
 
   268 void GetCmdLineParamsAndSqlConfigString(RTest& aTest, const TDesC& aTestName, TCmdLineParams& aCmdLineParams, TDes8& aConfigStr)
 
   271 	GetCmdLine(aTest, aTestName, cmdLine);
 
   272 	RArray<TPtrC> prmNames;
 
   273 	RArray<TPtrC> prmValues;
 
   274 	ExtractCmdLineParams(cmdLine, prmNames, prmValues);
 
   275 	ExtractParamNamesAndValues(prmNames, prmValues, aCmdLineParams);
 
   278 	PrepareSqlConfigString(aTest, aCmdLineParams, aConfigStr);
 
   281 void PrepareDbName(const TDesC& aDeafultDbName, const TDriveName& aDriveName, TDes& aDbName)
 
   284 	parse.Set(aDriveName, &aDeafultDbName, 0);
 
   285 	const TDesC& dbFilePath = parse.FullName();
 
   286 	aDbName.Copy(dbFilePath);
 
   289 void SetSoftHeapLimit(TInt aSoftHeapLimit)
 
   291 	if(aSoftHeapLimit > 0)
 
   293 #ifdef SQL_SOFT_HEAP_LIMIT_TEST	
 
   295 		configBuf.Format(_L("soft_heap_limit_kb=%d"), aSoftHeapLimit);
 
   296 		ReplaceConfigFile(configBuf);
 
   298 		RDebug::Print(_L("The soft heap limit cannot be set if \"SQL_SOFT_HEAP_LIMIT_TEST\" macro is not defined!\r\n"));
 
   301 	else if(aSoftHeapLimit < 0)
 
   303 		RDebug::Print(_L("Soft heap limit of %d Kb cannot be set!\r\n"), aSoftHeapLimit);
 
   307 void ResetSoftHeapLimit()
 
   309 #ifdef SQL_SOFT_HEAP_LIMIT_TEST