Update contrib.
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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
21 ///////////////////////////////////////////////////////////////////////////////////////
23 #define UNUSED_VAR(a) (a) = (a)
25 RTest TheTest(_L("t_sqlsecurity1 test"));
27 _LIT(KTestDir, "c:\\test\\");
28 _LIT(KTestDbName, "c:[98765432]t_sqlsecurity1_1.db");
29 _LIT(KTestDbName2, "c:\\test\\t_sqlsecurity1_2.db");
30 _LIT(KTestDbName3, "c:[98765432]t_sqlsecurity1_3.db");
31 _LIT(KTestDbName4, "c:\\private\\98765432\\t_sqlsecurity1_4.db");
32 _LIT(KTestDbName5, "|00110022|33005500|c:");
33 _LIT(KTestFile, "c:\\test\\policy.strm");
35 _LIT(KDb1, "C:[98765432]t_nosettings.db");
36 _LIT(KDb2, "C:[98765432]t_emptysettings.db");
37 _LIT(KDb3, "C:[98765432]t_invversion.db");
38 _LIT(KDb4, "C:[98765432]t_nocollation.db");
39 _LIT(KDb5, "C:[98765432]t_longcollation.db");
40 _LIT(KDb6, "C:[98765432]t_nodefaultpolicy.db");
41 _LIT(KDb7, "C:[98765432]t_invobject.db");
42 _LIT(KDb8, "C:[98765432]t_2defaultpolicies.db");
44 const TSecurityPolicy KDefaultPolicy(ECapabilityDRM);
45 const TSecurityPolicy KPolicy1(ECapabilityTrustedUI, ECapabilityDiskAdmin);
46 const TSecurityPolicy KPolicy2(ECapabilityNetworkControl, ECapabilityPowerMgmt, ECapabilityCommDD);
47 const TSecurityPolicy KPolicy3(ECapabilityTCB, ECapabilityWriteUserData, ECapabilityUserEnvironment);
49 _LIT(KTableName1, "A-tbl");
50 _LIT(KTableName2, "B-table");
51 _LIT(KTableName3, "T-CCCCCCCCCCCCCC");
52 _LIT(KTableName11, "A-TBL");
56 ///////////////////////////////////////////////////////////////////////////////////////
58 void DeleteTestFiles()
60 (void)TheFs.Delete(KTestFile);
61 (void)RSqlDatabase::Delete(KTestDbName4);
62 (void)RSqlDatabase::Delete(KTestDbName3);
63 (void)RSqlDatabase::Delete(KTestDbName2);
64 (void)RSqlDatabase::Delete(KTestDbName);
67 ///////////////////////////////////////////////////////////////////////////////////////
68 ///////////////////////////////////////////////////////////////////////////////////////
69 //Test macros and functions
70 void Check1(TInt aValue, TInt aLine, TBool aPrintThreadName = EFalse)
78 TName name = th.Name();
79 RDebug::Print(_L("*** Thread %S, Line %d\r\n"), &name, aLine);
83 RDebug::Print(_L("*** Line %d\r\n"), aLine);
85 TheTest(EFalse, aLine);
88 void Check2(TInt aValue, TInt aExpected, TInt aLine, TBool aPrintThreadName = EFalse)
90 if(aValue != aExpected)
96 TName name = th.Name();
97 RDebug::Print(_L("*** Thread %S, Line %d Expected error: %d, got: %d\r\n"), &name, aLine, aExpected, aValue);
101 RDebug::Print(_L("*** Line %d, Expected error: %d, got: %d\r\n"), aLine, aExpected, aValue);
103 TheTest(EFalse, aLine);
106 #define TEST(arg) ::Check1((arg), __LINE__)
107 #define TEST2(aValue, aExpected) ::Check2(aValue, aExpected, __LINE__)
108 #define TTEST(arg) ::Check1((arg), __LINE__, ETrue)
109 #define TTEST2(aValue, aExpected) ::Check2(aValue, aExpected, __LINE__, ETrue)
111 ///////////////////////////////////////////////////////////////////////////////////////
115 TInt err = TheFs.Connect();
116 TEST2(err, KErrNone);
118 err = TheFs.MkDir(KTestDir);
119 TEST(err == KErrNone || err == KErrAlreadyExists);
121 err = TheFs.CreatePrivatePath(EDriveC);
122 TEST(err == KErrNone || err == KErrAlreadyExists);
125 ///////////////////////////////////////////////////////////////////////////////////////
127 TBool operator==(const TSecurityPolicy& aLeft, const TSecurityPolicy& aRight)
129 return aLeft.Package() == aRight.Package();
132 void CreateTestSecurityPolicy(RSqlSecurityPolicy& aSecurityPolicy)
134 TInt err = aSecurityPolicy.Create(KDefaultPolicy);
135 TEST2(err, KErrNone);
137 err = aSecurityPolicy.SetDbPolicy(RSqlSecurityPolicy::ESchemaPolicy, KPolicy1);
138 TEST2(err, KErrNone);
139 err = aSecurityPolicy.SetDbPolicy(RSqlSecurityPolicy::EWritePolicy, KPolicy3);
140 TEST2(err, KErrNone);
142 err = aSecurityPolicy.SetPolicy(RSqlSecurityPolicy::ETable, KTableName1, RSqlSecurityPolicy::EReadPolicy, KPolicy1);
143 TEST2(err, KErrNone);
144 err = aSecurityPolicy.SetPolicy(RSqlSecurityPolicy::ETable, KTableName1, RSqlSecurityPolicy::EWritePolicy, KPolicy2);
145 TEST2(err, KErrNone);
147 err = aSecurityPolicy.SetPolicy(RSqlSecurityPolicy::ETable, KTableName2, RSqlSecurityPolicy::EReadPolicy, KPolicy1);
148 TEST2(err, KErrNone);
149 err = aSecurityPolicy.SetPolicy(RSqlSecurityPolicy::ETable, KTableName3, RSqlSecurityPolicy::EReadPolicy, KPolicy2);
150 TEST2(err, KErrNone);
152 err = aSecurityPolicy.SetPolicy(RSqlSecurityPolicy::ETable, KTableName2, RSqlSecurityPolicy::EWritePolicy, KPolicy1);
153 TEST2(err, KErrNone);
155 err = aSecurityPolicy.SetPolicy(RSqlSecurityPolicy::ETable, KTableName3, RSqlSecurityPolicy::EWritePolicy, KPolicy3);
156 TEST2(err, KErrNone);
159 void CheckTestSecurityPolicy(const RSqlSecurityPolicy& aSecurityPolicy)
162 TSecurityPolicy policy = aSecurityPolicy.DbPolicy(RSqlSecurityPolicy::ESchemaPolicy);
163 TEST(policy == KPolicy1);
164 policy = aSecurityPolicy.DbPolicy(RSqlSecurityPolicy::EReadPolicy);
165 TEST(policy == KDefaultPolicy);
166 policy = aSecurityPolicy.DbPolicy(RSqlSecurityPolicy::EWritePolicy);
167 TEST(policy == KPolicy3);
169 policy = aSecurityPolicy.Policy(RSqlSecurityPolicy::ETable, KTableName11, RSqlSecurityPolicy::EReadPolicy);
170 TEST(policy == KPolicy1);
171 policy = aSecurityPolicy.Policy(RSqlSecurityPolicy::ETable, KTableName2, RSqlSecurityPolicy::EReadPolicy);
172 TEST(policy == KPolicy1);
173 policy = aSecurityPolicy.Policy(RSqlSecurityPolicy::ETable, KTableName3, RSqlSecurityPolicy::EReadPolicy);
174 TEST(policy == KPolicy2);
175 policy = aSecurityPolicy.Policy(RSqlSecurityPolicy::ETable, KTableName1, RSqlSecurityPolicy::EWritePolicy);
176 TEST(policy == KPolicy2);
177 policy = aSecurityPolicy.Policy(RSqlSecurityPolicy::ETable, KTableName3, RSqlSecurityPolicy::EWritePolicy);
178 TEST(policy == KPolicy3);
179 policy = aSecurityPolicy.Policy(RSqlSecurityPolicy::ETable, KTableName2, RSqlSecurityPolicy::EWritePolicy);
180 TEST(policy == KPolicy1);
184 @SYMTestCaseID SYSLIB-SQL-CT-1635
185 @SYMTestCaseDesc RSqlSecurityPolicy test.
186 Tests RSqlSecurityPolicy API.
187 @SYMTestPriority High
188 @SYMTestActions RSqlSecurityPolicy test.
189 @SYMTestExpectedResults Test must not fail
193 void SqlSecurityPolicyTest()
195 RSqlSecurityPolicy securityPolicy;
196 TInt err = securityPolicy.Create(KDefaultPolicy);
197 TEST2(err, KErrNone);
199 //Empty RSqlSecurityPolicy object tests
201 TSecurityPolicy policy = securityPolicy.DbPolicy(RSqlSecurityPolicy::ESchemaPolicy);
202 TEST(policy == KDefaultPolicy);
203 policy = securityPolicy.DbPolicy(RSqlSecurityPolicy::EReadPolicy);
204 TEST(policy == KDefaultPolicy);
205 policy = securityPolicy.DbPolicy(RSqlSecurityPolicy::EWritePolicy);
206 TEST(policy == KDefaultPolicy);
208 policy = securityPolicy.Policy(RSqlSecurityPolicy::ETable, _L("A-Tbl"), RSqlSecurityPolicy::EReadPolicy);
209 TEST(policy == KDefaultPolicy);
210 policy = securityPolicy.Policy(RSqlSecurityPolicy::ETable, _L("B-Tbl"), RSqlSecurityPolicy::EWritePolicy);
211 TEST(policy == KDefaultPolicy);
213 securityPolicy.Close();
215 //Tests with initialized RSqlSecurityPolicy object
216 CreateTestSecurityPolicy(securityPolicy);
217 //Check the created and initialized object
218 CheckTestSecurityPolicy(securityPolicy);
220 //Externalize, then internalize and check the security policy object
221 RFileWriteStream out;
222 err = out.Replace(TheFs, KTestFile, EFileRead | EFileWrite);
223 TEST2(err, KErrNone);
224 TRAP(err, securityPolicy.ExternalizeL(out));
225 TEST2(err, KErrNone);
226 TRAP(err, out.CommitL());
228 TEST2(err, KErrNone);
230 securityPolicy.Close();
231 err = securityPolicy.Create(KDefaultPolicy);
232 TEST2(err, KErrNone);
234 err = in.Open(TheFs, KTestFile, EFileRead);
235 TEST2(err, KErrNone);
236 TRAP(err, securityPolicy.InternalizeL(in));
238 TEST2(err, KErrNone);
239 CheckTestSecurityPolicy(securityPolicy);
241 //Change some security policies and check that the RqlSecurityPolicy content was properly updated
242 err = securityPolicy.SetPolicy(RSqlSecurityPolicy::ETable, KTableName11, RSqlSecurityPolicy::EReadPolicy, KPolicy3);
243 TEST2(err, KErrNone);
244 policy = securityPolicy.Policy(RSqlSecurityPolicy::ETable, KTableName1, RSqlSecurityPolicy::EReadPolicy);
245 TEST(policy == KPolicy3);
246 policy = securityPolicy.Policy(RSqlSecurityPolicy::ETable, KTableName11, RSqlSecurityPolicy::EWritePolicy);
247 TEST(policy == KPolicy2);
249 securityPolicy.Close();
253 @SYMTestCaseID SYSLIB-SQL-CT-1637
254 @SYMTestCaseDesc RSqlDatabase::Create() test.
255 Tests RSqlDatabase::Create() call when the request is for creation of a secure database.
256 @SYMTestPriority High
257 @SYMTestActions RSqlDatabase::Create() test.
258 @SYMTestExpectedResults Test must not fail
262 void CreateSecureDatabaseTest()
264 RSqlSecurityPolicy securityPolicy;
266 //Create and initialize RSqlSecurityPolicy object.
267 CreateTestSecurityPolicy(securityPolicy);
269 //Create secure database using the data from securityPolicy object.
271 TInt err = db.Create(KTestDbName, securityPolicy);
272 TEST2(err, KErrNone);
273 securityPolicy.Close();
275 //Check that the database security policy matches the policy used when the database was created.
276 err = db.GetSecurityPolicy(securityPolicy);
277 TEST2(err, KErrNone);
278 CheckTestSecurityPolicy(securityPolicy);
279 securityPolicy.Close();
283 //Reopen the database and check the security policies
284 err = db.Open(KTestDbName);
285 TEST2(err, KErrNone);
287 //Check that the database security policy matches the policy used when the database was created.
288 err = db.GetSecurityPolicy(securityPolicy);
289 TEST2(err, KErrNone);
290 CheckTestSecurityPolicy(securityPolicy);
291 securityPolicy.Close();
295 err = RSqlDatabase::Delete(KTestDbName);
296 TEST2(err, KErrNone);
298 //An attempt to create a secure database specifying the full database path
299 CreateTestSecurityPolicy(securityPolicy);
300 err = db.Create(KTestDbName2, securityPolicy);
301 securityPolicy.Close();
302 TEST2(err, KErrArgument);
304 //An attempt to create a non-secure database formatting the database file name as <drive:><dbFileName>
305 err = db.Create(KTestDbName);
306 TEST2(err, KErrArgument);
310 @SYMTestCaseID SYSLIB-SQL-CT-1643
311 @SYMTestCaseDesc SQL server private path in database file name test.
312 Verify that SQL API returns appropriate error, if an attempt is made to create, open
313 or delete a secure database, with the full path specified in the database file name.
314 @SYMTestPriority High
315 @SYMTestActions SQL server private path in database file name test.
316 @SYMTestExpectedResults Test must not fail
320 void PrivatePathTest()
322 RSqlSecurityPolicy securityPolicy;
323 CreateTestSecurityPolicy(securityPolicy);
327 TInt err = db.Create(_L("C:\\PrIVATE\\10281E17\\[98765432]A.DB"), securityPolicy);
328 TEST2(err, KErrArgument);
329 err = db.Create(_L("C:\\PRIVATE\\10281E17\\[98765432]A.DB"));
330 TEST2(err, KErrArgument);
331 err = db.Open(_L("C:\\PRIVATE\\10281E17\\[98765432]A.DB"));
332 TEST2(err, KErrArgument);
333 err = db.Open(_L("C:\\PRIvaTe\\10281E17\\[98765432]A.DB"));
334 TEST2(err, KErrArgument);
336 err = db.Create(KTestDbName2);
337 TEST2(err, KErrNone);
338 err = db.Attach(_L("C:\\PRIVATe\\10281E17\\[98765432]A.DB"), _L("Db"));
339 TEST2(err, KErrArgument);
340 err = db.Attach(_L("C:\\PRIVAtE\\10281E17\\[98765432]A.DB"), _L("Db"));
341 TEST2(err, KErrArgument);
342 //This is an attempt to attach a database from the application's private data cage
343 err = db.Attach(KTestDbName4, _L("Db"));
344 TEST2(err, KErrNotFound);//There is no "Db" database file in the application's private data cage
347 err = RSqlDatabase::Delete(_L("C:\\pRIVATE\\10281E17\\[98765432]A.DB"));
348 TEST2(err, KErrArgument);
349 err = RSqlDatabase::Delete(_L("C:\\PRIvATE\\10281E17\\[98765432]A.DB"));
350 TEST2(err, KErrArgument);
352 //This is an attempt to create a database in the application's private data cage
353 err = db.Create(KTestDbName4);
354 TEST2(err, KErrNone);
356 err = RSqlDatabase::Delete(KTestDbName4);
357 TEST2(err, KErrNone);
359 err = db.Create(KTestDbName5);
360 TEST2(err, KErrArgument);
362 securityPolicy.Close();
365 //This test is here to check that the construction of the second database object on the server side passes
366 //without problems, if there is already one other secure database object (authorizer callback possible problems
367 //during the construction phase)
368 //Shortly: the test checks how the construction of the secure database object 2 might be influenced by
369 // the already existing secure database object 1.
370 void TwoSecureDbTest()
372 RSqlSecurityPolicy securityPolicy;
373 CreateTestSecurityPolicy(securityPolicy);
376 TInt err = db1.Create(KTestDbName, securityPolicy);
377 TEST2(err, KErrNone);
380 err = db2.Create(KTestDbName3, securityPolicy);
381 TEST2(err, KErrNone);
383 securityPolicy.Close();
389 @SYMTestCaseID SYSLIB-SQL-UT-3508
390 @SYMTestCaseDesc Test for DEF109100: SQL, code coverage for TSqlDbSysSettings is low.
391 The test attempts to create a secure shared database with a security policy
392 specified for the system tables, which is not allowed.
393 @SYMTestPriority High
394 @SYMTestActions Test for DEF109100: SQL, code coverage for TSqlDbSysSettings is low.
395 @SYMTestExpectedResults Test must not fail
398 void StoreSysTableSecurityPolicyTest()
400 _LIT(KSecurityTable, "symbian_security");
401 _LIT(KSettingsTable, "symbian_settings");
403 //Case 1: "symbian_security" table
404 RSqlSecurityPolicy sp;
405 TInt err = sp.Create(KDefaultPolicy);
406 TEST2(err, KErrNone);
407 err = sp.SetDbPolicy(RSqlSecurityPolicy::ESchemaPolicy, KPolicy1);
408 TEST2(err, KErrNone);
409 err = sp.SetPolicy(RSqlSecurityPolicy::ETable, KSecurityTable, RSqlSecurityPolicy::EReadPolicy, KPolicy1);
410 TEST2(err, KErrNone);
411 (void)RSqlDatabase::Delete(KTestDbName);
413 err = db.Create(KTestDbName, sp);
414 TEST2(err, KErrArgument);
417 //Case 2: "symbian_settings" table
418 err = sp.Create(KDefaultPolicy);
419 TEST2(err, KErrNone);
420 err = sp.SetDbPolicy(RSqlSecurityPolicy::ESchemaPolicy, KPolicy1);
421 TEST2(err, KErrNone);
422 err = sp.SetPolicy(RSqlSecurityPolicy::ETable, KSettingsTable, RSqlSecurityPolicy::EReadPolicy, KPolicy1);
423 TEST2(err, KErrNone);
424 err = RSqlDatabase::Delete(KTestDbName);
425 TEST2(err, KErrNotFound);
426 err = db.Create(KTestDbName, sp);
427 TEST2(err, KErrArgument);
430 err = RSqlDatabase::Delete(KTestDbName);
431 TEST2(err, KErrNotFound);
435 @SYMTestCaseID SYSLIB-SQL-UT-3509
436 @SYMTestCaseDesc Test for DEF109100: SQL, code coverage for TSqlDbSysSettings is low.
437 The test has 5 sub-cases, where the test attempts to open
438 a secure shared database with:
440 - empty settings table;
441 - invalid version number of the settings;
442 - NULL collation dll name, stored in the settings;
443 - too long collation dll name, stored in the settings;
444 @SYMTestPriority High
445 @SYMTestActions Test for DEF109100: SQL, code coverage for TSqlDbSysSettings is low.
446 @SYMTestExpectedResults Test must not fail
449 void LoadSysSettingsTest()
451 //Case 1: no "symbian_settings" table. No error returned, because the table is recreated.
453 TInt err = db.Open(KDb1);
454 TEST2(err, KErrNone);
457 //Case 2: "symbian_settings" table is empty.
459 TEST2(err, KErrGeneral);
462 //Case 3: Invalid version number in "symbian_settings" table.
464 TEST2(err, KErrGeneral);
467 //Case 4: NULL collation dll name in "symbian_settings" table.
469 TEST2(err, KErrGeneral);
472 //Case 5: Too long collation dll name in "symbian_settings" table.
474 TEST2(err, KErrOverflow);
479 @SYMTestCaseID SYSLIB-SQL-UT-3510
480 @SYMTestCaseDesc Test for DEF109100: SQL, code coverage for TSqlDbSysSettings is low.
481 The test has 3 sub-cases, where the test attempts to open
482 a secure shared database with:
483 - missing default policy in the security policy table;
484 - invalid database object type in the security policy table;
485 - 2 default policies in the security policy table;
486 @SYMTestPriority High
487 @SYMTestActions Test for DEF109100: SQL, code coverage for TSqlDbSysSettings is low.
488 @SYMTestExpectedResults Test must not fail
491 void LoadSecurityPolicyTest()
493 //Case 1: missing default policy.
495 TInt err = db.Open(KDb6);
496 TEST2(err, KErrGeneral);
499 //Case 2: invalid database object type.
501 TEST2(err, KErrGeneral);
504 //Case 3: 2 default policies.
506 TEST2(err, KErrGeneral);
512 TheTest.Start(_L(" @SYMTestCaseID:SYSLIB-SQL-CT-1635 RSqlSecurityPolicy test "));
513 SqlSecurityPolicyTest();
515 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-CT-1637 Create secure database test "));
516 CreateSecureDatabaseTest();
518 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-CT-1643 Sql server private path in the file name test "));
521 TheTest.Next(_L("Two secure databases at the same time test"));
524 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-3508 DEF109100 - 1 - SQL, code coverage for TSqlBufRIterator,TSqlAttachDbRefCounter,TSqlDbSysSettings is low "));
525 StoreSysTableSecurityPolicyTest();
527 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-3509 DEF109100 - 2 - SQL, code coverage for TSqlBufRIterator,TSqlAttachDbRefCounter,TSqlDbSysSettings is low "));
528 LoadSysSettingsTest();
530 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-3510 DEF109100 - 3 - SQL, code coverage for TSqlBufRIterator,TSqlAttachDbRefCounter,TSqlDbSysSettings is low "));
531 LoadSecurityPolicyTest();
538 CTrapCleanup* tc = CTrapCleanup::New();
555 User::Heap().Check();