persistentstorage/sqlite3api/TEST/t_sqliteapi.c
changeset 0 08ec8eefde2f
child 23 26645d81f48d
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     1 /*
       
     2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include <stdio.h>
       
    21 #include <stdlib.h>
       
    22 #include <string.h>
       
    23 #include <wchar.h>
       
    24 #include <errno.h>
       
    25 #include <sys/stat.h>
       
    26 #include <pthread.h>
       
    27 #include <semaphore.h>
       
    28 #include <unistd.h>
       
    29 #include <sqlite3.h>
       
    30 #include "sqliteTestUtl.h"
       
    31 
       
    32 static sqlite3* 		TheDb = 0;
       
    33 static sqlite3* 		TheDb2 = 0;
       
    34 static sqlite3_stmt*	TheStmt = 0;
       
    35 static sqlite3_stmt*	TheStmt2 = 0;
       
    36 const char* const		TheTestDirName = "c:\\test";
       
    37 const char* const		TheTestDbName  = "c:\\test\\a1.db";
       
    38 
       
    39 const int 			KTestThreadCount = 2;
       
    40 static int 			TheInsertRecCnt[KTestThreadCount] = {0, 0};
       
    41 static int 			TheLockErrCnt[KTestThreadCount] = {0, 0};
       
    42 const char* const 	KThreadNames[KTestThreadCount] = {"THRD1", "THRD2"};
       
    43 static sem_t 		TheSemaphores[KTestThreadCount];
       
    44 
       
    45 #define UNUSED_ARG(a) (a) = (a)
       
    46 
       
    47 /* ///////////////////////////////////////////////////////////////////////////////////// */
       
    48 
       
    49 /* The standard C library does not have abs() function with a double argument */
       
    50 static double dabs(double arg)
       
    51 	{
       
    52 	return arg < 0.0 ? -arg : arg;
       
    53 	}
       
    54 
       
    55 /* ///////////////////////////////////////////////////////////////////////////////////// */
       
    56 
       
    57 static void TestEnvDestroy()
       
    58 	{
       
    59 	if(TheStmt2)
       
    60 		{
       
    61 		(void)sqlite3_finalize(TheStmt2);
       
    62 		TheStmt2 = 0;
       
    63 		}
       
    64 	if(TheStmt)
       
    65 		{
       
    66 		(void)sqlite3_finalize(TheStmt);
       
    67 		TheStmt = 0;
       
    68 		}
       
    69 	if(TheDb2)
       
    70 		{
       
    71 		(void)sqlite3_close(TheDb2);
       
    72 		TheDb2 = 0;
       
    73 		}
       
    74 	if(TheDb)
       
    75 		{
       
    76 		(void)sqlite3_close(TheDb);
       
    77 		TheDb = 0;
       
    78 		}
       
    79 	(void)remove(TheTestDbName);
       
    80 	}
       
    81 
       
    82 /* ///////////////////////////////////////////////////////////////////////////////////// */
       
    83 /* Test macros and functions */
       
    84 
       
    85 static void Check1(int aValue, int aLine)
       
    86 	{
       
    87 	if(!aValue)
       
    88 		{
       
    89 		if(TheDb)
       
    90 			{
       
    91 			const char* errmsg = sqlite3_errmsg(TheDb);
       
    92 			PrintS("*** DB1: SQLITE error message: %s\r\n", errmsg);
       
    93 			}
       
    94 		if(TheDb2)
       
    95 			{
       
    96 			const char* errmsg = sqlite3_errmsg(TheDb2);
       
    97 			PrintS("*** DB2: SQLITE error message: %s\r\n", errmsg);
       
    98 			}
       
    99 		PrintI("*** Test check failed! Line=%d\r\n", aLine);
       
   100 		TestEnvDestroy();
       
   101 		TestAbort(aLine);
       
   102 		}
       
   103 	}
       
   104 	
       
   105 static void Check2(int aValue, int aExpected, int aLine)
       
   106 	{
       
   107 	if(aValue != aExpected)
       
   108 		{
       
   109 		if(TheDb)
       
   110 			{
       
   111 			const char* errmsg = sqlite3_errmsg(TheDb);
       
   112 			PrintS("*** DB1: SQLITE error message: %s\r\n", errmsg);
       
   113 			}
       
   114 		if(TheDb2)
       
   115 			{
       
   116 			const char* errmsg = sqlite3_errmsg(TheDb2);
       
   117 			PrintS("*** DB2: SQLITE error message: %s\r\n", errmsg);
       
   118 			}
       
   119 		PrintIII("*** Test check failed! Line=%d. Expected error: %d, got: %d\r\n", aLine, aExpected, aValue);
       
   120 		TestEnvDestroy();
       
   121 		TestAbort(aLine);
       
   122 		}
       
   123 	}
       
   124 	
       
   125 static void Check64(sqlite_int64 aValue, sqlite_int64 aExpected, int aLine)
       
   126 	{
       
   127 	if(aValue != aExpected)
       
   128 		{
       
   129 		if(TheDb)
       
   130 			{
       
   131 			const char* errmsg = sqlite3_errmsg(TheDb);
       
   132 			PrintS("*** DB1: SQLITE error message: %s\r\n", errmsg);
       
   133 			}
       
   134 		if(TheDb2)
       
   135 			{
       
   136 			const char* errmsg = sqlite3_errmsg(TheDb2);
       
   137 			PrintS("*** DB2: SQLITE error message: %s\r\n", errmsg);
       
   138 			}
       
   139 		PrintII64I64("*** Test check failed! Line=%ld. Expected error: %ld, got: %d\r\n", aLine, aExpected, aValue);
       
   140 		TestEnvDestroy();
       
   141 		TestAbort(aLine);
       
   142 		}
       
   143 	}
       
   144 #define TEST(arg) Check1((arg), __LINE__)
       
   145 #define TEST2(aValue, aExpected) Check2(aValue, aExpected, __LINE__)
       
   146 #define TEST64(aValue, aExpected) Check64(aValue, aExpected, __LINE__)
       
   147 
       
   148 /* ///////////////////////////////////////////////////////////////////////////////////// */
       
   149 
       
   150 static void TestEnvCreate()
       
   151 	{
       
   152 	int err; 
       
   153 	(void)remove(TheTestDbName);
       
   154 	err = mkdir(TheTestDirName, S_IREAD | S_IWRITE);
       
   155 	if(err != 0)
       
   156 		{
       
   157 		err = errno;	
       
   158 		}
       
   159 	TEST(err == 0 || err == EEXIST);
       
   160 	
       
   161 	//Creating the private data cage directory here to suppress a capability warning
       
   162 	CreatePrivateDir();
       
   163 	}
       
   164 
       
   165 /* ///////////////////////////////////////////////////////////////////////////////////// */
       
   166 /* Callbacks */
       
   167 
       
   168 static int exec_callback(void* udata, int argc, char** argv, char** colname)
       
   169 	{
       
   170 	UNUSED_ARG(udata);
       
   171 	UNUSED_ARG(argc);
       
   172 	UNUSED_ARG(argv);
       
   173 	UNUSED_ARG(colname);
       
   174 	return 0;
       
   175 	}
       
   176 
       
   177 static int authorizer_callback(void* udata, int optype, const char* name1, const char* name2, const char* name, const char* viewname)
       
   178 	{
       
   179 	UNUSED_ARG(udata);
       
   180 	UNUSED_ARG(optype);
       
   181 	UNUSED_ARG(name1);
       
   182 	UNUSED_ARG(name2);
       
   183 	UNUSED_ARG(name);
       
   184 	UNUSED_ARG(viewname);
       
   185 	return SQLITE_OK;
       
   186 	}
       
   187 
       
   188 static int commit_hook(void* udata)
       
   189 	{
       
   190 	UNUSED_ARG(udata);
       
   191 	return SQLITE_OK;	
       
   192 	}
       
   193 	
       
   194 static void rollback_hook(void* udata)
       
   195 	{
       
   196 	UNUSED_ARG(udata);
       
   197 	}
       
   198 
       
   199 static void update_hook(void* udata, int type, char const* dbname, char const* tblname, sqlite_int64 rowid)
       
   200 	{
       
   201 	UNUSED_ARG(udata);
       
   202 	UNUSED_ARG(type);
       
   203 	UNUSED_ARG(dbname);
       
   204 	UNUSED_ARG(tblname);
       
   205 	UNUSED_ARG(rowid);
       
   206 	}
       
   207 
       
   208 /* ///////////////////////////////////////////////////////////////////////////////////// */
       
   209 
       
   210 /**
       
   211 @SYMTestCaseID			SYSLIB-SQLITE3-UT-4001
       
   212 @SYMTestCaseDesc		sqlite3_exec() tests.
       
   213 						List of called SQLITE3 functions:
       
   214 						 - sqlite3_exec;
       
   215 						 - sqlite3_errcode;
       
   216 						 - sqlite3_errmsg;
       
   217 						 - sqlite3_last_insert_rowid;
       
   218 						 - sqlite3_changes;
       
   219 						 - sqlite3_total_changes;
       
   220 						 - sqlite3_get_autocommit;
       
   221 @SYMTestPriority		High
       
   222 @SYMTestActions			sqlite3_exec() tests.
       
   223 @SYMTestExpectedResults Test must not fail
       
   224 @SYMREQ					REQ8782
       
   225 */
       
   226 static void TestExec()
       
   227 	{
       
   228 	int err;
       
   229 	sqlite_int64 lastrowid;
       
   230 	int val;
       
   231 	
       
   232 	TEST(TheDb != 0);
       
   233 	
       
   234 	err = sqlite3_exec(TheDb, "CREATE TABLE A(F1 INTEGER, F2 BIGINT, F3 REAL, F4 TEXT, F5 BLOB)", &exec_callback, 0, 0);
       
   235 	TEST2(err, SQLITE_OK);
       
   236 	
       
   237 	err = sqlite3_exec(TheDb, "INSERT INTO A VALUES(1, 1234567891234, 56.12, 'TEXT', x'313233343536')", &exec_callback, 0, 0);
       
   238 	TEST2(err, SQLITE_OK);
       
   239 	
       
   240 	err = sqlite3_errcode(TheDb);
       
   241 	TEST2(err, SQLITE_OK);
       
   242 	
       
   243 	(void)sqlite3_errmsg(TheDb);
       
   244 	
       
   245 	lastrowid = sqlite3_last_insert_rowid(TheDb);
       
   246 	TEST(lastrowid > 0LL);
       
   247 	
       
   248 	val = sqlite3_changes(TheDb);
       
   249 	TEST2(val, 1);
       
   250 	
       
   251 	val = sqlite3_total_changes(TheDb);
       
   252 	TEST(val >= 1);
       
   253 	
       
   254 	val = sqlite3_get_autocommit(TheDb);
       
   255 	TEST(val != 0);
       
   256 	}
       
   257 
       
   258 /* ///////////////////////////////////////////////////////////////////////////////////// */
       
   259 
       
   260 static void DoTestStatement()
       
   261 	{
       
   262 	int err, val;
       
   263 	sqlite_int64 val64;
       
   264 	double dblval;
       
   265 	const unsigned char* textval;
       
   266 	const unsigned short* textval16;
       
   267 	const unsigned char* blob;
       
   268 	const char *coltype, *colname;
       
   269 	const unsigned short *coltype16, *colname16;
       
   270 	sqlite3* db;
       
   271 
       
   272 	TEST(TheDb != 0);
       
   273 	TEST(TheStmt != 0);
       
   274 	
       
   275 	val = sqlite3_column_count(TheStmt);
       
   276 	TEST2(val, 5);
       
   277 	
       
   278 	db = sqlite3_db_handle(TheStmt);
       
   279 	TEST2((unsigned int)db, (unsigned int)TheDb);
       
   280 	
       
   281 	err = sqlite3_step(TheStmt);
       
   282 	TEST2(err, SQLITE_ROW);
       
   283 	
       
   284 #ifdef SQLITE_ENABLE_COLUMN_METADATA
       
   285 	sqlite3_column_database_name(TheStmt, 0);
       
   286 	sqlite3_column_database_name16(TheStmt, 1);
       
   287 	sqlite3_column_table_name(TheStmt, 2);
       
   288 	sqlite3_column_table_name16(TheStmt, 3);
       
   289 	sqlite3_column_origin_name(TheStmt, 4);
       
   290 	sqlite3_column_origin_name16(TheStmt, 0);
       
   291 #endif	
       
   292 
       
   293 	coltype = sqlite3_column_decltype(TheStmt, 0);
       
   294 	TEST2(strcmp(coltype, "INTEGER"), 0);
       
   295 	
       
   296 	coltype16 = (const unsigned short*)sqlite3_column_decltype16(TheStmt, 2);
       
   297 	TEST2(wcscmp(coltype16, L"REAL"), 0);
       
   298 
       
   299 	colname = sqlite3_column_name(TheStmt, 1);
       
   300 	TEST2(strcmp(colname, "F2"), 0);
       
   301 	
       
   302 	colname16 = (const unsigned short *)sqlite3_column_name16(TheStmt, 4);
       
   303 	TEST2(wcscmp(colname16, L"F5"), 0);
       
   304 
       
   305 	val = sqlite3_column_int(TheStmt, 0);
       
   306 	TEST2(val, 1);
       
   307 	
       
   308 	val64 = sqlite3_column_int64(TheStmt, 1);
       
   309 	TEST64(val64, 1234567891234LL);
       
   310 	
       
   311 	dblval = sqlite3_column_double(TheStmt, 2);
       
   312 	TEST(dabs(dblval - 56.12) < 0.00001);
       
   313 
       
   314 	textval = sqlite3_column_text(TheStmt, 3);
       
   315 	TEST2(strcmp((const char*)textval, "TEXT"), 0);
       
   316 
       
   317 	textval16 = sqlite3_column_text16(TheStmt, 3);
       
   318 	TEST2(wcscmp(textval16, L"TEXT"), 0);
       
   319 
       
   320 	blob = (const unsigned char*)sqlite3_column_blob(TheStmt, 4);
       
   321 	TEST2(memcmp(blob, "123456", 6), 0);
       
   322 
       
   323 	err = sqlite3_step(TheStmt);
       
   324 	TEST2(err, SQLITE_DONE);
       
   325 	}
       
   326 
       
   327 /* ///////////////////////////////////////////////////////////////////////////////////// */
       
   328 
       
   329 /**
       
   330 @SYMTestCaseID			SYSLIB-SQLITE3-UT-4002
       
   331 @SYMTestCaseDesc		Statement handle SQLITE3 tests.
       
   332 						List of called SQLITE3 functions:
       
   333 						 - sqlite3_complete;
       
   334 						 - sqlite3_complete16;
       
   335 						 - sqlite3_prepare;
       
   336 						 - sqlite3_finalize;
       
   337 						 - sqlite3_column_count;
       
   338 						 - sqlite3_db_handle;
       
   339 						 - sqlite3_step;
       
   340 						 - sqlite3_column_decltype;
       
   341 						 - sqlite3_column_decltype16;
       
   342 						 - sqlite3_column_name;
       
   343 						 - sqlite3_column_name16;
       
   344 						 - sqlite3_column_int;
       
   345 						 - sqlite3_column_int64;
       
   346 						 - sqlite3_column_double;
       
   347 						 - sqlite3_column_text;
       
   348 						 - sqlite3_column_text16;
       
   349 						 - sqlite3_column_blob;
       
   350 @SYMTestPriority		High
       
   351 @SYMTestActions			Statement handle SQLITE3 tests.
       
   352 @SYMTestExpectedResults Test must not fail
       
   353 @SYMREQ					REQ8782
       
   354 */
       
   355 static void TestStatement1()
       
   356 	{
       
   357   	const char* tail = 0;
       
   358 	int err;
       
   359 	
       
   360 	TEST(TheDb != 0);
       
   361 	TEST(!TheStmt);
       
   362 
       
   363 	err = sqlite3_complete("SELECT * FROM A;");
       
   364 	TEST(err != 0);
       
   365 	
       
   366 	err = sqlite3_complete16(L"SELECT * FROM A;");
       
   367 	TEST(err != 0);
       
   368 	
       
   369 	err = sqlite3_prepare(TheDb, "SELECT * FROM A", -1, &TheStmt, &tail);
       
   370 	TEST2(err, SQLITE_OK);
       
   371 	TEST((unsigned int)TheStmt);
       
   372 	TEST(!tail || strlen(tail) == 0);
       
   373 	
       
   374 	DoTestStatement();
       
   375 	
       
   376 	err = sqlite3_finalize(TheStmt);
       
   377 	TEST2(err, SQLITE_OK);
       
   378 	TheStmt = 0;
       
   379 	}
       
   380 
       
   381 /* ///////////////////////////////////////////////////////////////////////////////////// */
       
   382 
       
   383 /**
       
   384 @SYMTestCaseID			SYSLIB-SQLITE3-UT-4003
       
   385 @SYMTestCaseDesc		Statement handle SQLITE3 tests.
       
   386 						List of called SQLITE3 functions:
       
   387 						 - sqlite3_prepare;
       
   388 						 - sqlite3_finalize;
       
   389 						 - sqlite3_bind_int;
       
   390 						 - sqlite3_bind_int64;
       
   391 						 - sqlite3_bind_double;
       
   392 						 - sqlite3_bind_text;
       
   393 @SYMTestPriority		High
       
   394 @SYMTestActions			Statement handle SQLITE3 tests.
       
   395 @SYMTestExpectedResults Test must not fail
       
   396 @SYMREQ					REQ8782
       
   397 */
       
   398 static void TestStatement2()
       
   399 	{
       
   400   	const char* tail = 0;
       
   401 	int err;
       
   402 	
       
   403 	TEST(TheDb != 0);
       
   404 	TEST(!TheStmt);
       
   405 	
       
   406 	err = sqlite3_prepare(TheDb, "SELECT * FROM A WHERE F1=? AND F2=? AND F3<? AND F4=?", -1, &TheStmt, &tail);
       
   407 	TEST2(err, SQLITE_OK);
       
   408 	TEST((unsigned int)TheStmt);
       
   409 	TEST(!tail || strlen(tail) == 0);
       
   410 	
       
   411 	err = sqlite3_bind_int(TheStmt, 1, 1);
       
   412 	TEST2(err, SQLITE_OK);
       
   413 	
       
   414 	err = sqlite3_bind_int64(TheStmt, 2, 1234567891234LL);
       
   415 	TEST2(err, SQLITE_OK);
       
   416 	
       
   417 	err = sqlite3_bind_double(TheStmt, 3, 70.0);
       
   418 	TEST2(err, SQLITE_OK);
       
   419 	
       
   420 	err = sqlite3_bind_text(TheStmt, 4, "TEXT", -1, SQLITE_STATIC);
       
   421 	TEST2(err, SQLITE_OK);
       
   422 	
       
   423 	DoTestStatement();
       
   424 	
       
   425 	err = sqlite3_finalize(TheStmt);
       
   426 	TEST2(err, SQLITE_OK);
       
   427 	TheStmt = 0;
       
   428 	}
       
   429 
       
   430 /* ///////////////////////////////////////////////////////////////////////////////////// */
       
   431 
       
   432 /**
       
   433 @SYMTestCaseID			PDS-SQLITE3-UT-4038
       
   434 @SYMTestCaseDesc		Database handle SQLITE3 tests.
       
   435 						List of called SQLITE3 functions:
       
   436 						 - sqlite3_db_status;
       
   437 						 - sqlite3_file_control;
       
   438 						 - sqlite3_limit;
       
   439 						 - sqlite3_next_stmt;
       
   440 						 - sqlite3_randomness;
       
   441 @SYMTestPriority		High
       
   442 @SYMTestActions			Database handle SQLITE3 tests.
       
   443 @SYMTestExpectedResults Test must not fail
       
   444 @SYMREQ					REQ10424
       
   445 */
       
   446 static void TestSqliteApi2()
       
   447 	{
       
   448 	int used = 0;
       
   449 	int high = 0;
       
   450 	int lock = -1;
       
   451 	int limit = 0;
       
   452 	sqlite3_stmt* next = 0;
       
   453 	int err;
       
   454 	unsigned char buf[10];
       
   455 	
       
   456 	TEST(TheDb != 0);
       
   457 	
       
   458 	err = sqlite3_db_status(TheDb, SQLITE_DBSTATUS_LOOKASIDE_USED, &used, &high, 0);
       
   459 	TEST2(err, SQLITE_OK);
       
   460 	PrintI("Lookaside slots: %d\r\n", used);
       
   461 	PrintI("Max used lookaside slots: %d\r\n", high);
       
   462 		
       
   463 	err = sqlite3_file_control(TheDb, "main", SQLITE_FCNTL_LOCKSTATE, &lock);
       
   464 	TEST2(err, SQLITE_OK);
       
   465 	TEST2(lock, SQLITE_LOCK_NONE);
       
   466 	
       
   467 	limit = sqlite3_limit(TheDb, SQLITE_LIMIT_LENGTH, -1);
       
   468 	TEST(limit > 0);
       
   469 		
       
   470 	next = sqlite3_next_stmt(TheDb, 0);
       
   471 	TEST(!next);
       
   472 		
       
   473 	memset(buf, 0, sizeof(buf));
       
   474 	sqlite3_randomness(8, buf);
       
   475 
       
   476 	memset(buf, 0, sizeof(buf));
       
   477 	sqlite3_randomness(7, buf);
       
   478 
       
   479 	memset(buf, 0, sizeof(buf));
       
   480 	sqlite3_randomness(3, buf);
       
   481 
       
   482 	}
       
   483 
       
   484 /* ///////////////////////////////////////////////////////////////////////////////////// */
       
   485 
       
   486 /**
       
   487 @SYMTestCaseID			PDS-SQLITE3-UT-4039
       
   488 @SYMTestCaseDesc		SQLITE3 - blob API tests.
       
   489 						List of called SQLITE3 functions:
       
   490 						 - sqlite3_bind_zeroblob;
       
   491 						 - sqlite3_blob_bytes;
       
   492 						 - sqlite3_blob_close;
       
   493 						 - sqlite3_blob_open;
       
   494 						 - sqlite3_blob_read;
       
   495 						 - sqlite3_blob_write;
       
   496 						 - sqlite3_sql;
       
   497 @SYMTestPriority		High
       
   498 @SYMTestActions			SQLITE3 - blob API tests.
       
   499 @SYMTestExpectedResults Test must not fail
       
   500 @SYMREQ					REQ10424
       
   501 */
       
   502 static void TestSqliteBlobApi()
       
   503 	{
       
   504 	int err;
       
   505 	const char* tail = 0;
       
   506 	sqlite3_blob* blob = 0;
       
   507 	int bytes = 0;
       
   508 	const char KBlobData[] = "ABCDEFGH";
       
   509 	char sql[100];
       
   510 	const int KBlobLen = strlen(KBlobData);
       
   511 	const char* sql2 = 0;
       
   512 	const char KSqlFmt[] = "UPDATE BlobTbl SET B=:Prm WHERE ROWID=1";
       
   513 	
       
   514 	TEST(TheDb != 0);
       
   515 	TEST(!TheStmt);
       
   516 
       
   517 	/* Create the table, insert one record with a blob */
       
   518 	
       
   519 	err = sqlite3_exec(TheDb, "CREATE TABLE BlobTbl(I INTEGER PRIMARY KEY, B BLOB)", 0, 0, 0);
       
   520 	TEST2(err, SQLITE_OK);
       
   521 
       
   522 	sprintf(sql, "INSERT INTO BlobTbl VALUES(1, zeroblob(%d))", KBlobLen);
       
   523 	err = sqlite3_exec(TheDb, sql, 0, 0, 0);
       
   524 	TEST2(err, SQLITE_OK);
       
   525 
       
   526 	err = sqlite3_prepare_v2(TheDb, KSqlFmt, -1, &TheStmt, &tail);
       
   527 	TEST2(err, SQLITE_OK);
       
   528 	TEST(TheStmt != 0);
       
   529 
       
   530 	sql2 = sqlite3_sql(TheStmt);
       
   531 	TEST(sql2 != NULL);
       
   532 	err = strcmp(sql2, KSqlFmt);
       
   533 	TEST2(err, 0);
       
   534 
       
   535 	err = sqlite3_bind_zeroblob(TheStmt, 1, KBlobLen);
       
   536 	TEST2(err, SQLITE_OK);
       
   537 
       
   538 	err = sqlite3_step(TheStmt);
       
   539 	TEST2(err, SQLITE_DONE);
       
   540 
       
   541 	err = sqlite3_finalize(TheStmt);
       
   542 	TEST2(err, SQLITE_OK);
       
   543 	TheStmt = 0;
       
   544 
       
   545 	/* Open the blob and write to it */
       
   546 	
       
   547 	err = sqlite3_blob_open(TheDb, "main", "BlobTbl", "B", 1, 1, &blob);
       
   548 	TEST2(err, SQLITE_OK);
       
   549 	TEST(blob != 0);
       
   550 
       
   551 	bytes = sqlite3_blob_bytes(blob);
       
   552 	TEST2(bytes, KBlobLen);
       
   553 
       
   554 	err = sqlite3_blob_write(blob, KBlobData, KBlobLen, 0);
       
   555 	TEST2(err, SQLITE_OK);
       
   556 
       
   557 	err = sqlite3_blob_close(blob);
       
   558 	TEST2(err, SQLITE_OK);
       
   559 	blob = 0;
       
   560 
       
   561 	/* Open the blob and read from it */
       
   562 
       
   563 	err = sqlite3_blob_open(TheDb, "main", "BlobTbl", "B", 1, 1, &blob);
       
   564 	TEST2(err, SQLITE_OK);
       
   565 	TEST(blob != 0);
       
   566 
       
   567 	bytes = sqlite3_blob_bytes(blob);
       
   568 	TEST2(bytes, KBlobLen);
       
   569 
       
   570 	err = sqlite3_blob_read(blob, sql, KBlobLen, 0);
       
   571 	TEST2(err, SQLITE_OK);
       
   572 	sql[bytes] = 0;
       
   573 
       
   574 	err = sqlite3_blob_close(blob);
       
   575 	TEST2(err, SQLITE_OK);
       
   576 	blob = 0;
       
   577 
       
   578 	err = strcmp(sql, KBlobData);
       
   579 	TEST2(err, 0);
       
   580 
       
   581 	err = sqlite3_exec(TheDb, "DROP TABLE BlobTbl", 0, 0, 0);
       
   582 	TEST2(err, SQLITE_OK);
       
   583 	}
       
   584 
       
   585 /* ///////////////////////////////////////////////////////////////////////////////////// */
       
   586 
       
   587 /**
       
   588 @SYMTestCaseID			PDS-SQLITE3-UT-4040
       
   589 @SYMTestCaseDesc		SQLITE3 - mutex API tests.
       
   590 						List of called SQLITE3 functions:
       
   591 						 - sqlite3_mutex_alloc;
       
   592 						 - sqlite3_mutex_enter;
       
   593 						 - sqlite3_mutex_free;
       
   594 						 - sqlite3_mutex_held;
       
   595 						 - sqlite3_mutex_leave;
       
   596 						 - sqlite3_mutex_notheld;
       
   597 						 - sqlite3_mutex_try;
       
   598 @SYMTestPriority		High
       
   599 @SYMTestActions			SQLITE3 - mutex API tests.
       
   600 @SYMTestExpectedResults Test must not fail
       
   601 @SYMREQ					REQ10424
       
   602 */
       
   603 static void TestSqliteMutexApi()
       
   604 	{
       
   605 	sqlite3_mutex* mtx = 0;
       
   606 	int err;
       
   607 	int type;
       
   608 	
       
   609 	TEST(TheDb != 0);
       
   610 		
       
   611 	for(type=SQLITE_MUTEX_FAST;type<=SQLITE_MUTEX_STATIC_LRU2;++type)
       
   612 		{
       
   613 		mtx = sqlite3_mutex_alloc(type);
       
   614 		TEST(mtx != NULL);
       
   615 
       
   616 		err = sqlite3_mutex_try(mtx);
       
   617 		TEST(err == SQLITE_BUSY || err == SQLITE_OK);
       
   618 
       
   619 		sqlite3_mutex_enter(mtx);
       
   620 
       
   621 		sqlite3_mutex_leave(mtx);
       
   622 		
       
   623 		if(type <= SQLITE_MUTEX_RECURSIVE)
       
   624 			{
       
   625 			sqlite3_mutex_free(mtx);
       
   626 			}
       
   627 		mtx = 0;
       
   628 		}
       
   629 	}
       
   630 
       
   631 /* ///////////////////////////////////////////////////////////////////////////////////// */
       
   632 
       
   633 /**
       
   634 @SYMTestCaseID			SYSLIB-SQLITE3-UT-4004
       
   635 @SYMTestCaseDesc		Database handle SQLITE3 tests.
       
   636 						List of called SQLITE3 functions:
       
   637 						 - sqlite3_config;
       
   638 						 - sqlite3_initialize;
       
   639 						 - sqlite3_threadsafe;
       
   640 						 - sqlite3_vfs_find;
       
   641 						 - sqlite3_open;
       
   642 						 - sqlite3_db_config;
       
   643 						 - sqlite3_libversion;
       
   644 						 - sqlite3_libversion_number;
       
   645 						 - sqlite3_set_authorizer;
       
   646 						 - sqlite3_commit_hook;
       
   647 						 - sqlite3_rollback_hook;
       
   648 						 - sqlite3_update_hook;
       
   649 						 - sqlite3_close;
       
   650 						 - sqlite3_shutdown;
       
   651 @SYMTestPriority		High
       
   652 @SYMTestActions			Database handle SQLITE3 tests.
       
   653 @SYMTestExpectedResults Test must not fail
       
   654 @SYMREQ					REQ8782
       
   655 */
       
   656 static void TestSqliteApi()
       
   657 	{
       
   658 	void* prev = 0;
       
   659 	const char* libverstr = 0;
       
   660 	int libvernum = 0;
       
   661 	int err;
       
   662 	int threadSafe = -1;
       
   663 	sqlite3_vfs* vfs = 0;
       
   664 
       
   665 	TEST(!TheDb);
       
   666 	
       
   667 	TestStart("@SYMTestCaseID:SYSLIB-SQLITE3-UT-4004: Test \"sqlite3_config()\"");
       
   668 	err = sqlite3_config(SQLITE_CONFIG_MEMSTATUS, 0);
       
   669 	TEST2(err, SQLITE_OK);
       
   670 	
       
   671 	TestNext("Test \"sqlite3_initialize()\"");
       
   672 	err = sqlite3_initialize();
       
   673 	TEST2(err, SQLITE_OK);
       
   674 
       
   675 	TestNext("Test \"sqlite3_threadsafe()\"");
       
   676 	threadSafe = sqlite3_threadsafe();
       
   677 	PrintI("SQLITE_THREADSAFE=%d\r\n", threadSafe);
       
   678 	
       
   679 	vfs = sqlite3_vfs_find(0);
       
   680 	TEST(vfs != NULL);
       
   681 	PrintS("Vfs name=\"%s\"\r\n", vfs->zName);
       
   682 	
       
   683 	err = sqlite3_open(TheTestDbName, &TheDb);
       
   684 	TEST2(err, SQLITE_OK);
       
   685 	TEST(TheDb != 0);
       
   686 	
       
   687 	err = sqlite3_db_config(TheDb, SQLITE_DBCONFIG_LOOKASIDE, 0, 128, 100);
       
   688 	TEST2(err, SQLITE_OK);
       
   689 	
       
   690 	libverstr = sqlite3_libversion();
       
   691 	libvernum = sqlite3_libversion_number();
       
   692 	PrintSI("SQLITE version: \"%s\", Number: %d\r\n", libverstr, libvernum);
       
   693 	
       
   694 	err = sqlite3_set_authorizer(TheDb, &authorizer_callback, 0);
       
   695 	TEST2(err, SQLITE_OK);
       
   696 	
       
   697 	prev = sqlite3_commit_hook(TheDb, &commit_hook, 0);
       
   698 	TEST(!prev);
       
   699 	prev = sqlite3_rollback_hook(TheDb, &rollback_hook, 0);
       
   700 	TEST(!prev);
       
   701 	prev = sqlite3_update_hook(TheDb, &update_hook, 0);
       
   702 	TEST(!prev);
       
   703 	
       
   704 	TestNext("@SYMTestCaseID:SYSLIB-SQLITE3-UT-4001: Test \"sqlite3\" handle API");
       
   705 	TestExec();
       
   706 	TestNext("@SYMTestCaseID:SYSLIB-SQLITE3-UT-4002: Test \"sqlite3_stmt\" handle API-1");
       
   707 	TestStatement1();
       
   708 	TestNext("@SYMTestCaseID:SYSLIB-SQLITE3-UT-4003: Test \"sqlite3_stmt\" handle API-2");
       
   709 	TestStatement2();
       
   710 	TestNext("@SYMTestCaseID:PDS-SQLITE3-UT-4038: Test more sqlite3 API");
       
   711 	TestSqliteApi2();
       
   712 	TestNext("@SYMTestCaseID:PDS-SQLITE3-UT-4039: Test blob API");
       
   713 	TestSqliteBlobApi();
       
   714 	TestNext("@SYMTestCaseID:PDS-SQLITE3-UT-4040: Test mutex API");
       
   715 	TestSqliteMutexApi();
       
   716 
       
   717 	err = sqlite3_close(TheDb);
       
   718 	TEST2(err, SQLITE_OK);
       
   719 	TheDb = 0;
       
   720 	
       
   721 	TestNext("Test \"sqlite3_shutdown()\"");
       
   722 	err = sqlite3_shutdown();
       
   723 	TEST2(err, SQLITE_OK);
       
   724 	
       
   725 	err = remove(TheTestDbName);
       
   726 	TEST2(err, 0);
       
   727 	}
       
   728 
       
   729 static void CreateTestDb()
       
   730 	{
       
   731 	int err;
       
   732 	
       
   733 	err = sqlite3_open(TheTestDbName, &TheDb);
       
   734 	TEST2(err, SQLITE_OK);
       
   735 	TEST(TheDb != 0);
       
   736 
       
   737 	err = sqlite3_exec(TheDb, "CREATE TABLE A(F1 INTEGER, F2 BIGINT, F3 REAL, F4 TEXT, F5 BLOB)", &exec_callback, 0, 0);
       
   738 	TEST2(err, SQLITE_OK);
       
   739 	
       
   740 	err = sqlite3_exec(TheDb, "INSERT INTO A VALUES(1, 1234567891234, 56.12, 'TEXT', x'313233343536')", &exec_callback, 0, 0);
       
   741 	TEST2(err, SQLITE_OK);
       
   742 
       
   743 	err = sqlite3_close(TheDb);
       
   744 	TEST2(err, SQLITE_OK);
       
   745 	TheDb = 0;
       
   746 	}
       
   747 	
       
   748 /**
       
   749 @SYMTestCaseID			SYSLIB-SQLITE3-UT-4005
       
   750 @SYMTestCaseDesc		Two database connections in the same thread - "read operations" test. 
       
   751 						The test creates two connections to the same database in the same thread.
       
   752 						Both connections prepare a SELECT statement and call sqlite3_step() each
       
   753 						thus testing that the file locking in the OS porting layer works properly
       
   754 						(the SQLite library permits multiple connections to read from the database simultaneously,
       
   755 						using a shared file locking mode).
       
   756 						List of called SQLITE3 functions:
       
   757 						 - sqlite3_open;
       
   758 						 - sqlite3_prepare;
       
   759 						 - sqlite3_step;
       
   760 						 - sqlite3_finalize;
       
   761 						 - sqlite3_close;
       
   762 @SYMTestPriority		High
       
   763 @SYMTestActions			Two database connections in the same thread - "read operations" test.
       
   764 @SYMTestExpectedResults Test must not fail
       
   765 @SYMREQ					REQ8782
       
   766 */
       
   767 static void TwoReadersTest()
       
   768 	{
       
   769 	int err;
       
   770   	const char* tail = 0;
       
   771 
       
   772 	TestNext("@SYMTestCaseID:SYSLIB-SQLITE3-UT-4005: Test two readers");
       
   773   	
       
   774 	CreateTestDb();
       
   775 
       
   776 	err = sqlite3_open(TheTestDbName, &TheDb);
       
   777 	TEST2(err, SQLITE_OK);
       
   778 	TEST(TheDb != 0);
       
   779 
       
   780 	err = sqlite3_open(TheTestDbName, &TheDb2);
       
   781 	TEST2(err, SQLITE_OK);
       
   782 	TEST(TheDb2 != 0);
       
   783 
       
   784 	/* ------------- */
       
   785 	err = sqlite3_prepare(TheDb, "SELECT * FROM A", -1, &TheStmt, &tail);
       
   786 	TEST2(err, SQLITE_OK);
       
   787 	TEST((unsigned int)TheStmt);
       
   788 	TEST(!tail || strlen(tail) == 0);
       
   789 
       
   790 	err = sqlite3_prepare(TheDb2, "SELECT * FROM A", -1, &TheStmt2, &tail);
       
   791 	TEST2(err, SQLITE_OK);
       
   792 	TEST((unsigned int)TheStmt2);
       
   793 	TEST(!tail || strlen(tail) == 0);
       
   794 
       
   795 	err = sqlite3_step(TheStmt);
       
   796 	TEST2(err, SQLITE_ROW);
       
   797 
       
   798 	err = sqlite3_step(TheStmt2);
       
   799 	TEST2(err, SQLITE_ROW);
       
   800 	
       
   801 	/* ------------- */
       
   802 
       
   803 	(void)sqlite3_finalize(TheStmt2);
       
   804 	TheStmt2 = 0;
       
   805 	
       
   806 	(void)sqlite3_finalize(TheStmt);
       
   807 	TheStmt = 0;
       
   808 
       
   809 	err = sqlite3_close(TheDb2);
       
   810 	TEST2(err, SQLITE_OK);
       
   811 	TheDb2 = 0;
       
   812 
       
   813 	err = sqlite3_close(TheDb);
       
   814 	TEST2(err, SQLITE_OK);
       
   815 	TheDb = 0;
       
   816 	
       
   817 	(void)remove(TheTestDbName);
       
   818 	}
       
   819 
       
   820 /* ///////////////////////////////////////////////////////////////////////////////////// */
       
   821 
       
   822 /**
       
   823 @SYMTestCaseID			SYSLIB-SQLITE3-UT-4006
       
   824 @SYMTestCaseDesc		Two database connections in the same thread - "write operations" test.
       
   825 						The test creates two connections to the same database in the same thread.
       
   826 						Both connections attempt to execute INSERT statements using an explicit transaction
       
   827 						thus testing that the file locking in the OS porting layer works properly
       
   828 						(the SQLite library permits only one connection at a time to write to the database).
       
   829 						List of called SQLITE3 functions:
       
   830 						 - sqlite3_open;
       
   831 						 - sqlite3_exec;
       
   832 						 - sqlite3_free;
       
   833 						 - sqlite3_close;
       
   834 						Test case steps:
       
   835 						 - Two connections to the same database created;
       
   836 						 - Both connections begin a deferred transaction;
       
   837 						 - The first connection executes an INSERT statement. The database file should be locked
       
   838 						   for a writing by this connection;
       
   839 						 - The second connection attempts to execute an INSERT statement too. The operation should fail,
       
   840 						   because the database file has been locked by the first connection;
       
   841 						 - The first connection completes the transaction executing a ROLLBACK statement;
       
   842 						 - The second connection makes second attempt to execute the INSERT statement. The execution should 
       
   843 						   complete without errors;
       
   844 @SYMTestPriority		High
       
   845 @SYMTestActions			Two database connections in the same thread - "write operations" test.
       
   846 @SYMTestExpectedResults Test must not fail
       
   847 @SYMREQ					REQ8782
       
   848 */
       
   849 static void TwoWritersTest()
       
   850 	{
       
   851 	int err;
       
   852 	char* errmsg = 0;
       
   853 
       
   854 	TestNext("@SYMTestCaseID:SYSLIB-SQLITE3-UT-4006: Test two writers");
       
   855   	
       
   856 	CreateTestDb();
       
   857 	
       
   858 	err = sqlite3_open(TheTestDbName, &TheDb);
       
   859 	TEST2(err, SQLITE_OK);
       
   860 	TEST(TheDb != 0);
       
   861 
       
   862 	err = sqlite3_open(TheTestDbName, &TheDb2);
       
   863 	TEST2(err, SQLITE_OK);
       
   864 	TEST(TheDb2 != 0);
       
   865 
       
   866 	/* ------------- */
       
   867 	Print("Two database connections, begin a transaction in each of them.\r\n");
       
   868 	err = sqlite3_exec(TheDb, "BEGIN", 0, 0, 0);
       
   869 	TEST2(err, SQLITE_OK);
       
   870 
       
   871 	err = sqlite3_exec(TheDb2, "BEGIN", 0, 0, 0);
       
   872 	TEST2(err, SQLITE_OK);
       
   873 	
       
   874 	Print("Connection 1. Execute an \"INSERT\" statement. Success.\r\n");
       
   875 	err = sqlite3_exec(TheDb, "INSERT INTO A VALUES(0,0,0.0,'',x'00')", 0, 0, &errmsg);
       
   876 	if(errmsg)
       
   877 		{
       
   878 		PrintSI("Err msg: %s. Err: %d.\r\n", errmsg, err);
       
   879 		sqlite3_free(errmsg);
       
   880 		}
       
   881 	errmsg = 0;
       
   882 	TEST2(err, SQLITE_OK);
       
   883 	
       
   884 	Print("Connection 2. Execute an \"INSERT\" statement. Failure. The database is locked.\r\n");
       
   885 	err = sqlite3_exec(TheDb2, "INSERT INTO A VALUES(0,0,0.0,'',x'00')", 0, 0, &errmsg);
       
   886 	if(errmsg)
       
   887 		{
       
   888 		PrintSI("*** %s. Err: %d.\r\n", errmsg, err);
       
   889 		sqlite3_free(errmsg);
       
   890 		}
       
   891 	errmsg = 0;
       
   892 	TEST2(err, SQLITE_BUSY);
       
   893 
       
   894 	Print("Connection 1. Rollback. The database is unlocked.\r\n");
       
   895 	err = sqlite3_exec(TheDb, "ROLLBACK", 0, 0, 0);
       
   896 	TEST2(err, SQLITE_OK);
       
   897 
       
   898 	Print("Connection 2. Execute an \"INSERT\" statement. Success.\r\n");
       
   899 	err = sqlite3_exec(TheDb2, "INSERT INTO A VALUES(0,0,0.0,'',x'00')", 0, 0, &errmsg);
       
   900 	if(errmsg)
       
   901 		{
       
   902 		PrintSI("*** %s. Err: %d.\r\n", errmsg, err);
       
   903 		sqlite3_free(errmsg);
       
   904 		}
       
   905 	errmsg = 0;
       
   906 	TEST2(err, SQLITE_OK);
       
   907 
       
   908 	err = sqlite3_exec(TheDb2, "ROLLBACK", 0, 0, 0);
       
   909 	TEST2(err, SQLITE_OK);
       
   910 
       
   911 	Print("Close database connections.\r\n");
       
   912 	/* ------------- */
       
   913 	
       
   914 	err = sqlite3_close(TheDb2);
       
   915 	TEST2(err, SQLITE_OK);
       
   916 	TheDb2 = 0;
       
   917 
       
   918 	err = sqlite3_close(TheDb);
       
   919 	TEST2(err, SQLITE_OK);
       
   920 	TheDb = 0;
       
   921 	
       
   922 	(void)remove(TheTestDbName);
       
   923 	}
       
   924 
       
   925 /* ///////////////////////////////////////////////////////////////////////////////////// */
       
   926 
       
   927 static void* ThreadFunc(void* pname)
       
   928 	{
       
   929 	int records = 0, err, i;
       
   930 	sqlite3* db;
       
   931 	char* errmsg = 0;
       
   932 	int threadIdx = -1;
       
   933 	const int KRecordsCount = 500;
       
   934 	const int KCommitRecordsCount = 2;
       
   935 	
       
   936 	for(i=0;i<KTestThreadCount;++i)
       
   937 		{
       
   938 		if(strcmp(pname, KThreadNames[i]) == 0)
       
   939 			{
       
   940 			threadIdx = i;
       
   941 			break;
       
   942 			}
       
   943 		}
       
   944 	
       
   945 	PrintS("Thread \"%s\" - begin\r\n", (char*)pname);
       
   946 	err = sqlite3_open(TheTestDbName, &db);
       
   947 	TEST2(err, SQLITE_OK);
       
   948 	TEST(db != 0);
       
   949 	
       
   950 	while(records < KRecordsCount)
       
   951 		{
       
   952 		err = sqlite3_exec(db, "BEGIN", 0, 0, &errmsg);
       
   953 		if(err == SQLITE_OK)
       
   954 			{
       
   955 			err = sqlite3_exec(db, "INSERT INTO A VALUES(0,0,0.0,'',x'00')", 0, 0, &errmsg);
       
   956 			if(err == SQLITE_OK)
       
   957 				{
       
   958 				err = sqlite3_exec(db, "INSERT INTO A VALUES(0,0,0.0,'',x'00')", 0, 0, &errmsg);
       
   959 				if(err == SQLITE_OK)
       
   960 					{
       
   961 					err = sqlite3_exec(db, "COMMIT", 0, 0, &errmsg);
       
   962 					}
       
   963 				}
       
   964 			}
       
   965 		TEST(err == SQLITE_OK || err == SQLITE_BUSY);
       
   966 		if(err == SQLITE_OK)
       
   967 			{
       
   968 			TheInsertRecCnt[threadIdx]	+= KCommitRecordsCount;
       
   969 			records += 2;
       
   970 			}
       
   971 		else if(err == SQLITE_BUSY)
       
   972 			{
       
   973 			++TheLockErrCnt[threadIdx];
       
   974 			(void)sqlite3_exec(db, "ROLLBACK", 0, 0, 0);
       
   975 			if(errmsg)
       
   976 				{
       
   977 				PrintSI("Err msg: %s. Err: %d.\r\n", errmsg, err);
       
   978 				sqlite3_free(errmsg);
       
   979 				errmsg = 0;
       
   980 				}
       
   981 			usleep(100);
       
   982 			}
       
   983 		}
       
   984 
       
   985 	err = sqlite3_close(db);
       
   986 	TEST2(err, SQLITE_OK);
       
   987 	
       
   988 	PrintS("Thread \"%s\" - end\r\n", (char*)pname);
       
   989 	return &TheInsertRecCnt[threadIdx];
       
   990 	}
       
   991 
       
   992 /**
       
   993 @SYMTestCaseID			SYSLIB-SQLITE3-UT-4007
       
   994 @SYMTestCaseDesc		Two database connections from different threads - "write operations" test.
       
   995 						The test creates two connections to the same database from two different threads.
       
   996 						Both connections attempt to execute INSERT statements using an explicit transaction
       
   997 						thus testing that the file locking in the OS porting layer works properly
       
   998 						(the SQLite library permits only one connection at a time to write to the database).
       
   999 						The threads do not use any thread synchronisation objects (mutexes, critical sections, etc...).
       
  1000 						List of called SQLITE3 functions:
       
  1001 						 - sqlite3_open;
       
  1002 						 - sqlite3_exec;
       
  1003 						 - sqlite3_free;
       
  1004 						 - sqlite3_close;
       
  1005 						Test case steps:
       
  1006 						 - Two threads created;
       
  1007 						 - Each thread opens a connection to the same database;
       
  1008 						 - No thread synchronisation is used;
       
  1009 						 - Each database connection attempts to insert records in a loop using an explicit transaction;
       
  1010 						 - If the database file is currently locked by the other database connection, the INSERT operation
       
  1011 						   should fail with SQLITE_BUSY error. Otherwise the operation should complete successfully.
       
  1012 @SYMTestPriority		High
       
  1013 @SYMTestActions			Two database connections from different threads - "write operations" test.
       
  1014 @SYMTestExpectedResults Test must not fail
       
  1015 @SYMREQ					REQ8782
       
  1016 */
       
  1017 static void ThreadsTest()
       
  1018 	{
       
  1019 	pthread_t threadIds[KTestThreadCount];
       
  1020 	int err, i;
       
  1021 	
       
  1022 	TestNext("@SYMTestCaseID:SYSLIB-SQLITE3-UT-4007: Test two writers in two threads");
       
  1023 
       
  1024 	CreateTestDb();
       
  1025 	
       
  1026 	for(i=0;i<KTestThreadCount;++i)
       
  1027 		{
       
  1028 		err = pthread_create(&threadIds[i], 0, &ThreadFunc, (void*)KThreadNames[i]);
       
  1029 		TEST2(err, 0);
       
  1030 		}
       
  1031 	
       
  1032 	for(i=0;i<KTestThreadCount;++i)
       
  1033 		{
       
  1034 		(void)pthread_join(threadIds[i], 0);
       
  1035 		PrintIII("Thread %d records written: %d, \"db locked\" errors: %d\r\n", i + 1, TheInsertRecCnt[i], TheLockErrCnt[i]);
       
  1036 		}
       
  1037 	
       
  1038 	Print("Test two writers in two threads - end\r\n");
       
  1039 	(void)remove(TheTestDbName);
       
  1040 	}
       
  1041 
       
  1042 /* ///////////////////////////////////////////////////////////////////////////////////// */
       
  1043 	
       
  1044 static void* ThreadFunc1(void* parg)
       
  1045 	{
       
  1046 	int err;
       
  1047 	sqlite3* db;
       
  1048 
       
  1049 	UNUSED_ARG(parg);
       
  1050 	PrintS("Thread \"%s\" - begin\r\n", KThreadNames[0]);
       
  1051 	
       
  1052 	err = sqlite3_open(TheTestDbName, &db);
       
  1053 	TEST2(err, SQLITE_OK);
       
  1054 	TEST(db != 0);
       
  1055 	
       
  1056 	(void)sem_wait(&TheSemaphores[0]);/* Wait for a notification from the main thread to begin */
       
  1057 	
       
  1058 	err = sqlite3_exec(db, "BEGIN", 0, 0, 0);
       
  1059 	TEST2(err, SQLITE_OK);
       
  1060 	
       
  1061 	err = sqlite3_exec(db, "INSERT INTO A VALUES(0,0,0.0,'',x'00')", 0, 0, 0);
       
  1062 	TEST2(err, SQLITE_OK);
       
  1063 	
       
  1064 	(void)sem_post(&TheSemaphores[1]);/* The database is locked now. Notify thread 2 to attempt an INSERT operation */
       
  1065 	(void)sem_wait(&TheSemaphores[0]);/* Wait for a notification from thread 2 to continue with the COMMIT operation */
       
  1066 
       
  1067 	err = sqlite3_exec(db, "COMMIT", 0, 0, 0);
       
  1068 	TEST2(err, SQLITE_OK);
       
  1069 	
       
  1070 	(void)sem_post(&TheSemaphores[1]);/* The database is unlocked. Notify thread 2 to attempt the INSERT operation again */
       
  1071 	
       
  1072 	err = sqlite3_close(db);
       
  1073 	TEST2(err, SQLITE_OK);
       
  1074 	
       
  1075 	PrintS("Thread \"%s\" - end\r\n", KThreadNames[0]);
       
  1076 	return 0;
       
  1077 	}
       
  1078 
       
  1079 static void* ThreadFunc2(void* parg)
       
  1080 	{
       
  1081 	int err;
       
  1082 	sqlite3* db;
       
  1083 	
       
  1084 	UNUSED_ARG(parg);
       
  1085 	PrintS("Thread \"%s\" - begin\r\n", KThreadNames[1]);
       
  1086 	
       
  1087 	err = sqlite3_open(TheTestDbName, &db);
       
  1088 	TEST2(err, SQLITE_OK);
       
  1089 	TEST(db != 0);
       
  1090 	
       
  1091 	(void)sem_wait(&TheSemaphores[1]);/* Wait for a notification from thread 1 to attempt an INSERT operation */
       
  1092 	
       
  1093 	err = sqlite3_exec(db, "INSERT INTO A VALUES(0,0,0.0,'',x'00')", 0, 0, 0);
       
  1094 	TEST2(err, SQLITE_BUSY);
       
  1095 	
       
  1096 	(void)sem_post(&TheSemaphores[0]);/* The database is locked. Notify thread 1 to commit and unlock the database */
       
  1097 	(void)sem_wait(&TheSemaphores[1]);/* Wait for a notification from thread 1 to attempt the INSERT operation again */
       
  1098 
       
  1099 	err = sqlite3_exec(db, "INSERT INTO A VALUES(0,0,0.0,'',x'00')", 0, 0, 0);
       
  1100 	TEST2(err, SQLITE_OK);
       
  1101 	
       
  1102 	err = sqlite3_close(db);
       
  1103 	TEST2(err, SQLITE_OK);
       
  1104 	
       
  1105 	PrintS("Thread \"%s\" - end\r\n", KThreadNames[1]);
       
  1106 	return 0;
       
  1107 	}
       
  1108 
       
  1109 /**
       
  1110 @SYMTestCaseID			SYSLIB-SQLITE3-UT-4008
       
  1111 @SYMTestCaseDesc		Two database connections from different threads - synchronised "write operations" test.
       
  1112 						The test creates two connections to the same database from two different threads.
       
  1113 						Both connections attempt to execute INSERT statements using an explicit transaction
       
  1114 						thus testing that the file locking in the OS porting layer works properly
       
  1115 						(the SQLite library permits only one connection at a time to write to the database).
       
  1116 						The threads use semaphores in order to synchronise the INSERT statements execution.
       
  1117 						List of called SQLITE3 functions:
       
  1118 						 - sqlite3_open;
       
  1119 						 - sqlite3_exec;
       
  1120 						 - sqlite3_free;
       
  1121 						 - sqlite3_close;
       
  1122 						Test case steps:
       
  1123 						 - Two threads created;
       
  1124 						 - Each thread opens a connection to the same database;
       
  1125 						 - The first thread begins an explicit transaction and inserts one record to the database. 
       
  1126 						   The database file is locked. The first thread notifies the second thread and blocks
       
  1127 						   waiting for a notification from the second thread;
       
  1128 						 - The second thread attempts to execute an INSERT statement. Since the database file is locked 
       
  1129 						   by the first thread the INSERT statement execution fails with SQLITE_BUSY. The second thread 
       
  1130 						   sends a notification to the first thread to continue and blocks waiting for a notification;
       
  1131 						 - The first thread commits the transaction thus unlocking the database file. A notification is
       
  1132 						   sent to the second thread;
       
  1133 						 - The second thread makes second attempt to INSERT a record and this time the operation should 
       
  1134 						   complete successfully;
       
  1135 @SYMTestPriority		High
       
  1136 @SYMTestActions			Two database connections from different threads - synchronised "write operations" test.
       
  1137 @SYMTestExpectedResults Test must not fail
       
  1138 @SYMREQ					REQ8782
       
  1139 */
       
  1140 static void TwoSyncThreadsTest()
       
  1141 	{
       
  1142 	const int KTestThreadCount = 2;
       
  1143 	int i, err;
       
  1144 	pthread_t threadIds[KTestThreadCount] = {0};
       
  1145 	thread_begin_routine threadFuncs[KTestThreadCount] = {&ThreadFunc1, &ThreadFunc2};
       
  1146 
       
  1147 	TestNext("@SYMTestCaseID:SYSLIB-SQLITE3-UT-4008: Test two writers in two synchronized threads");
       
  1148 	
       
  1149 	CreateTestDb();
       
  1150 
       
  1151 	for(i=0;i<KTestThreadCount;++i)
       
  1152 		{
       
  1153 		err = sem_init(&TheSemaphores[i], 0, 0);
       
  1154 		TEST2(err, 0);
       
  1155 		}
       
  1156 	
       
  1157 	for(i=0;i<KTestThreadCount;++i)
       
  1158 		{
       
  1159 		err = pthread_create(&threadIds[i], 0, threadFuncs[i], 0);
       
  1160 		TEST2(err, 0);
       
  1161 		}
       
  1162 	
       
  1163 	(void)sem_post(&TheSemaphores[0]);/* Notify thread 1 to begin */
       
  1164 
       
  1165 	for(i=0;i<KTestThreadCount;++i)
       
  1166 		{
       
  1167 		(void)pthread_join(threadIds[i], 0);
       
  1168 		}
       
  1169 	
       
  1170 	(void)remove(TheTestDbName);
       
  1171 	
       
  1172 	for(i=0;i<KTestThreadCount;++i)
       
  1173 		{
       
  1174 		err = sem_destroy(&TheSemaphores[i]);
       
  1175 		TEST2(err, 0);
       
  1176 		}
       
  1177 	}
       
  1178 
       
  1179 /**
       
  1180 @SYMTestCaseID			SYSLIB-SQLITE3-UT-4009
       
  1181 @SYMTestCaseDesc		Two database connections "RFileBuf64" test.
       
  1182 						This test verifies that the file buffer used by the SQLITE OS porting layer works properly.
       
  1183 						Test steps (in time order):
       
  1184 						1) Two connections to the same database are created.
       
  1185 						2) The first connection creates a table and executes a couple of INSERT statements.
       
  1186 						3) The second connection prepares a SELECT statement against the table, created in (2)
       
  1187 						Now, if the file buffer in the OS porting layer does not work as expected, step (3) fails,
       
  1188 						because at step (1) the database size is 0 and that is what the file buffer, used by the second 
       
  1189 						connection, "knows". The "prepare SQL statement" call will fail with a "no such table" error.
       
  1190 						But if the file buffer works properly, the buffer will be flushed during the "lock file" operation
       
  1191 						inside the OS porting layer and reloaded with the database data later.
       
  1192 @SYMTestPriority		High
       
  1193 @SYMTestActions			Two database connections "RFileBuf64" test.
       
  1194 @SYMTestExpectedResults Test must not fail
       
  1195 @SYMREQ					REQ8782
       
  1196 */
       
  1197 static void TwoConnectionsTest(void)
       
  1198 	{
       
  1199 	int err;
       
  1200   	const char* tail = 0;
       
  1201   	const char* errmsg = 0;
       
  1202 
       
  1203 	TestNext("@SYMTestCaseID:SYSLIB-SQLITE3-UT-4009: Two database connections test (OS porting layer, RFileBuf64 related)");
       
  1204 	(void)remove(TheTestDbName);
       
  1205 	
       
  1206 	TEST(!TheDb);
       
  1207 	err = sqlite3_open(TheTestDbName, &TheDb);
       
  1208 	TEST2(err, SQLITE_OK);
       
  1209 	TEST(TheDb != 0);
       
  1210 
       
  1211 	TEST(!TheDb2);
       
  1212 	err = sqlite3_open(TheTestDbName, &TheDb2);
       
  1213 	TEST2(err, SQLITE_OK);
       
  1214 	TEST(TheDb2 != 0);
       
  1215 	
       
  1216 	err = sqlite3_exec(TheDb, "CREATE TABLE t1(x);INSERT INTO t1 VALUES(1);INSERT INTO t1 VALUES(2);SELECT * FROM t1", 0, 0, 0);
       
  1217 	TEST2(err, SQLITE_OK);
       
  1218 	
       
  1219 	TEST(!TheStmt);
       
  1220 	err = sqlite3_prepare(TheDb2, "SELECT * FROM t1", -1, &TheStmt, &tail);
       
  1221 	if(err != SQLITE_OK)
       
  1222 		{
       
  1223 		errmsg = sqlite3_errmsg(TheDb2);
       
  1224 		PrintSI("*** Stmt prepare err msg: \"%s\". Error: %d\r\n", errmsg, err);
       
  1225 		}
       
  1226 	TEST2(err, SQLITE_OK);
       
  1227 	TEST((unsigned int)TheStmt);
       
  1228 	TEST(!tail || strlen(tail) == 0);
       
  1229 
       
  1230 	err = sqlite3_step(TheStmt);
       
  1231 	TEST2(err, SQLITE_ROW);
       
  1232 
       
  1233 	err = sqlite3_finalize(TheStmt);
       
  1234 	TEST2(err, SQLITE_OK);
       
  1235 	TheStmt = 0;
       
  1236 
       
  1237 	err = sqlite3_close(TheDb2);
       
  1238 	TEST2(err, SQLITE_OK);
       
  1239 	TheDb2 = 0;
       
  1240 
       
  1241 	err = sqlite3_close(TheDb);
       
  1242 	TEST2(err, SQLITE_OK);
       
  1243 	TheDb = 0;
       
  1244 	(void)remove(TheTestDbName);
       
  1245 	}
       
  1246 
       
  1247 static void UdfInsertFunc(sqlite3_context* aCtx, int aCnt, sqlite3_value** aValues)
       
  1248 	{
       
  1249 	int err;
       
  1250   	const char* tail = 0;
       
  1251   	sqlite3* db = 0;
       
  1252   	
       
  1253 	TEST2(aCnt, 1);
       
  1254 	
       
  1255 	db = sqlite3_context_db_handle(aCtx);/* to test that sqlite3_context_db_handle() can be called */
       
  1256 	TEST(db != 0);
       
  1257 	
       
  1258 	TEST(!TheStmt);
       
  1259 	err = sqlite3_prepare(TheDb, "INSERT INTO t1(x) VALUES(:Val)", -1, &TheStmt, &tail);
       
  1260 	if(err == SQLITE_OK)
       
  1261 		{
       
  1262 		err = sqlite3_bind_value(TheStmt, 1, aValues[0]);
       
  1263 		if(err == SQLITE_OK)
       
  1264 			{
       
  1265 			err = sqlite3_step(TheStmt);
       
  1266 			}
       
  1267 		}
       
  1268 	(void)sqlite3_finalize(TheStmt);
       
  1269 	TheStmt = 0;
       
  1270 	
       
  1271 	sqlite3_result_int(aCtx, err);		
       
  1272 	}
       
  1273 
       
  1274 /**
       
  1275 @SYMTestCaseID			SYSLIB-SQLITE3-UT-4027
       
  1276 @SYMTestCaseDesc		sqlite3_bind_value() and sqlite3_column_value() test.
       
  1277 						List of covered SQLITE3 functions:
       
  1278 						 - sqlite3_bind_value();
       
  1279 						 - sqlite3_column_value();
       
  1280 						Test case steps: 
       
  1281 						 - The test creates and calls a user defined function - UdfInsertFunc().
       
  1282 						 - The user defined function prepares and executes an INSERT statement,
       
  1283 							where the sqlite3_bind_value() call is used to bind the passed from the caller
       
  1284 							integer column value.
       
  1285 						 - After the user defined function call completes, the test prepares a SELECT
       
  1286 						    statement to verify the just inserted column value using sqlite3_column_int()
       
  1287 @SYMTestPriority		High
       
  1288 @SYMTestActions			sqlite3_bind_value() and sqlite3_column_int() test.
       
  1289 @SYMTestExpectedResults Test must not fail
       
  1290 @SYMREQ					REQ8782
       
  1291 */
       
  1292 static void UdfTest()
       
  1293 	{
       
  1294 	int err;
       
  1295 	int val;
       
  1296   	const char* tail = 0;
       
  1297   	const int KTestColumnValue = 11234;
       
  1298   	char sqlBuf[100];
       
  1299 
       
  1300 	TestNext("@SYMTestCaseID:SYSLIB-SQLITE3-UT-4027: User defined function test");
       
  1301 
       
  1302 	(void)remove(TheTestDbName);
       
  1303 	TEST(!TheDb);
       
  1304 	err = sqlite3_open(TheTestDbName, &TheDb);
       
  1305 	TEST2(err, SQLITE_OK);
       
  1306 	TEST(TheDb != 0);
       
  1307 
       
  1308 	err = sqlite3_exec(TheDb, "CREATE TABLE t1(x INTEGER)", 0, 0, 0);
       
  1309 	TEST2(err, SQLITE_OK);
       
  1310 
       
  1311 	err = sqlite3_create_function(TheDb, "UdfInsert", 1, SQLITE_UTF8, NULL, &UdfInsertFunc, NULL, NULL);
       
  1312 	TEST2(err, SQLITE_OK);
       
  1313 
       
  1314 	//Execute an INSERT statement via UDF
       
  1315 	TEST(!TheStmt2);
       
  1316 	sprintf(sqlBuf, "SELECT UdfInsert(%d)", KTestColumnValue);
       
  1317 	err = sqlite3_prepare(TheDb, sqlBuf, -1, &TheStmt2, &tail);
       
  1318 	TEST2(err, SQLITE_OK);
       
  1319 	err = sqlite3_step(TheStmt2);
       
  1320 	TEST2(err, SQLITE_ROW);
       
  1321 	val = sqlite3_column_int(TheStmt2, 0);
       
  1322 	TEST2(val, SQLITE_DONE);
       
  1323 	(void)sqlite3_finalize(TheStmt2);
       
  1324 	TheStmt2 = 0;
       
  1325 
       
  1326 	//Verify the inserted column value
       
  1327 	TEST(!TheStmt2);
       
  1328 	err = sqlite3_prepare(TheDb, "SELECT x FROM t1", -1, &TheStmt2, &tail);
       
  1329 	TEST2(err, SQLITE_OK);
       
  1330 	err = sqlite3_step(TheStmt2);
       
  1331 	TEST2(err, SQLITE_ROW);
       
  1332 	val = sqlite3_column_int(TheStmt2,0);
       
  1333 	TEST2(val, KTestColumnValue);
       
  1334 	(void)sqlite3_finalize(TheStmt2);
       
  1335 	TheStmt2 = 0;
       
  1336 
       
  1337 	err = sqlite3_close(TheDb);
       
  1338 	TEST2(err, SQLITE_OK);
       
  1339 	TheDb = 0;
       
  1340 	(void)remove(TheTestDbName);
       
  1341 	}
       
  1342 
       
  1343 /* ///////////////////////////////////////////////////////////////////////////////////// */
       
  1344 
       
  1345 int main(int argc, void** argv)
       
  1346 	{
       
  1347 	UNUSED_ARG(argc);
       
  1348 	UNUSED_ARG(argv);
       
  1349 
       
  1350 	TestTitle();
       
  1351 
       
  1352 	TestHeapMark();
       
  1353 		
       
  1354 	TestEnvCreate();
       
  1355 	
       
  1356 	TestSqliteApi();
       
  1357 	TwoReadersTest();
       
  1358 	TwoWritersTest();
       
  1359 	ThreadsTest();
       
  1360 	TwoSyncThreadsTest();
       
  1361 	TwoConnectionsTest();
       
  1362 	UdfTest();
       
  1363 	
       
  1364 	TestEnvDestroy();
       
  1365 
       
  1366 	TestHeapMarkEnd();
       
  1367 
       
  1368 	TestEnd();
       
  1369 	TestClose();
       
  1370 	
       
  1371 	return 0;	
       
  1372 	}