Skip to content

Commit

Permalink
Batch 13: Swiftify analyze Profile contents (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
CyonAlexRDX authored Apr 26, 2024
1 parent d3af2cc commit 6b3a807
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sargon"
version = "0.6.51"
version = "0.6.52"
edition = "2021"
build = "build.rs"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import SargonUniFFI

extension Profile {

public static func analyzeFile(contents: some DataProtocol) -> ProfileFileContents {
profileAnalyzeContentsOfFile(bytes: Data(contents))
}

public init(jsonData bytes: some DataProtocol) throws {
self = try newProfileFromJsonBytes(jsonBytes: Data(bytes))
}
Expand All @@ -17,6 +21,7 @@ extension Profile {
public func profileSnapshot() -> Data {
profileToJsonBytes(profile: self)
}

public func jsonData() -> Data {
profileSnapshot()
}
Expand Down
34 changes: 34 additions & 0 deletions apple/Tests/TestCases/Profile/ProfileTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,38 @@ final class ProfileTests: Test<Profile> {
XCTAssertEqual(sut.networks, [])
XCTAssertEqual(sut.factorSources.elements, [dfs.asGeneral])
}

func test_analyze_file_not_profile() {
XCTAssertEqual(SUT.analyzeFile(contents: Data()), .notProfile)
}

func test_analyze_file_profile() {
func doTest(_ sut: SUT) {
XCTAssertEqual(
SUT.analyzeFile(contents: sut.jsonData()),
.plaintextProfile(sut)
)
}
SUT.sampleValues.forEach(doTest)
}

func test_analyze_file_encrypted_profile() {
func doTest(_ sut: SUT) {
let encrypted = sut.encrypt(password: "melon")
XCTAssertEqual(
SUT.analyzeFile(contents: encrypted),
.encryptedProfile
)
}
SUT.sampleValues.forEach(doTest)
}

func test_encrypted_profile_contents() throws {
let encrypted = SUT.sample.encrypt(password: "open sesame")
let jsonString = try XCTUnwrap(String(data: encrypted, encoding: .utf8))
XCTAssertTrue(jsonString.contains("encryptionScheme"))
XCTAssertTrue(jsonString.contains("keyDerivationScheme"))
XCTAssertTrue(jsonString.contains("encryptedSnapshot"))
XCTAssertTrue(jsonString.contains("version"))
}
}

0 comments on commit 6b3a807

Please sign in to comment.