Update contrib.
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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
17 #include "SqlResourceTester.h" //TSqlResourceTester
20 TInt TheHandleCount1B = 0;
21 TInt TheHandleCount2B = 0;
22 TInt TheAllocatedCellsCountB = 0;
23 TOomTestType TheOomTestType[2] = {EClientSideTest, EServerSideTest};
25 ///////////////////////////////////////////////////////////////////////////////////////
26 /////////////// OOM tests - utility functions //////////////////////////
27 ///////////////////////////////////////////////////////////////////////////////////////
29 void DeleteTestFiles()
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);
40 void Check(TInt64 aValue, TInt aLine)
45 TheTest(EFalse, aLine);
49 void Check(TInt64 aValue, TInt64 aExpected, TInt aLine)
51 if(aValue != aExpected)
54 RDebug::Print(_L("*** Expected error: %ld, got: %ld\r\n"), aExpected, aValue);
55 TheTest(EFalse, aLine);
62 TInt err = fs.Connect();
65 err = fs.MkDir(KTestDir);
66 TEST(err == KErrNone || err == KErrAlreadyExists);
68 err = fs.CreatePrivatePath(EDriveC);
69 TEST(err == KErrNone || err == KErrAlreadyExists);
74 void PrepareAttachFromHandle()
76 (void)RSqlDatabase::Delete(KAttachDb);
77 (void)RSqlDatabase::Delete(KPrivateTestDb);
80 TInt err = db.Create(KAttachDb);
88 err = BaflUtils::CopyFile(fs, KAttachDb, KPrivateTestDb);
94 RThread().HandleCount(TheHandleCount1B, TheHandleCount2B);
99 TInt endHandleCount1E;
100 TInt endHandleCount2E;
102 RThread().HandleCount(endHandleCount1E, endHandleCount2E);
104 TEST(TheHandleCount1B == endHandleCount1E);
105 TEST(TheHandleCount2B == endHandleCount2E);
108 void MarkAllocatedCells()
110 TheAllocatedCellsCountB = User::CountAllocCells();
113 void CheckAllocatedCells()
115 TInt allocatedCellsCountE = User::CountAllocCells();
116 TEST(allocatedCellsCountE == TheAllocatedCellsCountB);
119 void PrintEndOfOomTest(TOomTestType aOomTestType, TInt aFailingAllocationNo)
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);
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)
131 const TInt KDelayedDbHeapFailureMask = 0x1000;
132 if(aOomTestType == EClientSideTest)
134 __UHEAP_SETFAIL(RHeap::EDeterministic, aFailingAllocationNo);
138 TSqlResourceTester::SetDbHeapFailure(RHeap::EDeterministic | (aDelayed ? KDelayedDbHeapFailureMask : 0), aFailingAllocationNo);
142 void ResetDbHeapFailure(TOomTestType aOomTestType)
144 if(aOomTestType == EClientSideTest)
146 __UHEAP_SETFAIL(RHeap::ENone, 0);
150 TSqlResourceTester::SetDbHeapFailure(RHeap::ENone, 0);
154 void SetHeapFailure(TOomTestType aOomTestType, TInt aFailingAllocationNo)
156 if(aOomTestType == EClientSideTest)
158 __UHEAP_SETFAIL(RHeap::EDeterministic, aFailingAllocationNo);
162 TSqlResourceTester::Mark();
163 TSqlResourceTester::SetHeapFailure(RHeap::EDeterministic, aFailingAllocationNo);
167 void ResetHeapFailure(TOomTestType aOomTestType)
169 if(aOomTestType == EClientSideTest)
171 __UHEAP_SETFAIL(RHeap::ENone, 0);
175 TSqlResourceTester::SetHeapFailure(RHeap::ENone, 0);
176 TSqlResourceTester::Check();
180 void CreateTestSecurityPolicy(RSqlSecurityPolicy& aSecurityPolicy)
182 TInt err = aSecurityPolicy.Create(KDefaultPolicy);
183 TEST2(err, KErrNone);
185 err = aSecurityPolicy.SetDbPolicy(RSqlSecurityPolicy::ESchemaPolicy, KPolicy1);
186 TEST2(err, KErrNone);
187 err = aSecurityPolicy.SetDbPolicy(RSqlSecurityPolicy::EWritePolicy, KPolicy3);
188 TEST2(err, KErrNone);
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);
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);
200 err = aSecurityPolicy.SetPolicy(RSqlSecurityPolicy::ETable, KTableName2, RSqlSecurityPolicy::EWritePolicy, KPolicy1);
201 TEST2(err, KErrNone);
203 err = aSecurityPolicy.SetPolicy(RSqlSecurityPolicy::ETable, KTableName3, RSqlSecurityPolicy::EWritePolicy, KPolicy3);
204 TEST2(err, KErrNone);
207 void CreateAttachDb()
211 TInt err = db.Create(KAttachDb);
212 TEST2(err, KErrNone);
213 err = db.Exec(_L("CREATE TABLE Aab(F1 INTEGER, F2 TEXT)"));
217 RSqlSecurityPolicy securityPolicy;
218 CreateTestSecurityPolicy(securityPolicy);
220 err = db.Create(KSecureAttachDb, securityPolicy);
221 TEST2(err, KErrNone);
222 err = db.Exec(_L("CREATE TABLE Aab(F1 INTEGER, F2 TEXT)"));
226 securityPolicy.Close();