persistentstorage/dbms/tdbms/t_dbplatsecutl.cpp
changeset 0 08ec8eefde2f
child 55 44f437012c90
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     1 // Copyright (c) 2004-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 // DBMS security policy - testing new APIs - utility functions
       
    15 // 
       
    16 //
       
    17 
       
    18 #include <e32test.h>
       
    19 #include "t_dbplatsecutl.h"
       
    20 
       
    21 //Supply your a pointer to your cleanup function or NULL.
       
    22 TDBSCUtils::TDBSCUtils(RTest& aTest, TCleanupFunc aFunc) :
       
    23 	iTest(aTest),
       
    24 	iFunc(aFunc)
       
    25 	{
       
    26 	}
       
    27 
       
    28 //Create a CDbColSet instance. The caller is responsible for deleting it.
       
    29 CDbColSet* TDBSCUtils::CreateColSetLC(const TColDef* aColDef)
       
    30 	{
       
    31 	CDbColSet* set = CDbColSet::NewLC();
       
    32 	for(;aColDef->iName;++aColDef)
       
    33 		{
       
    34 		TDbCol col(TPtrC(aColDef->iName), aColDef->iType);
       
    35 		col.iAttributes = aColDef->iAttributes;
       
    36 		set->AddL(col);
       
    37 		}
       
    38 	return set;
       
    39 	}
       
    40 
       
    41 //Create a CDbKey instance. The caller is responsible for deleting it.
       
    42 CDbKey* TDBSCUtils::CreateKeyLC(const TDesC& aColName)
       
    43 	{
       
    44 	CDbKey* key = CDbKey::NewLC();
       
    45 	key->AddL(aColName);
       
    46 	return key;
       
    47 	}
       
    48 
       
    49 //Create a DBMS table.
       
    50 void TDBSCUtils::CreateTableL(RDbNamedDatabase& aDb, const TDesC& aTblName, const TColDef* aColDef)
       
    51 	{
       
    52 	CDbColSet* colset = TDBSCUtils::CreateColSetLC(aColDef);
       
    53 	TInt err = aDb.CreateTable(aTblName, *colset);
       
    54 	TEST2(err, KErrNone);
       
    55 	CleanupStack::PopAndDestroy(colset);
       
    56 	}
       
    57 
       
    58 // Create secure shared database. The caller is responsible for closing it.
       
    59 RDbNamedDatabase TDBSCUtils::CreateDatabase(RDbs& aDbs, TUid aPolicyUid, const TDesC& aDbName)
       
    60 	{
       
    61 	_LIT(KSecure, "SECURE");
       
    62 	TBuf<32> format;
       
    63 	format.Copy(KSecure);
       
    64 	format.Append(aPolicyUid.Name());
       
    65 	RDbNamedDatabase db;
       
    66 	TInt err = db.Create(aDbs, aDbName, format);
       
    67 	TEST2(err, KErrNone);
       
    68 	return db;
       
    69 	}
       
    70 
       
    71 // Delete secure shared database.
       
    72 TInt TDBSCUtils::DeleteDatabase(RDbs& aDbs, TUid aPolicyUid, const TDesC& aDbName)
       
    73 	{
       
    74 	RDebug::Print(_L("Deleting %X \"%S\" database.\n"), aPolicyUid.iUid, &aDbName);
       
    75 	TInt err = aDbs.DeleteDatabase(aDbName, aPolicyUid);
       
    76 	if(err != KErrNone && err != KErrNotFound) 
       
    77 		{
       
    78 		RDebug::Print(_L("Error %d deleting \"%S\" database.\n"), err, &aDbName);
       
    79 		}
       
    80 	return err;
       
    81 	}
       
    82 
       
    83 //Check if a secure shared database with "aDatabaseName" name exists.
       
    84 //aDatabaseName format: <name>.<ext>
       
    85 TBool TDBSCUtils::IsDatabaseThereL(RDbs& aDbs, TUid aPolicyUid, TDriveNumber aDriveNumber, const TDesC& aDatabaseName)
       
    86 	{
       
    87 	CDbDatabaseNames* dbNames = aDbs.DatabaseNamesL(aDriveNumber, aPolicyUid);
       
    88 	TEST(dbNames != NULL);
       
    89 	TBool found = EFalse;
       
    90 	for(TInt i=0;i<dbNames->Count();++i)
       
    91 		{
       
    92 		const TDesC& dbName = (*dbNames)[i];
       
    93 		if(aDatabaseName.CompareF(dbName) == 0)
       
    94 			{
       
    95 			found = ETrue;
       
    96 			break;
       
    97 			}
       
    98 		}
       
    99 	delete dbNames;
       
   100 	return found;
       
   101 	}
       
   102 
       
   103 //Check if the supplied aPolicy parameter has aCapability capability.
       
   104 TBool TDBSCUtils::HasCapability(const TCompiledSecurityPolicy& aPolicy, TCapability aCapability)
       
   105 	{
       
   106 	TInt maxCount = 0;
       
   107 	if(aPolicy.Type() == TSecurityPolicy::ETypeS3 || 
       
   108 	   aPolicy.Type() == TSecurityPolicy::ETypeV3 ||
       
   109 	   aPolicy.Type() == TSecurityPolicy::ETypeC3)
       
   110 		{
       
   111 		maxCount = 3;	
       
   112 		}
       
   113 	else if(aPolicy.Type() == TSecurityPolicy::ETypeC7)
       
   114 		{
       
   115 		maxCount = 7;	
       
   116 		}
       
   117 	else
       
   118 		{
       
   119 		TEST(0);
       
   120 		}
       
   121 	for(TInt i=0;i<maxCount;++i)
       
   122 		{
       
   123 		if(aPolicy.Capability(i) == aCapability)
       
   124 			{
       
   125 			return ETrue;	
       
   126 			}
       
   127 		}
       
   128 	return EFalse;
       
   129 	}
       
   130 
       
   131 //If aValue is 0 then the method prints an error message and terminates the application.
       
   132 //It will do a test cleanup before the termination, if iFunc data member is not null.
       
   133 void TDBSCUtils::Check(TInt aValue, TInt aLine)
       
   134 	{
       
   135 	if(!aValue)
       
   136 		{
       
   137 		if(TheDbscUtils.iFunc)
       
   138 			{
       
   139 			TheDbscUtils.iFunc();
       
   140 			}
       
   141 		TheDbscUtils.iTest(EFalse, aLine);
       
   142 		}
       
   143 	}
       
   144 	
       
   145 //If aValue != aExpected then the method prints an error message and terminates the application.
       
   146 //It will do a test cleanup before the termination, if iFunc data member is not null.
       
   147 void TDBSCUtils::Check(TInt aValue, TInt aExpected, TInt aLine)
       
   148 	{
       
   149 	if(aValue != aExpected)
       
   150 		{
       
   151 		RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
       
   152 		if(TheDbscUtils.iFunc)
       
   153 			{
       
   154 			TheDbscUtils.iFunc();
       
   155 			}
       
   156 		TheDbscUtils.iTest(EFalse, aLine);
       
   157 		}
       
   158 	}