Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename B64 to Base64, Rfc2045 to Base64_rfc2045 #25

Merged
merged 5 commits into from
Jan 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ Simple encoding and decoding.

```shell
utop # #require "base64";;
utop # let enc = B64.encode "OCaml rocks!";;
utop # let enc = Base64.encode_exn "OCaml rocks!";;
val enc : string = "T0NhbWwgcm9ja3Mh"
utop # let plain = B64.decode enc;;
utop # let plain = B64.decode_exn enc;;
val plain : string = "OCaml rocks!"
```

Expand Down
6 changes: 3 additions & 3 deletions bench/benchmarks.ml
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ open Core_bench
let b64_encode_and_decode len =
let input = random len in
Staged.stage @@ fun () ->
let encoded = B64.encode_exn input in
let _decoded = B64.decode_exn encoded in
let encoded = Base64.encode_exn input in
let _decoded = Base64.decode_exn encoded in
()

let old_encode_and_decode len =
Expand All @@ -102,7 +102,7 @@ let old_encode_and_decode len =
let args = [ 0; 10; 50; 100; 500; 1000; 2500; 5000 ]

let test_b64 =
Test.create_indexed ~name:"B64"
Test.create_indexed ~name:"Base64"
~args b64_encode_and_decode

let test_old =
Expand Down
2 changes: 1 addition & 1 deletion fuzz/dune
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(executable
(name fuzz_rfc2045)
(modules fuzz_rfc2045)
(libraries astring crowbar fmt rfc2045))
(libraries astring crowbar fmt base64.rfc2045))

(executable
(name fuzz_rfc4648)
Expand Down
18 changes: 9 additions & 9 deletions fuzz/fuzz_rfc2045.ml
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,24 @@ let check_encode str =

let encode input =
let buf = Buffer.create 80 in
let encoder = Rfc2045.encoder (`Buffer buf) in
let encoder = Base64_rfc2045.encoder (`Buffer buf) in
String.iter
(fun c ->
let ret = Rfc2045.encode encoder (`Char c) in
let ret = Base64_rfc2045.encode encoder (`Char c) in
match ret with `Ok -> () | _ -> assert false )
(* XXX(dinosaure): [`Partial] can never occur. *)
input ;
let encode = Rfc2045.encode encoder `End in
let encode = Base64_rfc2045.encode encoder `End in
match encode with
| `Ok -> Buffer.contents buf |> check_encode
| _ -> (* XXX(dinosaure): [`Partial] can never occur. *) assert false

let decode input =
let decoder = Rfc2045.decoder (`String input) in
let decoder = Base64_rfc2045.decoder (`String input) in
let rec go acc =
if Rfc2045.decoder_dangerous decoder then
if Base64_rfc2045.decoder_dangerous decoder then
raise (Decode_error "Dangerous input") ;
match Rfc2045.decode decoder with
match Base64_rfc2045.decode decoder with
| `End -> List.rev acc
| `Flush output -> go (output :: acc)
| `Malformed _ -> raise (Decode_error "Malformed")
Expand All @@ -88,7 +88,7 @@ let char_from_alpha alpha : string gen =

let string_from_alpha n =
let acc = const "" in
let alpha = Rfc2045.default_alphabet in
let alpha = Base64_rfc2045.default_alphabet in
let rec add_char_from_alpha alpha acc = function
| 0 -> acc
| n ->
Expand Down Expand Up @@ -116,10 +116,10 @@ let set_canonic str =
then (
let buf = Bytes.of_string str in
let value =
String.index Rfc2045.default_alphabet (Bytes.get buf (l - 1))
String.index Base64_rfc2045.default_alphabet (Bytes.get buf (l - 1))
in
let canonic =
Rfc2045.default_alphabet.[value land lnot ((1 lsl to_drop) - 1)]
Base64_rfc2045.default_alphabet.[value land lnot ((1 lsl to_drop) - 1)]
in
Bytes.set buf (l - 1) canonic ;
Bytes.unsafe_to_string buf )
Expand Down
32 changes: 16 additions & 16 deletions fuzz/fuzz_rfc4648.ml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ let pp = pp_scalar ~get:String.get ~length:String.length
let (<.>) f g x = f (g x)

let char_from_alphabet alphabet : string gen =
map [ range 64 ] (String.make 1 <.> Char.chr <.> Array.unsafe_get (B64.alphabet alphabet))
map [ range 64 ] (String.make 1 <.> Char.chr <.> Array.unsafe_get (Base64.alphabet alphabet))

let random_string_from_alphabet alphabet len : string gen =
let rec add_char_from_alphabet acc = function
Expand All @@ -54,20 +54,20 @@ let random_string_from_alphabet ~max alphabet =
@@ fun off -> map [ range (real_len - off) ] (fun len -> (input, off, len))

let encode_and_decode (input, off, len) =
match B64.encode ~pad:true ~off ~len input with
match Base64.encode ~pad:true ~off ~len input with
| Error (`Msg err) -> fail err
| Ok result ->
match B64.decode ~pad:true result with
match Base64.decode ~pad:true result with
| Error (`Msg err) -> fail err
| Ok result ->
check_eq ~pp ~cmp:String.compare ~eq:String.equal result (String.sub input off len)

let decode_and_encode (input, off, len) =
match B64.decode ~pad:true ~off ~len input with
match Base64.decode ~pad:true ~off ~len input with
| Error (`Msg err) ->
fail err
| Ok result ->
match B64.encode ~pad:true result with
match Base64.encode ~pad:true result with
| Error (`Msg err) -> fail err
| Ok result ->
check_eq ~pp:Fmt.string ~cmp:String.compare ~eq:String.equal result (String.sub input off len)
Expand All @@ -79,7 +79,7 @@ let (//) x y =

let canonic alphabet =
let dmap = Array.make 256 (-1) in
Array.iteri (fun i x -> Array.set dmap x i) (B64.alphabet alphabet) ;
Array.iteri (fun i x -> Array.set dmap x i) (Base64.alphabet alphabet) ;
fun (input, off, len) ->
let real_len = String.length input in
let input_len = len in
Expand All @@ -101,30 +101,30 @@ let canonic alphabet =
| _ -> assert false in
let decoded = Array.get dmap (Char.code last) in
let canonic = (decoded land mask) in
let encoded = Array.get (B64.alphabet alphabet) canonic in
let encoded = Array.get (Base64.alphabet alphabet) canonic in
Bytes.set output (off + input_len - 1) (Char.chr encoded) ;
(Bytes.unsafe_to_string output, off, normalized_len)
end

let isomorphism0 (input, off, len) =
(* x0 = decode(input) && x1 = decode(encode(x0)) && x0 = x1 *)
match B64.decode ~pad:false ~off ~len input with
match Base64.decode ~pad:false ~off ~len input with
| Error (`Msg err) ->
fail err
| Ok result0 ->
let result1 = B64.encode_exn result0 in
match B64.decode ~pad:true result1 with
let result1 = Base64.encode_exn result0 in
match Base64.decode ~pad:true result1 with
| Error (`Msg err) ->
fail err
| Ok result2 ->
check_eq ~pp ~cmp:String.compare ~eq:String.equal result0 result2

let isomorphism1 (input, off, len) =
let result0 = B64.encode_exn ~off ~len input in
match B64.decode ~pad:true result0 with
let result0 = Base64.encode_exn ~off ~len input in
match Base64.decode ~pad:true result0 with
| Error (`Msg err) -> fail err
| Ok result1 ->
let result2 = B64.encode_exn result1 in
let result2 = Base64.encode_exn result1 in
check_eq ~pp:Fmt.string ~cmp:String.compare ~eq:String.equal result0 result2

let bytes_and_range : (string * int * int) gen =
Expand All @@ -144,12 +144,12 @@ let range_of_max max : (int * int) gen =
let failf fmt = Fmt.kstrf fail fmt

let no_exception pad off len input =
try let _ = B64.decode ?pad ?off ?len ~alphabet:B64.default_alphabet input in ()
try let _ = Base64.decode ?pad ?off ?len ~alphabet:Base64.default_alphabet input in ()
with exn -> failf "decode fails with: %s." (Printexc.to_string exn)

let () =
add_test ~name:"rfc4648: encode -> decode" [ bytes_and_range ] encode_and_decode ;
add_test ~name:"rfc4648: decode -> encode" [ random_string_from_alphabet ~max:1000 B64.default_alphabet ] (decode_and_encode <.> canonic B64.default_alphabet) ;
add_test ~name:"rfc4648: x = decode(encode(x))" [ random_string_from_alphabet ~max:1000 B64.default_alphabet ] isomorphism0 ;
add_test ~name:"rfc4648: decode -> encode" [ random_string_from_alphabet ~max:1000 Base64.default_alphabet ] (decode_and_encode <.> canonic Base64.default_alphabet) ;
add_test ~name:"rfc4648: x = decode(encode(x))" [ random_string_from_alphabet ~max:1000 Base64.default_alphabet ] isomorphism0 ;
add_test ~name:"rfc4648: x = encode(decode(x))" [ bytes_and_range ] isomorphism1 ;
add_test ~name:"rfc4648: no exception leak" [ option bool; option int; option int; bytes ] no_exception
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 3 additions & 5 deletions src/dune
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
(library
(name base64)
(modules b64)
(wrapped false)
(modules base64)
(public_name base64)
(libraries bytes))

(library
(name rfc2045)
(modules rfc2045)
(wrapped false)
(name base64_rfc2045)
(modules base64_rfc2045)
(public_name base64.rfc2045)
(libraries bytes))
8 changes: 8 additions & 0 deletions test/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(executable
(name test)
(libraries base64 rresult alcotest bos))

(alias
(name runtest)
(deps (:exe test.exe))
(action (run %{exe} --color=always)))
10 changes: 0 additions & 10 deletions test/jbuild

This file was deleted.

30 changes: 15 additions & 15 deletions test/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ let cfcs_tests = [
let alphabet_size () =
List.iter (fun (name,alphabet) ->
Alcotest.(check int) (sprintf "Alphabet size %s = 64" name)
64 (B64.length_alphabet alphabet))
["default",B64.default_alphabet; "uri_safe",B64.uri_safe_alphabet]
64 (Base64.length_alphabet alphabet))
["default",Base64.default_alphabet; "uri_safe",Base64.uri_safe_alphabet]

(* Encode using OpenSSL `base64` utility *)
let openssl_encode buf =
Expand All @@ -82,42 +82,42 @@ let openssl_encode buf =

(* Encode using this library *)
let lib_encode buf =
B64.encode_exn ~pad:true buf
Base64.encode_exn ~pad:true buf

let test_rfc4648 () =
List.iter (fun (c,r) ->
(* B64 vs openssl *)
(* Base64 vs openssl *)
Alcotest.(check string) (sprintf "encode %s" c) (openssl_encode c) (lib_encode c);
(* B64 vs test cases above *)
(* Base64 vs test cases above *)
Alcotest.(check string) (sprintf "encode rfc4648 %s" c) r (lib_encode c);
(* B64 decode vs library *)
Alcotest.(check string) (sprintf "decode %s" r) c (B64.decode_exn r);
(* Base64 decode vs library *)
Alcotest.(check string) (sprintf "decode %s" r) c (Base64.decode_exn r);
) rfc4648_tests

let test_rfc3548 () =
List.iter (fun (c,r) ->
(* B64 vs openssl *)
(* Base64 vs openssl *)
Alcotest.(check string) (sprintf "encode %s" c) (openssl_encode c) (lib_encode c);
(* B64 vs test cases above *)
(* Base64 vs test cases above *)
Alcotest.(check string) (sprintf "encode rfc3548 %s" c) r (lib_encode c);
(* B64 decode vs library *)
Alcotest.(check string) (sprintf "decode %s" r) c (B64.decode_exn r);
(* Base64 decode vs library *)
Alcotest.(check string) (sprintf "decode %s" r) c (Base64.decode_exn r);
) rfc3548_tests

let test_hannes () =
List.iter (fun (c,r) ->
(* B64 vs test cases above *)
Alcotest.(check string) (sprintf "decode %s" r) c (B64.decode_exn ~pad:false r);
(* Base64 vs test cases above *)
Alcotest.(check string) (sprintf "decode %s" r) c (Base64.decode_exn ~pad:false r);
) hannes_tests

let test_php () =
List.iter (fun (c,r) ->
Alcotest.(check string) (sprintf "decode %s" r) c (B64.decode_exn ~pad:false ~alphabet:B64.uri_safe_alphabet r);
Alcotest.(check string) (sprintf "decode %s" r) c (Base64.decode_exn ~pad:false ~alphabet:Base64.uri_safe_alphabet r);
) php_tests

let test_cfcs () =
List.iter (fun (off, len, c,r) ->
Alcotest.(check string) (sprintf "decode %s" r) c (B64.decode_exn ~pad:false ~off ~len r);
Alcotest.(check string) (sprintf "decode %s" r) c (Base64.decode_exn ~pad:false ~off ~len r);
) cfcs_tests


Expand Down