persistentstorage/sqlite3api/TEST/t_sqlitewsd.cpp
changeset 0 08ec8eefde2f
child 23 26645d81f48d
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     1 // Copyright (c) 2007-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 <e32test.h>
       
    17 #include <e32uid.h>
       
    18 #include <f32file.h>
       
    19 #include <e32math.h>
       
    20 #include <sqlite3.h>
       
    21 #include "t_sqlitewsd.h"
       
    22 
       
    23 #include <spawn.h>
       
    24 #include <sys/wait.h>
       
    25 
       
    26 ///////////////////////////////////////////////////////////////////////////////////////
       
    27 
       
    28 static RTest	TheTest(_L("t_sqlitewsd test"));
       
    29 static RFs		TheFs;
       
    30 
       
    31 static pid_t	TheKSqliteWsdProc2Pid = 0;
       
    32 const char* 	KSqliteWsdProc2Name = "z:\\sys\\bin\\t_sqlitewsd2.exe";
       
    33 
       
    34 const char* KTestDir = "c:\\test\\";
       
    35 const char* KTestDb  = "c:\\test\\t_sqlitewsd.db";
       
    36 
       
    37 sqlite3* TheDb = 0;
       
    38 
       
    39 ///////////////////////////////////////////////////////////////////////////////////////
       
    40 
       
    41 static void DestroyTestEnv()
       
    42 	{
       
    43 	if(TheDb)
       
    44 		{
       
    45 		(void)sqlite3_close(TheDb);
       
    46 		TheDb = 0;
       
    47 		}
       
    48 	if(TheFs.Handle() != KNullHandle)
       
    49 		{
       
    50 		TFileName fname;
       
    51 		fname.Copy(TPtrC8((const TUint8*)KTestDb));
       
    52 		(void)TheFs.Delete(fname);
       
    53 		}
       
    54 	TheFs.Close();
       
    55 	}
       
    56 
       
    57 ///////////////////////////////////////////////////////////////////////////////////////
       
    58 
       
    59 //Test macros and functions
       
    60 void Check(TInt aValue, TInt aLine)
       
    61 	{
       
    62 	if(!aValue)
       
    63 		{
       
    64 		DestroyTestEnv();
       
    65 		TheTest(EFalse, aLine);
       
    66 		}
       
    67 	}
       
    68 	
       
    69 void Check(TInt aValue, TInt aExpected, TInt aLine)
       
    70 	{
       
    71 	if(aValue != aExpected)
       
    72 		{
       
    73 		RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
       
    74 		const char* errMsg = sqlite3_errmsg(TheDb);
       
    75 		if(errMsg)
       
    76 			{
       
    77 			TBuf<200> msgBuf;
       
    78 			msgBuf.Copy(TPtrC8((const TUint8*)errMsg));
       
    79 			RDebug::Print(_L("*** SQLITE error msg: \"%S\".\r\n"), &msgBuf);
       
    80 			}
       
    81 		DestroyTestEnv();
       
    82 		TheTest(EFalse, aLine);
       
    83 		}
       
    84 	}
       
    85 
       
    86 ///////////////////////////////////////////////////////////////////////////////////////
       
    87 
       
    88 static void CreateTestEnv()
       
    89     {
       
    90 	TInt err = TheFs.Connect();
       
    91 	TEST2(err, KErrNone);
       
    92 
       
    93     TFileName testDir;
       
    94     testDir.Copy(TPtrC8((const TUint8*)KTestDir));
       
    95 	err = TheFs.MkDir(testDir);
       
    96 	TEST(err == KErrNone || err == KErrAlreadyExists);
       
    97 
       
    98 	TFileName fname;
       
    99 	fname.Copy(TPtrC8((const TUint8*)KTestDb));
       
   100 	(void)TheFs.Delete(fname);
       
   101 
       
   102 	err = sqlite3_open(KTestDb, &TheDb);
       
   103 	TEST2(err, SQLITE_OK);
       
   104 	TEST(TheDb != 0);
       
   105 	}
       
   106 
       
   107 ///////////////////////////////////////////////////////////////////////////////////////
       
   108 
       
   109 static void CreateDb()
       
   110 	{
       
   111 	TEST(TheDb != 0);
       
   112 	TInt err = sqlite3_exec(TheDb, "CREATE TABLE A(F1 INTEGER)", 0, 0, 0);
       
   113 	TEST2(err, SQLITE_OK);
       
   114 	}
       
   115 
       
   116 static void RunSqliteWsd2()
       
   117 	{
       
   118 	TInt err = posix_spawn(&TheKSqliteWsdProc2Pid, KSqliteWsdProc2Name, 0, 0, 0, 0);
       
   119 	TEST2(err, 0);
       
   120 	}
       
   121 	
       
   122 static void DestroySqliteWsd2()
       
   123 	{
       
   124 	(void)waitpid(TheKSqliteWsdProc2Pid, 0, 0);
       
   125 	}
       
   126 
       
   127 void DoVerify()
       
   128 	{
       
   129 	sqlite3_stmt* stmt = 0;
       
   130   	const char* tail = 0;
       
   131 	TInt err = sqlite3_prepare(TheDb, "SELECT * FROM A", -1, &stmt, &tail);
       
   132 	TEST2(err, SQLITE_OK);
       
   133 	TEST(!tail || tail[0] == 0);
       
   134 	TInt proc1Id1recCnt = 0;
       
   135 	TInt proc1Id2recCnt = 0;
       
   136 	TInt proc2Id1recCnt = 0;
       
   137 	TInt proc2Id2recCnt = 0;
       
   138 	while((err = sqlite3_step(stmt)) == SQLITE_ROW)
       
   139 		{
       
   140 		TInt val = sqlite3_column_int(stmt, 0);
       
   141 		switch(val)
       
   142 			{
       
   143 			case KWsdProc1RecId1: 
       
   144 				++proc1Id1recCnt;
       
   145 				break;
       
   146 			case KWsdProc1RecId2: 
       
   147 				++proc1Id2recCnt;
       
   148 				break;
       
   149 			case KWsdProc2RecId1: 
       
   150 				++proc2Id1recCnt;
       
   151 				break;
       
   152 			case KWsdProc2RecId2: 
       
   153 				++proc2Id2recCnt;
       
   154 				break;
       
   155 			default:
       
   156 				TEST(0);
       
   157 				break;
       
   158 			}
       
   159 		}
       
   160 	sqlite3_finalize(stmt);
       
   161 	TEST2(err, SQLITE_DONE);
       
   162 	TEST2((proc1Id1recCnt + proc1Id2recCnt), KTestRecordCnt);
       
   163 	TEST2((proc2Id1recCnt + proc2Id2recCnt), KTestRecordCnt);
       
   164 	}
       
   165 
       
   166 /**
       
   167 @SYMTestCaseID			SYSLIB-SQLITE3-UT-4026
       
   168 @SYMTestCaseDesc		SQLITE OS porting layer - WSD test.
       
   169 						The test verifies that the WSD object allocation and access	inside the OS porting layer 
       
   170 						works properly on both the emulator and the hardware.
       
   171 						The test runs two separate processes. Each process establishes a connection to the same
       
   172 						database and inserts 500 records simultaneously.
       
   173 						During the inserts, the SQLITE OS porting layer will use a mutex to synchronise the database
       
   174 						operations between the two processes. If the WSD implementation does not work properly,
       
   175 						then the mutex object won't be allocated per process and the same mutex instance will be used by
       
   176 						both processes on the emulator. This will lead to panics/asserts inside the OS porting layer.
       
   177 						The number of the inserted record and record ids is verified at the end of the test.
       
   178 @SYMTestPriority		High
       
   179 @SYMTestActions			SQLITE OS porting layer - WSD test.
       
   180 @SYMTestExpectedResults Test must not fail
       
   181 @SYMREQ					REQ8782
       
   182 */
       
   183 static void DoWsdTests()
       
   184 	{
       
   185 	TheTest.Start(_L(" @SYMTestCaseID:SYSLIB-SQLITE3-UT-4026 Create the test database "));
       
   186 	CreateDb();
       
   187 	TheTest.Next(_L("Run the second process: t_sqlitewsd2"));
       
   188 	RunSqliteWsd2();
       
   189 	TheTest.Next(_L("Insert the records"));
       
   190 	DoInserts(KWsdProc1Id, KWsdProc1RecId1, KWsdProc1RecId2);
       
   191 	DestroySqliteWsd2();
       
   192 	TheTest.Next(_L("Verify the inserted records"));
       
   193 	DoVerify();
       
   194 	}
       
   195 
       
   196 ///////////////////////////////////////////////////////////////////////////////////////
       
   197 
       
   198 TInt E32Main()
       
   199 	{
       
   200 	TheTest.Title();
       
   201 	
       
   202 	CTrapCleanup* tc = CTrapCleanup::New();
       
   203 	
       
   204 	__UHEAP_MARK;
       
   205 	
       
   206 	CreateTestEnv();
       
   207 	DoWsdTests();
       
   208 	DestroyTestEnv();
       
   209 	
       
   210 	__UHEAP_MARKEND;
       
   211 	
       
   212 	TheTest.End();
       
   213 	TheTest.Close();
       
   214 	
       
   215 	delete tc;
       
   216 	
       
   217 	User::Heap().Check();
       
   218 	return KErrNone;
       
   219 	}