Test for the immutable flag

https://sqlite.org/uri.html#uriimmutable
This commit is contained in:
Gwendal Roué committed 2020-05-10 12:38:04 +02:00
1 parent 68203b5373
commit 8a595be54f
1 file changed
+49 -4
+49 -4
View File
@@ -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)
}
}
}