os/persistentdata/persistentstorage/dbms/pcdbms/tdbms/src/t_panic.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.
     1 // Copyright (c) 2003-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 //
    15 
    16 #include <d32dbms.h>
    17 #include <s32file.h>
    18 #include <e32test.h>
    19 #include <e32math.h>
    20 #include <s32mem.h>
    21 
    22 #include "crccheck.h"
    23 
    24 LOCAL_D TDBMS_CRCChecks TheCrcChecker;
    25 
    26 #ifndef __linux__ //No CRC test on LINUX
    27 #ifdef __TOOLS2__
    28 const TPtrC	KCrcRecord=_L("\\epoc32\\winscw\\c\\dbms-tst\\T_PANIC.CRC");
    29 #else
    30 const TPtrC	KCrcRecord=_L("C:\\dbms-tst\\T_PANIC.CRC");
    31 #endif
    32 #endif
    33 
    34 
    35 LOCAL_D RTest test(_L("T_PANIC - Panic test when cancelling two blobs transactions"));
    36 LOCAL_D CTrapCleanup* TheTrapCleanup;
    37 LOCAL_D RDbTable TheTables[2];
    38 LOCAL_D RFs TheFs;
    39 #ifndef __TOOLS2__
    40 LOCAL_D RDbs TheDbs;
    41 #endif
    42 LOCAL_D RDbNamedDatabase TheDatabase;
    43 
    44 #ifdef __TOOLS2__
    45 const TPtrC KTestDatabase=_L(".\\dbms-tst\\T_PANIC.DB");
    46 #else
    47 const TPtrC KTestDatabase=_L("C:\\dbms-tst\\T_PANIC.DB");
    48 #endif
    49 
    50 
    51 /**
    52 @SYMTestCaseID          SYSLIB-DBMS-CT-0641
    53 @SYMTestCaseDesc        Tests for creating the database and tables
    54 @SYMTestPriority        Medium
    55 @SYMTestActions         Tests for creating the tables.Leave on error.
    56 @SYMTestExpectedResults Test must not fail
    57 @SYMREQ                 REQ0000
    58 */
    59 LOCAL_C void PreTestL()
    60 	{
    61 	test.Next(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0641 "));
    62 	// Create the database:
    63 	User::LeaveIfError(TheDatabase.Replace(TheFs,KTestDatabase));
    64 	CleanupClosePushL(TheDatabase);
    65 
    66 	// Create tables in the database:
    67 	User::LeaveIfError(TheDatabase.Execute(_L("create table ta (a integer, b Long Varbinary)")));
    68 	User::LeaveIfError(TheDatabase.Execute(_L("create table tb (a integer, b Long Varbinary)")));
    69 
    70 	// Open the tables:
    71 	User::LeaveIfError(TheTables[0].Open(TheDatabase, _L("ta")));
    72 	CleanupClosePushL(TheTables[0]);
    73 	User::LeaveIfError(TheTables[1].Open(TheDatabase, _L("tb")));
    74 	CleanupClosePushL(TheTables[1]);
    75 	}
    76 
    77 /**
    78 @SYMTestCaseID          SYSLIB-DBMS-CT-0642
    79 @SYMTestCaseDesc        Tests for transaction of large data
    80 @SYMTestPriority        Medium
    81 @SYMTestActions         Tests for streaming of blob data
    82 @SYMTestExpectedResults Test must not fail
    83 @SYMREQ                 REQ0000
    84 */
    85 LOCAL_C void TestL()
    86 	{
    87 	test.Next(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0642 "));
    88 	// Start a transaction:
    89 	TheDatabase.Begin();
    90 
    91 	// Create a new row on each table:
    92 	TheTables[0].InsertL();
    93 	TheTables[1].InsertL();
    94 
    95 	for(TInt i = 0; i < 2; ++i)
    96 		{
    97 		// Setting to null sets the dirty flag:
    98 		TheTables[i].SetColNullL(1);
    99 
   100 		// Create a blob of data:
   101 		_LIT8(blobdata, "abcdefghijklmnopqrstuvwxyz");
   102 		CBufFlat * blobbuff = CBufFlat::NewL(32);
   103 		CleanupStack::PushL(blobbuff);
   104 		blobbuff->InsertL(0, blobdata());
   105 
   106 		// Open a read stream on the blob:
   107 		RBufReadStream blobstream;
   108 		blobstream.Open(*blobbuff, 0);
   109 		CleanupClosePushL(blobstream);
   110 
   111 		// Open a write stream on the table:
   112 		RDbColWriteStream blobwrite;
   113 		blobwrite.OpenLC(TheTables[i], 2);
   114 
   115 		// Stream data from the read stream to the write stream:
   116 		blobwrite.WriteL(blobstream);
   117 		blobwrite.CommitL();
   118 
   119 		// Close the write stream:
   120 		CleanupStack::PopAndDestroy();
   121 		// Close the read stream:
   122 		CleanupStack::PopAndDestroy();
   123 		// Delete the blob of data:
   124 		CleanupStack::PopAndDestroy(blobbuff);
   125 		}
   126 	
   127 	TheTables[0].Cancel();
   128 	TheTables[1].Cancel(); //This call to cancel panics.
   129 	
   130 
   131 	// End the transaction:
   132 	TheDatabase.Commit();
   133 	}
   134 
   135 /**
   136 @SYMTestCaseID          SYSLIB-DBMS-CT-0643
   137 @SYMTestCaseDesc        Tests for closing of tables
   138 @SYMTestPriority        Medium
   139 @SYMTestActions         Tests for closing of tables and database
   140 @SYMTestExpectedResults Test must not fail
   141 @SYMREQ                 REQ0000
   142 */
   143 LOCAL_C void PostTestL()
   144 	{
   145 	test.Next(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0643 "));
   146 	// Close the tables:
   147 	TheTables[0].Close();
   148 	CleanupStack::Pop(); // Table close
   149 	TheTables[1].Close();
   150 	CleanupStack::Pop(); // Table close
   151 
   152 	// Close the database:
   153 	CleanupStack::PopAndDestroy();
   154 
   155 	TInt err;
   156 	TRAPD(lc, err = TheCrcChecker.GenerateCrcL(KTestDatabase));
   157 	}
   158 
   159 void doTest()
   160 	{
   161 	test.Start(_L("bang"));
   162 
   163 	// Open a connection to the DBMS server:
   164 #ifndef __TOOLS2__
   165 	User::LeaveIfError(TheDbs.Connect());
   166 	CleanupClosePushL(TheDbs);
   167 #endif
   168 
   169 	PreTestL();
   170 	TestL();
   171 	PostTestL();
   172 		
   173 #ifndef __TOOLS2__
   174 	CleanupStack::PopAndDestroy(); // TheDbs close
   175 #endif
   176 	}
   177 
   178 LOCAL_C void DeleteDataFile(const TDesC& aFullName)
   179 	{
   180 	RFs fsSession;
   181 	TInt err = fsSession.Connect();
   182 	if(err == KErrNone)
   183 		{
   184 		TEntry entry;
   185 		if(fsSession.Entry(aFullName, entry) == KErrNone)
   186 			{
   187 			RDebug::Print(_L("Deleting \"%S\" file.\n"), &aFullName);
   188 			err = fsSession.SetAtt(aFullName, 0, KEntryAttReadOnly);
   189 			if(err != KErrNone) 
   190 				{
   191 				RDebug::Print(_L("Error %d changing \"%S\" file attributes.\n"), err, &aFullName);
   192 				}
   193 			err = fsSession.Delete(aFullName);
   194 			if(err != KErrNone) 
   195 				{
   196 				RDebug::Print(_L("Error %d deleting \"%S\" file.\n"), err, &aFullName);
   197 				}
   198 			}
   199 		fsSession.Close();
   200 		}
   201 	else
   202 		{
   203 		RDebug::Print(_L("Error %d connecting file session. File: %S.\n"), err, &aFullName);
   204 		}
   205 	}
   206 
   207 //
   208 // Test streaming conversions.
   209 //
   210 GLDEF_C TInt E32Main()
   211     {
   212 	test.Title();
   213 	TheTrapCleanup=CTrapCleanup::New();
   214 
   215 	User::LeaveIfError(TheFs.Connect());
   216 	TInt r=TheFs.MkDir(KTestDatabase);
   217 	test(r==KErrNone || r==KErrAlreadyExists);
   218 
   219 	TRAP(r, doTest());
   220 	test(r == KErrNone);
   221 
   222 	test.Printf(_L("Waiting for server exit\n"));
   223 	const TUint KExitDelay=6*0x100000;	// ~6 seconds
   224 	User::After(KExitDelay);
   225 
   226 	::DeleteDataFile(KTestDatabase);
   227 	
   228 #ifndef __linux__
   229 	TInt err;
   230 #ifndef __TOOLS2__
   231 	TRAPD(lc, err = TheCrcChecker.DumpCrcRecordsL(KCrcRecord));
   232 	test(err==KErrNone);
   233 	test(lc==KErrNone);
   234 #else
   235 	TRAPD(lc, err = TheCrcChecker.ValidateCrcRecordsL(KCrcRecord));
   236 	TPtrC errmsg;
   237 	TheCrcChecker.ErrorReportL(err, errmsg);
   238 	RDebug::Print(errmsg);
   239 	test(err==KErrNone || err==TDBMS_CRCChecks::ECrcCheckOk);
   240 #endif
   241 #endif
   242 	test.End();
   243 	
   244 	TheFs.Close();
   245 	delete TheTrapCleanup;
   246 	test.Close();
   247 	return r;
   248     }