X-Git-Url: http://git.euphorik.ch/?a=blobdiff_plain;f=labo2-fsharp%2FCryptoFileTests%2FTests.fs;h=aeea186d2bf60bc064f754017161ae09c4510313;hb=57d7ae8d75a854296718cb1056efbeb476309908;hp=eb8a2902805fe33956e32b1309f771c1a70028ec;hpb=0508ca9ddb817b14ca747f8736ea1960cb70d2e7;p=crypto_lab2.git diff --git a/labo2-fsharp/CryptoFileTests/Tests.fs b/labo2-fsharp/CryptoFileTests/Tests.fs index eb8a290..aeea186 100644 --- a/labo2-fsharp/CryptoFileTests/Tests.fs +++ b/labo2-fsharp/CryptoFileTests/Tests.fs @@ -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 @@ -25,51 +25,65 @@ let doSomeTests () = API.decryptFile cipherFilename "." keySigPub keyCryptPriv let incrementByteCipherFileAt position = - using (new FileStream (cipherFilename, FileMode.Open, FileAccess.ReadWrite)) - (fun fs -> fs.Position <- position - let byte = fs.ReadByte () |> byte - fs.Position <- position - fs.Write ([| byte + 1uy |], 0, 1)) // Automatically modulo 256. + 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..." - incrementByteCipherFileAt 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 () - incrementByteCipherFileAt 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 () - incrementByteCipherFileAt (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 cyphertext..." - encrypt () - incrementByteCipherFileAt (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