persistentstorage/sql/TEST/t_sqloslayer.cpp
changeset 0 08ec8eefde2f
child 8 fa9941cf3867
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     1 // Copyright (c) 2006-2009 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 <stdlib.h>
       
    19 #include <string.h>
       
    20 #include <hal.h>
       
    21 #include "SqliteSymbian.h"
       
    22 #ifdef __cplusplus
       
    23 extern "C" {
       
    24 #endif
       
    25 	#include "sqliteInt.h"
       
    26 	#include "os.h"
       
    27 #ifdef __cplusplus
       
    28 }  /* End of the 'extern "C"' block */
       
    29 #endif
       
    30 
       
    31 ///////////////////////////////////////////////////////////////////////////////////////
       
    32 
       
    33 const char* KSymbianVfsNameZ = "SymbianSql";
       
    34 
       
    35 RTest TheTest(_L("t_sqloslayer test"));
       
    36 RFs   TheFs;
       
    37 
       
    38 _LIT(KTestDir, "c:\\test\\");
       
    39 _LIT(KTestFile1, "c:\\test\\t_sqloslayer.bin");
       
    40 _LIT(KTestFile3, "c:\\test\\t_sqloslayer.db");
       
    41 const char* KTestFile1Z = "c:\\test\\t_sqloslayer.bin";
       
    42 const char* KTestFile2Z = "z:\\test\\TestDb1.db";
       
    43 const char* KTestFile3Z = "c:\\test\\t_sqloslayer.db";
       
    44 
       
    45 //In order to be able to compile the test, the following variables are defined (used inside the OS porting layer, when _SQLPROFILER macro is defined)
       
    46 #ifdef _SQLPROFILER
       
    47 TInt TheSqlSrvProfilerFileRead = 0;
       
    48 TInt TheSqlSrvProfilerFileWrite = 0;
       
    49 TInt TheSqlSrvProfilerFileSync = 0;
       
    50 TInt TheSqlSrvProfilerFileSetSize = 0;
       
    51 #endif
       
    52 
       
    53 ///////////////////////////////////////////////////////////////////////////////////////
       
    54 
       
    55 void DeleteTestFiles()
       
    56 	{
       
    57 	(void)TheFs.Delete(KTestFile3);
       
    58 	(void)TheFs.Delete(KTestFile1);
       
    59 	}
       
    60 
       
    61 void TestEnvDestroy()
       
    62 	{
       
    63 	sqlite3SymbianLibFinalize();
       
    64 	DeleteTestFiles();
       
    65 	TheFs.Close();
       
    66 	}
       
    67 
       
    68 ///////////////////////////////////////////////////////////////////////////////////////
       
    69 ///////////////////////////////////////////////////////////////////////////////////////
       
    70 //Test macros and functions
       
    71 void Check1(TInt aValue, TInt aLine)
       
    72 	{
       
    73 	if(!aValue)
       
    74 		{
       
    75 		TestEnvDestroy();
       
    76 		RDebug::Print(_L("*** Line %d\r\n"), aLine);
       
    77 		TheTest(EFalse, aLine);
       
    78 		}
       
    79 	}
       
    80 void Check2(TInt aValue, TInt aExpected, TInt aLine)
       
    81 	{
       
    82 	if(aValue != aExpected)
       
    83 		{
       
    84 		TestEnvDestroy();
       
    85 		RDebug::Print(_L("*** Line %d, Expected error: %d, got: %d\r\n"), aLine, aExpected, aValue);
       
    86 		TheTest(EFalse, aLine);
       
    87 		}
       
    88 	}
       
    89 #define TEST(arg) ::Check1((arg), __LINE__)
       
    90 #define TEST2(aValue, aExpected) ::Check2(aValue, aExpected, __LINE__)
       
    91 
       
    92 ///////////////////////////////////////////////////////////////////////////////////////
       
    93 
       
    94 void TestEnvInit()
       
    95     {
       
    96 	TInt err = TheFs.Connect();
       
    97 	TEST2(err, KErrNone);
       
    98 
       
    99 	err = TheFs.MkDir(KTestDir);
       
   100 	TEST(err == KErrNone || err == KErrAlreadyExists);
       
   101 
       
   102 	err = sqlite3SymbianLibInit();
       
   103 	TEST2(err, KErrNone);
       
   104 	}
       
   105 
       
   106 TInt CalcMs(TUint32 aStartTicks, TUint32 aEndTicks)
       
   107 	{
       
   108 	static TInt freq = 0;
       
   109 	if(freq == 0)
       
   110 		{
       
   111 		TEST2(HAL::Get(HAL::EFastCounterFrequency, freq), KErrNone);
       
   112 		}
       
   113 	TInt64 diffTicks = (TInt64)aEndTicks - (TInt64)aStartTicks;
       
   114 	if(diffTicks < 0)
       
   115 		{
       
   116 		diffTicks = KMaxTUint32 + diffTicks + 1;
       
   117 		}
       
   118 	const TInt KMicroSecIn1Sec = 1000000;
       
   119 	TInt32 us = (diffTicks * KMicroSecIn1Sec) / freq;
       
   120 	return us / 1000;
       
   121 	}
       
   122 	
       
   123 ///////////////////////////////////////////////////////////////////////////////////////
       
   124 
       
   125 //Create/open/close/delete a file
       
   126 void Test1()
       
   127 	{
       
   128 	sqlite3_vfs* vfs = sqlite3_vfs_find(KSymbianVfsNameZ);
       
   129 	TEST(vfs != NULL);
       
   130 
       
   131   	sqlite3_file* osFile = (sqlite3_file*)User::Alloc(vfs->szOsFile);
       
   132 	TEST(osFile != NULL);
       
   133 		
       
   134 	//Creating a new file
       
   135 	int outFlags = 0;
       
   136 	int err = sqlite3OsOpen(vfs, KTestFile1Z, osFile, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, &outFlags);
       
   137 	TEST2(err, SQLITE_OK);
       
   138 	TEST(outFlags & SQLITE_OPEN_READWRITE);
       
   139 	err = sqlite3OsClose(osFile);
       
   140 	TEST2(err, SQLITE_OK);
       
   141 	//Opening an existing file for R/W
       
   142 	err = sqlite3OsOpen(vfs, KTestFile1Z, osFile, SQLITE_OPEN_READWRITE, &outFlags);
       
   143 	TEST2(err, SQLITE_OK);
       
   144 	TEST(outFlags & SQLITE_OPEN_READWRITE);
       
   145 	err = sqlite3OsClose(osFile);
       
   146 	TEST2(err, SQLITE_OK);
       
   147 	//Opening a read-only file
       
   148 	err = sqlite3OsOpen(vfs, KTestFile2Z, osFile, SQLITE_OPEN_READWRITE, &outFlags);
       
   149 	TEST2(err, SQLITE_OK);
       
   150 	TEST(outFlags & SQLITE_OPEN_READONLY);
       
   151 	//Truncate a read-only file
       
   152 	err = osFile->pMethods->xTruncate(osFile, 0);
       
   153 	TEST2(err, SQLITE_IOERR);
       
   154 	//xAccess - read-only file
       
   155 	int res = 0;
       
   156 	err = vfs->xAccess(vfs, KTestFile2Z, SQLITE_ACCESS_READ, &res);
       
   157 	TEST2(err, SQLITE_OK);
       
   158 	TEST(res != 0);
       
   159 	//xAccess - invalid request
       
   160 	res = 0;
       
   161 	err = vfs->xAccess(vfs, KTestFile2Z, 122, &res);
       
   162 	TEST2(err, SQLITE_OK);
       
   163 	TEST2(res, 0);
       
   164 	//
       
   165 	err = sqlite3OsClose(osFile);
       
   166 	TEST2(err, SQLITE_OK);
       
   167 	//Creating a new file
       
   168 	err = sqlite3OsOpen(vfs, KTestFile3Z, osFile, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, &outFlags);
       
   169 	TEST2(err, SQLITE_OK);
       
   170 	TEST(outFlags & SQLITE_OPEN_READWRITE);
       
   171 	err = sqlite3OsClose(osFile);
       
   172 	TEST2(err, SQLITE_OK);
       
   173 	//Open a file for a read-only access
       
   174 	err = sqlite3OsOpen(vfs, KTestFile1Z, osFile, SQLITE_OPEN_READONLY, &outFlags);
       
   175 	TEST2(err, SQLITE_OK);
       
   176 	TEST(outFlags & SQLITE_OPEN_READONLY);
       
   177 	err = sqlite3OsWrite(osFile, "1234", 4, 0);
       
   178 	TEST(err != SQLITE_OK);
       
   179 	err = sqlite3SymbianLastOsError();
       
   180 	TEST2(err, KErrAccessDenied);
       
   181 	err = vfs->xGetLastError(vfs, 0, 0);
       
   182 	TEST2(err, 0);//Default implementation
       
   183 	err = sqlite3OsClose(osFile);
       
   184 	TEST2(err, SQLITE_OK);
       
   185 	//Delete KTestFile3Z file
       
   186 	err = sqlite3OsDelete(vfs, KTestFile3Z, 0);
       
   187 	TEST2(err, SQLITE_OK);
       
   188 	res = 0;
       
   189 	err = sqlite3OsAccess(vfs, KTestFile3Z, SQLITE_ACCESS_EXISTS, &res);
       
   190 	TEST2(err, SQLITE_OK);
       
   191 	TEST2(res, 0);
       
   192 	//Open a file for an exclusive access
       
   193 	err = sqlite3OsOpen(vfs, KTestFile3Z, osFile, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_DELETEONCLOSE | SQLITE_OPEN_EXCLUSIVE, &outFlags);
       
   194 	TEST2(err, SQLITE_OK);
       
   195 	err = sqlite3OsClose(osFile);
       
   196 	TEST2(err, SQLITE_OK);
       
   197 	//The file should not exist now
       
   198 	err = sqlite3OsAccess(vfs, KTestFile3Z, SQLITE_ACCESS_EXISTS, &res);
       
   199 	TEST2(err, SQLITE_OK);
       
   200 	TEST2(res, 0);
       
   201 	//Open a file for an exclusive access without deleting it after 
       
   202 	err = sqlite3OsOpen(vfs, KTestFile3Z, osFile, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_EXCLUSIVE, &outFlags);
       
   203 	TEST2(err, SQLITE_OK);
       
   204 	err = sqlite3OsClose(osFile);
       
   205 	TEST2(err, SQLITE_OK);
       
   206 	//The file should exist now
       
   207 	err = sqlite3OsAccess(vfs, KTestFile3Z, SQLITE_ACCESS_EXISTS, &res);
       
   208 	TEST2(err, SQLITE_OK);
       
   209 	TEST2(res, 1);
       
   210 	//Delete KTestFile3Z file
       
   211 	err = sqlite3OsDelete(vfs, KTestFile3Z, 0);
       
   212 	TEST2(err, SQLITE_OK);
       
   213 	err = sqlite3OsAccess(vfs, KTestFile3Z, SQLITE_ACCESS_EXISTS, &res);
       
   214 	TEST2(err, SQLITE_OK);
       
   215 	TEST2(res, 0);
       
   216 	//
       
   217 	User::Free(osFile);
       
   218 	}
       
   219 
       
   220 //Read/Write/Seek/Truncate test	
       
   221 void Test2()
       
   222 	{
       
   223 	sqlite3_vfs* vfs = sqlite3_vfs_find(KSymbianVfsNameZ);
       
   224 	TEST(vfs != NULL);
       
   225 
       
   226   	sqlite3_file* osFile = (sqlite3_file*)User::Alloc(vfs->szOsFile);
       
   227 	TEST(osFile != NULL);
       
   228 	
       
   229 	//Creating a new file
       
   230 	int err = sqlite3OsDelete(vfs, KTestFile1Z, 0);
       
   231 	TEST2(err, SQLITE_OK);
       
   232 	int res = 0;
       
   233 	err = sqlite3OsAccess(vfs, KTestFile1Z, SQLITE_ACCESS_EXISTS, &res);
       
   234 	TEST2(err, SQLITE_OK);
       
   235 	TEST2(res, 0);
       
   236 	err = sqlite3OsOpen(vfs, KTestFile1Z, osFile, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
       
   237 	TEST2(err, SQLITE_OK);
       
   238 	//Writing at the beginning of the file
       
   239 	err = sqlite3OsWrite(osFile, "123456", 6, 0);
       
   240 	TEST2(err, SQLITE_OK);
       
   241 	//Verify the written data
       
   242 	char data[20];
       
   243 	err = sqlite3OsRead(osFile, data, 6, 0);
       
   244 	TEST2(err, SQLITE_OK);
       
   245 	err = memcmp(data, "123456", 6);
       
   246 	TEST2(err, 0);
       
   247 	//Writing at beyond the end of the file
       
   248 	err = sqlite3OsWrite(osFile, "abcdefgh", 8, 100);
       
   249 	TEST2(err, SQLITE_OK);
       
   250 	//Verify the written data
       
   251 	err = sqlite3OsRead(osFile, data, 8, 100);
       
   252 	TEST2(err, SQLITE_OK);
       
   253 	err = memcmp(data, "abcdefgh", 8);
       
   254 	TEST2(err, 0);
       
   255 	//Truncate the file
       
   256 	err = sqlite3OsTruncate(osFile, 3);
       
   257 	TEST2(err, SQLITE_OK);
       
   258 	//Write more data
       
   259 	err = sqlite3OsWrite(osFile, "xyz", 3, 3);
       
   260 	TEST2(err, SQLITE_OK);
       
   261 	//Verify the written data
       
   262 	err = sqlite3OsRead(osFile, data, 6, 0);
       
   263 	TEST2(err, SQLITE_OK);
       
   264 	err = memcmp(data, "123xyz", 6);
       
   265 	TEST2(err, 0);
       
   266 	//Check the file size
       
   267 	TInt64 fileSize = 0;
       
   268 	err = sqlite3OsFileSize(osFile, &fileSize);
       
   269 	TEST2(err, SQLITE_OK);
       
   270 	TEST(fileSize == 6);
       
   271 	//FileControl - lock type
       
   272 	int lockType = -1;
       
   273 	err = osFile->pMethods->xFileControl(osFile, SQLITE_FCNTL_LOCKSTATE, &lockType);
       
   274 	TEST2(err, SQLITE_OK);
       
   275 	TEST2(lockType, NO_LOCK);
       
   276 	//FileControl - set callback - NULL callback
       
   277 	err = osFile->pMethods->xFileControl(osFile, KSqlFcntlRegisterFreePageCallback, 0);
       
   278 	TEST2(err, SQLITE_ERROR);
       
   279 	//FileControl - set callback - invalid callback object
       
   280 	TSqlFreePageCallback cbck;
       
   281 	err = osFile->pMethods->xFileControl(osFile, KSqlFcntlRegisterFreePageCallback, &cbck);
       
   282 	TEST2(err, SQLITE_ERROR);
       
   283 	//FileControl - invalid op-code
       
   284 	err = osFile->pMethods->xFileControl(osFile, 90234, 0);
       
   285 	TEST2(err, SQLITE_ERROR);
       
   286 	//Close the file
       
   287 	err = sqlite3OsClose(osFile);
       
   288 	TEST2(err, SQLITE_OK);
       
   289 	//
       
   290 	err = sqlite3OsDelete(vfs, KTestFile1Z, 0);
       
   291 	TEST2(err, SQLITE_OK);
       
   292 	User::Free(osFile);
       
   293 	}
       
   294 
       
   295 //Miscellaneous tests
       
   296 void Test3()
       
   297 	{
       
   298 	sqlite3_vfs* vfs = sqlite3_vfs_find(KSymbianVfsNameZ);
       
   299 	TEST(vfs != NULL);
       
   300 	//Full path name - the full file name is specified
       
   301 	char fname[KMaxFileName];
       
   302 	const char* testFileName1 = "c:\\test\\abv.db";
       
   303 	int err = sqlite3OsFullPathname(vfs, testFileName1, KMaxFileName, fname);
       
   304 	TEST2(err, SQLITE_OK);
       
   305 	err = strncasecmp(fname, testFileName1, strlen(testFileName1));
       
   306 	TEST2(err, 0);
       
   307 	//Full path name - no drive, the full file name is not specified
       
   308 	const char* testFileName2 = "abv.db";
       
   309 	const char* expectedFileName2 = "c:\\private\\21F12127\\abv.db";//"21F12127", the UID of the current test app
       
   310 	err = sqlite3OsFullPathname(vfs, testFileName2, KMaxFileName, fname);
       
   311 	TEST2(err, SQLITE_OK);
       
   312 	err = strncasecmp(fname, expectedFileName2, strlen(expectedFileName2));
       
   313 	TEST2(err, 0);
       
   314 	//Full path name - drive present, the full file name is not specified
       
   315 	const char* testFileName3 = "z:abv.db";
       
   316 	const char* expectedFileName3 = "z:\\private\\21F12127\\abv.db";//"21F12127", the UID of the current test app
       
   317 	err = sqlite3OsFullPathname(vfs, testFileName3, KMaxFileName, fname);
       
   318 	TEST2(err, SQLITE_OK);
       
   319 	err = strncasecmp(fname, expectedFileName3, strlen(expectedFileName3));
       
   320 	TEST2(err, 0);
       
   321 	//Is directory writable - test 1
       
   322 	int res = 0;
       
   323 	err = sqlite3OsAccess(vfs, "c:\\test", SQLITE_ACCESS_READWRITE, &res);
       
   324 	TEST2(err, SQLITE_OK);
       
   325 	TEST2(res, 1);
       
   326 	//Is directory writable - test 2
       
   327 	err = sqlite3OsAccess(vfs, "c:\\test\\", SQLITE_ACCESS_READWRITE, &res);
       
   328 	TEST2(err, SQLITE_OK);
       
   329 	TEST2(res, 1);
       
   330 	//Is directory writable - test 3
       
   331 	//Not a valid test. It is the media that's read only, not the directory.
       
   332 	//err = sqlite3OsAccess(vfs, "z:\\test\\", SQLITE_ACCESS_READWRITE, &res);
       
   333 	//TEST2(err, SQLITE_OK);
       
   334 	//TEST2(res, 0);
       
   335 	//Random seed
       
   336 	const int KBufLen = 100;
       
   337 	char buf[KBufLen];
       
   338 	err = sqlite3OsRandomness(vfs, KBufLen, buf);
       
   339 	TEST2(err, KBufLen);
       
   340 	//Sleep
       
   341 	TUint32 start = User::FastCounter();
       
   342 	const TInt KSleepTime = 200000;//us
       
   343 	(void)sqlite3OsSleep(vfs, KSleepTime);
       
   344 	TUint32 end = User::FastCounter();
       
   345 	TInt ms = CalcMs(start, end);
       
   346 	TheTest.Printf(_L(" === sqlite3OsSleep() time=%d ms\r\n"), ms);
       
   347 	TEST(ms >= 80 && ms <= 320);// -60%..+60%
       
   348 	}
       
   349 
       
   350 //Lock/unlock tests
       
   351 void Test5()
       
   352 	{
       
   353 	sqlite3_vfs* vfs = sqlite3_vfs_find(KSymbianVfsNameZ);
       
   354 	TEST(vfs != NULL);
       
   355 
       
   356   	sqlite3_file* osFile = (sqlite3_file*)User::Alloc(vfs->szOsFile);
       
   357 	TEST(osFile != NULL);
       
   358 	
       
   359 	//Creating a new file
       
   360 	int res = 0;
       
   361 	int err = sqlite3OsAccess(vfs, KTestFile1Z, SQLITE_ACCESS_EXISTS, &res);
       
   362 	TEST2(err, SQLITE_OK);
       
   363 	TEST2(res, 0);
       
   364 	err = sqlite3OsOpen(vfs, KTestFile1Z, osFile, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
       
   365 	TEST2(err, SQLITE_OK);
       
   366 	//Lock/unlock
       
   367 	//SHARED_LOCK
       
   368 	err = sqlite3OsLock(osFile, SHARED_LOCK);
       
   369 	TEST2(err, SQLITE_OK);
       
   370 	err = sqlite3OsCheckReservedLock(osFile, &res);
       
   371 	TEST2(err, SQLITE_OK);
       
   372 	TEST2(res, 0);
       
   373 	//RESERVED_LOCK
       
   374 	err = sqlite3OsLock(osFile, RESERVED_LOCK);
       
   375 	TEST2(err, SQLITE_OK);
       
   376 	err = sqlite3OsCheckReservedLock(osFile, &res);
       
   377 	TEST2(err, SQLITE_OK);
       
   378 	TEST2(res, 1);
       
   379 	//PENDING_LOCK
       
   380 	err = sqlite3OsLock(osFile, PENDING_LOCK);
       
   381 	TEST2(err, SQLITE_OK);
       
   382 	//EXCLUSIVE_LOCK
       
   383 	err = sqlite3OsLock(osFile, EXCLUSIVE_LOCK);
       
   384 	TEST2(err, SQLITE_OK);
       
   385 	//back to SHARED_LOCK
       
   386 	err = sqlite3OsLock(osFile, SHARED_LOCK);
       
   387 	TEST2(err, SQLITE_OK);
       
   388 	//UNLOCK
       
   389 	err = sqlite3OsUnlock(osFile, NO_LOCK);
       
   390 	//Close the file
       
   391 	err = sqlite3OsClose(osFile);
       
   392 	TEST2(err, SQLITE_OK);
       
   393 	//
       
   394 	err = sqlite3OsDelete(vfs, KTestFile1Z, 0);
       
   395 	TEST2(err, SQLITE_OK);
       
   396 	User::Free(osFile);
       
   397 	}
       
   398 
       
   399 //sqlite3SymbianLibInit() - OOM test
       
   400 void sqlite3SymbianLibInitOomTest()
       
   401 	{
       
   402 	sqlite3SymbianLibFinalize();
       
   403 	
       
   404 	TInt failingAllocNum = 0;
       
   405 	TInt err = KErrNoMemory;
       
   406 	while(err == KErrNoMemory)
       
   407 		{
       
   408 		TInt processHandleCnt = 0;
       
   409 		TInt threadHandleCnt = 0;
       
   410 		RThread().HandleCount(processHandleCnt, threadHandleCnt);
       
   411 		TInt allocCellsCnt = User::CountAllocCells();
       
   412 		
       
   413 		__UHEAP_MARK;
       
   414 		
       
   415 		__UHEAP_SETBURSTFAIL(RHeap::EDeterministic, ++failingAllocNum, 10);
       
   416 		
       
   417 		err = sqlite3SymbianLibInit();
       
   418 		
       
   419 		__UHEAP_SETBURSTFAIL(RHeap::ENone, 0, 0);
       
   420 
       
   421 		if(err != KErrNoMemory)
       
   422 			{
       
   423 			TEST2(err, KErrNone);
       
   424 			sqlite3SymbianLibFinalize();
       
   425 			}
       
   426 
       
   427 		__UHEAP_MARKEND;
       
   428 
       
   429 		TInt processHandleCnt2 = 0;
       
   430 		TInt threadHandleCnt2 = 0;
       
   431 		RThread().HandleCount(processHandleCnt2, threadHandleCnt2);
       
   432 		TEST2(processHandleCnt2, processHandleCnt);
       
   433 		TEST2(threadHandleCnt2, threadHandleCnt);
       
   434 
       
   435 		TInt allocCellsCnt2 = User::CountAllocCells();
       
   436 		TEST2(allocCellsCnt2, allocCellsCnt);
       
   437 		}
       
   438 	TEST2(err, KErrNone);
       
   439 	TheTest.Printf(_L("=== sqlite3SymbianLibInit() OOM test succeeded at allcoation %d\r\n"), failingAllocNum);
       
   440 	}
       
   441 
       
   442 //sqlite3SymbianLibInit() - 'File I/O error simulation' test
       
   443 void sqlite3SymbianLibInitFsErrTest()
       
   444 	{
       
   445 	sqlite3SymbianLibFinalize();
       
   446 	
       
   447 	TInt sysDrive = static_cast<TInt>(RFs::GetSystemDrive());
       
   448 	TDriveUnit drvUnit(sysDrive);
       
   449 	TDriveName drvName = drvUnit.Name();
       
   450 	
       
   451 	TFileName path;
       
   452 	TInt err = TheFs.PrivatePath(path);
       
   453 	TEST2(err, KErrNone);
       
   454 	
       
   455 	TParse privDataCage;
       
   456 	err = privDataCage.Set(drvName, &path, 0);
       
   457 	TEST2(err, KErrNone);
       
   458 	
       
   459 	err = KErrNotFound;
       
   460 	TInt cnt = 1;
       
   461 	for(;err<KErrNone;++cnt)
       
   462 		{
       
   463 		for (TInt fsError=KErrNotFound;fsError>=KErrDied;--fsError)
       
   464 			{
       
   465 			(void)TheFs.RmDir(privDataCage.FullName());
       
   466 	
       
   467 			TInt processHandleCnt = 0;
       
   468 			TInt threadHandleCnt = 0;
       
   469 			RThread().HandleCount(processHandleCnt, threadHandleCnt);
       
   470 			TInt allocCellsCnt = User::CountAllocCells();
       
   471 			
       
   472 			(void)TheFs.SetErrorCondition(fsError, cnt);
       
   473 			err = sqlite3SymbianLibInit();
       
   474 			(void)TheFs.SetErrorCondition(KErrNone);
       
   475 			
       
   476 			if(err != KErrNone)
       
   477 				{
       
   478 				TInt processHandleCnt2 = 0;
       
   479 				TInt threadHandleCnt2 = 0;
       
   480 				RThread().HandleCount(processHandleCnt2, threadHandleCnt2);
       
   481 				TEST2(processHandleCnt2, processHandleCnt);
       
   482 				TEST2(threadHandleCnt2, threadHandleCnt);
       
   483 				TInt allocCellsCnt2 = User::CountAllocCells();
       
   484 				TEST2(allocCellsCnt2, allocCellsCnt);
       
   485 				}
       
   486 			else
       
   487 				{
       
   488 				sqlite3SymbianLibFinalize();
       
   489 				}
       
   490 			}
       
   491 		}
       
   492 	sqlite3SymbianLibFinalize();
       
   493 	TheTest.Printf(_L("=== sqlite3SymbianLibInit() 'File I/O error simulation' test succeeded at iteration %d\r\n"), cnt);
       
   494 	}
       
   495 
       
   496 //If _SQLPROFILER macro is not defined then all profiling PS porting layer functions should return KErrNotSupported.
       
   497 void ProfilerDisabledTest()
       
   498 	{
       
   499 #ifndef _SQLPROFILER
       
   500 	TInt err = sqlite3SymbianProfilerStart(0);
       
   501 	TEST2(err, KErrNotSupported);
       
   502 	
       
   503 	err = sqlite3SymbianProfilerStop(0);
       
   504 	TEST2(err, KErrNotSupported);
       
   505 	
       
   506 	err = sqlite3SymbianProfilerReset(0);
       
   507 	TEST2(err, KErrNotSupported);
       
   508 	
       
   509 	TBuf8<1> res;
       
   510 	err = sqlite3SymbianProfilerQuery(0, res);
       
   511 	TEST2(err, KErrNotSupported);
       
   512 	res = res;
       
   513 #else	
       
   514 	TheTest.Printf(_L("  The _SQLPROFILER macro is defined. The Profliling OS porting layer functions are tested in t_sqlapi2\r\n"));
       
   515 #endif
       
   516 	}
       
   517 
       
   518 void NegativeTest()
       
   519 	{
       
   520 	TInt err = sqlite3SymbianLibInit();
       
   521 	TEST2(err, KErrNone);
       
   522 	
       
   523 	sqlite3_vfs* vfs = sqlite3_vfs_find(KSymbianVfsNameZ);
       
   524 	TEST(vfs != NULL);
       
   525 
       
   526   	sqlite3_file* osFile = (sqlite3_file*)User::Alloc(vfs->szOsFile);
       
   527 	TEST(osFile != NULL);
       
   528 		
       
   529 	//Creating a new file - the name is too long
       
   530 	const char* KLongFileNameZ = "c:\\test\\0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789.bin";
       
   531 	int outFlags = 0;
       
   532 	err = sqlite3OsOpen(vfs, KLongFileNameZ, osFile, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, &outFlags);
       
   533 	TEST2(err, SQLITE_CANTOPEN);
       
   534 
       
   535 	//Open a file - the name contains the '|', but not at position 0 (private database - bad name)
       
   536 	const char* KBadFileNameZ = "c:\\test\\01|23456.bin";
       
   537 	err = sqlite3OsOpen(vfs, KBadFileNameZ, osFile, SQLITE_OPEN_READWRITE, &outFlags);
       
   538 	TEST2(err, SQLITE_CANTOPEN);
       
   539 
       
   540 	//FullPathName() - the output buffer is too small
       
   541 	const char* KFileNameZ = "c:\\test\\0123456.bin";
       
   542 	char buf[5];
       
   543 	err = vfs->xFullPathname(vfs, KFileNameZ, 5, buf);
       
   544 	TEST2(err, SQLITE_ERROR);
       
   545 
       
   546 	//FullPathName() - NULL output buffer
       
   547 	err = vfs->xFullPathname(vfs, KFileNameZ, 5, NULL);
       
   548 	TEST2(err, SQLITE_ERROR);
       
   549 
       
   550 	//FullPathName() - NULL file name
       
   551 	err = vfs->xFullPathname(vfs, NULL, 5, buf);
       
   552 	TEST2(err, SQLITE_ERROR);
       
   553 
       
   554 	//FullPathName() - the file name is too long
       
   555 	err = vfs->xFullPathname(vfs, KLongFileNameZ, 5, buf);
       
   556 	TEST2(err, SQLITE_ERROR);
       
   557 
       
   558 	//FullPathName() - NULL file name
       
   559 	err = vfs->xFullPathname(vfs, NULL, 5, buf);
       
   560 	TEST2(err, SQLITE_ERROR);
       
   561 
       
   562 	//Dellete file - NULL file name
       
   563 	err = vfs->xDelete(vfs, 0, 0);
       
   564 	TEST2(err, SQLITE_ERROR);
       
   565 
       
   566 	//Delete file - the output buffer for the unicode file name is too small	
       
   567 	err = vfs->xDelete(vfs, KLongFileNameZ, 0);
       
   568 	TEST2(err, SQLITE_ERROR);
       
   569 
       
   570 	//Open file - NULL file name - this is a temp file name
       
   571 	err = sqlite3OsOpen(vfs, NULL, osFile, SQLITE_OPEN_READWRITE, &outFlags);
       
   572 	TEST2(err, SQLITE_OK);
       
   573 	err = osFile->pMethods->xClose(osFile);
       
   574 	TEST2(err, SQLITE_OK);
       
   575 	
       
   576 	//xAccess - too long file name
       
   577 	int res = -1;
       
   578 	err = vfs->xAccess(vfs, KLongFileNameZ, SQLITE_ACCESS_EXISTS, &res);
       
   579 	TEST2(err, SQLITE_IOERR_ACCESS);
       
   580 
       
   581 	//xAccess - NULL file name
       
   582 	err = vfs->xAccess(vfs, 0, SQLITE_ACCESS_EXISTS, &res);
       
   583 	TEST2(err, SQLITE_IOERR_ACCESS);
       
   584 
       
   585 	User::Free(osFile);
       
   586 	}
       
   587 
       
   588 /**
       
   589 @SYMTestCaseID			SYSLIB-SQL-CT-1650
       
   590 @SYMTestCaseDesc		SQL, OS porting layer tests.
       
   591 @SYMTestPriority		High
       
   592 @SYMTestActions			SQL, OS porting layer tests.
       
   593 @SYMTestExpectedResults Test must not fail
       
   594 @SYMREQ					REQ5792
       
   595                         REQ5793
       
   596 */	
       
   597 void DoTests()
       
   598 	{
       
   599 	TheTest.Printf(_L("OS porting layer test - create/open/close/delete a file\r\n"));
       
   600 	Test1();
       
   601 	TheTest.Printf(_L("OS porting layer test - read/write/seek/truncate\r\n"));
       
   602 	Test2();
       
   603 	TheTest.Printf(_L("OS porting layer test - miscellaneous tests\r\n"));
       
   604 	Test3();
       
   605 	TheTest.Printf(_L("OS porting layer test - lock/unlock\r\n"));
       
   606 	Test5();
       
   607 	TheTest.Printf(_L("OS porting layer test - sqlite3SymbianLibInit() - OOM test\r\n"));
       
   608 	sqlite3SymbianLibInitOomTest();
       
   609 	TheTest.Printf(_L("OS porting layer test - sqlite3SymbianLibInit() - 'File I/O error simulation' test\r\n"));
       
   610 	sqlite3SymbianLibInitFsErrTest();
       
   611 	TheTest.Printf(_L("OS porting layer test - 'profiler disabled' tests\r\n"));
       
   612 	ProfilerDisabledTest();
       
   613 	TheTest.Printf(_L("OS porting layer test - negative tests\r\n"));
       
   614 	NegativeTest();
       
   615 	}
       
   616 
       
   617 TInt E32Main()
       
   618 	{
       
   619 	TheTest.Title();
       
   620 	
       
   621 	CTrapCleanup* tc = CTrapCleanup::New();
       
   622 	
       
   623 	__UHEAP_MARK;
       
   624 	
       
   625 	TestEnvInit();
       
   626 	DeleteTestFiles();
       
   627 	TheTest.Start(_L(" @SYMTestCaseID:SYSLIB-SQL-CT-1650 OS porting layer tests"));
       
   628 	DoTests();
       
   629 	TestEnvDestroy();
       
   630 
       
   631 	__UHEAP_MARKEND;
       
   632 	
       
   633 	TheTest.End();
       
   634 	TheTest.Close();
       
   635 	
       
   636 	delete tc;
       
   637 
       
   638 	User::Heap().Check();
       
   639 	return KErrNone;
       
   640 	}