X-Git-Url: http://git.euphorik.ch/?a=blobdiff_plain;f=labo2-fsharp%2FCryptoFileTests%2FTests.fs;h=03b5c4a3b137d1d64cd51fec37d4c6656b11a3ee;hb=96cd30efb6e215aec86803cc2a5f0a601930be1d;hp=dd1b3e04ba9b5848d324f7784a8887cfce505adf;hpb=61a8b3492e89e883b23c158eb52ba2b070234df8;p=crypto_lab2.git diff --git a/labo2-fsharp/CryptoFileTests/Tests.fs b/labo2-fsharp/CryptoFileTests/Tests.fs index dd1b3e0..03b5c4a 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 @@ -24,52 +24,68 @@ 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 - printfn "===== API tests OK" + printfn "===== API tests OK" \ No newline at end of file