Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure the valid key check works with read-only dbs #559

Merged
merged 3 commits into from
Dec 9, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Make sure the valid key check works with read-only dbs
  • Loading branch information
jberkel committed Dec 9, 2016
commit 1ed7070ade294051a3b11bf40cd345d7920258c8
5 changes: 1 addition & 4 deletions SQLite/Extensions/Cipher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 6 additions & 1 deletion SQLiteTests/CipherTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down