persistentstorage/sql/TEST/t_sqlcmdlineutil.cpp
branchRCL_3
changeset 15 fcc16690f446
equal deleted inserted replaced
14:04ec7606545c 15:fcc16690f446
       
     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".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 #include <e32test.h>
       
    16 #include "t_sqlcmdlineutil.h"
       
    17 
       
    18 static void GetCmdLine(RTest& aTest, const TDesC& aTestName, TDes& aCmdLine)
       
    19 	{
       
    20 	User::CommandLine(aCmdLine);
       
    21 	aCmdLine.TrimAll();
       
    22 	if(aCmdLine.Length() == 0)
       
    23 		{
       
    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);
       
    25 		return;
       
    26 		}
       
    27 	aCmdLine.Append(TChar('/'));
       
    28 	}
       
    29 
       
    30 static void ExtractCmdLineParams(TDes& aCmdLine,  RArray<TPtrC>& aPrmNames, RArray<TPtrC>& aPrmValues)
       
    31 	{
       
    32 	aPrmNames.Reset();	
       
    33 	aPrmValues.Reset();	
       
    34 	
       
    35 	enum TState{EWaitPrmStart, EReadPrmName, EReadPrmValue};
       
    36 	TState state = EWaitPrmStart;
       
    37 	TInt startPos = -1;
       
    38 	TPtr prmName(0, 0);
       
    39 	TPtr prmValue(0, 0);
       
    40 	
       
    41 	aCmdLine.Append(TChar('/'));
       
    42 	
       
    43 	for(TInt i=0;i<aCmdLine.Length();++i)
       
    44 		{
       
    45 		switch(state)
       
    46 			{
       
    47 			case EWaitPrmStart:
       
    48 				if(aCmdLine[i] == TChar('/'))
       
    49 					{
       
    50 					startPos = i + 1;
       
    51 					prmName.Zero();
       
    52 					state = EReadPrmName;
       
    53 					}
       
    54 				break;
       
    55 			case EReadPrmName:
       
    56 				if(aCmdLine[i] == TChar('='))
       
    57 					{
       
    58 					TPtr p = aCmdLine.MidTPtr(startPos, i - startPos);
       
    59 					prmName.Set(p);
       
    60 					prmName.TrimRight();
       
    61 					startPos = i + 1;
       
    62 					prmValue.Zero();
       
    63 					state = EReadPrmValue;
       
    64 					}
       
    65 				break;
       
    66 			case EReadPrmValue:
       
    67 				if(aCmdLine[i] == TChar('/'))
       
    68 					{
       
    69 					TPtr p = aCmdLine.MidTPtr(startPos, i - startPos);
       
    70 					prmValue.Set(p);
       
    71 					prmValue.Trim();
       
    72 					startPos = i + 1;
       
    73 					aPrmNames.Append(prmName);
       
    74 					aPrmValues.Append(prmValue);
       
    75 					prmName.Zero();
       
    76 					prmValue.Zero();
       
    77 					state = EReadPrmName;
       
    78 					}
       
    79 				break;
       
    80 			default:
       
    81 				break;
       
    82 			}
       
    83 		}
       
    84 	}
       
    85 
       
    86 static void ExtractParamNamesAndValues(const RArray<TPtrC>& aPrmNames, const RArray<TPtrC>& aPrmValues, TCmdLineParams& aCmdLineParams)
       
    87 	{
       
    88 	__ASSERT_ALWAYS(aPrmNames.Count() == aPrmValues.Count(), User::Invariant());
       
    89 	
       
    90 	aCmdLineParams.SetDefaults();
       
    91 	
       
    92 	for(TInt i=0;i<aPrmNames.Count();++i)
       
    93 		{
       
    94 		if(aPrmNames[i].CompareF(_L("enc")) == 0)
       
    95 			{
       
    96 			TLex lex(aPrmValues[i]);
       
    97 			TInt enc = 0;
       
    98 			TInt err = lex.Val(enc);
       
    99 			if(err == KErrNone)
       
   100 				{
       
   101 				if(enc == 8)
       
   102 					{
       
   103 					aCmdLineParams.iDbEncoding = TCmdLineParams::EDbUtf8;
       
   104 					}
       
   105 				else if(enc == 16)
       
   106 					{
       
   107 					aCmdLineParams.iDbEncoding = TCmdLineParams::EDbUtf16;
       
   108 					}
       
   109 				}
       
   110 			}
       
   111 		else if(aPrmNames[i].CompareF(_L("drv")) == 0)
       
   112 			{
       
   113 			if(aPrmValues[i].Length() == 2 && aPrmValues[i][1] == TChar(':'))
       
   114 				{
       
   115 				TChar ch(aPrmValues[i][0]);
       
   116 				ch.LowerCase();
       
   117 				if(ch >= TChar('a') && ch <= TChar('z'))
       
   118 					aCmdLineParams.iDriveName.Copy(aPrmValues[i]);
       
   119 				}
       
   120 			}
       
   121 		else if(aPrmNames[i].CompareF(_L("page")) == 0)
       
   122 			{
       
   123 			TLex lex(aPrmValues[i]);
       
   124 			TInt pageSize = 0;
       
   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))
       
   128 				{
       
   129 				aCmdLineParams.iPageSize = pageSize;
       
   130 				}
       
   131 			}
       
   132 		else if(aPrmNames[i].CompareF(_L("cache")) == 0)
       
   133 			{
       
   134 			TLex lex(aPrmValues[i]);
       
   135 			TInt cacheSize = 0;
       
   136 			TInt err = lex.Val(cacheSize);
       
   137 			if(err == KErrNone && (cacheSize > 0 && cacheSize < 1000000000))
       
   138 				{
       
   139 				aCmdLineParams.iCacheSize = cacheSize;
       
   140 				}
       
   141 			}
       
   142 		else if(aPrmNames[i].CompareF(_L("hlimit")) == 0)
       
   143 			{
       
   144 			TLex lex(aPrmValues[i]);
       
   145 			TInt softHeapLimit = 0;
       
   146 			TInt err = lex.Val(softHeapLimit);
       
   147 			if(err == KErrNone && (softHeapLimit >= 0 && softHeapLimit < 1000000000))
       
   148 				{
       
   149 				aCmdLineParams.iSoftHeapLimitKb = softHeapLimit;
       
   150 				}
       
   151 			}
       
   152 		}
       
   153 	}
       
   154 
       
   155 static void PrepareSqlConfigString(RTest& aTest, const TCmdLineParams& aCmdLineParams, TDes8& aConfigStr)
       
   156 	{
       
   157 	aConfigStr.Zero();
       
   158 	
       
   159 	if(aCmdLineParams.iDbEncoding == TCmdLineParams::EDbUtf8)
       
   160 		{
       
   161 		aTest.Printf(_L("--PRM--Database Encoding: UTF8\r\n"));
       
   162 		aConfigStr.Append(_L8("encoding=\"UTF-8\";"));
       
   163 		}
       
   164 	else
       
   165 		{
       
   166 		aTest.Printf(_L("--PRM--Database Encoding: UTF16\r\n"));
       
   167 		aConfigStr.Append(_L8("encoding=\"UTF-16\";"));
       
   168 		}
       
   169 	
       
   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);
       
   174 
       
   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);
       
   179 	
       
   180 	aTest.Printf(_L("--PRM--Database drive: %S\r\n"), &aCmdLineParams.iDriveName);
       
   181 
       
   182 	if(aCmdLineParams.iSoftHeapLimitKb > 0)
       
   183 		{
       
   184 		aTest.Printf(_L("--PRM--Soft heap limit: %d Kb\r\n"), aCmdLineParams.iSoftHeapLimitKb);
       
   185 		}
       
   186 	else
       
   187 		{
       
   188 		aTest.Printf(_L("--PRM--Soft heap limit: default\r\n"));
       
   189 		}
       
   190 	}
       
   191 
       
   192 #ifdef SQL_SOFT_HEAP_LIMIT_TEST	
       
   193 
       
   194 static TInt KillProcess(const TDesC& aProcessName)
       
   195 	{
       
   196 	TFullName name;
       
   197 	TBuf<64> pattern(aProcessName);
       
   198 	TInt length = pattern.Length();
       
   199 	pattern += _L("*");
       
   200 	TFindProcess procFinder(pattern);
       
   201 
       
   202 	while (procFinder.Next(name) == KErrNone)
       
   203 		{
       
   204 		if (name.Length() > length)
       
   205 			{//If found name is a string containing aProcessName string.
       
   206 			TChar c(name[length]);
       
   207 			if (c.IsAlphaDigit() ||
       
   208 				c == TChar('_') ||
       
   209 				c == TChar('-'))
       
   210 				{
       
   211 				// If the found name is other valid application name
       
   212 				// starting with aProcessName string.
       
   213 				continue;
       
   214 				}
       
   215 			}
       
   216 		RProcess proc;
       
   217 		if (proc.Open(name) == KErrNone)
       
   218 			{
       
   219 			proc.Kill(0);
       
   220 			}
       
   221 		proc.Close();
       
   222 		}
       
   223 	return KErrNone;
       
   224 	}
       
   225 
       
   226 _LIT(KSqlSrvName, "sqlsrv.exe");
       
   227 _LIT(KSqlSrvConfigFile, "c:\\test\\t_sqlserver.cfg");
       
   228 
       
   229 static void ReplaceConfigFile(const TDesC16& aConfig)
       
   230 	{
       
   231 	RFs fs;
       
   232 	TInt err = fs.Connect();
       
   233 	__ASSERT_ALWAYS(err == KErrNone, User::Invariant());
       
   234 	
       
   235 	(void)KillProcess(KSqlSrvName);
       
   236 	
       
   237 	(void)fs.MkDirAll(KSqlSrvConfigFile);
       
   238 	(void)fs.Delete(KSqlSrvConfigFile);
       
   239 	
       
   240 	RFile file;
       
   241 	err = file.Create(fs, KSqlSrvConfigFile, EFileRead | EFileWrite);
       
   242 	__ASSERT_ALWAYS(err == KErrNone, User::Invariant());
       
   243 	
       
   244 	TPtrC8 p((const TUint8*)aConfig.Ptr(), aConfig.Length() * sizeof(TUint16));
       
   245 	err = file.Write(p);
       
   246 	file.Close();
       
   247 	__ASSERT_ALWAYS(err == KErrNone, User::Invariant());
       
   248 	
       
   249 	fs.Close();
       
   250 	}
       
   251 
       
   252 static void DeleteConfigFile()
       
   253 	{
       
   254 	RFs fs;
       
   255 	TInt err = fs.Connect();
       
   256 	__ASSERT_ALWAYS(err == KErrNone, User::Invariant());
       
   257 	
       
   258 	(void)KillProcess(KSqlSrvName);
       
   259 	
       
   260 	(void)fs.MkDirAll(KSqlSrvConfigFile);
       
   261 	(void)fs.Delete(KSqlSrvConfigFile);
       
   262 	
       
   263 	fs.Close();
       
   264 	}
       
   265 
       
   266 #endif //SQL_SOFT_HEAP_LIMIT_TEST	
       
   267 
       
   268 void GetCmdLineParamsAndSqlConfigString(RTest& aTest, const TDesC& aTestName, TCmdLineParams& aCmdLineParams, TDes8& aConfigStr)
       
   269 	{
       
   270 	TBuf<200> cmdLine;
       
   271 	GetCmdLine(aTest, aTestName, cmdLine);
       
   272 	RArray<TPtrC> prmNames;
       
   273 	RArray<TPtrC> prmValues;
       
   274 	ExtractCmdLineParams(cmdLine, prmNames, prmValues);
       
   275 	ExtractParamNamesAndValues(prmNames, prmValues, aCmdLineParams);
       
   276 	prmValues.Close();
       
   277 	prmNames.Close();
       
   278 	PrepareSqlConfigString(aTest, aCmdLineParams, aConfigStr);
       
   279 	}
       
   280 
       
   281 void PrepareDbName(const TDesC& aDeafultDbName, const TDriveName& aDriveName, TDes& aDbName)
       
   282 	{
       
   283 	TParse parse;
       
   284 	parse.Set(aDriveName, &aDeafultDbName, 0);
       
   285 	const TDesC& dbFilePath = parse.FullName();
       
   286 	aDbName.Copy(dbFilePath);
       
   287 	}
       
   288 
       
   289 void SetSoftHeapLimit(TInt aSoftHeapLimit)
       
   290 	{
       
   291 	if(aSoftHeapLimit > 0)
       
   292 		{
       
   293 #ifdef SQL_SOFT_HEAP_LIMIT_TEST	
       
   294 		TBuf<50> configBuf;
       
   295 		configBuf.Format(_L("soft_heap_limit_kb=%d"), aSoftHeapLimit);
       
   296 		ReplaceConfigFile(configBuf);
       
   297 #else
       
   298 		RDebug::Print(_L("The soft heap limit cannot be set if \"SQL_SOFT_HEAP_LIMIT_TEST\" macro is not defined!\r\n"));
       
   299 #endif
       
   300 		}
       
   301 	else if(aSoftHeapLimit < 0)
       
   302 		{
       
   303 		RDebug::Print(_L("Soft heap limit of %d Kb cannot be set!\r\n"), aSoftHeapLimit);
       
   304 		}
       
   305 	}
       
   306 
       
   307 void ResetSoftHeapLimit()
       
   308 	{
       
   309 #ifdef SQL_SOFT_HEAP_LIMIT_TEST	
       
   310 	DeleteConfigFile();
       
   311 #endif
       
   312 	}
       
   313