tangled
alpha
login
or
join now
anil.recoil.org
/
unpac-unpac
0
fork
atom
The unpac monorepo manager self-hosting as a monorepo using unpac
0
fork
atom
overview
issues
pulls
pipelines
Add tests on Base64_rfc2045
dinosaure.tngl.sh
7 years ago
2ee4550b
ec43b374
+94
-1
2 changed files
expand all
collapse all
unified
split
test
dune
test.ml
+1
-1
test/dune
···
1
1
(executable
2
2
(name test)
3
3
-
(libraries base64 rresult alcotest bos))
3
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
123
+
exception Malformed
124
124
+
exception Wrong_padding
123
125
126
126
+
let strict_base64_rfc2045_of_string x =
127
127
+
let decoder = Base64_rfc2045.decoder (`String x) in
128
128
+
let res = Buffer.create 16 in
129
129
+
130
130
+
let rec go () = match Base64_rfc2045.decode decoder with
131
131
+
| `End -> ()
132
132
+
| `Wrong_padding -> raise Wrong_padding
133
133
+
| `Malformed _ -> raise Malformed
134
134
+
| `Flush x -> Buffer.add_string res x ; go ()
135
135
+
| `Await -> Alcotest.failf "Retrieve impossible case: `Await" in
136
136
+
137
137
+
Base64_rfc2045.src decoder (Bytes.unsafe_of_string x) 0 (String.length x) ;
138
138
+
go () ; Buffer.contents res
139
139
+
140
140
+
let relaxed_base64_rfc2045_of_string x =
141
141
+
let decoder = Base64_rfc2045.decoder (`String x) in
142
142
+
let res = Buffer.create 16 in
143
143
+
144
144
+
let rec go () = match Base64_rfc2045.decode decoder with
145
145
+
| `End -> ()
146
146
+
| `Wrong_padding -> go ()
147
147
+
| `Malformed _ -> go ()
148
148
+
| `Flush x -> Buffer.add_string res x ; go ()
149
149
+
| `Await -> Alcotest.failf "Retrieve impossible case: `Await" in
150
150
+
151
151
+
Base64_rfc2045.src decoder (Bytes.unsafe_of_string x) 0 (String.length x) ;
152
152
+
go () ; Buffer.contents res
153
153
+
154
154
+
let test_strict_rfc2045 =
155
155
+
[ "c2FsdXQgbGVzIGNvcGFpbnMgZmF1dCBhYnNvbHVtZW50IHF1ZSBqZSBkw6lwYXNzZSBsZXMgODAg\r\n\
156
156
+
Y2hhcmFjdGVycyBwb3VyIHZvaXIgc2kgbW9uIGVuY29kZXIgZml0cyBiaWVuIGRhbnMgbGVzIGxp\r\n\
157
157
+
bWl0ZXMgZGUgbGEgUkZDIDIwNDUgLi4u",
158
158
+
"salut les copains faut absolument que je dépasse les 80 characters pour voir si \
159
159
+
mon encoder fits bien dans les limites de la RFC 2045 ..."
160
160
+
; "", ""
161
161
+
; "Zg==", "f"
162
162
+
; "Zm8=", "fo"
163
163
+
; "Zm9v", "foo"
164
164
+
; "Zm9vYg==", "foob"
165
165
+
; "Zm9vYmE=", "fooba"
166
166
+
; "Zm9vYmFy", "foobar" ]
167
167
+
168
168
+
let test_relaxed_rfc2045 =
169
169
+
[ "Zg", "f"
170
170
+
; "Zm\n8", "fo"
171
171
+
; "Zm\r9v", "foo"
172
172
+
; "Zm9 vYg", "foob"
173
173
+
; "Zm9\r\n vYmE", "fooba"
174
174
+
; "Zm9évYmFy", "foobar" ]
175
175
+
176
176
+
let strict_base64_rfc2045_to_string x =
177
177
+
let res = Buffer.create 16 in
178
178
+
let encoder = Base64_rfc2045.encoder (`Buffer res) in
179
179
+
String.iter
180
180
+
(fun chr -> match Base64_rfc2045.encode encoder (`Char chr) with
181
181
+
| `Ok -> ()
182
182
+
| `Partial -> Alcotest.failf "Retrieve impossible case for (`Char %02x): `Partial" (Char.code chr))
183
183
+
x ;
184
184
+
match Base64_rfc2045.encode encoder `End with
185
185
+
| `Ok -> Buffer.contents res
186
186
+
| `Partial -> Alcotest.fail "Retrieve impossible case for `End: `Partial"
187
187
+
188
188
+
let test_strict_with_malformed_input_rfc2045 =
189
189
+
List.mapi (fun i (has, _) ->
190
190
+
Alcotest.test_case (Fmt.strf "strict rfc2045 - %02d" i) `Quick @@ fun () ->
191
191
+
try
192
192
+
let _ = strict_base64_rfc2045_of_string has in
193
193
+
Alcotest.failf "Strict parser valids malformed input: %S" has
194
194
+
with Malformed | Wrong_padding -> () )
195
195
+
test_relaxed_rfc2045
196
196
+
197
197
+
let test_strict_rfc2045 =
198
198
+
List.mapi (fun i (has, expect) ->
199
199
+
Alcotest.test_case (Fmt.strf "strict rfc2045 - %02d" i) `Quick @@ fun () ->
200
200
+
try
201
201
+
let res0 = strict_base64_rfc2045_of_string has in
202
202
+
let res1 = strict_base64_rfc2045_to_string res0 in
203
203
+
Alcotest.(check string) "encode(decode(x)) = x" res1 has ;
204
204
+
Alcotest.(check string) "decode(x)" res0 expect
205
205
+
with Malformed | Wrong_padding -> Alcotest.failf "Invalid input %S" has)
206
206
+
test_strict_rfc2045
207
207
+
208
208
+
let test_relaxed_rfc2045 =
209
209
+
List.mapi (fun i (has, expect) ->
210
210
+
Alcotest.test_case (Fmt.strf "relaxed rfc2045 - %02d" i) `Quick @@ fun () ->
211
211
+
let res0 = relaxed_base64_rfc2045_of_string has in
212
212
+
Alcotest.(check string) "decode(x)" res0 expect)
213
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
226
+
"rfc2045", test_strict_rfc2045;
227
227
+
"rfc2045", test_strict_with_malformed_input_rfc2045;
228
228
+
"rfc2045", test_relaxed_rfc2045;
136
229
]
137
230