AryaWu/sqlite
0
1#include "sqliteInt.h"2#include "unity.h"3 4#include <stdio.h>5#include <string.h>6#include <stdlib.h>7 8/* Some platforms may not define O/S unlink; use standard C remove() instead */9static void delete_file_if_exists(const char *zPath){10 if( zPath && zPath[0] ){11 /* Ignore errors */12 (void)remove(zPath);13 }14}15 16/* Helper: lock/unlock db mutex if non-NULL */17static void db_mutex_enter(sqlite3 *db){18 if( db && db->mutex ){19 sqlite3_mutex_enter(db->mutex);20 }21}22static void db_mutex_leave(sqlite3 *db){23 if( db && db->mutex ){24 sqlite3_mutex_leave(db->mutex);25 }26}27 28void setUp(void){29 /* no-op */30}31void tearDown(void){32 /* no-op */33}34 35/* Test opening an in-memory Btree (":memory:") */36void test_sqlite3BtreeOpen_memory_db_success(void){37 sqlite3 *db = NULL;38 int rc = sqlite3_open(":memory:", &db);39 TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);40 TEST_ASSERT_NOT_NULL(db);41 42 sqlite3_vfs *pVfs = sqlite3_vfs_find(NULL);43 TEST_ASSERT_NOT_NULL(pVfs);44 45 Btree *pBt = NULL;46 47 /* Lock DB mutex as sqlite3BtreeOpen asserts it is held */48 db_mutex_enter(db);49 rc = sqlite3BtreeOpen(50 pVfs,51 ":memory:",52 db,53 &pBt,54 0, /* flags */55 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MAIN_DB56 );57 db_mutex_leave(db);58 59 TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);60 TEST_ASSERT_NOT_NULL(pBt);61 TEST_ASSERT_NOT_NULL(pBt->pBt);62 63 /* Check that openFlags indicates BTREE_MEMORY for in-memory */64 TEST_ASSERT_TRUE( (pBt->pBt->openFlags & BTREE_MEMORY) != 0 );65 66 /* Basic sanity on page size */67 int pageSize = sqlite3BtreeGetPageSize(pBt);68 TEST_ASSERT_TRUE(pageSize >= 512 && pageSize <= SQLITE_MAX_PAGE_SIZE);69 70 /* Close Btree */71 db_mutex_enter(db);72 rc = sqlite3BtreeClose(pBt);73 db_mutex_leave(db);74 TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);75 76 sqlite3_close(db);77}78 79/* Test shared cache: two Btrees on same on-disk db share the same BtShared */80void test_sqlite3BtreeOpen_shared_cache_shares_BtShared(void){81 const char *zFile = "btree_open_test_shared.db";82 delete_file_if_exists(zFile);83 84 sqlite3 *db = NULL;85 int rc = sqlite3_open(":memory:", &db);86 TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);87 88 sqlite3_vfs *pVfs = sqlite3_vfs_find(NULL);89 TEST_ASSERT_NOT_NULL(pVfs);90 91 int vfsFlags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE92 | SQLITE_OPEN_MAIN_DB | SQLITE_OPEN_SHAREDCACHE;93 94 Btree *p1 = NULL;95 Btree *p2 = NULL;96 97 db_mutex_enter(db);98 rc = sqlite3BtreeOpen(pVfs, zFile, db, &p1, 0, vfsFlags);99 db_mutex_leave(db);100 TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);101 TEST_ASSERT_NOT_NULL(p1);102 TEST_ASSERT_NOT_NULL(p1->pBt);103 104 db_mutex_enter(db);105 rc = sqlite3BtreeOpen(pVfs, zFile, db, &p2, 0, vfsFlags);106 db_mutex_leave(db);107 TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);108 TEST_ASSERT_NOT_NULL(p2);109 TEST_ASSERT_NOT_NULL(p2->pBt);110 111 /* They should share the same BtShared when using shared-cache */112 TEST_ASSERT_TRUE(p1->pBt == p2->pBt);113 114 /* Connection count should reflect two users */115 int nConn = sqlite3BtreeConnectionCount(p1);116 TEST_ASSERT_TRUE(nConn >= 2);117 118 /* Close the first Btree and verify connection count decreases */119 db_mutex_enter(db);120 rc = sqlite3BtreeClose(p1);121 db_mutex_leave(db);122 TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);123 124 int nConnAfter = sqlite3BtreeConnectionCount(p2);125 TEST_ASSERT_TRUE(nConnAfter >= 1);126 127 /* Close the second and cleanup */128 db_mutex_enter(db);129 rc = sqlite3BtreeClose(p2);130 db_mutex_leave(db);131 TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);132 133 sqlite3_close(db);134 delete_file_if_exists(zFile);135}136 137/* Test opening an ephemeral database (zFilename == NULL) */138void test_sqlite3BtreeOpen_ephemeral_null_filename_success(void){139 sqlite3 *db = NULL;140 int rc = sqlite3_open(":memory:", &db);141 TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);142 143 sqlite3_vfs *pVfs = sqlite3_vfs_find(NULL);144 TEST_ASSERT_NOT_NULL(pVfs);145 146 Btree *pBt = NULL;147 db_mutex_enter(db);148 rc = sqlite3BtreeOpen(149 pVfs,150 NULL, /* zFilename==NULL -> ephemeral */151 db,152 &pBt,153 0,154 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MAIN_DB155 );156 db_mutex_leave(db);157 158 TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);159 TEST_ASSERT_NOT_NULL(pBt);160 TEST_ASSERT_NOT_NULL(pBt->pBt);161 162 /* Basic invariants */163 TEST_ASSERT_TRUE(sqlite3BtreeConnectionCount(pBt) >= 1);164 int pageSize = sqlite3BtreeGetPageSize(pBt);165 TEST_ASSERT_TRUE(pageSize >= 512 && pageSize <= SQLITE_MAX_PAGE_SIZE);166 167 /* Close Btree */168 db_mutex_enter(db);169 rc = sqlite3BtreeClose(pBt);170 db_mutex_leave(db);171 TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);172 173 sqlite3_close(db);174}175 176int main(void){177 UNITY_BEGIN();178 RUN_TEST(test_sqlite3BtreeOpen_memory_db_success);179 RUN_TEST(test_sqlite3BtreeOpen_shared_cache_shares_BtShared);180 RUN_TEST(test_sqlite3BtreeOpen_ephemeral_null_filename_success);181 return UNITY_END();182}