From 1ed7070ade294051a3b11bf40cd345d7920258c8 Mon Sep 17 00:00:00 2001 From: Jan Berkel Date: Fri, 9 Dec 2016 17:25:56 +0000 Subject: [PATCH 1/3] Make sure the valid key check works with read-only dbs --- SQLite/Extensions/Cipher.swift | 5 +---- SQLiteTests/CipherTests.swift | 7 ++++++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/SQLite/Extensions/Cipher.swift b/SQLite/Extensions/Cipher.swift index 9be3fa91..6c0d4657 100644 --- a/SQLite/Extensions/Cipher.swift +++ b/SQLite/Extensions/Cipher.swift @@ -55,10 +55,7 @@ extension Connection { // the key provided is incorrect. To test that the database can be successfully opened with the // provided key, it is necessary to perform some operation on the database (i.e. read from it). private func cipher_key_check() throws { - try execute( - "CREATE TABLE \"__SQLCipher.swift__\" (\"cipher key check\");\n" + - "DROP TABLE \"__SQLCipher.swift__\";" - ) + try scalar("SELECT count(*) FROM sqlite_master;") } } #endif diff --git a/SQLiteTests/CipherTests.swift b/SQLiteTests/CipherTests.swift index b3ff2d33..46e7024e 100644 --- a/SQLiteTests/CipherTests.swift +++ b/SQLiteTests/CipherTests.swift @@ -59,11 +59,12 @@ class CipherTests: XCTestCase { try! connA.key("hello") - let connB = try! Connection(path) + let connB = try! Connection(path, readonly: true) var rc: Int32? do { try connB.key("world") + XCTFail("expected exception") } catch Result.error(_, let code, _) { rc = code } catch { @@ -78,6 +79,10 @@ class CipherTests: XCTestCase { // sqlite> CREATE TABLE foo (bar TEXT); // sqlite> INSERT INTO foo (bar) VALUES ('world'); let encryptedFile = fixture("encrypted", withExtension: "sqlite") + + try! FileManager.default.setAttributes([FileAttributeKey.immutable : 1], ofItemAtPath: encryptedFile) + XCTAssertFalse(FileManager.default.isWritableFile(atPath: encryptedFile)) + let conn = try! Connection(encryptedFile) try! conn.key("sqlcipher-test") XCTAssertEqual(1, try! conn.scalar("SELECT count(*) FROM foo") as? Int64) From be3dc0b02ca1d3e42ff327818f4befe07ac8152d Mon Sep 17 00:00:00 2001 From: Jan Berkel Date: Fri, 9 Dec 2016 17:39:50 +0000 Subject: [PATCH 2/3] Update changelog --- CHANGELOG.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 394f651f..56b84ce4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +0.11.2 (unreleased), [diff][diff-0.11.2] +======================================== + +* Fixed SQLCipher integration with read-only databases ([#559][]) 0.11.1 (06-12-2016), [diff][diff-0.11.1] ======================================== @@ -7,15 +11,17 @@ * Fix for ~= operator used with Double ranges * Various documentation updates -0.11.0 (10-19-2016) +0.11.0 (19-10-2016) =================== * Swift3 migration ([diff][diff-0.11.0]) -[diff-0.11.1]: https://github.com/stephencelis/SQLite.swift/compare/0.11.0...0.11.1 [diff-0.11.0]: https://github.com/stephencelis/SQLite.swift/compare/0.10.1...0.11.0 +[diff-0.11.1]: https://github.com/stephencelis/SQLite.swift/compare/0.11.0...0.11.1 +[diff-0.11.2]: https://github.com/stephencelis/SQLite.swift/compare/0.11.1...0.11.2 [#532]: https://github.com/stephencelis/SQLite.swift/issues/532 [#546]: https://github.com/stephencelis/SQLite.swift/issues/546 [#553]: https://github.com/stephencelis/SQLite.swift/pull/553 +[#559]: https://github.com/stephencelis/SQLite.swift/pull/559 From 7b724b05f11d17d278a32c2ad83b08c28ab2a331 Mon Sep 17 00:00:00 2001 From: Jan Berkel Date: Fri, 9 Dec 2016 19:24:23 +0000 Subject: [PATCH 3/3] Fix tests --- SQLiteTests/CipherTests.swift | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/SQLiteTests/CipherTests.swift b/SQLiteTests/CipherTests.swift index 46e7024e..3ee0b135 100644 --- a/SQLiteTests/CipherTests.swift +++ b/SQLiteTests/CipherTests.swift @@ -58,19 +58,18 @@ class CipherTests: XCTestCase { defer { try! FileManager.default.removeItem(atPath: path) } try! connA.key("hello") + try! connA.run("CREATE TABLE foo (bar TEXT)") let connB = try! Connection(path, readonly: true) - var rc: Int32? do { try connB.key("world") XCTFail("expected exception") } catch Result.error(_, let code, _) { - rc = code + XCTAssertEqual(SQLITE_NOTADB, code) } catch { - XCTFail() + XCTFail("unexpected error: \(error)") } - XCTAssertEqual(SQLITE_NOTADB, rc) } func test_open_db_encrypted_with_sqlcipher() {