Skip to content

Commit 9f457dc

Browse files
committed
Test encryption
1 parent 9085743 commit 9f457dc

File tree

4 files changed

+93
-5
lines changed

4 files changed

+93
-5
lines changed

Package.resolved

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ let packageName = "PowerSync"
77

88
// Set this to the absolute path of your Kotlin SDK checkout if you want to use a local Kotlin
99
// build. Also see docs/LocalBuild.md for details
10-
let localKotlinSdkOverride: String? = nil
10+
let localKotlinSdkOverride: String? = "/Users/simon/src/powersync-kotlin"
1111

1212
// Set this to the absolute path of your powersync-sqlite-core checkout if you want to use a
1313
// local build of the core extension.
1414
let localCoreExtension: String? = nil
1515

16+
let encryption = true
17+
1618
// Our target and dependency setup is different when a local Kotlin SDK is used. Without the local
1719
// SDK, we have no package dependency on Kotlin and download the XCFramework from Kotlin releases as
1820
// a binary target.
@@ -22,10 +24,16 @@ var conditionalDependencies: [Package.Dependency] = []
2224
var conditionalTargets: [Target] = []
2325
var kotlinTargetDependency = Target.Dependency.target(name: "PowerSyncKotlin")
2426

27+
if encryption {
28+
conditionalDependencies.append(.package(url: "https://github.com/sqlcipher/SQLCipher.swift.git", from: "4.10.0"))
29+
} else {
30+
conditionalDependencies.append(.package(url: "https://github.com/sbooth/CSQLite.git", from: "3.50.4"))
31+
}
32+
2533
if let kotlinSdkPath = localKotlinSdkOverride {
2634
// We can't depend on local XCFrameworks outside of this project's root, so there's a Package.swift
2735
// in the PowerSyncKotlin project pointing towards a local build.
28-
conditionalDependencies.append(.package(path: "\(kotlinSdkPath)/PowerSyncKotlin"))
36+
conditionalDependencies.append(.package(path: "\(kotlinSdkPath)/internal/PowerSyncKotlin"))
2937

3038
kotlinTargetDependency = .product(name: "PowerSyncKotlin", package: "PowerSyncKotlin")
3139
} else {
@@ -81,7 +89,10 @@ let package = Package(
8189
name: packageName,
8290
dependencies: [
8391
kotlinTargetDependency,
84-
.product(name: "PowerSyncSQLiteCore", package: corePackageName)
92+
.product(name: "PowerSyncSQLiteCore", package: corePackageName),
93+
encryption ?
94+
.product(name: "SQLCipher", package: "SQLCipher.swift") :
95+
.product(name: "CSQLite", package: "CSQLite"),
8596
]
8697
),
8798
.testTarget(

Sources/PowerSync/Kotlin/KotlinPowerSyncDatabaseImpl.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol,
1313
init(
1414
schema: Schema,
1515
dbFilename: String,
16-
logger: DatabaseLogger
16+
logger: DatabaseLogger,
17+
initialStatements: [String] = [],
1718
) {
18-
let factory = PowerSyncKotlin.DatabaseDriverFactory()
19+
let factory = sqlite3DatabaseFactory(initialStatements: initialStatements)
1920
kotlinDatabase = PowerSyncDatabase(
2021
factory: factory,
2122
schema: KotlinAdapter.Schema.toKotlin(schema),
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
@testable import PowerSync
2+
import XCTest
3+
4+
5+
final class EncryptionTests: XCTestCase {
6+
7+
func testLinksSqlcipher() async throws {
8+
let database = KotlinPowerSyncDatabaseImpl(
9+
schema: Schema(),
10+
dbFilename: ":memory:",
11+
logger: DatabaseLogger(DefaultLogger())
12+
)
13+
14+
let version = try await database.get("pragma cipher_version", mapper: {cursor in
15+
try cursor.getString(index: 0)
16+
});
17+
18+
XCTAssertEqual(version, "4.11.0 community")
19+
try await database.close()
20+
}
21+
22+
func testEncryption() async throws {
23+
let database = KotlinPowerSyncDatabaseImpl(
24+
schema: Schema(tables: [
25+
Table(
26+
name: "users",
27+
columns: [
28+
.text("name")
29+
]
30+
),
31+
]),
32+
dbFilename: "encrypted.db",
33+
logger: DatabaseLogger(DefaultLogger()),
34+
initialStatements: [
35+
"pragma key = 'foobar'"
36+
],
37+
)
38+
39+
try await database.execute("INSERT INTO users (id, name) VALUES (uuid(), 'test')")
40+
try await database.close()
41+
42+
let another = KotlinPowerSyncDatabaseImpl(
43+
schema: Schema(tables: [
44+
Table(
45+
name: "users",
46+
columns: [
47+
.text("name")
48+
]
49+
),
50+
]),
51+
dbFilename: "encrypted.db",
52+
logger: DatabaseLogger(DefaultLogger()),
53+
initialStatements: [
54+
"pragma key = 'wrong password'"
55+
],
56+
)
57+
58+
var hadError = false
59+
do {
60+
try await database.execute("DELETE FROM users")
61+
} catch let error {
62+
hadError = true
63+
}
64+
65+
XCTAssertTrue(hadError)
66+
}
67+
}

0 commit comments

Comments
 (0)