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