-
Notifications
You must be signed in to change notification settings - Fork 2
/
CryptoFileTests.swift
58 lines (54 loc) · 2.44 KB
/
CryptoFileTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//
// CryptoFileTests.swift
// ProtonCore-Crypto-Tests - Created on 07/15/22.
//
// Copyright (c) 2022 Proton Technologies AG
//
// This file is part of Proton Technologies AG and ProtonCore.
//
// ProtonCore is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ProtonCore is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ProtonCore. If not, see <https://www.gnu.org/licenses/>.
import XCTest
import ProtonCoreCryptoGoInterface
@testable import ProtonCoreCrypto
import ProtonCoreUtilities
class CryptoFileTests: CryptoTestBase {
#if !os(macOS)
func testWriteAndRead() {
do {
let url = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let fileToWrite = url.appendingPathComponent("write.dat")
if FileManager.default.fileExists(atPath: fileToWrite.path) {
try FileManager.default.removeItem(at: fileToWrite)
}
FileManager.default.createFile(atPath: fileToWrite.path, contents: Data(), attributes: nil)
let writeFileHandle = try FileHandle(forWritingTo: fileToWrite)
guard let size = try FileManager.default.attributesOfItem(atPath: fileToWrite.path)[.size] as? Int else {
throw CryptoError.streamCleartextFileHasNoSize
}
XCTAssertTrue(size == 0)
let fileMobileWriter = File.FileMobileWriter(file: writeFileHandle)
let testdata = "this is test data to write!".utf8!
try fileMobileWriter.write(testdata, n: nil)
defer { writeFileHandle.closeFile() }
let readFileHandle = try FileHandle(forReadingFrom: fileToWrite)
defer { readFileHandle.closeFile() }
let fileMobileReader = File.FileMobileReader(file: readFileHandle)
let readData = try fileMobileReader.read(1000).data
XCTAssertEqual(testdata, readData)
} catch {
XCTFail("Should not happen: \(error)")
}
}
#endif
}