Typos in report + clean up.
[crypto_lab2.git] / labo2-fsharp / CryptoFileTests / Tests.fs
index dd1b3e0..aeea186 100644 (file)
@@ -5,7 +5,7 @@ open CryptoFile
 
 let doSomeTests () =
     printfn "===== Unit tests"
-    CryptoFile.UnitTests.runAllUnitTests ()
+    UnitTests.runAllUnitTests ()
     printfn "===== Unit tests OK"
 
     printfn "===== API tests"
@@ -15,8 +15,8 @@ let doSomeTests () =
 
     File.WriteAllText (plainFilename, fileContent)
 
-    let keyCryptPub, keyCryptPriv = API.generatKeysPair
-    let keySigPub, keySigPriv = API.generatKeysPair
+    let keyCryptPub, keyCryptPriv = API.generatKeysPair ()
+    let keySigPub, keySigPriv = API.generatKeysPair ()
 
     let encrypt () = 
         API.encryptFile plainFilename cipherFilename keySigPriv keyCryptPub
@@ -24,50 +24,66 @@ let doSomeTests () =
     let decrypt () = 
         API.decryptFile cipherFilename "." keySigPub keyCryptPriv
 
-    let writeByteToCipherFileAt byte position = 
-        using (new FileStream (cipherFilename, FileMode.Open, FileAccess.Write))
-              (fun fs -> fs.Position <- position
-                         fs.Write ([| byte |], 0, 1))
+    let incrementByteCipherFileAt position = 
+        use fs = new FileStream (cipherFilename, FileMode.Open, FileAccess.ReadWrite)
+        fs.Position <- position
+        let byte = fs.ReadByte () |> byte
+        fs.Position <- position
+        fs.Write ([| byte + 1uy |], 0, 1) // Modulo 256.
 
     encrypt ()
     File.Delete plainFilename
     decrypt ()
     assert (File.ReadAllText plainFilename = fileContent)
 
-    printfn "== Altering the MAC..."
-    writeByteToCipherFileAt 0uy 0L
-    try
-        decrypt ()
-        assert false
-    with 
-        | error -> assert (error :? IntegrityError)
+    printfn "== Altering the MAC... (%d bytes)" (API.hmacSize - 1)
+    for i in 0 .. API.hmacSize - 1 do
+        printf "."
+        encrypt ()
+        incrementByteCipherFileAt (int64 i)
+        try
+            decrypt ()
+            assert false
+        with
+            | error -> assert (error :? IntegrityError)
+    printfn ""
 
-    printfn "== Altering the signature..."
-    encrypt ()
-    writeByteToCipherFileAt 0uy 32L
-    try
-        decrypt ()
-        assert false
-    with
-        | error -> assert (error :? SignatureMismatch)
+    printfn "== Altering the signature... (%d bytes)" (API.signatureSize - 1)
+    for i in 0 .. API.signatureSize - 1 do
+        printf "."
+        encrypt ()
+        incrementByteCipherFileAt (int64 <| API.hmacSize + i)
+        try
+            decrypt ()
+            assert false
+        with
+            | error -> assert (error :? SignatureMismatch)
+    printfn ""
 
-    printfn "== Altering the keys..."
-    encrypt ()
-    writeByteToCipherFileAt 0uy (32L + 256L)
-    try
-        decrypt ()
-        assert false
-    with
-        | error -> assert (error :? UnableToDecryptAESKeys)
+    printfn "== Altering the keys... (%d bytes)" (API.keysSize - 1)
+    for i in 0 .. API.keysSize - 1 do
+        printf "."
+        encrypt ()
+        incrementByteCipherFileAt (int64 <| API.hmacSize + API.signatureSize + i)
+        try
+            decrypt ()
+            assert false
+        with
+            | error -> assert (error :? UnableToDecryptKeys)
+    printfn ""
 
-    printfn "== Altering the cyphering..."
-    encrypt ()
-    writeByteToCipherFileAt 0uy (32L + 256L + 256L)
-    try
-        decrypt ()
-        assert false
-    with 
-        | error -> assert (error :? IntegrityError)
+    let cyphertextLength = (int (FileInfo (cipherFilename)).Length) + API.hmacSize + API.signatureSize + API.keysSize
+    printfn "== Altering the cyphertext... (%d bytes)" cyphertextLength
+    for i in 0 .. cyphertextLength do 
+        printf "."
+        encrypt ()
+        incrementByteCipherFileAt (int64 <| API.hmacSize + API.signatureSize + API.keysSize + i)
+        try
+            decrypt ()
+            assert false
+        with
+            | error -> assert (error :? IntegrityError)
+    printfn ""
 
     File.Delete cipherFilename
     File.Delete plainFilename