...
 
Commits (2)
......@@ -19,3 +19,6 @@ let hash algorithm text =
let cmd = Cmd.(v "openssl" % "dgst" % "-sha256" % "-hex") in
let result = OS.Cmd.(run_io cmd (in_string text) |> to_string) in
{ algorithm = algorithm; hash = unwrap result }
let format { algorithm; hash } =
HashAlgorithm.to_string algorithm, Base64.encode_exn hash
......@@ -3,10 +3,10 @@ type t =
| Sha256
let of_string = function
| "Sha-1" -> Some Sha1
| "Sha-2" -> Some Sha256
| "sha1" -> Some Sha1
| "sha256" -> Some Sha256
| _ -> None
let to_string = function
| Sha1 -> "Sha-1"
| Sha256 -> "Sha-2"
| Sha1 -> "sha1"
| Sha256 -> "sha256"
(tests
(names test power_test hash_test hashAlgorithm_test)
(names power_test hash_test hashAlgorithm_test)
(libraries alcotest yayakaChain))
......@@ -10,13 +10,13 @@ let pp ppf value =
let hash_algorithm = Test.testable pp ( = )
let test_of_string () =
Test.(check (option hash_algorithm)) "Sha-1" (Some Sha1) (of_string "Sha-1");
Test.(check (option hash_algorithm)) "Sha-2" (Some Sha256) (of_string "Sha-2");
Test.(check (option hash_algorithm)) "sha1" (Some Sha1) (of_string "sha1");
Test.(check (option hash_algorithm)) "sha256" (Some Sha256) (of_string "sha256");
Test.(check (option hash_algorithm)) "Sha256" None (of_string "Sha256")
let test_to_string () =
Test.(check string) "Sha-1" "Sha-1" (to_string Sha1);
Test.(check string) "Sha-2" "Sha-2" (to_string Sha256)
Test.(check string) "sha1" "sha1" (to_string Sha1);
Test.(check string) "sha256" "sha256" (to_string Sha256)
let hash_algorithm_tests = [
"of_string", `Quick, test_of_string;
......
......@@ -27,8 +27,21 @@ let test_hash () =
hash = (h "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad") }
(Hash.hash Sha256 "abc")
let test_format () =
Test.(check (pair string string)) "sha1"
("sha1", "qZk+NkcGgWq6PiVxeFDCbJzQ2J0=")
(Hash.format {
algorithm = Sha1;
hash = (h "a9993e364706816aba3e25717850c26c9cd0d89d") });
Test.(check (pair string string)) "sha256"
("sha256", "ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0=")
(Hash.format {
algorithm = Sha256;
hash = (h "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad") })
let hash_tests = [
"hash", `Quick, test_hash;
"format", `Quick, test_format;
]
let () =
......
let success () =
Alcotest.(check int) "1 = 1" 1 (2 - 1)
let test_set = [
"Sample test" , `Quick, success;
]
let () =
Alcotest.run "test for nothing" [
"test_set", test_set;
]