persistentstorage/sql/TEST/t_sqloomutil.cpp
changeset 0 08ec8eefde2f
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     1 // Copyright (c) 2005-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 "t_sqloom.h"
       
    17 #include "SqlResourceTester.h"		//TSqlResourceTester
       
    18 
       
    19 
       
    20 TInt TheHandleCount1B = 0;
       
    21 TInt TheHandleCount2B = 0;
       
    22 TInt TheAllocatedCellsCountB = 0;
       
    23 TOomTestType TheOomTestType[2] = {EClientSideTest, EServerSideTest};
       
    24 
       
    25 ///////////////////////////////////////////////////////////////////////////////////////
       
    26 ///////////////         OOM tests - utility functions        //////////////////////////
       
    27 ///////////////////////////////////////////////////////////////////////////////////////
       
    28 
       
    29 void DeleteTestFiles()
       
    30 	{
       
    31 	(void)RSqlDatabase::Delete(KSecureAttachDb);
       
    32 	(void)RSqlDatabase::Delete(KAttachDb);
       
    33 	(void)RSqlDatabase::Delete(KSecureTestDb);
       
    34 	(void)RSqlDatabase::Delete(KTestDb);
       
    35 	(void)RSqlDatabase::Delete(KSecureDb2);
       
    36 	(void)RSqlDatabase::Delete(KTestDb2);
       
    37 	(void)RSqlDatabase::Delete(KPrivateTestDb);
       
    38 	}
       
    39 
       
    40 void Check(TInt64 aValue, TInt aLine)
       
    41 	{
       
    42 	if(!aValue)
       
    43 		{
       
    44 		DeleteTestFiles();
       
    45 		TheTest(EFalse, aLine);
       
    46 		}
       
    47 	}
       
    48 	
       
    49 void Check(TInt64 aValue, TInt64 aExpected, TInt aLine)
       
    50 	{
       
    51 	if(aValue != aExpected)
       
    52 		{
       
    53 		DeleteTestFiles();
       
    54 		RDebug::Print(_L("*** Expected error: %ld, got: %ld\r\n"), aExpected, aValue);
       
    55 		TheTest(EFalse, aLine);
       
    56 		}
       
    57 	}
       
    58 
       
    59 void CreateTestDir()
       
    60     {
       
    61     RFs fs;
       
    62 	TInt err = fs.Connect();
       
    63 	TEST2(err, KErrNone);
       
    64 
       
    65 	err = fs.MkDir(KTestDir);
       
    66 	TEST(err == KErrNone || err == KErrAlreadyExists);
       
    67 
       
    68 	err = fs.CreatePrivatePath(EDriveC);
       
    69 	TEST(err == KErrNone || err == KErrAlreadyExists);
       
    70 
       
    71 	fs.Close();
       
    72 	}
       
    73 
       
    74 void PrepareAttachFromHandle()
       
    75 	{
       
    76 	(void)RSqlDatabase::Delete(KAttachDb);
       
    77 	(void)RSqlDatabase::Delete(KPrivateTestDb);
       
    78 
       
    79 	RSqlDatabase db;
       
    80 	TInt err = db.Create(KAttachDb);
       
    81 	db.Close();
       
    82 	TEST2(err, KErrNone);
       
    83 
       
    84     RFs fs;
       
    85 	err = fs.Connect();
       
    86 	TEST2(err, KErrNone);
       
    87 
       
    88 	err = BaflUtils::CopyFile(fs, KAttachDb, KPrivateTestDb);
       
    89 	TEST2(err, KErrNone);
       
    90 	}
       
    91 
       
    92 void MarkHandles()
       
    93 	{
       
    94 	RThread().HandleCount(TheHandleCount1B, TheHandleCount2B);
       
    95 	}
       
    96 
       
    97 void CheckHandles()
       
    98 	{
       
    99 	TInt endHandleCount1E;
       
   100 	TInt endHandleCount2E;
       
   101 
       
   102 	RThread().HandleCount(endHandleCount1E, endHandleCount2E);
       
   103 
       
   104 	TEST(TheHandleCount1B == endHandleCount1E);
       
   105 	TEST(TheHandleCount2B == endHandleCount2E);
       
   106 	}
       
   107 
       
   108 void MarkAllocatedCells()
       
   109 	{
       
   110 	TheAllocatedCellsCountB = User::CountAllocCells();
       
   111 	}
       
   112 
       
   113 void CheckAllocatedCells()
       
   114 	{
       
   115 	TInt allocatedCellsCountE = User::CountAllocCells();
       
   116 	TEST(allocatedCellsCountE == TheAllocatedCellsCountB);
       
   117 	}
       
   118 
       
   119 void PrintEndOfOomTest(TOomTestType aOomTestType, TInt aFailingAllocationNo)
       
   120 	{
       
   121 	_LIT(KClientSide, "Client side");
       
   122 	_LIT(KServerSide, "Server side");
       
   123 	RDebug::Print(_L("=== %S OOM Test succeeded at heap failure rate of %d ===\r\n"),
       
   124 						aOomTestType == EClientSideTest ? &KClientSide() : &KServerSide(),
       
   125 						aFailingAllocationNo);
       
   126 	}
       
   127 
       
   128 //If aDelayed is true, then the SQL server will delay the heap failure simulation until the database is opened.
       
   129 void SetDbHeapFailure(TOomTestType aOomTestType, TInt aFailingAllocationNo, TBool aDelayed)
       
   130 	{
       
   131 	const TInt KDelayedDbHeapFailureMask = 0x1000;
       
   132 	if(aOomTestType == EClientSideTest)
       
   133 		{
       
   134 		__UHEAP_SETFAIL(RHeap::EDeterministic, aFailingAllocationNo);
       
   135 		}
       
   136 	else
       
   137 		{
       
   138 		TSqlResourceTester::SetDbHeapFailure(RHeap::EDeterministic | (aDelayed ? KDelayedDbHeapFailureMask : 0), aFailingAllocationNo);
       
   139 		}
       
   140 	}
       
   141 
       
   142 void ResetDbHeapFailure(TOomTestType aOomTestType)
       
   143 	{
       
   144 	if(aOomTestType == EClientSideTest)
       
   145 		{
       
   146 		__UHEAP_SETFAIL(RHeap::ENone, 0);
       
   147 		}
       
   148 	else
       
   149 		{
       
   150 		TSqlResourceTester::SetDbHeapFailure(RHeap::ENone, 0);
       
   151 		}
       
   152 	}
       
   153 
       
   154 void SetHeapFailure(TOomTestType aOomTestType, TInt aFailingAllocationNo)
       
   155 	{
       
   156 	if(aOomTestType == EClientSideTest)
       
   157 		{
       
   158 		__UHEAP_SETFAIL(RHeap::EDeterministic, aFailingAllocationNo);
       
   159 		}
       
   160 	else
       
   161 		{
       
   162 		TSqlResourceTester::Mark();
       
   163 		TSqlResourceTester::SetHeapFailure(RHeap::EDeterministic, aFailingAllocationNo);
       
   164 		}
       
   165 	}
       
   166 
       
   167 void ResetHeapFailure(TOomTestType aOomTestType)
       
   168 	{
       
   169 	if(aOomTestType == EClientSideTest)
       
   170 		{
       
   171 		__UHEAP_SETFAIL(RHeap::ENone, 0);
       
   172 		}
       
   173 	else
       
   174 		{
       
   175 		TSqlResourceTester::SetHeapFailure(RHeap::ENone, 0);
       
   176 		TSqlResourceTester::Check();
       
   177 		}
       
   178 	}
       
   179 
       
   180 void CreateTestSecurityPolicy(RSqlSecurityPolicy& aSecurityPolicy)
       
   181 	{
       
   182 	TInt err = aSecurityPolicy.Create(KDefaultPolicy);
       
   183 	TEST2(err, KErrNone);
       
   184 
       
   185 	err = aSecurityPolicy.SetDbPolicy(RSqlSecurityPolicy::ESchemaPolicy, KPolicy1);
       
   186 	TEST2(err, KErrNone);
       
   187 	err = aSecurityPolicy.SetDbPolicy(RSqlSecurityPolicy::EWritePolicy, KPolicy3);
       
   188 	TEST2(err, KErrNone);
       
   189 
       
   190 	err = aSecurityPolicy.SetPolicy(RSqlSecurityPolicy::ETable, KTableName1, RSqlSecurityPolicy::EReadPolicy, KPolicy1);
       
   191 	TEST2(err, KErrNone);
       
   192 	err = aSecurityPolicy.SetPolicy(RSqlSecurityPolicy::ETable, KTableName1, RSqlSecurityPolicy::EWritePolicy, KPolicy2);
       
   193 	TEST2(err, KErrNone);
       
   194 
       
   195 	err = aSecurityPolicy.SetPolicy(RSqlSecurityPolicy::ETable, KTableName2, RSqlSecurityPolicy::EReadPolicy, KPolicy1);
       
   196 	TEST2(err, KErrNone);
       
   197 	err = aSecurityPolicy.SetPolicy(RSqlSecurityPolicy::ETable, KTableName3, RSqlSecurityPolicy::EReadPolicy, KPolicy2);
       
   198 	TEST2(err, KErrNone);
       
   199 
       
   200 	err = aSecurityPolicy.SetPolicy(RSqlSecurityPolicy::ETable, KTableName2, RSqlSecurityPolicy::EWritePolicy, KPolicy1);
       
   201 	TEST2(err, KErrNone);
       
   202 
       
   203 	err = aSecurityPolicy.SetPolicy(RSqlSecurityPolicy::ETable, KTableName3, RSqlSecurityPolicy::EWritePolicy, KPolicy3);
       
   204 	TEST2(err, KErrNone);
       
   205 	}
       
   206 
       
   207 void CreateAttachDb()
       
   208 	{
       
   209 	RSqlDatabase db;
       
   210 
       
   211 	TInt err = db.Create(KAttachDb);
       
   212 	TEST2(err, KErrNone);
       
   213 	err = db.Exec(_L("CREATE TABLE Aab(F1 INTEGER, F2 TEXT)"));
       
   214 	TEST(err >= 0);
       
   215 	db.Close();
       
   216 
       
   217 	RSqlSecurityPolicy securityPolicy;
       
   218 	CreateTestSecurityPolicy(securityPolicy);
       
   219 
       
   220 	err = db.Create(KSecureAttachDb, securityPolicy);
       
   221 	TEST2(err, KErrNone);
       
   222 	err = db.Exec(_L("CREATE TABLE Aab(F1 INTEGER, F2 TEXT)"));
       
   223 	TEST(err >= 0);
       
   224 	db.Close();
       
   225 
       
   226 	securityPolicy.Close();
       
   227 	}