458b1beeb1bde578d5d931034a399762d1ab0615
1
module CryptoFileTests.Tests
7 printfn
"===== Unit tests"
8 UnitTests.runAllUnitTests
()
9 printfn
"===== Unit tests OK"
11 printfn
"===== API tests"
12 let plainFilename = "test.txt"
13 let cipherFilename = "test.cipher"
14 let fileContent = "Screw the NSA"
16 File.WriteAllText (plainFilename, fileContent)
18 let keyCryptPub, keyCryptPriv
= API.generatKeysPair
()
19 let keySigPub, keySigPriv
= API.generatKeysPair
()
22 API.encryptFile
plainFilename cipherFilename keySigPriv keyCryptPub
25 API.decryptFile
cipherFilename "." keySigPub keyCryptPriv
27 let incrementByteCipherFileAt position
=
28 use fs = new FileStream (cipherFilename, FileMode.Open, FileAccess.ReadWrite)
29 fs.Position <- position
30 let byte = fs.ReadByte () |> byte
31 fs.Position <- position
32 fs.Write ([| byte + 1uy |], 0, 1) // Modulo 256.
35 File.Delete plainFilename
37 assert (File.ReadAllText plainFilename = fileContent)
39 printfn
"== Altering the MAC..."
40 for i
in 0 .. API.hmacSize
- 1 do
43 incrementByteCipherFileAt (int64 i
)
48 | error -> assert (error :? IntegrityError)
51 printfn
"== Altering the signature..."
52 for i
in 0 .. API.signatureSize
- 1 do
55 incrementByteCipherFileAt (int64
<| API.hmacSize
+ i
)
60 | error -> assert (error :? SignatureMismatch)
63 printfn
"== Altering the keys..."
64 for i
in 0 .. API.keysSize
- 1 do
67 incrementByteCipherFileAt (int64
<| API.hmacSize
+ API.signatureSize
+ i
)
72 | error -> assert (error :? UnableToDecryptKeys)
75 printfn
"== Altering the cyphertext..."
77 incrementByteCipherFileAt (int64
<| API.hmacSize
+ API.signatureSize
+ API.keysSize
)
82 | error -> assert (error :? IntegrityError)
84 File.Delete cipherFilename
85 File.Delete plainFilename
87 printfn
"===== API tests OK"