feat: add KDBXDocument with KeePassKit-backed KDBX parsing
Implements KDBXError, KDBXMapper, and KDBXDocument for reading and writing KDBX files using KeePassKit. Adds KDBXDocumentTests with fixture from KeePassKit's own test suite (Test_Password_1234.kdbx, password "1234"). Fixes KeePassKit Package.swift to include defaultLocalization for SPM compatibility. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Binary file not shown.
@@ -0,0 +1,36 @@
|
||||
import XCTest
|
||||
@testable import MyPassCore
|
||||
|
||||
final class KDBXDocumentTests: XCTestCase {
|
||||
var fixtureURL: URL!
|
||||
|
||||
override func setUp() {
|
||||
super.setUp()
|
||||
fixtureURL = Bundle.module.url(forResource: "test", withExtension: "kdbx", subdirectory: "Fixtures")!
|
||||
}
|
||||
|
||||
func test_read_parsesRootGroup() throws {
|
||||
let doc = KDBXDocument(url: fixtureURL)
|
||||
let db = try doc.read(password: "1234")
|
||||
XCTAssertFalse(db.root.name.isEmpty)
|
||||
}
|
||||
|
||||
func test_read_wrongPassword_throwsInvalidPassword() throws {
|
||||
let doc = KDBXDocument(url: fixtureURL)
|
||||
XCTAssertThrowsError(try doc.read(password: "wrong")) { error in
|
||||
XCTAssertEqual(error as? KDBXError, .invalidPassword)
|
||||
}
|
||||
}
|
||||
|
||||
func test_roundTrip_preservesEntryTitle() throws {
|
||||
let doc = KDBXDocument(url: fixtureURL)
|
||||
var db = try doc.read(password: "1234")
|
||||
let newEntry = Entry(title: "RoundTripTest", username: "user", password: ProtectedString("pass"))
|
||||
db.root.entries.append(newEntry)
|
||||
let tmpURL = FileManager.default.temporaryDirectory.appendingPathComponent("roundtrip.kdbx")
|
||||
let tmpDoc = KDBXDocument(url: tmpURL)
|
||||
try tmpDoc.write(db, password: "1234")
|
||||
let reloaded = try KDBXDocument(url: tmpURL).read(password: "1234")
|
||||
XCTAssertTrue(reloaded.root.entries.contains { $0.title == "RoundTripTest" })
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user