// ---------------------------------------------------------
// TXdmSettingsApi::RemovePropertyL
//
// ---------------------------------------------------------
//
EXPORT_C void TXdmSettingsApi::RemovePropertyL( TInt aSettingsId,
        TXdmSettingsProperty aPropertyName )
{
#ifdef _DEBUG
    WriteToLog( _L8( "TXdmSettingsApi::RemovePropertyL() - ID: %d" ), aSettingsId );
#endif
    TInt error = KErrNotFound;
    CRepository* repository = CRepository::NewL( KCRUidXdmEngine );
    CleanupStack::PushL( repository );
    TInt row = FindRowL( aSettingsId, repository );
    if( row >= 0 )
    {
        TInt32 key = ( row * 0x100 ) | aPropertyName;
        error = repository->Delete( key );
        if( error != KErrNone )
        {
#ifdef _DEBUG
            WriteToLog( _L8( "  Delete() failed with %d" ), error );
#endif
            User::Leave( error );
        }
    }
    else                            //No keys or too many
    {
#ifdef _DEBUG
        WriteToLog( _L8( "  Property set not found - Error: %d" ), row );
#endif
        User::Leave( error );
    }
    CleanupStack::PopAndDestroy();  //repository
}
void TCmdUNSAFTimers::RemoveParameterL( CRepository& aRepository,
										TUint32 aKey ) const
	{
	const TUint32 KSingleKeyMask = 0xffffffff;
	TUint32 dummy( 0 );
	TInt status = aRepository.Delete( aKey, KSingleKeyMask, dummy );
	__ASSERT_ALWAYS( status == KErrNone || status == KErrNotFound,
					 User::Leave( status ) );	
	}
// ---------------------------------------------------------------------------
// CNATFWTraversalAdapter::DeleteL
//
// ---------------------------------------------------------------------------
//
void CNATFWTraversalAdapter::DeleteL( const TDesC8& aSaveData )
    {
    DBG_PRINT( "CNATFWTraversalAdapter::DeleteL - begin" );
    // Central Repository for NAT-FW Traversal settings.
    CRepository* rep = CRepository::NewLC( KCRUidUNSAFProtocols );
    TInt step( 0 );

    // Delete all keys that are stored.
    while ( step < aSaveData.Length() )
        {
        TUint32 key = DesToTUint( aSaveData.Mid( step, KMaxCharsInTUint32 ) );
        key &= KUNSAFProtocolsTableMask;
        TUint32 errorKey;
        rep->Delete( key, KUNSAFProtocolsTableMask, errorKey );
        step += KMaxCharsInTUint32;
        }
    
    CleanupStack::PopAndDestroy( rep );
    DBG_PRINT( "CNATFWTraversalAdapter::DeleteL - end" );
    }
// ---------------------------------------------------------
// TXdmSettingsApi::RemoveCollectionL
//
// ---------------------------------------------------------
//
EXPORT_C void TXdmSettingsApi::RemoveCollectionL( TInt aSettingsId )
{
#ifdef _DEBUG
    WriteToLog( _L8( "TXdmSettingsApi::RemoveCollectionL() - ID: %d" ), aSettingsId );
#endif
    CRepository* repository = CRepository::NewL( KCRUidXdmEngine );
    CleanupStack::PushL( repository );
    TInt row = FindRowL( aSettingsId, repository );
    if( row >= KErrNone )
    {
        TInt32 key = -1;
        TInt error = KErrNone;
        for( TInt i = 0; i < KXdmPropertyCount; i++ )
        {
            key = ( row * 0x100 ) | i;
            error = repository->Delete( key );
#ifdef _DEBUG
            WriteToLog( _L8( " Key %x deleted - Error: %d" ), key, error );
#endif
        }
    }
    else User::Leave( KErrNotFound );
    CleanupStack::PopAndDestroy();
}
void CTe_LbsRootApiTestSuiteStepBase::SetTestRepositorySettingsL(TInt aTestStep)
	{
	CRepository* rep = CRepository::NewL(KLbsCenRepUid);
	CleanupStack::PushL(rep);
	
	// clean out any existing entries
	TUint32 errorKey;
	User::LeaveIfError(rep->Delete(KLbsSystemTransientKey));
	User::LeaveIfError(rep->Delete(KLbsRootProcessTransientKey));
	User::LeaveIfError(rep->Delete(KLbsRootProcessNameFirstKey, 0xFFFFF000, errorKey));
	User::LeaveIfError(rep->Delete(KLbsRootProcessUidFirstKey, 0xFFFFF000, errorKey));
	User::LeaveIfError(rep->Delete(KLbsRootProcessIsServerFirstKey, 0xFFFFF000, errorKey));
	
	// For testing, set LbsRoot to 'transient' mode
	User::LeaveIfError(rep->Set(KLbsRootProcessTransientKey, EFalse));
	User::LeaveIfError(rep->Set(KLbsSystemTransientKey, ETrue));
	
	// set the entries for the 'dummy' processes to 
	// be used for the test
	TUint nameKey = KLbsRootProcessNameFirstKey;
	TUint uidKey = KLbsRootProcessUidFirstKey;
	TUint isProcessKey = KLbsRootProcessIsServerFirstKey;
	
	switch(aTestStep)
		{
			case 0:
			case 1:
			case 2:
			case 3:
			case 4:
			case 5:
			case 6:
			case 7:
			case 8:
			case 9:
			default:
				{
				User::LeaveIfError(rep->Set(nameKey++, KNrhName));
				User::LeaveIfError(rep->Set(uidKey++, TInt(KNrhUid.iUid)));
				User::LeaveIfError(rep->Set(isProcessKey++, EFalse));
	
				User::LeaveIfError(rep->Set(nameKey++, KAgpsName));
				User::LeaveIfError(rep->Set(uidKey++, TInt(KAgpsUid.iUid)));
				User::LeaveIfError(rep->Set(isProcessKey++, EFalse));
	
				User::LeaveIfError(rep->Set(nameKey++, KNgName));
				User::LeaveIfError(rep->Set(uidKey++, TInt(KNgUid.iUid)));
				User::LeaveIfError(rep->Set(isProcessKey++, EFalse));
				break;
				}
// cases 10-18 yet to be ported 				
			case 19:
				{
				// Force a panic on the first process to be started
				User::LeaveIfError(rep->Set(nameKey++, KPanickingProcessName));
				User::LeaveIfError(rep->Set(uidKey++, TInt(KPanickingUid.iUid)));
				User::LeaveIfError(rep->Set(isProcessKey++, EFalse));
	
				User::LeaveIfError(rep->Set(nameKey++, KAgpsName));
				User::LeaveIfError(rep->Set(uidKey++, TInt(KAgpsUid.iUid)));
				User::LeaveIfError(rep->Set(isProcessKey++, EFalse));
	
				User::LeaveIfError(rep->Set(nameKey++, KNgName));
				User::LeaveIfError(rep->Set(uidKey++, TInt(KNgUid.iUid)));
				User::LeaveIfError(rep->Set(isProcessKey++, EFalse));
				break;			
				}
		
			case 20:
				{
				// Force a panic on the second process to be started
				User::LeaveIfError(rep->Set(nameKey++, KNrhName));
				User::LeaveIfError(rep->Set(uidKey++, TInt(KNrhUid.iUid)));
				User::LeaveIfError(rep->Set(isProcessKey++, EFalse));

				User::LeaveIfError(rep->Set(nameKey++, KPanickingProcessName));
				User::LeaveIfError(rep->Set(uidKey++, TInt(KPanickingUid.iUid)));
				User::LeaveIfError(rep->Set(isProcessKey++, EFalse));
	
				User::LeaveIfError(rep->Set(nameKey++, KNgName));
				User::LeaveIfError(rep->Set(uidKey++, TInt(KNgUid.iUid)));
				User::LeaveIfError(rep->Set(isProcessKey++, EFalse));
				break;			
				}
		
		}
	
	CleanupStack::PopAndDestroy(rep);
	}
LOCAL_C void RestoreFactorySettingsTestL()
	{
	TheTest.Start(_L("ResetAllRepositoriesTestL"));
	TInt r;
	TInt i;
	TBuf<20> str;

	TheTest.Next(_L("Open repository to ensure server is running"));
	CRepository* repository = CRepository::NewLC(KUidRfsRepository);

	TheTest.Next(_L("Add a Setting"));
	const TInt KIntValue = 1234;
	r = repository->Create(KNewInt, KIntValue);
	TEST2(r, KErrNone);

	TheTest.Next(_L("Delete a Setting"));
	r = repository->Delete(KReal1);
	TEST2(r, KErrNone);

	TheTest.Next(_L("Modify a Setting"));
	r = repository->Set(KInt1, KInt1_UpdatedValue);
	TEST2(r, KErrNone);

	TheTest.Next(_L("Modify a String Setting"));
	r = repository->Set(KString1, KString1_UpdatedValue);

	TEST2(r, KErrNone);
	// Close repository
	CleanupStack::PopAndDestroy(repository);

	TheTest.Next(_L("Kill the server process"));
	_LIT( KCentralRepositoryServerName, "Centralrepositorysrv");
	r = KillProcess(KCentralRepositoryServerName);
	TEST2(r,KErrNone);

	User::After(KGeneralDelay);

	TheTest.Next(_L("Manually start central respository"));
	ReStartServerInSoftResetMode();

	TheTest.Next(_L("Re-create the repository to ensure server is running"));
	repository = CRepository::NewLC(KUidRfsRepository);

	TheTest.Next(_L("Get 'Added' value"));
	r = repository->Get(KNewInt, i);
	TEST2(r, KErrNone);

	TReal real;
	TheTest.Next(_L("Get 'Deleted' value"));
	r = repository->Get(KReal1, real);
	TEST2(r, KErrNone);
	TEST(real == KReal1_InitialValue);

	TheTest.Next(_L("Get 'Modified' value"));
	r = repository->Get(KInt1, i);
	TEST2(r, KErrNone);
	TEST(i == KInt1_UpdatedValue);

	r = repository->Get(KString1, str);
	TEST2(r, KErrNone);
	TEST(str==KString1_InitialValue);

	// Close repository
	CleanupStack::PopAndDestroy(repository);

	TheTest.End();
	}
LOCAL_C void CreateDeleteL()
	{
	CRepository* repository;
	User::LeaveIfNull(repository = CRepository::NewLC(KUidPlatsecTestRepository));
	// Remember that this repository need to be reset
	::AddRepositoryToReset(KUidPlatsecTestRepository);

	TInt x;
	TInt r = repository->Get(KNewInt, x);
	TEST2(r, KErrNotFound);

	const TInt KIntValue = 1234;
	r = repository->Create(KNewInt, KIntValue);
	TEST2(r, KErrNone);

	r = repository->Get(KNewInt, x);
	TEST2(r, KErrNone);
	TEST(x==KIntValue);

	r = repository->Delete(KNewInt);
	TEST2(r, KErrNone);

	r = repository->Get(KNewInt, x);
	TEST2(r, KErrNotFound);

	r = repository->Create(KNewInt, KIntValue);
	TEST2(r, KErrNone);

	r = repository->Get(KNewInt, x);
	TEST2(r, KErrNone);
	TEST(x==KIntValue);

	r = repository->Get(KNewInt, x);
	TEST2(r, KErrNone);
	TEST(x==KIntValue);

	TReal y;
	r = repository->Get(KNewReal, y);
	TEST2(r, KErrNotFound);

	const TReal KRealValue = 5678.0;
	r = repository->Create(KNewReal, KRealValue);
	TEST2(r, KErrNone);

	r = repository->Get(KNewReal, y);
	TEST2(r, KErrNone);
	TEST(y==KRealValue);

	r = repository->Delete(KNewReal);
	TEST2(r, KErrNone);

	r = repository->Get(KNewReal, y);
	TEST2(r, KErrNotFound);

	r = repository->Create(KNewReal, KRealValue);
	TEST2(r, KErrNone);

	r = repository->Get(KNewReal, y);
	TEST2(r, KErrNone);
	TEST(y==KRealValue);

	r = repository->Get(KNewReal, y);
	TEST2(r, KErrNone);
	TEST(y==KRealValue);

	TBuf8<20> z8;
	r = repository->Get(KNewString8, z8);
	TEST2(r, KErrNotFound);

	_LIT8(KString8Value, "ABCDEF");
	r = repository->Create(KNewString8, KString8Value);
	TEST2(r, KErrNone);

	r = repository->Get(KNewString8, z8);
	TEST2(r, KErrNone);
	TEST(z8==KString8Value);

	r = repository->Delete(KNewString8);
	TEST2(r, KErrNone);

	r = repository->Get(KNewString8, z8);
	TEST2(r, KErrNotFound);

	r = repository->Create(KNewString8, KString8Value);
	TEST2(r, KErrNone);

	r = repository->Get(KNewString8, z8);
	TEST2(r, KErrNone);
	TEST(z8==KString8Value);

	r = repository->Get(KNewString8, z8);
	TEST2(r, KErrNone);
	TEST(z8==KString8Value);

	TBuf16<20> z16;
	r = repository->Get(KNewString16, z16);
	TEST2(r, KErrNotFound);

	_LIT(KString16Value, "ghijklmn");
	r = repository->Create(KNewString16, KString16Value);
	TEST2(r, KErrNone);

	r = repository->Get(KNewString16, z16);
	TEST2(r, KErrNone);
	TEST(z16==KString16Value);

	r = repository->Delete(KNewString16);
	TEST2(r, KErrNone);

	r = repository->Get(KNewString16, z16);
	TEST2(r, KErrNotFound);

	r = repository->Create(KNewString16, KString16Value);
	TEST2(r, KErrNone);

	r = repository->Get(KNewString16, z16);
	TEST2(r, KErrNone);
	TEST(z16==KString16Value);

	r = repository->Get(KNewString16, z16);
	TEST2(r, KErrNone);
	TEST(z16==KString16Value);

	CleanupStack::PopAndDestroy(repository);
	}
LOCAL_C void CreateSetIntTransactionL()
	{
	CRepository* repository;
	User::LeaveIfNull(repository = CRepository::NewLC(KUidTransactionTestRepository));

	// Remember that this repository need to be reset
	::AddRepositoryToReset(KUidTransactionTestRepository);

	//
	// Integer get/set
	//
	TInt r;
	TInt i = 0;
	const TInt imax = 0x0ff;
	TUint32 errorId;

	r = repository->StartTransaction(CRepository::EReadWriteTransaction);
	repository->CleanupRollbackTransactionPushL();
	TEST2(r, KErrNone);

	// create
	TInt KIntStartValue = 100;
	const TUint32 KNewIntBase = 1;
	for(i=0;i<imax;i++)
		{
		r = repository->Create(KNewIntBase+i, KIntStartValue+i);
		if(OomTesting && (r==KErrNoMemory))
			User::Leave(KErrNoMemory);
		else
			TEST2(r, KErrNone);
		}

	r = repository->CommitTransaction(errorId);
	CleanupStack::Pop();
	if(OomTesting && r!=KErrNone)
		{
		// in OOM test we may have alredy created settings
		TEST2(r, KErrAlreadyExists);
		}
	else
		TEST2(r, KErrNone);

	TInt integer;
	if(!(OomTesting && r==KErrAlreadyExists))
	{
		// during OOM tests we have values from Sets rather then Create...
		for(i=0;i<imax;i++)
			{
			r = repository->Get(KNewIntBase+i,integer );
			TEST2(r, KErrNone);
			TEST(KIntStartValue+i==integer);
			}
	}
	r = repository->StartTransaction(CRepository::EReadWriteTransaction);
	TEST2(r, KErrNone);

    repository->CleanupRollbackTransactionPushL();

	// set
	KIntStartValue = 200;
	for(i=0;i<imax;i++)
		{
		r = repository->Set(KNewIntBase+i, KIntStartValue+i);
		if(OomTesting && (r==KErrNoMemory))
			User::Leave(KErrNoMemory);
		else
			TEST2(r, KErrNone);
		}

	r = repository->CommitTransaction(errorId);
	CleanupStack::Pop();
	TEST2(r, KErrNone);
	for(i=0;i<imax;i++)
		{
		r = repository->Get(KNewIntBase+i,integer );
		TEST2(r, KErrNone);
		TEST(KIntStartValue+i==integer);
		}

	// lets check set where some are going to fail
	// set
	r = repository->StartTransaction(CRepository::EReadWriteTransaction);
	repository->CleanupRollbackTransactionPushL();
	TEST2(r, KErrNone);
	KIntStartValue = 400;
	TInt base = KNewIntBase + 0x7f; // half the range ok other half should fail
	for(i=0;i<imax;i++)
		{
		r = repository->Set(base+i, KIntStartValue+i);
		if(OomTesting && (r==KErrNoMemory))
			User::Leave(KErrNoMemory);
		else
		    {
		    if (base + i < 0x100)
		        {
			    TEST2(r, KErrNone);
		        }
		    else if (base + i == 0x100)
		        {
			    TEST2(r, KErrNone);
		        // this causes transaction to fail
                const TReal KRealValue = 1.234;
		        r = repository->Set(base+i, KRealValue);
			    TEST2(r, KErrArgument);
		        }
		    else
		        {
		        // after transaction has failed, repository returns KErrAbort for every request
			    TEST2(r, KErrAbort);
		        }
		    }
		}

	r = repository->CommitTransaction(errorId);
	TEST2(r, KErrArgument);
	TEST2(errorId, 0x100);
	CleanupStack::Pop();
	// should be able to open again
	r = repository->StartTransaction(CRepository::EReadWriteTransaction);
	TEST2(r, KErrNone);
	repository->RollbackTransaction();

	// and after the failed transaction it should be in previous state!!
	KIntStartValue = 200;
	for(i=0;i<imax;i++)
		{
		r = repository->Get(KNewIntBase+i,integer );
		TEST2(r, KErrNone);
		TEST(KIntStartValue+i==integer);
		}

	// lets clear after ourselves
	for(i=0;i<imax;i++)
		{
		r = repository->Delete(KNewIntBase+i);
		TEST2(r, KErrNone);
		}

	CleanupStack::PopAndDestroy(repository);
	}
LOCAL_C void GetSetL()
	{
	CRepository* repository;
	User::LeaveIfNull(repository = CRepository::NewLC(KUidPlatsecTestRepository));

	// Remember that this repository need to be reset
	::AddRepositoryToReset(KUidPlatsecTestRepository);

	//
	// Integer get/set
	//

	TInt i = 0;

	TInt r = repository->Get(KNonExisitent, i);
	TEST2(r, KErrNotFound);
	TEST(i==0);

	// no capability specified so deafult should do and should succeed
	r = repository->Get(KReal1, i);
	TEST2(r, KErrArgument);
	TEST(i==0);

	// no capability specified so deafult should do and should succeed
	r = repository->Get(KInt1, i);
	TEST2(r, KErrNone);
	TEST(i==KInt1_InitialValue);

	r = repository->Set(KNonExisitent, 0);
	TEST2(r, KErrNone);

	r = repository->Delete(KNonExisitent);
	TEST2(r, KErrNone);

	r = repository->Set(KReal1, 0);
	TEST2(r, KErrArgument);

	r = repository->Set(KInt1, KInt1_UpdatedValue);
	TEST2(r, KErrNone);

	r = repository->Get(KInt1, i);
	TEST2(r, KErrNone);
	TEST(i==KInt1_UpdatedValue);

	r = repository->Get(KIntAlwaysPass, i);
	// range policy AlwaysPass
	TEST2(r, KErrNone);
	TEST(i==KIntAlwaysPass_Value);

	i=0;
	r = repository->Get(KIntAlwaysPassFail, i);
	// range policy overidden
	TEST2(r, KErrPermissionDenied);
	TEST(i!=KIntAlwaysPass_Value);


	//
	// Real get/set
	//

	TReal y = 0;

	r = repository->Get(KNonExisitent, y);
	TEST2(r, KErrNotFound);
	TEST(y==0);

	r = repository->Get(KInt1, y);
	TEST2(r, KErrArgument);
	TEST(y==0);

	r = repository->Get(KReal1, y);
	TEST2(r, KErrNone);
	TEST(y==KReal1_InitialValue);

	r = repository->Set(KNonExisitent, 0.0);
	TEST2(r, KErrNone);

	r = repository->Delete(KNonExisitent);
	TEST2(r, KErrNone);

	r = repository->Set(KInt1, 0.0);
	TEST2(r, KErrArgument);

	r = repository->Set(KReal1, KReal1_UpdatedValue);
	TEST2(r, KErrNone);

	r = repository->Get(KReal1, y);
	TEST2(r, KErrNone);
	TEST(y==KReal1_UpdatedValue);

	//
	// String get/set
	//

	TBuf<20> str;

	r = repository->Get(KNonExisitent, str);
	TEST2(r, KErrNotFound);
	TEST(str.Length()==0);

	r = repository->Get(KInt1, str);
	TEST2(r, KErrArgument);
	TEST(str.Length()==0);

	r = repository->Get(KString1, str);
	PlatSec platsec;

	if(!platsec.ConfigSetting(PlatSec::EPlatSecEnforcement))
		{
		TEST2(r, KErrNone);
		TEST(str==KString1_InitialValue);
		}
	else
		TEST2(r, KErrPermissionDenied);


	TBuf<10> tooShort;
	r = repository->Get(KString1, tooShort);

	if(!platsec.ConfigSetting(PlatSec::EPlatSecEnforcement))
		{
		TEST2(r, KErrOverflow);
		TEST(tooShort.Length()==0);
		}
	else
		TEST2(r, KErrPermissionDenied);


	r = repository->Set(KNonExisitent, str);
	TEST2(r, KErrNone);

	r = repository->Delete(KNonExisitent);
	TEST2(r, KErrNone);

	r = repository->Set(KInt1, str);
	TEST2(r, KErrArgument);

	r = repository->Set(KString1, KString1_UpdatedValue);
	if(!platsec.ConfigSetting(PlatSec::EPlatSecEnforcement))
		{
		TEST2(r, KErrNone);
		}
	else
		TEST2(r, KErrPermissionDenied);

	r = repository->Get(KString1, str);

	if(!platsec.ConfigSetting(PlatSec::EPlatSecEnforcement))
		{
		TEST2(r, KErrNone);
		TEST(str==KString1_UpdatedValue);
		}
	else
		TEST2(r, KErrPermissionDenied);

	const TInt KStringLengthTooBig = KMaxUnicodeStringLength  + 1;
	TBuf<KStringLengthTooBig> tooBig;
	tooBig.SetLength(KStringLengthTooBig);
	r = repository->Set(KString1, tooBig);
	TEST2(r, KErrArgument);

	CleanupStack::PopAndDestroy(repository);
	}