From 1fb7f9ff5b4cfb3e75a68d552fe5061d4fc4a0a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Fri, 26 May 2017 09:29:30 +0200 Subject: [PATCH] Disable query tracing inside EncryptionTests.testExportPlainTextDatabaseToEncryptedDatabase (workaround https://github.com/sqlcipher/sqlcipher/issues/216) --- TODO.md | 2 - Tests/GRDBTests/EncryptionTests.swift | 83 ++++++++++++++------------- 2 files changed, 44 insertions(+), 41 deletions(-) diff --git a/TODO.md b/TODO.md index b80e077da..279c7a864 100644 --- a/TODO.md +++ b/TODO.md @@ -1,5 +1,3 @@ -- [ ] Fix flaky ConcurrencyTests.testReaderInDeferredTransactionDuringDefaultTransaction -- [ ] Upgrade SQLCipher to v3.4.1 - [ ] We share the database cache between database pool writers and readers. But what if a writer modifies the database schema within a transaction, and a concurrent reader reads the cache? Bad things, isn't it? Write failing tests first, and fix the bug. - [ ] Attach databases. Interesting question: what happens when one attaches a non-WAL db to a databasePool? - [ ] SQLCipher: sqlite3_rekey is discouraged (https://github.com/ccgus/fmdb/issues/547#issuecomment-259219320) diff --git a/Tests/GRDBTests/EncryptionTests.swift b/Tests/GRDBTests/EncryptionTests.swift index d3d87be96..a7d01243d 100644 --- a/Tests/GRDBTests/EncryptionTests.swift +++ b/Tests/GRDBTests/EncryptionTests.swift @@ -332,44 +332,49 @@ class EncryptionTests: GRDBTestCase { } } } - - // TODO: restore this test once https://github.com/sqlcipher/sqlcipher/issues/216 is fixed -// 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 { -// dbConfiguration.passphrase = nil -// let plainTextDBQueue = try makeDatabaseQueue(filename: "plaintext.sqlite") -// try plainTextDBQueue.inDatabase { db in -// try db.execute("CREATE TABLE data (value INTEGER)") -// try db.execute("INSERT INTO data (value) VALUES (1)") -// } -// -// dbConfiguration.passphrase = "secret" -// do { -// _ = try makeDatabaseQueue(filename: "plaintext.sqlite") -// } catch let error as DatabaseError { -// XCTAssertEqual(error.resultCode, .SQLITE_NOTADB) -// XCTAssertEqual(error.message!, "file is encrypted or is not a database") -// XCTAssertTrue(error.sql == nil) -// XCTAssertEqual(error.description, "SQLite error 26: file is encrypted or is not a database") -// } -// -// let encryptedDBQueue = try makeDatabaseQueue(filename: "encrypted.sqlite") -// -// try plainTextDBQueue.inDatabase { db in -// try db.execute("ATTACH DATABASE ? AS encrypted KEY ?", arguments: [encryptedDBQueue.path, "secret"]) -// try db.execute("SELECT sqlcipher_export('encrypted')") -// try db.execute("DETACH DATABASE encrypted") -// } -// } -// -// do { -// dbConfiguration.passphrase = "secret" -// let dbQueue = try makeDatabaseQueue(filename: "encrypted.sqlite") -// try dbQueue.inDatabase { db in -// XCTAssertEqual(try Int.fetchOne(db, "SELECT COUNT(*) FROM data")!, 1) -// } -// } -// } + + 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") + try plainTextDBQueue.inDatabase { db in + try db.execute("CREATE TABLE data (value INTEGER)") + try db.execute("INSERT INTO data (value) VALUES (1)") + } + + dbConfiguration.passphrase = "secret" + do { + _ = try makeDatabaseQueue(filename: "plaintext.sqlite") + } catch let error as DatabaseError { + XCTAssertEqual(error.resultCode, .SQLITE_NOTADB) + XCTAssertEqual(error.message!, "file is encrypted or is not a database") + XCTAssertTrue(error.sql == nil) + XCTAssertEqual(error.description, "SQLite error 26: file is encrypted or is not a database") + } + + let encryptedDBQueue = try makeDatabaseQueue(filename: "encrypted.sqlite") + + try plainTextDBQueue.inDatabase { db in + try db.execute("ATTACH DATABASE ? AS encrypted KEY ?", arguments: [encryptedDBQueue.path, "secret"]) + try db.execute("SELECT sqlcipher_export('encrypted')") + try db.execute("DETACH DATABASE encrypted") + } + } + + do { + dbConfiguration.passphrase = "secret" + let dbQueue = try makeDatabaseQueue(filename: "encrypted.sqlite") + try dbQueue.inDatabase { db in + XCTAssertEqual(try Int.fetchOne(db, "SELECT COUNT(*) FROM data")!, 1) + } + } + } } #endif