@@ -10,17 +10,19 @@ import XCTest
10
10
@testable import CryptomatorCryptoLib
11
11
12
12
class CryptorTests : XCTestCase {
13
+ var contentCryptor : ContentCryptor !
13
14
var cryptor : Cryptor !
14
15
var tmpDirURL : URL !
15
16
16
- override func setUpWithError( ) throws {
17
- let aesKey = [ UInt8] ( repeating: 0x55 , count: 32 )
18
- let macKey = [ UInt8] ( repeating: 0x77 , count: 32 )
19
- let masterkey = Masterkey . createFromRaw ( aesMasterKey: aesKey, macMasterKey: macKey)
20
- let cryptoSupport = CryptoSupportMock ( )
21
- let contentCryptor = CtrThenHmacContentCryptor ( macKey: macKey, cryptoSupport: cryptoSupport)
22
- cryptor = Cryptor ( masterkey: masterkey, cryptoSupport: cryptoSupport, contentCryptor: contentCryptor)
17
+ override class var defaultTestSuite : XCTestSuite {
18
+ // Return empty `XCTestSuite` so that no tests from this "abstract" `XCTestCase` is run.
19
+ // Make sure to override this in subclasses so that the implemented test case can run.
20
+ return XCTestSuite ( name: " InterfaceTests Excluded " )
21
+ }
23
22
23
+ func setUpWithError( masterkey: Masterkey , cryptoSupport: CryptoSupport , contentCryptor: ContentCryptor ) throws {
24
+ self . contentCryptor = contentCryptor
25
+ cryptor = Cryptor ( masterkey: masterkey, cryptoSupport: cryptoSupport, contentCryptor: contentCryptor)
24
26
tmpDirURL = URL ( fileURLWithPath: NSTemporaryDirectory ( ) , isDirectory: true ) . appendingPathComponent ( UUID ( ) . uuidString, isDirectory: true )
25
27
try FileManager . default. createDirectory ( at: tmpDirURL, withIntermediateDirectories: true )
26
28
}
@@ -59,50 +61,6 @@ class CryptorTests: XCTestCase {
59
61
}
60
62
}
61
63
62
- func testCreateHeader( ) throws {
63
- let header = try cryptor. createHeader ( )
64
- XCTAssertEqual ( [ UInt8] ( repeating: 0xF0 , count: 16 ) , header. nonce)
65
- XCTAssertEqual ( [ UInt8] ( repeating: 0xF0 , count: 32 ) , header. contentKey)
66
- }
67
-
68
- func testEncryptHeader( ) throws {
69
- let header = try cryptor. createHeader ( )
70
- let encrypted = try cryptor. encryptHeader ( header)
71
- let expected : [ UInt8 ] = [
72
- 0xF0 , 0xF0 , 0xF0 , 0xF0 , 0xF0 , 0xF0 , 0xF0 , 0xF0 ,
73
- 0xF0 , 0xF0 , 0xF0 , 0xF0 , 0xF0 , 0xF0 , 0xF0 , 0xF0 ,
74
- 0x0D , 0x91 , 0xF2 , 0x9C , 0xC6 , 0x35 , 0xD7 , 0x5E ,
75
- 0x1E , 0x42 , 0x23 , 0x1E , 0xC7 , 0x90 , 0x57 , 0xE3 ,
76
- 0x8D , 0x98 , 0xF3 , 0x58 , 0x07 , 0x2C , 0x9F , 0x03 ,
77
- 0xBC , 0xEA , 0x5A , 0x98 , 0x3B , 0x68 , 0x62 , 0x89 ,
78
- 0x3E , 0xBC , 0x5E , 0x5E , 0x27 , 0x39 , 0xCB , 0x8E ,
79
- 0xD4 , 0x27 , 0x61 , 0x06 , 0x8E , 0x7F , 0x3A , 0x4E ,
80
- 0xC7 , 0x9F , 0x4D , 0x3E , 0x20 , 0x57 , 0xDC , 0xE4 ,
81
- 0x65 , 0xA5 , 0xFF , 0x93 , 0xC2 , 0x7B , 0xD2 , 0xB8 ,
82
- 0x3F , 0xE3 , 0xD0 , 0x8C , 0xB3 , 0x92 , 0xED , 0x96
83
- ]
84
- XCTAssertEqual ( expected, encrypted)
85
- }
86
-
87
- func testDecryptHeader( ) throws {
88
- let ciphertext : [ UInt8 ] = [
89
- 0xF0 , 0xF0 , 0xF0 , 0xF0 , 0xF0 , 0xF0 , 0xF0 , 0xF0 ,
90
- 0xF0 , 0xF0 , 0xF0 , 0xF0 , 0xF0 , 0xF0 , 0xF0 , 0xF0 ,
91
- 0x0D , 0x91 , 0xF2 , 0x9C , 0xC6 , 0x35 , 0xD7 , 0x5E ,
92
- 0x1E , 0x42 , 0x23 , 0x1E , 0xC7 , 0x90 , 0x57 , 0xE3 ,
93
- 0x8D , 0x98 , 0xF3 , 0x58 , 0x07 , 0x2C , 0x9F , 0x03 ,
94
- 0xBC , 0xEA , 0x5A , 0x98 , 0x3B , 0x68 , 0x62 , 0x89 ,
95
- 0x3E , 0xBC , 0x5E , 0x5E , 0x27 , 0x39 , 0xCB , 0x8E ,
96
- 0xD4 , 0x27 , 0x61 , 0x06 , 0x8E , 0x7F , 0x3A , 0x4E ,
97
- 0xC7 , 0x9F , 0x4D , 0x3E , 0x20 , 0x57 , 0xDC , 0xE4 ,
98
- 0x65 , 0xA5 , 0xFF , 0x93 , 0xC2 , 0x7B , 0xD2 , 0xB8 ,
99
- 0x3F , 0xE3 , 0xD0 , 0x8C , 0xB3 , 0x92 , 0xED , 0x96
100
- ]
101
- let decrypted = try cryptor. decryptHeader ( ciphertext)
102
- XCTAssertEqual ( [ UInt8] ( repeating: 0xF0 , count: 16 ) , decrypted. nonce)
103
- XCTAssertEqual ( [ UInt8] ( repeating: 0xF0 , count: 32 ) , decrypted. contentKey)
104
- }
105
-
106
64
func testEncryptAndDecryptContent( ) throws {
107
65
let originalData = Data ( repeating: 0x0F , count: 65 * 1024 )
108
66
let originalURL = tmpDirURL. appendingPathComponent ( UUID ( ) . uuidString, isDirectory: false )
@@ -128,7 +86,7 @@ class CryptorTests: XCTestCase {
128
86
}
129
87
130
88
func testEncryptAndDecryptSingleChunk( ) throws {
131
- let nonce = [ UInt8] ( repeating: 0x00 , count: 16 )
89
+ let nonce = [ UInt8] ( repeating: 0x00 , count: contentCryptor . nonceLen )
132
90
let filekey = [ UInt8] ( repeating: 0x00 , count: 32 )
133
91
let cleartext = [ UInt8] ( " hello world " . data ( using: . ascii) !)
134
92
@@ -139,47 +97,57 @@ class CryptorTests: XCTestCase {
139
97
}
140
98
141
99
func testCalculateCiphertextSize( ) {
100
+ let overheadPerChunk = contentCryptor. nonceLen + contentCryptor. tagLen
101
+
142
102
XCTAssertEqual ( 0 , cryptor. calculateCiphertextSize ( 0 ) )
143
103
144
- XCTAssertEqual ( 1 + 48 , cryptor. calculateCiphertextSize ( 1 ) )
145
- XCTAssertEqual ( 32 * 1024 - 1 + 48 , cryptor. calculateCiphertextSize ( 32 * 1024 - 1 ) )
146
- XCTAssertEqual ( 32 * 1024 + 48 , cryptor. calculateCiphertextSize ( 32 * 1024 ) )
104
+ XCTAssertEqual ( 1 + overheadPerChunk , cryptor. calculateCiphertextSize ( 1 ) )
105
+ XCTAssertEqual ( 32 * 1024 - 1 + overheadPerChunk , cryptor. calculateCiphertextSize ( 32 * 1024 - 1 ) )
106
+ XCTAssertEqual ( 32 * 1024 + overheadPerChunk , cryptor. calculateCiphertextSize ( 32 * 1024 ) )
147
107
148
- XCTAssertEqual ( 32 * 1024 + 1 + 48 * 2 , cryptor. calculateCiphertextSize ( 32 * 1024 + 1 ) )
149
- XCTAssertEqual ( 32 * 1024 + 2 + 48 * 2 , cryptor. calculateCiphertextSize ( 32 * 1024 + 2 ) )
150
- XCTAssertEqual ( 64 * 1024 - 1 + 48 * 2 , cryptor. calculateCiphertextSize ( 64 * 1024 - 1 ) )
151
- XCTAssertEqual ( 64 * 1024 + 48 * 2 , cryptor. calculateCiphertextSize ( 64 * 1024 ) )
108
+ XCTAssertEqual ( 32 * 1024 + 1 + overheadPerChunk * 2 , cryptor. calculateCiphertextSize ( 32 * 1024 + 1 ) )
109
+ XCTAssertEqual ( 32 * 1024 + 2 + overheadPerChunk * 2 , cryptor. calculateCiphertextSize ( 32 * 1024 + 2 ) )
110
+ XCTAssertEqual ( 64 * 1024 - 1 + overheadPerChunk * 2 , cryptor. calculateCiphertextSize ( 64 * 1024 - 1 ) )
111
+ XCTAssertEqual ( 64 * 1024 + overheadPerChunk * 2 , cryptor. calculateCiphertextSize ( 64 * 1024 ) )
152
112
153
- XCTAssertEqual ( 64 * 1024 + 1 + 48 * 3 , cryptor. calculateCiphertextSize ( 64 * 1024 + 1 ) )
113
+ XCTAssertEqual ( 64 * 1024 + 1 + overheadPerChunk * 3 , cryptor. calculateCiphertextSize ( 64 * 1024 + 1 ) )
154
114
}
155
115
156
116
func testCalculateCleartextSize( ) throws {
117
+ let overheadPerChunk = contentCryptor. nonceLen + contentCryptor. tagLen
118
+
157
119
XCTAssertEqual ( 0 , try cryptor. calculateCleartextSize ( 0 ) )
158
120
159
- XCTAssertEqual ( 1 , try cryptor. calculateCleartextSize ( 1 + 48 ) )
160
- XCTAssertEqual ( 32 * 1024 - 1 , try cryptor. calculateCleartextSize ( 32 * 1024 - 1 + 48 ) )
161
- XCTAssertEqual ( 32 * 1024 , try cryptor. calculateCleartextSize ( 32 * 1024 + 48 ) )
121
+ XCTAssertEqual ( 1 , try cryptor. calculateCleartextSize ( 1 + overheadPerChunk ) )
122
+ XCTAssertEqual ( 32 * 1024 - 1 , try cryptor. calculateCleartextSize ( 32 * 1024 - 1 + overheadPerChunk ) )
123
+ XCTAssertEqual ( 32 * 1024 , try cryptor. calculateCleartextSize ( 32 * 1024 + overheadPerChunk ) )
162
124
163
- XCTAssertEqual ( 32 * 1024 + 1 , try cryptor. calculateCleartextSize ( 32 * 1024 + 1 + 48 * 2 ) )
164
- XCTAssertEqual ( 32 * 1024 + 2 , try cryptor. calculateCleartextSize ( 32 * 1024 + 2 + 48 * 2 ) )
165
- XCTAssertEqual ( 64 * 1024 - 1 , try cryptor. calculateCleartextSize ( 64 * 1024 - 1 + 48 * 2 ) )
166
- XCTAssertEqual ( 64 * 1024 , try cryptor. calculateCleartextSize ( 64 * 1024 + 48 * 2 ) )
125
+ XCTAssertEqual ( 32 * 1024 + 1 , try cryptor. calculateCleartextSize ( 32 * 1024 + 1 + overheadPerChunk * 2 ) )
126
+ XCTAssertEqual ( 32 * 1024 + 2 , try cryptor. calculateCleartextSize ( 32 * 1024 + 2 + overheadPerChunk * 2 ) )
127
+ XCTAssertEqual ( 64 * 1024 - 1 , try cryptor. calculateCleartextSize ( 64 * 1024 - 1 + overheadPerChunk * 2 ) )
128
+ XCTAssertEqual ( 64 * 1024 , try cryptor. calculateCleartextSize ( 64 * 1024 + overheadPerChunk * 2 ) )
167
129
168
- XCTAssertEqual ( 64 * 1024 + 1 , try cryptor. calculateCleartextSize ( 64 * 1024 + 1 + 48 * 3 ) )
130
+ XCTAssertEqual ( 64 * 1024 + 1 , try cryptor. calculateCleartextSize ( 64 * 1024 + 1 + overheadPerChunk * 3 ) )
169
131
}
170
132
171
133
func testCalculateCleartextSizeWithInvalidCiphertextSize( ) throws {
172
134
XCTAssertThrowsError ( try cryptor. calculateCleartextSize ( 1 ) , " invalid ciphertext size " ) { error in
173
135
XCTAssertEqual ( . invalidParameter( " Method not defined for input value 1 " ) , error as? CryptoError )
174
136
}
175
- XCTAssertThrowsError ( try cryptor. calculateCleartextSize ( 48 ) , " invalid ciphertext size " ) { error in
176
- XCTAssertEqual ( . invalidParameter( " Method not defined for input value 48 " ) , error as? CryptoError )
137
+
138
+ let emptyPayload = contentCryptor. nonceLen + contentCryptor. tagLen
139
+ XCTAssertThrowsError ( try cryptor. calculateCleartextSize ( emptyPayload) , " invalid ciphertext size " ) { error in
140
+ XCTAssertEqual ( . invalidParameter( " Method not defined for input value \( emptyPayload) " ) , error as? CryptoError )
177
141
}
178
- XCTAssertThrowsError ( try cryptor. calculateCleartextSize ( 32 * 1024 + 1 + 48 ) , " invalid ciphertext size " ) { error in
179
- XCTAssertEqual ( . invalidParameter( " Method not defined for input value 32817 " ) , error as? CryptoError )
142
+
143
+ let oneChunkPlusOneByte = cryptor. ciphertextChunkSize + 1
144
+ XCTAssertThrowsError ( try cryptor. calculateCleartextSize ( oneChunkPlusOneByte) , " invalid ciphertext size " ) { error in
145
+ XCTAssertEqual ( . invalidParameter( " Method not defined for input value \( oneChunkPlusOneByte) " ) , error as? CryptoError )
180
146
}
181
- XCTAssertThrowsError ( try cryptor. calculateCleartextSize ( 32 * 1024 + 48 * 2 ) , " invalid ciphertext size " ) { error in
182
- XCTAssertEqual ( . invalidParameter( " Method not defined for input value 32864 " ) , error as? CryptoError )
147
+
148
+ let oneChunkPlusEmptySecondChunk = cryptor. ciphertextChunkSize + contentCryptor. nonceLen + contentCryptor. tagLen
149
+ XCTAssertThrowsError ( try cryptor. calculateCleartextSize ( oneChunkPlusEmptySecondChunk) , " invalid ciphertext size " ) { error in
150
+ XCTAssertEqual ( . invalidParameter( " Method not defined for input value \( oneChunkPlusEmptySecondChunk) " ) , error as? CryptoError )
183
151
}
184
152
}
185
153
}
0 commit comments