From 8a595be54f84b0207ccd15a106b6bc0fbf8be7b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 10 May 2020 12:38:04 +0200 Subject: [PATCH] Test for the immutable flag https://sqlite.org/uri.html#uriimmutable --- Tests/GRDBTests/DatabasePoolTests.swift | 53 +++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 4 deletions(-) diff --git a/Tests/GRDBTests/DatabasePoolTests.swift b/Tests/GRDBTests/DatabasePoolTests.swift index af195068e..a7d794cf9 100644 --- a/Tests/GRDBTests/DatabasePoolTests.swift +++ b/Tests/GRDBTests/DatabasePoolTests.swift @@ -14,7 +14,8 @@ class DatabasePoolTests: GRDBTestCase { func testPersistentWALModeEnabled() throws { let path: String do { - dbConfiguration.prepareDatabase = { db in + var configuration = dbConfiguration! + configuration.prepareDatabase = { db in var flag: CInt = 1 let code = withUnsafeMutablePointer(to: &flag) { flagP in sqlite3_file_control(db.sqliteConnection, nil, SQLITE_FCNTL_PERSIST_WAL, flagP) @@ -23,7 +24,7 @@ class DatabasePoolTests: GRDBTestCase { throw DatabaseError(resultCode: ResultCode(rawValue: code)) } } - let dbPool = try makeDatabasePool() + let dbPool = try makeDatabasePool(configuration: configuration) path = dbPool.path } let fm = FileManager() @@ -35,7 +36,8 @@ class DatabasePoolTests: GRDBTestCase { func testPersistentWALModeDisabled() throws { let path: String do { - dbConfiguration.prepareDatabase = { db in + var configuration = dbConfiguration! + configuration.prepareDatabase = { db in var flag: CInt = 0 let code = withUnsafeMutablePointer(to: &flag) { flagP in sqlite3_file_control(db.sqliteConnection, nil, SQLITE_FCNTL_PERSIST_WAL, flagP) @@ -44,7 +46,7 @@ class DatabasePoolTests: GRDBTestCase { throw DatabaseError(resultCode: ResultCode(rawValue: code)) } } - let dbPool = try makeDatabasePool() + let dbPool = try makeDatabasePool(configuration: configuration) path = dbPool.path } let fm = FileManager() @@ -52,4 +54,47 @@ class DatabasePoolTests: GRDBTestCase { XCTAssertFalse(fm.fileExists(atPath: path + "-wal")) XCTAssertFalse(fm.fileExists(atPath: path + "-shm")) } + + func testReadonlyAccessWithPersistentWALModeDisabled() throws { + // Create a WAL database without -shm and -wal temporary files: + let path: String + do { + var configuration = dbConfiguration! + configuration.prepareDatabase = { db in + var flag: CInt = 0 + let code = withUnsafeMutablePointer(to: &flag) { flagP in + sqlite3_file_control(db.sqliteConnection, nil, SQLITE_FCNTL_PERSIST_WAL, flagP) + } + guard code == SQLITE_OK else { + throw DatabaseError(resultCode: ResultCode(rawValue: code)) + } + } + let dbPool = try makeDatabasePool(configuration: configuration) + path = dbPool.path + } + + // CAN NOT open readonly connection to WAL database without -shm + // and -wal temporary files: + // https://www.sqlite.org/walformat.html#operations_that_require_locks_and_which_locks_those_operations_use + do { + var configuration = dbConfiguration! + configuration.readonly = true + _ = try DatabaseQueue(path: path, configuration: configuration) + XCTFail("Expected error") + } catch DatabaseError.SQLITE_CANTOPEN { + } + + // CAN open readonly connection to WAL database without -shm + // and -wal temporary files, if database is declared as immutable: + // https://www.sqlite.org/walformat.html#operations_that_require_locks_and_which_locks_those_operations_use + do { + var urlComponents = URLComponents(url: URL(fileURLWithPath: path), resolvingAgainstBaseURL: false)! + urlComponents.queryItems = [URLQueryItem(name: "immutable", value: "1")] + let immutableURL = urlComponents.url! + + var configuration = dbConfiguration! + configuration.readonly = true + _ = try DatabaseQueue(path: immutableURL.absoluteString, configuration: configuration) + } + } }