sl@0: // Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0: // All rights reserved.
sl@0: // This component and the accompanying materials are made available
sl@0: // under the terms of "Eclipse Public License v1.0"
sl@0: // which accompanies this distribution, and is available
sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0: //
sl@0: // Initial Contributors:
sl@0: // Nokia Corporation - initial contribution.
sl@0: //
sl@0: // Contributors:
sl@0: //
sl@0: // Description:
sl@0: //
sl@0: 
sl@0: #include <d32dbms.h>
sl@0: #include <s32file.h>
sl@0: #include <e32test.h>
sl@0: #include <e32math.h>
sl@0: #include <s32mem.h>
sl@0: 
sl@0: #include "crccheck.h"
sl@0: 
sl@0: LOCAL_D TDBMS_CRCChecks TheCrcChecker;
sl@0: 
sl@0: #ifndef __linux__ //No CRC test on LINUX
sl@0: #ifdef __TOOLS2__
sl@0: const TPtrC	KCrcRecord=_L("\\epoc32\\winscw\\c\\dbms-tst\\T_PANIC.CRC");
sl@0: #else
sl@0: const TPtrC	KCrcRecord=_L("C:\\dbms-tst\\T_PANIC.CRC");
sl@0: #endif
sl@0: #endif
sl@0: 
sl@0: 
sl@0: LOCAL_D RTest test(_L("T_PANIC - Panic test when cancelling two blobs transactions"));
sl@0: LOCAL_D CTrapCleanup* TheTrapCleanup;
sl@0: LOCAL_D RDbTable TheTables[2];
sl@0: LOCAL_D RFs TheFs;
sl@0: #ifndef __TOOLS2__
sl@0: LOCAL_D RDbs TheDbs;
sl@0: #endif
sl@0: LOCAL_D RDbNamedDatabase TheDatabase;
sl@0: 
sl@0: #ifdef __TOOLS2__
sl@0: const TPtrC KTestDatabase=_L(".\\dbms-tst\\T_PANIC.DB");
sl@0: #else
sl@0: const TPtrC KTestDatabase=_L("C:\\dbms-tst\\T_PANIC.DB");
sl@0: #endif
sl@0: 
sl@0: 
sl@0: /**
sl@0: @SYMTestCaseID          SYSLIB-DBMS-CT-0641
sl@0: @SYMTestCaseDesc        Tests for creating the database and tables
sl@0: @SYMTestPriority        Medium
sl@0: @SYMTestActions         Tests for creating the tables.Leave on error.
sl@0: @SYMTestExpectedResults Test must not fail
sl@0: @SYMREQ                 REQ0000
sl@0: */
sl@0: LOCAL_C void PreTestL()
sl@0: 	{
sl@0: 	test.Next(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0641 "));
sl@0: 	// Create the database:
sl@0: 	User::LeaveIfError(TheDatabase.Replace(TheFs,KTestDatabase));
sl@0: 	CleanupClosePushL(TheDatabase);
sl@0: 
sl@0: 	// Create tables in the database:
sl@0: 	User::LeaveIfError(TheDatabase.Execute(_L("create table ta (a integer, b Long Varbinary)")));
sl@0: 	User::LeaveIfError(TheDatabase.Execute(_L("create table tb (a integer, b Long Varbinary)")));
sl@0: 
sl@0: 	// Open the tables:
sl@0: 	User::LeaveIfError(TheTables[0].Open(TheDatabase, _L("ta")));
sl@0: 	CleanupClosePushL(TheTables[0]);
sl@0: 	User::LeaveIfError(TheTables[1].Open(TheDatabase, _L("tb")));
sl@0: 	CleanupClosePushL(TheTables[1]);
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: @SYMTestCaseID          SYSLIB-DBMS-CT-0642
sl@0: @SYMTestCaseDesc        Tests for transaction of large data
sl@0: @SYMTestPriority        Medium
sl@0: @SYMTestActions         Tests for streaming of blob data
sl@0: @SYMTestExpectedResults Test must not fail
sl@0: @SYMREQ                 REQ0000
sl@0: */
sl@0: LOCAL_C void TestL()
sl@0: 	{
sl@0: 	test.Next(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0642 "));
sl@0: 	// Start a transaction:
sl@0: 	TheDatabase.Begin();
sl@0: 
sl@0: 	// Create a new row on each table:
sl@0: 	TheTables[0].InsertL();
sl@0: 	TheTables[1].InsertL();
sl@0: 
sl@0: 	for(TInt i = 0; i < 2; ++i)
sl@0: 		{
sl@0: 		// Setting to null sets the dirty flag:
sl@0: 		TheTables[i].SetColNullL(1);
sl@0: 
sl@0: 		// Create a blob of data:
sl@0: 		_LIT8(blobdata, "abcdefghijklmnopqrstuvwxyz");
sl@0: 		CBufFlat * blobbuff = CBufFlat::NewL(32);
sl@0: 		CleanupStack::PushL(blobbuff);
sl@0: 		blobbuff->InsertL(0, blobdata());
sl@0: 
sl@0: 		// Open a read stream on the blob:
sl@0: 		RBufReadStream blobstream;
sl@0: 		blobstream.Open(*blobbuff, 0);
sl@0: 		CleanupClosePushL(blobstream);
sl@0: 
sl@0: 		// Open a write stream on the table:
sl@0: 		RDbColWriteStream blobwrite;
sl@0: 		blobwrite.OpenLC(TheTables[i], 2);
sl@0: 
sl@0: 		// Stream data from the read stream to the write stream:
sl@0: 		blobwrite.WriteL(blobstream);
sl@0: 		blobwrite.CommitL();
sl@0: 
sl@0: 		// Close the write stream:
sl@0: 		CleanupStack::PopAndDestroy();
sl@0: 		// Close the read stream:
sl@0: 		CleanupStack::PopAndDestroy();
sl@0: 		// Delete the blob of data:
sl@0: 		CleanupStack::PopAndDestroy(blobbuff);
sl@0: 		}
sl@0: 	
sl@0: 	TheTables[0].Cancel();
sl@0: 	TheTables[1].Cancel(); //This call to cancel panics.
sl@0: 	
sl@0: 
sl@0: 	// End the transaction:
sl@0: 	TheDatabase.Commit();
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: @SYMTestCaseID          SYSLIB-DBMS-CT-0643
sl@0: @SYMTestCaseDesc        Tests for closing of tables
sl@0: @SYMTestPriority        Medium
sl@0: @SYMTestActions         Tests for closing of tables and database
sl@0: @SYMTestExpectedResults Test must not fail
sl@0: @SYMREQ                 REQ0000
sl@0: */
sl@0: LOCAL_C void PostTestL()
sl@0: 	{
sl@0: 	test.Next(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0643 "));
sl@0: 	// Close the tables:
sl@0: 	TheTables[0].Close();
sl@0: 	CleanupStack::Pop(); // Table close
sl@0: 	TheTables[1].Close();
sl@0: 	CleanupStack::Pop(); // Table close
sl@0: 
sl@0: 	// Close the database:
sl@0: 	CleanupStack::PopAndDestroy();
sl@0: 
sl@0: 	TInt err;
sl@0: 	TRAPD(lc, err = TheCrcChecker.GenerateCrcL(KTestDatabase));
sl@0: 	}
sl@0: 
sl@0: void doTest()
sl@0: 	{
sl@0: 	test.Start(_L("bang"));
sl@0: 
sl@0: 	// Open a connection to the DBMS server:
sl@0: #ifndef __TOOLS2__
sl@0: 	User::LeaveIfError(TheDbs.Connect());
sl@0: 	CleanupClosePushL(TheDbs);
sl@0: #endif
sl@0: 
sl@0: 	PreTestL();
sl@0: 	TestL();
sl@0: 	PostTestL();
sl@0: 		
sl@0: #ifndef __TOOLS2__
sl@0: 	CleanupStack::PopAndDestroy(); // TheDbs close
sl@0: #endif
sl@0: 	}
sl@0: 
sl@0: LOCAL_C void DeleteDataFile(const TDesC& aFullName)
sl@0: 	{
sl@0: 	RFs fsSession;
sl@0: 	TInt err = fsSession.Connect();
sl@0: 	if(err == KErrNone)
sl@0: 		{
sl@0: 		TEntry entry;
sl@0: 		if(fsSession.Entry(aFullName, entry) == KErrNone)
sl@0: 			{
sl@0: 			RDebug::Print(_L("Deleting \"%S\" file.\n"), &aFullName);
sl@0: 			err = fsSession.SetAtt(aFullName, 0, KEntryAttReadOnly);
sl@0: 			if(err != KErrNone) 
sl@0: 				{
sl@0: 				RDebug::Print(_L("Error %d changing \"%S\" file attributes.\n"), err, &aFullName);
sl@0: 				}
sl@0: 			err = fsSession.Delete(aFullName);
sl@0: 			if(err != KErrNone) 
sl@0: 				{
sl@0: 				RDebug::Print(_L("Error %d deleting \"%S\" file.\n"), err, &aFullName);
sl@0: 				}
sl@0: 			}
sl@0: 		fsSession.Close();
sl@0: 		}
sl@0: 	else
sl@0: 		{
sl@0: 		RDebug::Print(_L("Error %d connecting file session. File: %S.\n"), err, &aFullName);
sl@0: 		}
sl@0: 	}
sl@0: 
sl@0: //
sl@0: // Test streaming conversions.
sl@0: //
sl@0: GLDEF_C TInt E32Main()
sl@0:     {
sl@0: 	test.Title();
sl@0: 	TheTrapCleanup=CTrapCleanup::New();
sl@0: 
sl@0: 	User::LeaveIfError(TheFs.Connect());
sl@0: 	TInt r=TheFs.MkDir(KTestDatabase);
sl@0: 	test(r==KErrNone || r==KErrAlreadyExists);
sl@0: 
sl@0: 	TRAP(r, doTest());
sl@0: 	test(r == KErrNone);
sl@0: 
sl@0: 	test.Printf(_L("Waiting for server exit\n"));
sl@0: 	const TUint KExitDelay=6*0x100000;	// ~6 seconds
sl@0: 	User::After(KExitDelay);
sl@0: 
sl@0: 	::DeleteDataFile(KTestDatabase);
sl@0: 	
sl@0: #ifndef __linux__
sl@0: 	TInt err;
sl@0: #ifndef __TOOLS2__
sl@0: 	TRAPD(lc, err = TheCrcChecker.DumpCrcRecordsL(KCrcRecord));
sl@0: 	test(err==KErrNone);
sl@0: 	test(lc==KErrNone);
sl@0: #else
sl@0: 	TRAPD(lc, err = TheCrcChecker.ValidateCrcRecordsL(KCrcRecord));
sl@0: 	TPtrC errmsg;
sl@0: 	TheCrcChecker.ErrorReportL(err, errmsg);
sl@0: 	RDebug::Print(errmsg);
sl@0: 	test(err==KErrNone || err==TDBMS_CRCChecks::ECrcCheckOk);
sl@0: #endif
sl@0: #endif
sl@0: 	test.End();
sl@0: 	
sl@0: 	TheFs.Close();
sl@0: 	delete TheTrapCleanup;
sl@0: 	test.Close();
sl@0: 	return r;
sl@0:     }