mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-27 04:14:21 +00:00
Deprecate Configuration.passphrase, introduce Database.usePassphrase(_:)
This commit is contained in:
1 parent
eb112c912c
commit
d644f01557
9 files changed
+252
-145
No files matched your search
@@ -78,10 +78,17 @@ public struct Configuration {
|
||||
// MARK: - Encryption
|
||||
|
||||
#if SQLITE_HAS_CODEC
|
||||
// TODO: remove when the deprecated passphrase turns unavailable.
|
||||
var _passphrase: String?
|
||||
|
||||
/// The passphrase for the encrypted database.
|
||||
///
|
||||
/// Default: nil
|
||||
public var passphrase: String?
|
||||
@available(*, deprecated, message: "Use Database.usePassphrase(_:) in Configuration.onConnect(execute:) instead.")
|
||||
public var passphrase: String? {
|
||||
get { return _passphrase }
|
||||
set { _passphrase = newValue }
|
||||
}
|
||||
#endif
|
||||
|
||||
// MARK: - Managing SQLite Connections
|
||||
@@ -99,7 +106,7 @@ public struct Configuration {
|
||||
/// config.prepareDatabase = { db in
|
||||
/// try db.execute(sql: "PRAGMA kdf_iter = 10000")
|
||||
/// }
|
||||
@available(*, deprecated, message: "Register the database preparation function with Configuration.onConnect { db in ... } instead") // swiftlint:disable:this line_length
|
||||
@available(*, deprecated, message: "Use onConnect instead")
|
||||
public var prepareDatabase: ((Database) throws -> Void)? {
|
||||
get { return _prepareDatabase }
|
||||
set { _prepareDatabase = newValue }
|
||||
|
||||
+26
-18
@@ -213,8 +213,8 @@ extension Database {
|
||||
|
||||
#if SQLITE_HAS_CODEC
|
||||
try validateSQLCipher()
|
||||
if let passphrase = configuration.passphrase {
|
||||
try setCipherPassphrase(passphrase)
|
||||
if let passphrase = configuration._passphrase {
|
||||
try usePassphrase(passphrase)
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -384,22 +384,6 @@ extension Database {
|
||||
""")
|
||||
}
|
||||
}
|
||||
|
||||
private func setCipherPassphrase(_ passphrase: String) throws {
|
||||
let data = passphrase.data(using: .utf8)!
|
||||
#if swift(>=5.0)
|
||||
let code = data.withUnsafeBytes {
|
||||
sqlite3_key(sqliteConnection, $0.baseAddress, Int32($0.count))
|
||||
}
|
||||
#else
|
||||
let code = data.withUnsafeBytes {
|
||||
sqlite3_key(sqliteConnection, $0, Int32(data.count))
|
||||
}
|
||||
#endif
|
||||
guard code == SQLITE_OK else {
|
||||
throw DatabaseError(resultCode: code, message: String(cString: sqlite3_errmsg(sqliteConnection)))
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
private func validateFormat() throws {
|
||||
@@ -974,6 +958,30 @@ extension Database {
|
||||
|
||||
// MARK: - Encryption
|
||||
|
||||
/// Sets the passphrase used to crypt and decrypt an SQLCipher database.
|
||||
///
|
||||
/// Call this method from Configuration.onConnect, as in the example below:
|
||||
///
|
||||
/// var config = Configuration()
|
||||
/// config.prepareDatabase = { db in
|
||||
/// try db.usePassphrase("secret")
|
||||
/// }
|
||||
public func usePassphrase(_ passphrase: String) throws {
|
||||
let data = passphrase.data(using: .utf8)!
|
||||
#if swift(>=5.0)
|
||||
let code = data.withUnsafeBytes {
|
||||
sqlite3_key(sqliteConnection, $0.baseAddress, Int32($0.count))
|
||||
}
|
||||
#else
|
||||
let code = data.withUnsafeBytes {
|
||||
sqlite3_key(sqliteConnection, $0, Int32(data.count))
|
||||
}
|
||||
#endif
|
||||
guard code == SQLITE_OK else {
|
||||
throw DatabaseError(resultCode: code, message: String(cString: sqlite3_errmsg(sqliteConnection)))
|
||||
}
|
||||
}
|
||||
|
||||
func change(passphrase: String) throws {
|
||||
// FIXME: sqlite3_rekey is discouraged.
|
||||
//
|
||||
|
||||
@@ -246,7 +246,7 @@ extension DatabasePool {
|
||||
try readerPool.barrier {
|
||||
try writer.sync { try $0.change(passphrase: passphrase) }
|
||||
readerPool.removeAll()
|
||||
readerConfig.passphrase = passphrase
|
||||
readerConfig._passphrase = passphrase
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,13 +40,14 @@
|
||||
let players = Player.filter(...) // Returns a request that filters on column A or column B depending on the argument
|
||||
players.update(...) // Runs the expected UPDATE statement
|
||||
```
|
||||
- [ ] Database.decrypt(with:)
|
||||
- [ ] Database.usePassphrase(_:)
|
||||
- [ ] In order to allow SQLCipher tests to run all tests in an encrypted database, we need to make it possible to register several independent preparation blocks:
|
||||
- [X] Deprecate DatabaseConfiguration.prepareDatabase
|
||||
- [X] Introduce DatabaseConfiguration.onConnect { ... } as a replacement for DatabaseConfiguration.prepareDatabase
|
||||
- [ ] Consider introducing DatabaseConfiguration.onDisconnect { ... }
|
||||
- [ ] Deprecate change(passphrase:), and provide a static function instead
|
||||
- [ ] Introduce Database.decrypt(with:). Make it a no-op if DatabaseConfiguration.passphrase is not nil.
|
||||
- [ ] Introduce Database.usePassphrase(_:).
|
||||
- [ ] Make Database.usePassphrase(_:) a no-op if DatabaseConfiguration._passphrase is not nil, as support for the deprecated change(passphrase:).
|
||||
- [ ] Deprecate DatabaseConfiguration.passphrase
|
||||
- [ ] Remove SQLiteConnectionDidOpen and
|
||||
|
||||
|
||||
@@ -8,15 +8,9 @@ import XCTest
|
||||
class DatabasePoolBackupTests: GRDBTestCase {
|
||||
|
||||
func testBackup() throws {
|
||||
#if GRDBCIPHER
|
||||
// SQLCipher can't backup encrypted databases: skip this test
|
||||
if dbConfiguration.passphrase != nil {
|
||||
return
|
||||
}
|
||||
#endif
|
||||
|
||||
let source = try makeDatabasePool(filename: "source.sqlite")
|
||||
let destination = try makeDatabasePool(filename: "destination.sqlite")
|
||||
// SQLCipher can't backup encrypted databases: use a pristine Configuration
|
||||
let source = try makeDatabasePool(filename: "source.sqlite", configuration: Configuration())
|
||||
let destination = try makeDatabasePool(filename: "destination.sqlite", configuration: Configuration())
|
||||
|
||||
try source.write { db in
|
||||
try db.execute(sql: "CREATE TABLE items (id INTEGER PRIMARY KEY)")
|
||||
|
||||
@@ -8,15 +8,9 @@ import XCTest
|
||||
class DatabaseQueueBackupTests: GRDBTestCase {
|
||||
|
||||
func testBackup() throws {
|
||||
#if GRDBCIPHER
|
||||
// SQLCipher can't backup encrypted databases: skip this test
|
||||
if dbConfiguration.passphrase != nil {
|
||||
return
|
||||
}
|
||||
#endif
|
||||
|
||||
let source = try makeDatabaseQueue(filename: "source.sqlite")
|
||||
let destination = try makeDatabaseQueue(filename: "destination.sqlite")
|
||||
// SQLCipher can't backup encrypted databases: use a pristine Configuration
|
||||
let source = try makeDatabaseQueue(filename: "source.sqlite", configuration: Configuration())
|
||||
let destination = try makeDatabaseQueue(filename: "destination.sqlite", configuration: Configuration())
|
||||
|
||||
try source.inDatabase { db in
|
||||
try db.execute(sql: "CREATE TABLE items (id INTEGER PRIMARY KEY)")
|
||||
|
||||
@@ -219,14 +219,8 @@ class DatabaseWriterTests : GRDBTestCase {
|
||||
|
||||
// See https://github.com/groue/GRDB.swift/issues/424
|
||||
func testIssue424Minimal() throws {
|
||||
#if GRDBCIPHER
|
||||
// SQLCipher can't backup encrypted databases: skip this test
|
||||
if dbConfiguration.passphrase != nil {
|
||||
return
|
||||
}
|
||||
#endif
|
||||
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
// SQLCipher can't backup encrypted databases: use a pristine Configuration
|
||||
let dbQueue = try makeDatabaseQueue(configuration: Configuration())
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: """
|
||||
CREATE TABLE t(a);
|
||||
|
||||
@@ -6,8 +6,11 @@ class EncryptionTests: GRDBTestCase {
|
||||
|
||||
func testDatabaseQueueWithPassphraseToDatabaseQueueWithPassphrase() throws {
|
||||
do {
|
||||
dbConfiguration.passphrase = "secret"
|
||||
let dbQueue = try makeDatabaseQueue(filename: "test.sqlite")
|
||||
var config = Configuration()
|
||||
config.onConnect { db in
|
||||
try db.usePassphrase("secret")
|
||||
}
|
||||
let dbQueue = try makeDatabaseQueue(filename: "test.sqlite", configuration: config)
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "CREATE TABLE data (value INTEGER)")
|
||||
try db.execute(sql: "INSERT INTO data (value) VALUES (1)")
|
||||
@@ -15,8 +18,11 @@ class EncryptionTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
do {
|
||||
dbConfiguration.passphrase = "secret"
|
||||
let dbQueue = try makeDatabaseQueue(filename: "test.sqlite")
|
||||
var config = Configuration()
|
||||
config.onConnect { db in
|
||||
try db.usePassphrase("secret")
|
||||
}
|
||||
let dbQueue = try makeDatabaseQueue(filename: "test.sqlite", configuration: config)
|
||||
try dbQueue.inDatabase { db in
|
||||
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM data")!, 1)
|
||||
}
|
||||
@@ -25,8 +31,11 @@ class EncryptionTests: GRDBTestCase {
|
||||
|
||||
func testDatabaseQueueWithPassphraseToDatabaseQueueWithoutPassphrase() throws {
|
||||
do {
|
||||
dbConfiguration.passphrase = "secret"
|
||||
let dbQueue = try makeDatabaseQueue(filename: "test.sqlite")
|
||||
var config = Configuration()
|
||||
config.onConnect { db in
|
||||
try db.usePassphrase("secret")
|
||||
}
|
||||
let dbQueue = try makeDatabaseQueue(filename: "test.sqlite", configuration: config)
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "CREATE TABLE data (value INTEGER)")
|
||||
try db.execute(sql: "INSERT INTO data (value) VALUES (1)")
|
||||
@@ -34,9 +43,8 @@ class EncryptionTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
do {
|
||||
dbConfiguration.passphrase = nil
|
||||
do {
|
||||
_ = try makeDatabaseQueue(filename: "test.sqlite")
|
||||
_ = try makeDatabaseQueue(filename: "test.sqlite", configuration: Configuration())
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_NOTADB)
|
||||
@@ -47,8 +55,11 @@ class EncryptionTests: GRDBTestCase {
|
||||
|
||||
func testDatabaseQueueWithPassphraseToDatabaseQueueWithWrongPassphrase() throws {
|
||||
do {
|
||||
dbConfiguration.passphrase = "secret"
|
||||
let dbQueue = try makeDatabaseQueue(filename: "test.sqlite")
|
||||
var config = Configuration()
|
||||
config.onConnect { db in
|
||||
try db.usePassphrase("secret")
|
||||
}
|
||||
let dbQueue = try makeDatabaseQueue(filename: "test.sqlite", configuration: config)
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "CREATE TABLE data (value INTEGER)")
|
||||
try db.execute(sql: "INSERT INTO data (value) VALUES (1)")
|
||||
@@ -56,9 +67,12 @@ class EncryptionTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
do {
|
||||
dbConfiguration.passphrase = "wrong"
|
||||
var config = Configuration()
|
||||
config.onConnect { db in
|
||||
try db.usePassphrase("wrong")
|
||||
}
|
||||
do {
|
||||
_ = try makeDatabaseQueue(filename: "test.sqlite")
|
||||
_ = try makeDatabaseQueue(filename: "test.sqlite", configuration: config)
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_NOTADB)
|
||||
@@ -69,8 +83,11 @@ class EncryptionTests: GRDBTestCase {
|
||||
|
||||
func testDatabaseQueueWithPassphraseToDatabaseQueueWithNewPassphrase() throws {
|
||||
do {
|
||||
dbConfiguration.passphrase = "secret"
|
||||
let dbQueue = try makeDatabaseQueue(filename: "test.sqlite")
|
||||
var config = Configuration()
|
||||
config.onConnect { db in
|
||||
try db.usePassphrase("secret")
|
||||
}
|
||||
let dbQueue = try makeDatabaseQueue(filename: "test.sqlite", configuration: config)
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "CREATE TABLE data (value INTEGER)")
|
||||
try db.execute(sql: "INSERT INTO data (value) VALUES (1)")
|
||||
@@ -78,8 +95,11 @@ class EncryptionTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
do {
|
||||
dbConfiguration.passphrase = "secret"
|
||||
let dbQueue = try makeDatabaseQueue(filename: "test.sqlite")
|
||||
var config = Configuration()
|
||||
config.onConnect { db in
|
||||
try db.usePassphrase("secret")
|
||||
}
|
||||
let dbQueue = try makeDatabaseQueue(filename: "test.sqlite", configuration: config)
|
||||
try dbQueue.change(passphrase: "newSecret")
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "INSERT INTO data (value) VALUES (2)")
|
||||
@@ -90,8 +110,11 @@ class EncryptionTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
do {
|
||||
dbConfiguration.passphrase = "newSecret"
|
||||
let dbQueue = try makeDatabaseQueue(filename: "test.sqlite")
|
||||
var config = Configuration()
|
||||
config.onConnect { db in
|
||||
try db.usePassphrase("newSecret")
|
||||
}
|
||||
let dbQueue = try makeDatabaseQueue(filename: "test.sqlite", configuration: config)
|
||||
try dbQueue.inDatabase { db in
|
||||
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM data")!, 2)
|
||||
}
|
||||
@@ -100,8 +123,11 @@ class EncryptionTests: GRDBTestCase {
|
||||
|
||||
func testDatabaseQueueWithPassphraseToDatabasePoolWithPassphrase() throws {
|
||||
do {
|
||||
dbConfiguration.passphrase = "secret"
|
||||
let dbQueue = try makeDatabaseQueue(filename: "test.sqlite")
|
||||
var config = Configuration()
|
||||
config.onConnect { db in
|
||||
try db.usePassphrase("secret")
|
||||
}
|
||||
let dbQueue = try makeDatabaseQueue(filename: "test.sqlite", configuration: config)
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "CREATE TABLE data (value INTEGER)")
|
||||
try db.execute(sql: "INSERT INTO data (value) VALUES (1)")
|
||||
@@ -109,8 +135,11 @@ class EncryptionTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
do {
|
||||
dbConfiguration.passphrase = "secret"
|
||||
let dbPool = try makeDatabasePool(filename: "test.sqlite")
|
||||
var config = Configuration()
|
||||
config.onConnect { db in
|
||||
try db.usePassphrase("secret")
|
||||
}
|
||||
let dbPool = try makeDatabasePool(filename: "test.sqlite", configuration: config)
|
||||
try dbPool.read { db in
|
||||
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM data")!, 1)
|
||||
}
|
||||
@@ -119,8 +148,11 @@ class EncryptionTests: GRDBTestCase {
|
||||
|
||||
func testDatabaseQueueWithPassphraseToDatabasePoolWithoutPassphrase() throws {
|
||||
do {
|
||||
dbConfiguration.passphrase = "secret"
|
||||
let dbQueue = try makeDatabaseQueue(filename: "test.sqlite")
|
||||
var config = Configuration()
|
||||
config.onConnect { db in
|
||||
try db.usePassphrase("secret")
|
||||
}
|
||||
let dbQueue = try makeDatabaseQueue(filename: "test.sqlite", configuration: config)
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "CREATE TABLE data (value INTEGER)")
|
||||
try db.execute(sql: "INSERT INTO data (value) VALUES (1)")
|
||||
@@ -128,9 +160,8 @@ class EncryptionTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
do {
|
||||
dbConfiguration.passphrase = nil
|
||||
do {
|
||||
_ = try makeDatabasePool(filename: "test.sqlite")
|
||||
_ = try makeDatabasePool(filename: "test.sqlite", configuration: Configuration())
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_NOTADB)
|
||||
@@ -141,8 +172,11 @@ class EncryptionTests: GRDBTestCase {
|
||||
|
||||
func testDatabaseQueueWithPassphraseToDatabasePoolWithWrongPassphrase() throws {
|
||||
do {
|
||||
dbConfiguration.passphrase = "secret"
|
||||
let dbQueue = try makeDatabaseQueue(filename: "test.sqlite")
|
||||
var config = Configuration()
|
||||
config.onConnect { db in
|
||||
try db.usePassphrase("secret")
|
||||
}
|
||||
let dbQueue = try makeDatabaseQueue(filename: "test.sqlite", configuration: config)
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "CREATE TABLE data (value INTEGER)")
|
||||
try db.execute(sql: "INSERT INTO data (value) VALUES (1)")
|
||||
@@ -150,9 +184,12 @@ class EncryptionTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
do {
|
||||
dbConfiguration.passphrase = "wrong"
|
||||
var config = Configuration()
|
||||
config.onConnect { db in
|
||||
try db.usePassphrase("wrong")
|
||||
}
|
||||
do {
|
||||
_ = try makeDatabasePool(filename: "test.sqlite")
|
||||
_ = try makeDatabasePool(filename: "test.sqlite", configuration: config)
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_NOTADB)
|
||||
@@ -163,8 +200,11 @@ class EncryptionTests: GRDBTestCase {
|
||||
|
||||
func testDatabaseQueueWithPassphraseToDatabasePoolWithNewPassphrase() throws {
|
||||
do {
|
||||
dbConfiguration.passphrase = "secret"
|
||||
let dbQueue = try makeDatabaseQueue(filename: "test.sqlite")
|
||||
var config = Configuration()
|
||||
config.onConnect { db in
|
||||
try db.usePassphrase("secret")
|
||||
}
|
||||
let dbQueue = try makeDatabaseQueue(filename: "test.sqlite", configuration: config)
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "CREATE TABLE data (value INTEGER)")
|
||||
try db.execute(sql: "INSERT INTO data (value) VALUES (1)")
|
||||
@@ -172,8 +212,11 @@ class EncryptionTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
do {
|
||||
dbConfiguration.passphrase = "secret"
|
||||
let dbPool = try makeDatabasePool(filename: "test.sqlite")
|
||||
var config = Configuration()
|
||||
config.onConnect { db in
|
||||
try db.usePassphrase("secret")
|
||||
}
|
||||
let dbPool = try makeDatabasePool(filename: "test.sqlite", configuration: config)
|
||||
try dbPool.change(passphrase: "newSecret")
|
||||
try dbPool.write { db in
|
||||
try db.execute(sql: "INSERT INTO data (value) VALUES (2)")
|
||||
@@ -182,8 +225,11 @@ class EncryptionTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
do {
|
||||
dbConfiguration.passphrase = "newSecret"
|
||||
let dbPool = try makeDatabasePool(filename: "test.sqlite")
|
||||
var config = Configuration()
|
||||
config.onConnect { db in
|
||||
try db.usePassphrase("newSecret")
|
||||
}
|
||||
let dbPool = try makeDatabasePool(filename: "test.sqlite", configuration: config)
|
||||
try dbPool.read { db in
|
||||
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM data")!, 2)
|
||||
}
|
||||
@@ -192,8 +238,11 @@ class EncryptionTests: GRDBTestCase {
|
||||
|
||||
func testDatabasePoolWithPassphraseToDatabasePoolWithPassphrase() throws {
|
||||
do {
|
||||
dbConfiguration.passphrase = "secret"
|
||||
let dbPool = try makeDatabasePool(filename: "test.sqlite")
|
||||
var config = Configuration()
|
||||
config.onConnect { db in
|
||||
try db.usePassphrase("secret")
|
||||
}
|
||||
let dbPool = try makeDatabasePool(filename: "test.sqlite", configuration: config)
|
||||
try dbPool.write { db in
|
||||
try db.execute(sql: "CREATE TABLE data (value INTEGER)")
|
||||
try db.execute(sql: "INSERT INTO data (value) VALUES (1)")
|
||||
@@ -201,8 +250,11 @@ class EncryptionTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
do {
|
||||
dbConfiguration.passphrase = "secret"
|
||||
let dbPool = try makeDatabasePool(filename: "test.sqlite")
|
||||
var config = Configuration()
|
||||
config.onConnect { db in
|
||||
try db.usePassphrase("secret")
|
||||
}
|
||||
let dbPool = try makeDatabasePool(filename: "test.sqlite", configuration: config)
|
||||
try dbPool.read { db in
|
||||
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM data")!, 1)
|
||||
}
|
||||
@@ -211,8 +263,11 @@ class EncryptionTests: GRDBTestCase {
|
||||
|
||||
func testDatabasePoolWithPassphraseToDatabasePoolWithoutPassphrase() throws {
|
||||
do {
|
||||
dbConfiguration.passphrase = "secret"
|
||||
let dbPool = try makeDatabasePool(filename: "test.sqlite")
|
||||
var config = Configuration()
|
||||
config.onConnect { db in
|
||||
try db.usePassphrase("secret")
|
||||
}
|
||||
let dbPool = try makeDatabasePool(filename: "test.sqlite", configuration: config)
|
||||
try dbPool.write { db in
|
||||
try db.execute(sql: "CREATE TABLE data (value INTEGER)")
|
||||
try db.execute(sql: "INSERT INTO data (value) VALUES (1)")
|
||||
@@ -220,9 +275,8 @@ class EncryptionTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
do {
|
||||
dbConfiguration.passphrase = nil
|
||||
do {
|
||||
_ = try makeDatabasePool(filename: "test.sqlite")
|
||||
_ = try makeDatabasePool(filename: "test.sqlite", configuration: Configuration())
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_NOTADB)
|
||||
@@ -233,8 +287,11 @@ class EncryptionTests: GRDBTestCase {
|
||||
|
||||
func testDatabasePoolWithPassphraseToDatabasePoolWithWrongPassphrase() throws {
|
||||
do {
|
||||
dbConfiguration.passphrase = "secret"
|
||||
let dbPool = try makeDatabasePool(filename: "test.sqlite")
|
||||
var config = Configuration()
|
||||
config.onConnect { db in
|
||||
try db.usePassphrase("secret")
|
||||
}
|
||||
let dbPool = try makeDatabasePool(filename: "test.sqlite", configuration: config)
|
||||
try dbPool.write { db in
|
||||
try db.execute(sql: "CREATE TABLE data (value INTEGER)")
|
||||
try db.execute(sql: "INSERT INTO data (value) VALUES (1)")
|
||||
@@ -242,9 +299,12 @@ class EncryptionTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
do {
|
||||
dbConfiguration.passphrase = "wrong"
|
||||
var config = Configuration()
|
||||
config.onConnect { db in
|
||||
try db.usePassphrase("wrong")
|
||||
}
|
||||
do {
|
||||
_ = try makeDatabasePool(filename: "test.sqlite")
|
||||
_ = try makeDatabasePool(filename: "test.sqlite", configuration: config)
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_NOTADB)
|
||||
@@ -256,8 +316,11 @@ class EncryptionTests: GRDBTestCase {
|
||||
func testDatabasePoolWithPassphraseToDatabasePoolWithNewPassphrase() throws {
|
||||
|
||||
do {
|
||||
dbConfiguration.passphrase = "secret"
|
||||
let dbPool = try makeDatabasePool(filename: "test.sqlite")
|
||||
var config = Configuration()
|
||||
config.onConnect { db in
|
||||
try db.usePassphrase("secret")
|
||||
}
|
||||
let dbPool = try makeDatabasePool(filename: "test.sqlite", configuration: config)
|
||||
try dbPool.write { db in
|
||||
try db.execute(sql: "CREATE TABLE data (value INTEGER)")
|
||||
try db.execute(sql: "INSERT INTO data (value) VALUES (1)")
|
||||
@@ -265,8 +328,11 @@ class EncryptionTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
do {
|
||||
dbConfiguration.passphrase = "secret"
|
||||
let dbPool = try makeDatabasePool(filename: "test.sqlite")
|
||||
var config = Configuration()
|
||||
config.onConnect { db in
|
||||
try db.usePassphrase("secret")
|
||||
}
|
||||
let dbPool = try makeDatabasePool(filename: "test.sqlite", configuration: config)
|
||||
try dbPool.change(passphrase: "newSecret")
|
||||
try dbPool.write { db in
|
||||
try db.execute(sql: "INSERT INTO data (value) VALUES (2)")
|
||||
@@ -275,8 +341,11 @@ class EncryptionTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
do {
|
||||
dbConfiguration.passphrase = "newSecret"
|
||||
let dbPool = try makeDatabasePool(filename: "test.sqlite")
|
||||
var config = Configuration()
|
||||
config.onConnect { db in
|
||||
try db.usePassphrase("newSecret")
|
||||
}
|
||||
let dbPool = try makeDatabasePool(filename: "test.sqlite", configuration: config)
|
||||
try dbPool.read { db in
|
||||
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM data")!, 2)
|
||||
}
|
||||
@@ -285,8 +354,7 @@ class EncryptionTests: GRDBTestCase {
|
||||
|
||||
func testDatabaseQueueWithPragmaPassphraseToDatabaseQueueWithPassphrase() throws {
|
||||
do {
|
||||
dbConfiguration.passphrase = nil
|
||||
let dbQueue = try makeDatabaseQueue(filename: "test.sqlite")
|
||||
let dbQueue = try makeDatabaseQueue(filename: "test.sqlite", configuration: Configuration())
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "PRAGMA key = 'secret'")
|
||||
try db.execute(sql: "CREATE TABLE data (value INTEGER)")
|
||||
@@ -295,8 +363,11 @@ class EncryptionTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
do {
|
||||
dbConfiguration.passphrase = "secret"
|
||||
let dbQueue = try makeDatabaseQueue(filename: "test.sqlite")
|
||||
var config = Configuration()
|
||||
config.onConnect { db in
|
||||
try db.usePassphrase("secret")
|
||||
}
|
||||
let dbQueue = try makeDatabaseQueue(filename: "test.sqlite", configuration: config)
|
||||
try dbQueue.inDatabase { db in
|
||||
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM data")!, 1)
|
||||
}
|
||||
@@ -305,8 +376,7 @@ class EncryptionTests: GRDBTestCase {
|
||||
|
||||
func testDatabaseQueueWithPragmaPassphraseToDatabaseQueueWithoutPassphrase() throws {
|
||||
do {
|
||||
dbConfiguration.passphrase = nil
|
||||
let dbQueue = try makeDatabaseQueue(filename: "test.sqlite")
|
||||
let dbQueue = try makeDatabaseQueue(filename: "test.sqlite", configuration: Configuration())
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "PRAGMA key = 'secret'")
|
||||
try db.execute(sql: "CREATE TABLE data (value INTEGER)")
|
||||
@@ -315,9 +385,8 @@ class EncryptionTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
do {
|
||||
dbConfiguration.passphrase = nil
|
||||
do {
|
||||
_ = try makeDatabaseQueue(filename: "test.sqlite")
|
||||
_ = try makeDatabaseQueue(filename: "test.sqlite", configuration: Configuration())
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_NOTADB)
|
||||
@@ -328,24 +397,26 @@ class EncryptionTests: GRDBTestCase {
|
||||
|
||||
func testCipherPageSize() throws {
|
||||
do {
|
||||
dbConfiguration.passphrase = "secret"
|
||||
dbConfiguration.onConnect { db in
|
||||
var config = Configuration()
|
||||
config.onConnect { db in
|
||||
try db.usePassphrase("secret")
|
||||
try db.execute(sql: "PRAGMA cipher_page_size = 8192")
|
||||
}
|
||||
|
||||
let dbQueue = try makeDatabaseQueue(filename: "test.sqlite")
|
||||
let dbQueue = try makeDatabaseQueue(filename: "test.sqlite", configuration: config)
|
||||
try dbQueue.inDatabase({ db in
|
||||
XCTAssertEqual(try Int.fetchOne(db, sql: "PRAGMA cipher_page_size")!, 8192)
|
||||
})
|
||||
}
|
||||
|
||||
do {
|
||||
dbConfiguration.passphrase = "secret"
|
||||
dbConfiguration.onConnect { db in
|
||||
var config = Configuration()
|
||||
config.onConnect { db in
|
||||
try db.usePassphrase("secret")
|
||||
try db.execute(sql: "PRAGMA cipher_page_size = 4096")
|
||||
}
|
||||
|
||||
let dbPool = try makeDatabasePool(filename: "testpool.sqlite")
|
||||
let dbPool = try makeDatabasePool(filename: "testpool.sqlite", configuration: config)
|
||||
try dbPool.write({ db in
|
||||
XCTAssertEqual(try Int.fetchOne(db, sql: "PRAGMA cipher_page_size")!, 4096)
|
||||
try db.execute(sql: "CREATE TABLE data(value INTEGER)")
|
||||
@@ -361,24 +432,26 @@ class EncryptionTests: GRDBTestCase {
|
||||
|
||||
func testCipherKDFIterations() throws {
|
||||
do {
|
||||
dbConfiguration.passphrase = "secret"
|
||||
dbConfiguration.onConnect { db in
|
||||
var config = Configuration()
|
||||
config.onConnect { db in
|
||||
try db.usePassphrase("secret")
|
||||
try db.execute(sql: "PRAGMA kdf_iter = 128000")
|
||||
}
|
||||
|
||||
let dbQueue = try makeDatabaseQueue(filename: "test.sqlite")
|
||||
let dbQueue = try makeDatabaseQueue(filename: "test.sqlite", configuration: config)
|
||||
try dbQueue.inDatabase { db in
|
||||
XCTAssertEqual(try Int.fetchOne(db, sql: "PRAGMA kdf_iter"), 128000)
|
||||
}
|
||||
}
|
||||
|
||||
do {
|
||||
dbConfiguration.passphrase = "secret"
|
||||
dbConfiguration.onConnect { db in
|
||||
var config = Configuration()
|
||||
config.onConnect { db in
|
||||
try db.usePassphrase("secret")
|
||||
try db.execute(sql: "PRAGMA kdf_iter = 128000")
|
||||
}
|
||||
|
||||
let dbPool = try makeDatabasePool(filename: "testpool.sqlite")
|
||||
let dbPool = try makeDatabasePool(filename: "testpool.sqlite", configuration: config)
|
||||
try dbPool.write { db in
|
||||
XCTAssertEqual(try Int.fetchOne(db, sql: "PRAGMA kdf_iter"), 128000)
|
||||
try db.execute(sql: "CREATE TABLE data(value INTEGER)")
|
||||
@@ -394,12 +467,13 @@ class EncryptionTests: GRDBTestCase {
|
||||
|
||||
func testCipherWithMismatchedKDFIterations() throws {
|
||||
do {
|
||||
dbConfiguration.passphrase = "secret"
|
||||
dbConfiguration.onConnect { db in
|
||||
var config = Configuration()
|
||||
config.onConnect { db in
|
||||
try db.usePassphrase("secret")
|
||||
try db.execute(sql: "PRAGMA kdf_iter = 128000")
|
||||
}
|
||||
|
||||
let dbPool = try makeDatabasePool(filename: "testpool.sqlite")
|
||||
let dbPool = try makeDatabasePool(filename: "testpool.sqlite", configuration: config)
|
||||
try dbPool.write { db in
|
||||
XCTAssertEqual(try Int.fetchOne(db, sql: "PRAGMA kdf_iter"), 128000)
|
||||
try db.execute(sql: "CREATE TABLE data(value INTEGER)")
|
||||
@@ -413,13 +487,14 @@ class EncryptionTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
do {
|
||||
dbConfiguration.passphrase = "secret"
|
||||
dbConfiguration.onConnect { db in
|
||||
var config = Configuration()
|
||||
config.onConnect { db in
|
||||
try db.usePassphrase("secret")
|
||||
try db.execute(sql: "PRAGMA kdf_iter = 64000")
|
||||
}
|
||||
|
||||
do {
|
||||
let dbPool = try makeDatabasePool(filename: "testpool.sqlite")
|
||||
let dbPool = try makeDatabasePool(filename: "testpool.sqlite", configuration: config)
|
||||
|
||||
try dbPool.read { db in
|
||||
XCTAssertEqual(try Int.fetchOne(db, sql: "PRAGMA kdf_iter"), 64000)
|
||||
@@ -436,29 +511,25 @@ class EncryptionTests: GRDBTestCase {
|
||||
func testExportPlainTextDatabaseToEncryptedDatabase() throws {
|
||||
// See https://discuss.zetetic.net/t/how-to-encrypt-a-plaintext-sqlite-database-to-use-sqlcipher-and-avoid-file-is-encrypted-or-is-not-a-database-errors/868?source_topic_id=939
|
||||
do {
|
||||
// https://github.com/sqlcipher/sqlcipher/issues/216
|
||||
// SQLCipher 3.4.1 crashes when sqlcipher_export() is called and a
|
||||
// trace hook has been installed. So we disable query tracing for
|
||||
// this test.
|
||||
dbConfiguration.trace = nil
|
||||
|
||||
dbConfiguration.passphrase = nil
|
||||
let plainTextDBQueue = try makeDatabaseQueue(filename: "plaintext.sqlite")
|
||||
let plainTextDBQueue = try makeDatabaseQueue(filename: "plaintext.sqlite", configuration: Configuration())
|
||||
try plainTextDBQueue.inDatabase { db in
|
||||
try db.execute(sql: "CREATE TABLE data (value INTEGER)")
|
||||
try db.execute(sql: "INSERT INTO data (value) VALUES (1)")
|
||||
}
|
||||
|
||||
dbConfiguration.passphrase = "secret"
|
||||
var config = Configuration()
|
||||
config.onConnect { db in
|
||||
try db.usePassphrase("secret")
|
||||
}
|
||||
do {
|
||||
_ = try makeDatabaseQueue(filename: "plaintext.sqlite")
|
||||
_ = try makeDatabaseQueue(filename: "plaintext.sqlite", configuration: config)
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_NOTADB)
|
||||
XCTAssertEqual(error.message!, "file is not a database")
|
||||
}
|
||||
|
||||
let encryptedDBQueue = try makeDatabaseQueue(filename: "encrypted.sqlite")
|
||||
let encryptedDBQueue = try makeDatabaseQueue(filename: "encrypted.sqlite", configuration: config)
|
||||
|
||||
try plainTextDBQueue.inDatabase { db in
|
||||
try db.execute(sql: "ATTACH DATABASE ? AS encrypted KEY ?", arguments: [encryptedDBQueue.path, "secret"])
|
||||
@@ -468,8 +539,11 @@ class EncryptionTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
do {
|
||||
dbConfiguration.passphrase = "secret"
|
||||
let dbQueue = try makeDatabaseQueue(filename: "encrypted.sqlite")
|
||||
var config = Configuration()
|
||||
config.onConnect { db in
|
||||
try db.usePassphrase("secret")
|
||||
}
|
||||
let dbQueue = try makeDatabaseQueue(filename: "encrypted.sqlite", configuration: config)
|
||||
try dbQueue.inDatabase { db in
|
||||
XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM data")!, 1)
|
||||
}
|
||||
@@ -487,8 +561,8 @@ class EncryptionTests: GRDBTestCase {
|
||||
let testBundle = Bundle(for: type(of: self))
|
||||
let path = testBundle.url(forResource: "db", withExtension: "SQLCipher3")!.path
|
||||
var configuration = Configuration()
|
||||
configuration.passphrase = "secret"
|
||||
configuration.onConnect { db in
|
||||
try db.usePassphrase("secret")
|
||||
try db.execute(sql: "PRAGMA cipher_compatibility = 3")
|
||||
}
|
||||
|
||||
@@ -505,5 +579,38 @@ class EncryptionTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// func testDeprecatedPassphrase() throws {
|
||||
// do {
|
||||
// var config = Configuration()
|
||||
// config.passphrase = "secret"
|
||||
// let dbQueue = try makeDatabaseQueue(filename: "test.sqlite", configuration: config)
|
||||
// try dbQueue.inDatabase { db in
|
||||
// try db.execute(sql: "CREATE TABLE data (value INTEGER)")
|
||||
// try db.execute(sql: "INSERT INTO data (value) VALUES (1)")
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// do {
|
||||
// var config = Configuration()
|
||||
// config.passphrase = "secret"
|
||||
// let dbQueue = try makeDatabaseQueue(filename: "test.sqlite", configuration: config)
|
||||
// try dbQueue.inDatabase { db in
|
||||
// XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM data")!, 1)
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// do {
|
||||
// var config = Configuration()
|
||||
// config.passphrase = nil
|
||||
// do {
|
||||
// _ = try makeDatabaseQueue(filename: "test.sqlite", configuration: config)
|
||||
// XCTFail("Expected error")
|
||||
// } catch let error as DatabaseError {
|
||||
// XCTAssertEqual(error.resultCode, .SQLITE_NOTADB)
|
||||
// XCTAssertEqual(error.message!, "file is not a database")
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
#endif
|
||||
@@ -117,8 +117,10 @@ class GRDBTestCase: XCTestCase {
|
||||
}
|
||||
|
||||
#if GRDBCIPHER_USE_ENCRYPTION
|
||||
// We are testing encrypted databases.
|
||||
dbConfiguration.passphrase = "secret"
|
||||
// Encrypt all databases by default.
|
||||
dbConfiguration.onConnect { db in
|
||||
try db.usePassphrase("secret")
|
||||
}
|
||||
#endif
|
||||
|
||||
sqlQueries = []
|
||||
|
||||
Reference in new issue
Block a user