persistentstorage/sql/TEST/t_sqloslayer.cpp
branchRCL_3
changeset 21 28839de615b4
parent 8 fa9941cf3867
child 23 26645d81f48d
equal deleted inserted replaced
18:3da531bb4329 21:28839de615b4
     1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
     1 // Copyright (c) 2006-2010 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     3 // This component and the accompanying materials are made available
     4 // under the terms of "Eclipse Public License v1.0"
     4 // under the terms of "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
   621 	TEST2(err, SQLITE_IOERR_ACCESS);
   621 	TEST2(err, SQLITE_IOERR_ACCESS);
   622 
   622 
   623 	User::Free(osFile);
   623 	User::Free(osFile);
   624 	}
   624 	}
   625 
   625 
       
   626 TInt DoDeleteTempFiles()
       
   627 	{
       
   628     CFileMan* fm = NULL;
       
   629     TRAPD(err, fm = CFileMan::NewL(TheFs));
       
   630     TEST2(err, KErrNone);
       
   631     TFileName path;
       
   632     path.Copy(KPrivateDir);
       
   633     path.Append(_L("temp\\"));
       
   634     path.Append(_L("*.$$$"));
       
   635     err = fm->Delete(path);
       
   636     delete fm;
       
   637     return err;
       
   638 	}
       
   639 
   626 void VfsOpenTempFileOomTest()
   640 void VfsOpenTempFileOomTest()
   627     {
   641     {
   628     //Delete all temp files in this test private data cage.
   642     //Delete all temp files in this test private data cage.
   629     CFileMan* fm = NULL;
   643 	TInt err = DoDeleteTempFiles();
   630     TRAPD(err, fm = CFileMan::NewL(TheFs));
       
   631     TEST2(err, KErrNone);
       
   632     TBuf<50> path;
       
   633     path.Copy(KPrivateDir);
       
   634     path.Append(_L("*.$$$"));
       
   635     err = fm->Delete(path);
       
   636     TEST(err == KErrNone || err == KErrNotFound);
   644     TEST(err == KErrNone || err == KErrNotFound);
   637     
   645     
   638     sqlite3_vfs* vfs = sqlite3_vfs_find(NULL);
   646     sqlite3_vfs* vfs = sqlite3_vfs_find(NULL);
   639     TEST(vfs != NULL);
   647     TEST(vfs != NULL);
   640 
   648 
   651         OomPreStep(failingAllocNum);
   659         OomPreStep(failingAllocNum);
   652         int outFlags = 0;
   660         int outFlags = 0;
   653         err = sqlite3OsOpen(vfs, NULL, osFile, SQLITE_OPEN_READWRITE, &outFlags);
   661         err = sqlite3OsOpen(vfs, NULL, osFile, SQLITE_OPEN_READWRITE, &outFlags);
   654         if(err == SQLITE_OK)
   662         if(err == SQLITE_OK)
   655             {
   663             {
   656             err = sqlite3OsClose(osFile);
   664 			//Since this is a temp file, its creation will be delayed till the first file write operation.
       
   665 			err = sqlite3OsWrite(osFile, "1234", 4, 0);
       
   666 			(void)sqlite3OsClose(osFile);
   657             }
   667             }
   658         OomPostStep();
   668         OomPostStep();
   659         if(err != SQLITE_OK)
   669         if(err != SQLITE_OK)
   660             {
   670             {
   661             TEST2(err, SQLITE_IOERR_NOMEM);
   671             TEST2(err, SQLITE_IOERR_NOMEM);
   662             }
   672             }
   663         //If the iteration has failed, then no temp file should exist in the test private data cage.
   673         //If the iteration has failed, then no temp file should exist in the test private data cage.
   664         //If the iteration has succeeded, then sqlite3OsClose() should have deleted the temp file.
   674         //If the iteration has succeeded, then sqlite3OsClose() should have deleted the temp file.
   665         TInt err2 = fm->Delete(path);
   675         TInt err2 = DoDeleteTempFiles();
   666         TEST2(err2, KErrNotFound);
   676         TEST2(err2, KErrNotFound);
   667         }
   677         }
   668     TEST2(err, SQLITE_OK);
   678     TEST2(err, SQLITE_OK);
   669     TheTest.Printf(_L("\r\n=== TVfs::Open(<temp file>) OOM test succeeded at allcoation %d\r\n"), failingAllocNum);
   679     TheTest.Printf(_L("\r\n=== TVfs::Open(<temp file>) OOM test succeeded at allcoation %d\r\n"), failingAllocNum);
   670     
   680     
   671     User::Free(osFile);
   681     User::Free(osFile);
   672     delete fm;
       
   673     }
   682     }
       
   683 
       
   684 void VfsOpenTempFileFileIoErrTest()
       
   685 	{
       
   686     //Delete all temp files in this test private data cage.
       
   687 	TInt err = DoDeleteTempFiles();
       
   688     TEST(err == KErrNone || err == KErrNotFound);
       
   689     
       
   690     sqlite3_vfs* vfs = sqlite3_vfs_find(NULL);
       
   691     TEST(vfs != NULL);
       
   692 
       
   693     sqlite3_file* osFile = (sqlite3_file*)User::Alloc(vfs->szOsFile);
       
   694     TEST(osFile != NULL);
       
   695     
       
   696 	err = SQLITE_ERROR;
       
   697 	TInt cnt = 1;
       
   698 	while(err != SQLITE_OK)
       
   699 		{
       
   700 		TInt processHandleCnt = 0;
       
   701 		TInt threadHandleCnt = 0;
       
   702 		RThread().HandleCount(processHandleCnt, threadHandleCnt);
       
   703 		TInt allocCellsCnt = User::CountAllocCells();
       
   704 
       
   705         TheTest.Printf(_L("%d "), cnt);
       
   706 		(void)TheFs.SetErrorCondition(KErrGeneral, cnt);
       
   707         int outFlags = 0;
       
   708         err = sqlite3OsOpen(vfs, NULL, osFile, SQLITE_OPEN_READWRITE, &outFlags);
       
   709         if(err == SQLITE_OK)
       
   710             {
       
   711 			//Since this is a temp file, its creation will be delayed till the first file write operation.
       
   712 			err = sqlite3OsWrite(osFile, "1234", 4, 0);
       
   713 			(void)sqlite3OsClose(osFile);
       
   714             }
       
   715 		(void)TheFs.SetErrorCondition(KErrNone);
       
   716 		if(err != SQLITE_OK)
       
   717 			{
       
   718 			TInt processHandleCnt2 = 0;
       
   719 			TInt threadHandleCnt2 = 0;
       
   720 			RThread().HandleCount(processHandleCnt2, threadHandleCnt2);
       
   721 			TEST2(processHandleCnt2, processHandleCnt);
       
   722 			TEST2(threadHandleCnt2, threadHandleCnt);
       
   723 			TInt allocCellsCnt2 = User::CountAllocCells();
       
   724 			TEST2(allocCellsCnt2, allocCellsCnt);
       
   725 	        ++cnt;
       
   726 			}
       
   727         //If the iteration has failed, then no temp file should exist in the test private data cage.
       
   728         //If the iteration has succeeded, then sqlite3OsClose() should have deleted the temp file.
       
   729         TInt err2 = DoDeleteTempFiles();
       
   730         TEST2(err2, KErrNotFound);
       
   731 		}
       
   732     TEST2(err, SQLITE_OK);
       
   733     TheTest.Printf(_L("\r\n=== TVfs::Open(<temp file>) file I/O error simulation test succeeded at iteration %d\r\n"), cnt);
       
   734     User::Free(osFile);
       
   735 	}
   674 
   736 
   675 void VfsCreateDeleteOnCloseFileOomTest()
   737 void VfsCreateDeleteOnCloseFileOomTest()
   676     {
   738     {
   677     sqlite3_vfs* vfs = sqlite3_vfs_find(NULL);
   739     sqlite3_vfs* vfs = sqlite3_vfs_find(NULL);
   678     TEST(vfs != NULL);
   740     TEST(vfs != NULL);
   738 	ProfilerDisabledTest();
   800 	ProfilerDisabledTest();
   739 	TheTest.Printf(_L("OS porting layer test - negative tests\r\n"));
   801 	TheTest.Printf(_L("OS porting layer test - negative tests\r\n"));
   740 	NegativeTest();
   802 	NegativeTest();
   741     TheTest.Printf(_L("TVfs::Open(<temp file>) OOM test\r\n"));
   803     TheTest.Printf(_L("TVfs::Open(<temp file>) OOM test\r\n"));
   742     VfsOpenTempFileOomTest();
   804     VfsOpenTempFileOomTest();
       
   805     TheTest.Printf(_L("TVfs::Open(<temp file>) file I/O error simulation test\r\n"));
       
   806     VfsOpenTempFileFileIoErrTest();
   743     TheTest.Printf(_L("TVfs::Open(<'delete on close' file>) OOM test\r\n"));
   807     TheTest.Printf(_L("TVfs::Open(<'delete on close' file>) OOM test\r\n"));
   744     VfsCreateDeleteOnCloseFileOomTest();
   808     VfsCreateDeleteOnCloseFileOomTest();
   745 	}
   809 	}
   746 
   810 
   747 TInt E32Main()
   811 TInt E32Main()