From c242bcc1353ce70f2f8258c42db7544fbae13a52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Reynir=20Bj=C3=B6rnsson?= Date: Mon, 30 Nov 2020 22:56:15 +0100 Subject: [PATCH 1/3] Add failing test --- test/test.ml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/test.ml b/test/test.ml index 41a4b9a..81d92ef 100644 --- a/test/test.ml +++ b/test/test.ml @@ -184,6 +184,13 @@ let test_nocrypto () = Alcotest.(check (option string)) (sprintf "decode %S" input) res' res) nocrypto_tests +let test_reynir () = + let r = "Hello, World!" in + let a = Base64.(alphabet default_alphabet) in + let () = Array.iteri (fun i _ -> a.(i) <- int_of_char 'A') a in + Alcotest.(check string) "decode encode after mutating default_alphabet" + r Base64.(decode_exn (encode_string r)) + exception Malformed exception Wrong_padding @@ -309,6 +316,7 @@ let test_codec = ("Cfcs test vectors", `Quick, test_cfcs); ("PHP test vectors", `Quick, test_php); ("Nocrypto test vectors", `Quick, test_nocrypto); + ("Reynir test vectors", `Quick, test_reynir); ] let () = From 0d7fdf775d8bce4f538daaf776823357028271b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Reynir=20Bj=C3=B6rnsson?= Date: Mon, 30 Nov 2020 23:13:22 +0100 Subject: [PATCH 2/3] Return string alphabet Change [Base64.alphabet] to return the string representation of the alphabet instead of leaking the internal array. --- src/base64.ml | 2 +- src/base64.mli | 2 +- test/test.ml | 8 -------- 3 files changed, 2 insertions(+), 10 deletions(-) diff --git a/src/base64.ml b/src/base64.ml index 7cb22e4..1a2e5fd 100644 --- a/src/base64.ml +++ b/src/base64.ml @@ -57,7 +57,7 @@ let make_alphabet alphabet = let length_alphabet { emap; _ } = Array.length emap -let alphabet { emap; _ } = emap +let alphabet { emap; _ } = String.init (Array.length emap) (fun i -> Char.chr emap.(i)) let default_alphabet = make_alphabet diff --git a/src/base64.mli b/src/base64.mli index a82b55e..239d5c8 100644 --- a/src/base64.mli +++ b/src/base64.mli @@ -45,7 +45,7 @@ val make_alphabet : string -> alphabet val length_alphabet : alphabet -> int (** Returns length of the alphabet, should be 64. *) -val alphabet : alphabet -> int array +val alphabet : alphabet -> string (** Returns the alphabet. *) val decode_exn : diff --git a/test/test.ml b/test/test.ml index 81d92ef..41a4b9a 100644 --- a/test/test.ml +++ b/test/test.ml @@ -184,13 +184,6 @@ let test_nocrypto () = Alcotest.(check (option string)) (sprintf "decode %S" input) res' res) nocrypto_tests -let test_reynir () = - let r = "Hello, World!" in - let a = Base64.(alphabet default_alphabet) in - let () = Array.iteri (fun i _ -> a.(i) <- int_of_char 'A') a in - Alcotest.(check string) "decode encode after mutating default_alphabet" - r Base64.(decode_exn (encode_string r)) - exception Malformed exception Wrong_padding @@ -316,7 +309,6 @@ let test_codec = ("Cfcs test vectors", `Quick, test_cfcs); ("PHP test vectors", `Quick, test_php); ("Nocrypto test vectors", `Quick, test_nocrypto); - ("Reynir test vectors", `Quick, test_reynir); ] let () = From 1414e51a0f0e910c600623ce7b88ce85845acea5 Mon Sep 17 00:00:00 2001 From: dinosaure Date: Mon, 8 Feb 2021 11:38:54 +0100 Subject: [PATCH 3/3] Apply ocamlformat --- src/base64.ml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/base64.ml b/src/base64.ml index 1a2e5fd..8160119 100644 --- a/src/base64.ml +++ b/src/base64.ml @@ -57,7 +57,8 @@ let make_alphabet alphabet = let length_alphabet { emap; _ } = Array.length emap -let alphabet { emap; _ } = String.init (Array.length emap) (fun i -> Char.chr emap.(i)) +let alphabet { emap; _ } = + String.init (Array.length emap) (fun i -> Char.chr emap.(i)) let default_alphabet = make_alphabet