@@ -19,29 +19,68 @@ public void Test_Attachment_EncryptDecrypt()
19
19
byte [ ] plaintextInput = Encoding . UTF8 . GetBytes ( "Peter Parker" ) ;
20
20
EncryptResult encryptResult = EncryptData ( plaintextInput , key ) ;
21
21
string cipherFile = WriteToFile ( encryptResult . ciphertext ) ;
22
- Stream inputStream = AttachmentCipherInputStream . CreateForAttachment ( File . Open ( cipherFile , FileMode . Open ) , plaintextInput . Length , key , encryptResult . digest ) ;
22
+ using Stream inputStream = AttachmentCipherInputStream . CreateForAttachment ( File . Open ( cipherFile , FileMode . Open ) , plaintextInput . Length , key , encryptResult . digest ) ;
23
23
byte [ ] plaintextOutput = ReadInputStreamFully ( inputStream ) ;
24
24
25
25
CollectionAssert . AreEqual ( plaintextInput , plaintextOutput ) ;
26
26
27
27
DeleteFile ( cipherFile ) ;
28
28
}
29
29
30
+ [ TestMethod ]
31
+ public void Test_Attachment_EncryptDecryptMultipleTimes ( )
32
+ {
33
+ // Test that the file passed to AttachmentCipherInputStream can be reused.
34
+ byte [ ] key = Util . GetSecretBytes ( 64 ) ;
35
+ byte [ ] plaintextInput = Encoding . UTF8 . GetBytes ( "Peter Parker" ) ;
36
+ EncryptResult encryptResult = EncryptData ( plaintextInput , key ) ;
37
+ string cipherFile = WriteToFile ( encryptResult . ciphertext ) ;
38
+
39
+ for ( int i = 0 ; i < 10 ; i ++ )
40
+ {
41
+ using Stream inputStream = AttachmentCipherInputStream . CreateForAttachment ( File . Open ( cipherFile , FileMode . Open ) , plaintextInput . Length , key , encryptResult . digest ) ;
42
+ byte [ ] plaintextOutput = ReadInputStreamFully ( inputStream ) ;
43
+
44
+ CollectionAssert . AreEqual ( plaintextInput , plaintextOutput ) ;
45
+ }
46
+
47
+ DeleteFile ( cipherFile ) ;
48
+ }
49
+
30
50
[ TestMethod ]
31
51
public void Test_Attachment_EncryptDecryptEmpty ( )
32
52
{
33
53
byte [ ] key = Util . GetSecretBytes ( 64 ) ;
34
54
byte [ ] plaintextInput = Encoding . UTF8 . GetBytes ( string . Empty ) ;
35
55
EncryptResult encryptResult = EncryptData ( plaintextInput , key ) ;
36
56
string cipherFile = WriteToFile ( encryptResult . ciphertext ) ;
37
- Stream inputStream = AttachmentCipherInputStream . CreateForAttachment ( File . Open ( cipherFile , FileMode . Open ) , plaintextInput . Length , key , encryptResult . digest ) ;
57
+ using Stream inputStream = AttachmentCipherInputStream . CreateForAttachment ( File . Open ( cipherFile , FileMode . Open ) , plaintextInput . Length , key , encryptResult . digest ) ;
38
58
byte [ ] plaintextOutput = ReadInputStreamFully ( inputStream ) ;
39
59
40
60
CollectionAssert . AreEqual ( plaintextInput , plaintextOutput ) ;
41
61
42
62
DeleteFile ( cipherFile ) ;
43
63
}
44
64
65
+ [ TestMethod ]
66
+ public void Test_Attachment_EncryptDecryptEmptyMultipleTimes ( )
67
+ {
68
+ byte [ ] key = Util . GetSecretBytes ( 64 ) ;
69
+ byte [ ] plaintextInput = Encoding . UTF8 . GetBytes ( string . Empty ) ;
70
+ EncryptResult encryptResult = EncryptData ( plaintextInput , key ) ;
71
+ string cipherFile = WriteToFile ( encryptResult . ciphertext ) ;
72
+
73
+ for ( int i = 0 ; i < 10 ; i ++ )
74
+ {
75
+ using Stream inputStream = AttachmentCipherInputStream . CreateForAttachment ( File . Open ( cipherFile , FileMode . Open ) , plaintextInput . Length , key , encryptResult . digest ) ;
76
+ byte [ ] plaintextOutput = ReadInputStreamFully ( inputStream ) ;
77
+
78
+ CollectionAssert . AreEqual ( plaintextInput , plaintextOutput ) ;
79
+ }
80
+
81
+ DeleteFile ( cipherFile ) ;
82
+ }
83
+
45
84
[ TestMethod ]
46
85
public void Test_Attachment_DecryptFailOnBadKey ( )
47
86
{
@@ -57,7 +96,8 @@ public void Test_Attachment_DecryptFailOnBadKey()
57
96
58
97
cipherFile = WriteToFile ( encryptResult . ciphertext ) ;
59
98
60
- AttachmentCipherInputStream . CreateForAttachment ( File . Open ( cipherFile , FileMode . Open ) , plaintextInput . Length , badKey , encryptResult . digest ) ;
99
+ using FileStream fileStream = File . Open ( cipherFile , FileMode . Open ) ;
100
+ AttachmentCipherInputStream . CreateForAttachment ( fileStream , plaintextInput . Length , badKey , encryptResult . digest ) ;
61
101
}
62
102
catch ( InvalidMessageException )
63
103
{
@@ -89,7 +129,8 @@ public void Test_Attachmetn_DecryptFailOnBadDigest()
89
129
90
130
cipherFile = WriteToFile ( encryptResult . ciphertext ) ;
91
131
92
- AttachmentCipherInputStream . CreateForAttachment ( File . Open ( cipherFile , FileMode . Open ) , plaintextInput . Length , key , badDigest ) ;
132
+ using FileStream fileStream = File . Open ( cipherFile , FileMode . Open ) ;
133
+ AttachmentCipherInputStream . CreateForAttachment ( fileStream , plaintextInput . Length , key , badDigest ) ;
93
134
}
94
135
catch ( InvalidMessageException )
95
136
{
@@ -120,7 +161,8 @@ public void Test_Attachment_DecryptFailOnNullDigest()
120
161
121
162
cipherFile = WriteToFile ( encryptResult . ciphertext ) ;
122
163
123
- AttachmentCipherInputStream . CreateForAttachment ( File . Open ( cipherFile , FileMode . Open ) , plaintextInput . Length , key , null ) ;
164
+ using FileStream fileStream = File . Open ( cipherFile , FileMode . Open ) ;
165
+ AttachmentCipherInputStream . CreateForAttachment ( fileStream , plaintextInput . Length , key , null ) ;
124
166
}
125
167
catch ( InvalidMessageException )
126
168
{
@@ -155,7 +197,8 @@ public void Test_Attachment_DecryptFailOnBadMac()
155
197
156
198
cipherFile = WriteToFile ( badMacCiphertext ) ;
157
199
158
- AttachmentCipherInputStream . CreateForAttachment ( File . Open ( cipherFile , FileMode . Open ) , plaintextInput . Length , key , encryptResult . digest ) ;
200
+ using FileStream fileStream = File . Open ( cipherFile , FileMode . Open ) ;
201
+ AttachmentCipherInputStream . CreateForAttachment ( fileStream , plaintextInput . Length , key , encryptResult . digest ) ;
159
202
}
160
203
catch ( InvalidMessageException )
161
204
{
@@ -178,7 +221,7 @@ public void Test_Sticker_EncryptDecrypt()
178
221
byte [ ] packKey = Util . GetSecretBytes ( 32 ) ;
179
222
byte [ ] plaintextInput = Encoding . UTF8 . GetBytes ( "Peter Parker" ) ;
180
223
EncryptResult encryptResult = EncryptData ( plaintextInput , ExpandPackKey ( packKey ) ) ;
181
- Stream inputStream = AttachmentCipherInputStream . CreateForStickerData ( encryptResult . ciphertext , packKey ) ;
224
+ using Stream inputStream = AttachmentCipherInputStream . CreateForStickerData ( encryptResult . ciphertext , packKey ) ;
182
225
byte [ ] plaintextOutput = ReadInputStreamFully ( inputStream ) ;
183
226
184
227
CollectionAssert . AreEqual ( plaintextInput , plaintextOutput ) ;
@@ -190,7 +233,7 @@ public void Test_Sticker_EncryptDecryptEmpty()
190
233
byte [ ] packKey = Util . GetSecretBytes ( 32 ) ;
191
234
byte [ ] plaintextInput = Encoding . UTF8 . GetBytes ( string . Empty ) ;
192
235
EncryptResult encryptResult = EncryptData ( plaintextInput , ExpandPackKey ( packKey ) ) ;
193
- Stream inputStream = AttachmentCipherInputStream . CreateForStickerData ( encryptResult . ciphertext , packKey ) ;
236
+ using Stream inputStream = AttachmentCipherInputStream . CreateForStickerData ( encryptResult . ciphertext , packKey ) ;
194
237
byte [ ] plaintextOutput = ReadInputStreamFully ( inputStream ) ;
195
238
196
239
CollectionAssert . AreEqual ( plaintextInput , plaintextOutput ) ;
@@ -270,14 +313,7 @@ private static void DeleteFile(string path)
270
313
{
271
314
if ( File . Exists ( path ) )
272
315
{
273
- try
274
- {
275
- File . Delete ( path ) ;
276
- }
277
- catch ( IOException )
278
- {
279
- // for some reason this fails
280
- }
316
+ File . Delete ( path ) ;
281
317
}
282
318
}
283
319
0 commit comments