The unpac monorepo manager self-hosting as a monorepo using unpac

Add tests on Base64_rfc2045

+94 -1
+1 -1
test/dune
··· 1 1 (executable 2 2 (name test) 3 - (libraries base64 rresult alcotest bos)) 3 + (libraries base64 base64.rfc2045 rresult alcotest bos)) 4 4 5 5 (alias 6 6 (name runtest)
+93
test/test.ml
··· 120 120 Alcotest.(check string) (sprintf "decode %s" r) c (Base64.decode_exn ~pad:false ~off ~len r); 121 121 ) cfcs_tests 122 122 123 + exception Malformed 124 + exception Wrong_padding 123 125 126 + let strict_base64_rfc2045_of_string x = 127 + let decoder = Base64_rfc2045.decoder (`String x) in 128 + let res = Buffer.create 16 in 129 + 130 + let rec go () = match Base64_rfc2045.decode decoder with 131 + | `End -> () 132 + | `Wrong_padding -> raise Wrong_padding 133 + | `Malformed _ -> raise Malformed 134 + | `Flush x -> Buffer.add_string res x ; go () 135 + | `Await -> Alcotest.failf "Retrieve impossible case: `Await" in 136 + 137 + Base64_rfc2045.src decoder (Bytes.unsafe_of_string x) 0 (String.length x) ; 138 + go () ; Buffer.contents res 139 + 140 + let relaxed_base64_rfc2045_of_string x = 141 + let decoder = Base64_rfc2045.decoder (`String x) in 142 + let res = Buffer.create 16 in 143 + 144 + let rec go () = match Base64_rfc2045.decode decoder with 145 + | `End -> () 146 + | `Wrong_padding -> go () 147 + | `Malformed _ -> go () 148 + | `Flush x -> Buffer.add_string res x ; go () 149 + | `Await -> Alcotest.failf "Retrieve impossible case: `Await" in 150 + 151 + Base64_rfc2045.src decoder (Bytes.unsafe_of_string x) 0 (String.length x) ; 152 + go () ; Buffer.contents res 153 + 154 + let test_strict_rfc2045 = 155 + [ "c2FsdXQgbGVzIGNvcGFpbnMgZmF1dCBhYnNvbHVtZW50IHF1ZSBqZSBkw6lwYXNzZSBsZXMgODAg\r\n\ 156 + Y2hhcmFjdGVycyBwb3VyIHZvaXIgc2kgbW9uIGVuY29kZXIgZml0cyBiaWVuIGRhbnMgbGVzIGxp\r\n\ 157 + bWl0ZXMgZGUgbGEgUkZDIDIwNDUgLi4u", 158 + "salut les copains faut absolument que je dépasse les 80 characters pour voir si \ 159 + mon encoder fits bien dans les limites de la RFC 2045 ..." 160 + ; "", "" 161 + ; "Zg==", "f" 162 + ; "Zm8=", "fo" 163 + ; "Zm9v", "foo" 164 + ; "Zm9vYg==", "foob" 165 + ; "Zm9vYmE=", "fooba" 166 + ; "Zm9vYmFy", "foobar" ] 167 + 168 + let test_relaxed_rfc2045 = 169 + [ "Zg", "f" 170 + ; "Zm\n8", "fo" 171 + ; "Zm\r9v", "foo" 172 + ; "Zm9 vYg", "foob" 173 + ; "Zm9\r\n vYmE", "fooba" 174 + ; "Zm9évYmFy", "foobar" ] 175 + 176 + let strict_base64_rfc2045_to_string x = 177 + let res = Buffer.create 16 in 178 + let encoder = Base64_rfc2045.encoder (`Buffer res) in 179 + String.iter 180 + (fun chr -> match Base64_rfc2045.encode encoder (`Char chr) with 181 + | `Ok -> () 182 + | `Partial -> Alcotest.failf "Retrieve impossible case for (`Char %02x): `Partial" (Char.code chr)) 183 + x ; 184 + match Base64_rfc2045.encode encoder `End with 185 + | `Ok -> Buffer.contents res 186 + | `Partial -> Alcotest.fail "Retrieve impossible case for `End: `Partial" 187 + 188 + let test_strict_with_malformed_input_rfc2045 = 189 + List.mapi (fun i (has, _) -> 190 + Alcotest.test_case (Fmt.strf "strict rfc2045 - %02d" i) `Quick @@ fun () -> 191 + try 192 + let _ = strict_base64_rfc2045_of_string has in 193 + Alcotest.failf "Strict parser valids malformed input: %S" has 194 + with Malformed | Wrong_padding -> () ) 195 + test_relaxed_rfc2045 196 + 197 + let test_strict_rfc2045 = 198 + List.mapi (fun i (has, expect) -> 199 + Alcotest.test_case (Fmt.strf "strict rfc2045 - %02d" i) `Quick @@ fun () -> 200 + try 201 + let res0 = strict_base64_rfc2045_of_string has in 202 + let res1 = strict_base64_rfc2045_to_string res0 in 203 + Alcotest.(check string) "encode(decode(x)) = x" res1 has ; 204 + Alcotest.(check string) "decode(x)" res0 expect 205 + with Malformed | Wrong_padding -> Alcotest.failf "Invalid input %S" has) 206 + test_strict_rfc2045 207 + 208 + let test_relaxed_rfc2045 = 209 + List.mapi (fun i (has, expect) -> 210 + Alcotest.test_case (Fmt.strf "relaxed rfc2045 - %02d" i) `Quick @@ fun () -> 211 + let res0 = relaxed_base64_rfc2045_of_string has in 212 + Alcotest.(check string) "decode(x)" res0 expect) 213 + test_relaxed_rfc2045 124 214 125 215 let test_invariants = [ "Alphabet size", `Quick, alphabet_size ] 126 216 let test_codec = [ "RFC4648 test vectors", `Quick, test_rfc4648 ··· 133 223 Alcotest.run "Base64" [ 134 224 "invariants", test_invariants; 135 225 "codec", test_codec; 226 + "rfc2045", test_strict_rfc2045; 227 + "rfc2045", test_strict_with_malformed_input_rfc2045; 228 + "rfc2045", test_relaxed_rfc2045; 136 229 ] 137 230