// -----------------------------------------------------------------------------
// CreateCRKeyL
// Create CenRep Key
// -----------------------------------------------------------------------------
//
LOCAL_C void CreateCRKeyL()
    {
#ifdef _DRM_TESTING
    TRAPD( r , WriteL(_L8("CreateCRKeyL")) );
#endif

  CRepository* repository = CRepository::NewL(KCRUidDRMHelperServer);
    CleanupStack::PushL( repository );
    TInt err = repository->Create(KDRMHelperServerNotification, KNullDesC8);
    if (err == KErrAlreadyExists)
        {
        err = KErrNone;
        }
#ifdef _DRM_TESTING
    TRAP( r , WriteL(_L8("CreateCRKeyL->Create1"),err) );
#endif

    User::LeaveIfError(err);
    err = repository->Create(KDRMHelperServerNotificationPassive, KNullDesC8);
    if (err == KErrAlreadyExists)
        {
        err = KErrNone;
        }
#ifdef _DRM_TESTING
    TRAP( r , WriteL(_L8("CreateCRKeyL->Create2"),err) );
#endif
    User::LeaveIfError(err);
    CleanupStack::PopAndDestroy(repository); // repository

#ifdef _DRM_TESTING
    TRAP( r , WriteL(_L8("CreateCRKeyL->End")) );
#endif
    }
// ---------------------------------------------------------
// TXdmSettingsApi::CreatePropertyL
//
// ---------------------------------------------------------
//
EXPORT_C void TXdmSettingsApi::CreatePropertyL( TInt aSettingsId,
        const TDesC& aPropertyValue,
        TXdmSettingsProperty aPropertyName )
{
#ifdef _DEBUG
    WriteToLog( _L8( "TXdmSettingsApi::UpdatePropertyL()" ) );
    WriteToLog( _L8( "  Property:           %d" ), aPropertyName );
    WriteToLog( _L8( "  Property set ID:    %d" ), aSettingsId );
    WriteToLog( _L8( "  Property value:     %S" ), &aPropertyValue );
#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->Create( key, aPropertyValue );
        if( error != KErrNone )
        {
#ifdef _DEBUG
            WriteToLog( _L8( "  Create() 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 CdmcertapiSession::DispatchMessageL(const RMessage2& aMessage)
	{
	RDEBUG("CdmcertapiSession::DispatchMessageL");
	TInt ret = KErrNone;
	switch(aMessage.Function())
		{
		case EGetCertificate:
		{
			TInt err = KErrNone;
	
			TCertInfo info ;
			CreateCertificate( info ); 	

			CRepository *re = NULL;
			TRAPD( erx, re = CRepository::NewL ( 
		            KCRUidPolicyManagementUtilInternalKeys ) );
			if (erx == KErrNone )
			{
				TPckg<TCertInfo> pcert( info );
				err = re->Create( KSyncMLSessionCertificate, pcert ) ;
				if ( err == KErrNone )
				{
					//DBG_ARGS8(_S8("Wrote reposotry key %S"), &pcert );
				}
				else
				{
					if ( err == KErrAlreadyExists )
					{
						err = re->Set( KSyncMLSessionCertificate, pcert ) ;
						if ( err != KErrNone )
							{
							//
							}
					}
					else
					{
						//
					}
				}	
				delete re ;
				re = NULL;
			}
			else
			{
				//	DBG_ARGS8(_S8("ERROR Failed to open reposiritry %d"), erx );	
			}
		
		TCertInfo info1 ;
		RDMCert rdm;
		rdm.Get(info1);
		ret = KErrNone;
		}
		break;
		default:
			break;
		}
		TPckgBuf<TInt> retPackage(ret);
		aMessage.WriteL(0,retPackage);
	}
// ---------------------------------------------------------
// TXdmSettingsApi::UpdatePropertyL
//
// ---------------------------------------------------------
//
EXPORT_C void TXdmSettingsApi::UpdatePropertyL( TInt aSettingsId,
        const TDesC& aPropertyValue,
        TXdmSettingsProperty aPropertyName )

{
#ifdef _DEBUG
    HBufC8* value = HBufC8::NewLC( aPropertyValue.Length() );
    TPtr8 prop( value->Des() );
    prop.Copy( aPropertyValue );
    WriteToLog( _L8( "TXdmSettingsApi::UpdatePropertyL()" ) );
    WriteToLog( _L8( "  Settings ID:      %d" ), aSettingsId );
    WriteToLog( _L8( "  Property name:    %d" ), aPropertyName );
    WriteToLog( _L8( "  Property value:   %S" ), &prop );
    CleanupStack::PopAndDestroy();  //value
#endif
    TInt error = KErrAlreadyExists;
    CRepository* repository = CRepository::NewL( KCRUidXdmEngine );
    CleanupStack::PushL( repository );
    RArray<TUint32> keys = FindByIdL( aSettingsId, repository );
    CleanupClosePushL( keys );
    TInt count = keys.Count();
    if( count == 1 )
    {
        TInt32 row = ( keys[0] & 0xFFFFFF00 ) / 0x100;
        TInt32 key = ( row * 0x100 ) | aPropertyName;
#ifdef _DEBUG
        WriteToLog( _L8( "  Key to update:    0x%08x" ), key );
#endif
        error = repository->Set( key, aPropertyValue );
        if( error != KErrNone )
        {
#ifdef _DEBUG
            WriteToLog( _L8( "  Set() failed with %d, try Create()" ), error );
#endif
            error = repository->Create( key, aPropertyValue );
#ifdef _DEBUG
            WriteToLog( _L8( "  Create() completed with %d" ), error );
#endif
        }
    }
    else                            //No keys or too many
    {
#ifdef _DEBUG
        TBuf<32> errBuf;
        count > 0 ? errBuf.Append( _L( "Too many results" ) ) :
        errBuf.Append( _L( "No results found" ) );
        WriteToLog( _L8( "  * Error - %S: %d" ), &errBuf, count );
#endif
        TInt error = count > 0 ? KErrGeneral : KErrNotFound;
        User::Leave( error );
    }
    CleanupStack::PopAndDestroy( 2 );  //keys, repository
}
LOCAL_C void CreateSetRealTransactionL()
	{
	CRepository* repository;
	User::LeaveIfNull(repository = CRepository::NewLC(KUidTransactionTestRepository));

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

	//
	// Real transaction get/set
	//
	TInt r;
	TInt i = 0;
	const TInt imax = 0x0ff;

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

	// create
	const TReal KRealStartValue = 100.05;
	const TUint32 KNewRealBase = 1;
	for(i=0;i<imax;i++)
		{
		r = repository->Create(KNewRealBase+i, KRealStartValue+i);
		TEST2(r, KErrNone);
		}

	TUint32 errorId;
	r = repository->CommitTransaction(errorId);
	TEST2(r, KErrNone);

	TReal real, real2;
	for(i=0; i<imax; i++)
		{
		r = repository->Get((KNewRealBase+i), real );
		real2 = KRealStartValue+i;
		TEST2(r, KErrNone);
		TEST( real2 == real );
		}

	CleanupStack::PopAndDestroy(repository);
	}
// ---------------------------------------------------------
// TXdmSettingsApi::CreateCollectionL
//
// ---------------------------------------------------------
//
EXPORT_C TInt TXdmSettingsApi::CreateCollectionL( const CXdmSettingsCollection& aCollection )
{
#ifdef _DEBUG
    WriteToLog( _L8( "TXdmSettingsApi::CreateCollectionL()" ) );
#endif
    TInt error = KErrArgument;
    CRepository* repository = CRepository::NewL( KCRUidXdmEngine );
    CleanupStack::PushL( repository );
    TInt row = LastRow( repository );
    TInt count = aCollection.Count();
    __ASSERT_DEBUG( count > 0, User::Leave( KErrArgument ) );
    for( TInt i = 0; i < count; i++ )
    {
        const CXdmSettingsProperty& prop = aCollection.Property( i );
        TInt32 column = prop.PropertyName();
        //Don't let the client set the settings ID, since
        //this might create conflicting IDs in the repository
        if( column != KXdmSettingsId )
        {
            //Now merge row & column
            TInt32 newKey = ( row * 0x100 ) | column;
            error = repository->Create( newKey, prop.PropertyValue() );
#ifdef _DEBUG
            HBufC8* buf = HBufC8::NewLC( prop.PropertyValue().Length() );
            buf->Des().Copy( prop.PropertyValue() );
            TPtrC8 des( buf->Des() );
            WriteToLog( _L8( " New key [0x%08x] created - Name: %d  Value: %S" ), newKey, column, &des );
            CleanupStack::PopAndDestroy();  //buf
#endif
        }
    }
    error = CreateUniqueIdL( repository, row );
#ifdef _DEBUG
    WriteToLog( _L8( " New settings ID created - ID: %d  Error: %d" ), row, error );
#endif
    CleanupStack::PopAndDestroy();  //repository
    return error == KErrNone ? row : error;
}
/**
@SYMTestCaseID			SYSLIB-CENTRALREPOSITORY-CT-3435
@SYMTestCaseDesc		PDEF105203: Start-up Settings: New access point remains after restoring factory setting
@SYMTestPriority		High
@SYMTestActions			Create a new setting inside a range with range meta RFS bit set, create another setting
						outside the range, restart server in RFS mode, check to see the setting created outside
						the range still exists, check to see the setting created inside the range has been deleted.
						Repeat same steps with another repository which has default meta having RFS on and range meta
						having RFS off, check to see the setting created outside the range has been deleted, check to
						see the setting created inside the range still exists.
@SYMTestExpectedResults Test must not fail
@SYMDEF					PDEF105203
*/
LOCAL_C void PDEF105203()
	{
	TheTest.Start(_L(" @SYMTestCaseID:SYSLIB-CENTRALREPOSITORY-CT-3435 PDEF105203: Start-up Settings: New access point remains after restoring factory setting "));
	TInt r;
	TInt i;

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

	TheTest.Next(_L("Create a new setting outside RFSable range meta area"));
	const TInt KIntValue = 999;
	r = repository->Create(KNewInt2, KIntValue);
	TEST2(r, KErrNone);

	TheTest.Next(_L("Create a new setting in RFSable range meta area"));
	r = repository->Create(KNewInt3, KIntValue);
	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 'created' value outside range meta"));
	r = repository->Get(KNewInt2, i);
	TEST2(r, KErrNone); // should still exist
	TEST(i == KIntValue);

	TheTest.Next(_L("Get 'created' value inside range meta"));
	r = repository->Get(KNewInt3, i);
	TEST2(r, KErrNotFound); // should have been deleted

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

	repository = CRepository::NewLC(KUidRfsRepositoryDefaultRfsOn);

	TheTest.Next(_L("Create a new setting in RFSable default meta area"));
	const TInt KIntValue2 = 990;
	r = repository->Create(KNewInt2, KIntValue2);
	TEST2(r, KErrNone);

	TheTest.Next(_L("Create a new setting in non-RFSable range meta area"));
	r = repository->Create(KNewInt3, KIntValue2);
	TEST2(r, KErrNone);

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

	TheTest.Next(_L("Kill the server process"));
	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(KUidRfsRepositoryDefaultRfsOn);

	TheTest.Next(_L("Get 'created' value outside range meta"));
	r = repository->Get(KNewInt2, i);
	TEST2(r, KErrNotFound); // should have been deleted

	TheTest.Next(_L("Get 'created' value inside range meta"));
	r = repository->Get(KNewInt3, i);
	TEST2(r, KErrNone); // should still exist
	TEST(i == KIntValue2);

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

	TheTest.End();
	}
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 ResetTransactionL()
	{
#ifdef PENDING_REMOVAL
    // reset operations are not supported in transactions for now.
    // ResetAll will not ever be: still to decide on individual setting reset
    // Both are very slow.

	TUint32 errorId;
	CRepository* repository;
	User::LeaveIfNull(repository = CRepository::NewLC(KUidPlatsecTestRepository));

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

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

	TInt x,y;
	r = repository->Get(KInt1, x);
	TEST2(r, KErrNone);

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

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

	r = repository->CommitTransaction(errorId);
	TEST2(r, KErrNone);

	r = repository->Get(KInt1, y);
	TEST2(r, KErrNone);

	// did we get what expected after commit?
	TEST(x!=KInt1_InitialValue);
	TEST(y==KInt1_InitialValue);

	CleanupStack::PopAndDestroy(repository);

	// Repository B
	// lets prepare for reset
	User::LeaveIfNull(repository = CRepository::NewLC(KUidTransactionTestRepository));

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

	const TInt KNewSettingAlwaysPass = 0x101;

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

	TInt i = 0;
	for(i=KNewSettingAlwaysPass;i<KNewSettingAlwaysPass+10;i++)
		{
		r = repository->Create(i, i);
		TEST2(r, KErrNone);
		}


	TRequestStatus status = -1;
	r = repository->NotifyRequest(0, 0, status);
	TEST2(r, KErrNone);

	r = repository->CommitTransaction(errorId);
	TEST2(r, KErrNone);

	TInt xArray[10];
	TInt xx=0;
	for(i=0;i<10;i++)
		{
		r = repository->Get(KNewSettingAlwaysPass+i, xArray[i]);
		TEST2(r, KErrNone);
		}

	//and do the reset business now
	r = repository->StartTransaction(CRepository::EReadWriteTransaction);
	TEST2(r, KErrNone);

	r = repository->Reset();
	TEST2(r, KErrNone);

	r = repository->CommitTransaction(errorId);
	TEST2(r, KErrNone);

	// test reset after commit
	TEST(status==KNewSettingAlwaysPass);

	for(i=0;i<10;i++)
		{
		TEST(xArray[i]==(KNewSettingAlwaysPass+i));
		}

	for(i=KNewSettingAlwaysPass;i<KNewSettingAlwaysPass+10;i++)
		{
		r = repository->Get(i, xx);
		TEST2(r, KErrNotFound);
		}

	CleanupStack::PopAndDestroy(repository);
#endif // PENDING_REMOVAL
	}
LOCAL_C void CreateSetDesTransactionL()
	{
	TUint32 errorId;
	CRepository* repository;
	User::LeaveIfNull(repository = CRepository::NewLC(KUidTransactionTestRepository));

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


	_LIT(KString1_TestValue, "test string 1");
	_LIT(KString2_TestValue, "test string 2");
	_LIT(KString3_TestValue, "test string 3");
	_LIT(KString4_TestValue, "test string 4");
	_LIT(KString5_TestValue, "test string 5");
	_LIT(KString1_UpdatedTestValue, "updated test string 1");
	_LIT(KString2_UpdatedTestValue, "updated test string 2");
	//
	// Descriptor get/set
	//
	TInt r;


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

	// create
	r = repository->Create(1, KString1_TestValue);
	if(OomTesting && (r==KErrNoMemory))
		User::Leave(KErrNoMemory);
	else
		TEST2(r, KErrNone);
	r = repository->Create(2, KString2_TestValue);
	if(OomTesting && (r==KErrNoMemory))
		User::Leave(KErrNoMemory);
	else
		TEST2(r, KErrNone);
	r = repository->Create(3, KString3_TestValue);
	if(OomTesting && (r==KErrNoMemory))
		User::Leave(KErrNoMemory);
	else
		TEST2(r, KErrNone);
	r = repository->Create(4, KString4_TestValue);
	if(OomTesting && (r==KErrNoMemory))
		User::Leave(KErrNoMemory);
	else
		TEST2(r, KErrNone);
	r = repository->Create(5, KString5_TestValue);
	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);

	TBuf<50> buf;
	if(!OomTesting)
		{
		r = repository->Get(1,buf);
		TEST2(r, KErrNone);
		TEST(buf==KString1_TestValue);
		r = repository->Get(2,buf);
		TEST2(r, KErrNone);
		TEST(buf==KString2_TestValue);
		r = repository->Get(3,buf);
		TEST2(r, KErrNone);
		TEST(buf==KString3_TestValue);
		r = repository->Get(4,buf);
		TEST2(r, KErrNone);
		TEST(buf==KString4_TestValue);
		r = repository->Get(5,buf);
		TEST2(r, KErrNone);
		TEST(buf==KString5_TestValue);
		}

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

	// set
	r = repository->Set(1, KString1_UpdatedTestValue);
	if(OomTesting && (r==KErrNoMemory))
		User::Leave(KErrNoMemory);
	else
		TEST2(r, KErrNone);
	r = repository->Set(2, KString2_UpdatedTestValue);
	if(OomTesting && (r==KErrNoMemory))
		User::Leave(KErrNoMemory);
	else
		TEST2(r, KErrNone);

	r = repository->CommitTransaction(errorId);
	CleanupStack::Pop();
	TEST2(r, KErrNone);

	TBuf<50> buf1,buf2;
	r = repository->Get(1,buf1);
	TEST2(r, KErrNone);
	r = repository->Get(2,buf2);
	TEST2(r, KErrNone);

	TEST(buf1==KString1_UpdatedTestValue);
	TEST(buf2==KString2_UpdatedTestValue);

	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 ResetL()
	{
	CRepository* repository;
	User::LeaveIfNull(repository = CRepository::NewLC(KUidPlatsecTestRepository));

	TInt x;
	TInt r = repository->Get(KInt1, x);
	TEST2(r, KErrNone);
	TEST(x!=KInt1_InitialValue);

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

	r = repository->Get(KInt1, x);
	TEST2(r, KErrNone);
	TEST(x==KInt1_InitialValue);

	CleanupStack::PopAndDestroy(repository);

	// Repository D
	User::LeaveIfNull(repository = CRepository::NewLC(KUidResetTestRepository));
	// Remember that this repository need to be reset
	::AddRepositoryToReset(KUidResetTestRepository);


	const TInt KNewSetting1 = 5;
	const TInt KNewSetting2 = 6;
	r = repository->Create(KNewSetting1, 0);
	TEST2(r, KErrNone);
	r = repository->Create(KNewSetting2, 0);
	TEST2(r, KErrNone);

	r = repository->Create(65, 1);
	TEST2(r, KErrNone);

	r = repository->Set(70, 1);
	TEST2(r, KErrNone);

	r = repository->Create(80, 1);
	TEST2(r, KErrNone);

	r = repository->Create(90, 1);
	TEST2(r, KErrNone);

	TRequestStatus status = -1;
	r = repository->NotifyRequest(0, 0, status);
	TEST2(r, KErrNone);

	r = repository->Reset();
	TEST2(r, KErrNone);
	TEST(status==KUnspecifiedKey);
	RThread thisThread;
	TEST(thisThread.RequestCount()==1);

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

	r = repository->Get(40, x);
	TEST2(r, KErrNone);
	TEST(x==0);

	r = repository->Get(50, x);
	TEST2(r, KErrNone);
	TEST(x==0);

	r = repository->Get(60, x);
	TEST2(r, KErrNone);
	TEST(x==0);

	r = repository->Get(70, x);
	TEST2(r, KErrNone);
	TEST(x==0);

	TBuf<10> z;
	r = repository->Get(20, z);
	TEST2(r, KErrNone);
	TEST(z==_L("string"));

	TReal y;
	r = repository->Get(30, y);
	TEST2(r, KErrNone);
	TEST(y==1);

	r = repository->Get(5, x);
	TEST2(r, KErrNotFound);
	r = repository->Get(6, x);
	TEST2(r, KErrNotFound);
	r = repository->Get(65, x);
	TEST2(r, KErrNotFound);
	r = repository->Get(80, x);
	TEST2(r, KErrNotFound);
	r = repository->Get(90, x);
	TEST2(r, KErrNotFound);

	CleanupStack::PopAndDestroy(repository);
	}
// ---------------------------------------------------------
// CProcessorPacketData::AddLingerL
// ---------------------------------------------------------
//
void CProcessorPacketData::AddLingerL( const TInt aIapId, const TInt aLingerInterval)
    {
    TInt    err( KErrNone );
    
    // Open repository
    CRepository* repository = CRepository::NewLC( KCRUidPDPContextManager );
    
    // Find if an entry for "iapId" already exists in CentRep 
    RArray< TUint32 > foundKeys;
    foundKeys.Reset();
    
    err = repository->FindEqL( KIapColumn,       // partial key
                               KColumnMask,      // key mask
                               aIapId,           // value
                               foundKeys );      // found keys
    
    if ( err == KErrNone || err == KErrNotFound )
        {
        if ( foundKeys.Count() == 0 )
            {
            // Add a new entry
            TInt arrayCount( 0 );
            
            // Get number of iapId&linger entries in Centrep
            err = repository->Get( KPdpContextManagerLingerArrayCount, 
                                   arrayCount );
        
            if ( err == KErrNone )
                {
                arrayCount++;
                
                err = repository->Create( ( KIapColumn | arrayCount ), aIapId ); 
                
                if ( err == KErrNone )
                    {
                    err = repository->Create( ( KLingerColumn | arrayCount ), 
                                               aLingerInterval );     
                    }
                
                if ( err == KErrNone )
                    {
                    // Update number of iapId&linger entries in Centrep
                    err = repository->Set( KPdpContextManagerLingerArrayCount, 
                                           arrayCount );
                    }
                }
            }
        else
            {
            // Update existing entry on current row
            TInt32 row = foundKeys[ 0 ] & KRowMask;
            
            err = repository->Set( ( KLingerColumn | row ), aLingerInterval ); 
            }         
        }
        
    foundKeys.Close();
    CleanupStack::PopAndDestroy( repository );
    
    User::LeaveIfError( err );
    }