|
| 1 | + |
| 2 | +// --- stubs --- |
| 3 | + |
| 4 | +enum URIQueryParameter { |
| 5 | +} |
| 6 | + |
| 7 | +struct Blob { |
| 8 | + public init(bytes: [UInt8]) { } |
| 9 | + public init(bytes: UnsafeRawPointer, length: Int) { } |
| 10 | +} |
| 11 | + |
| 12 | +class Connection { |
| 13 | + enum Location { |
| 14 | + case inMemory |
| 15 | + case uri(String, parameters: [URIQueryParameter] = []) |
| 16 | + } |
| 17 | + |
| 18 | + init(_ location: Location = .inMemory, readonly: Bool = false) throws { } |
| 19 | + convenience init(_ filename: String, readonly: Bool = false) throws { try self.init() } |
| 20 | +} |
| 21 | + |
| 22 | +extension Connection { |
| 23 | + func key(_ key: String, db: String = "main") throws { } |
| 24 | + func key(_ key: Blob, db: String = "main") throws { } |
| 25 | + func keyAndMigrate(_ key: String, db: String = "main") throws { } |
| 26 | + func keyAndMigrate(_ key: Blob, db: String = "main") throws { } |
| 27 | + |
| 28 | + func rekey(_ key: String, db: String = "main") throws { } |
| 29 | + func rekey(_ key: Blob, db: String = "main") throws { } |
| 30 | + |
| 31 | + func sqlcipher_export(_ location: Location, key: String) throws { } |
| 32 | +} |
| 33 | + |
| 34 | +// --- tests --- |
| 35 | + |
| 36 | +func test_sqlite_swift_api(dbPath: String, goodKey: String, goodArray: [UInt8]) throws { |
| 37 | + let db = try Connection(dbPath) |
| 38 | + let badArray: [UInt8] = [1, 2, 3] |
| 39 | + |
| 40 | + // methods taking a string key |
| 41 | + |
| 42 | + try db.key(goodKey) |
| 43 | + try db.key("hardcoded_key") // BAD [NOT DETECTED] |
| 44 | + try db.keyAndMigrate(goodKey) |
| 45 | + try db.keyAndMigrate("hardcoded_key") // BAD [NOT DETECTED] |
| 46 | + try db.rekey(goodKey) |
| 47 | + try db.rekey("hardcoded_key") // BAD [NOT DETECTED] |
| 48 | + try db.sqlcipher_export(Connection.Location.uri("encryptedDb.sqlite3"), key: goodKey) |
| 49 | + try db.sqlcipher_export(Connection.Location.uri("encryptedDb.sqlite3"), key: "hardcoded_key") // BAD [NOT DETECTED] |
| 50 | + |
| 51 | + // Blob variant |
| 52 | + |
| 53 | + try db.key(Blob(bytes: goodArray)) |
| 54 | + try db.key(Blob(bytes: [1, 2, 3])) // BAD [NOT DETECTED] |
| 55 | + |
| 56 | + try goodArray.withUnsafeBytes { bytes in |
| 57 | + if let ptr = bytes.baseAddress { |
| 58 | + try db.key(Blob(bytes: ptr, length: bytes.count)) |
| 59 | + } |
| 60 | + } |
| 61 | + try badArray.withUnsafeBytes { bytes in |
| 62 | + if let ptr = bytes.baseAddress { |
| 63 | + try db.key(Blob(bytes: ptr, length: bytes.count)) // BAD [NOT DETECTED] |
| 64 | + } |
| 65 | + } |
| 66 | +} |
0 commit comments