Fix the QRKeyScanner issue on reading footer

This commit is contained in:
Mingshen Sun 2021-01-09 16:26:56 -08:00
parent 1e1d6f0f44
commit 87d1dd5be1
No known key found for this signature in database
GPG key ID: 1F86BA2052FED3B4
2 changed files with 8 additions and 1 deletions

View file

@ -64,7 +64,7 @@ struct QRKeyScanner {
// Update the list of scanned segments and return. // Update the list of scanned segments and return.
segments.append(segment) segments.append(segment)
if scannedKey.contains(keyType.footerStart), scannedKey.hasSuffix(keyType.footerEnd) { if scannedKey.contains(keyType.footerStart), scannedKey.trimmed.hasSuffix(keyType.footerEnd) {
return .completed return .completed
} }
previousResult = .scanned(segments.count) previousResult = .scanned(segments.count)

View file

@ -14,6 +14,7 @@ class QRKeyScannerTest: XCTestCase {
private let header = "-----BEGIN PGP PUBLIC KEY BLOCK-----" private let header = "-----BEGIN PGP PUBLIC KEY BLOCK-----"
private let body = "key body" private let body = "key body"
private let footer = "-----END PGP PUBLIC KEY BLOCK-----" private let footer = "-----END PGP PUBLIC KEY BLOCK-----"
private let footerWithNewline = "-----END PGP PUBLIC KEY BLOCK-----\n"
private let privateHeader = "-----BEGIN PGP PRIVATE KEY BLOCK-----" private let privateHeader = "-----BEGIN PGP PRIVATE KEY BLOCK-----"
private var scanner = QRKeyScanner(keyType: .pgpPublic) private var scanner = QRKeyScanner(keyType: .pgpPublic)
@ -55,4 +56,10 @@ class QRKeyScannerTest: XCTestCase {
XCTAssertEqual(scanner.add(segment: "BLOCK-----"), .completed) XCTAssertEqual(scanner.add(segment: "BLOCK-----"), .completed)
XCTAssertEqual(scanner.scannedKey, header + footer) XCTAssertEqual(scanner.scannedKey, header + footer)
} }
func testFooterWithNewlien() {
XCTAssertEqual(scanner.add(segment: header), .scanned(1))
XCTAssertEqual(scanner.add(segment: footerWithNewline), .completed)
XCTAssertEqual(scanner.scannedKey, header + footerWithNewline)
}
} }