sim-codec-binary-base64 0.1.1

SIM workspace package for sim codec binary base64.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
use std::sync::Arc;

use sim_codec::{
    DecodeLimits, DecodePosition, DecodedForm, Input, Output, decode_datum_with_codec,
    decode_default_with_codec, decode_tree_with_codec, decode_with_codec,
    decode_with_codec_and_limits, encode_datum_with_codec, encode_located_with_codec,
    encode_tree_with_codec, encode_with_codec,
};
use sim_kernel::{
    Datum, DefaultFactory, EagerPolicy, EncodeOptions, Expr, LocatedExpr, LocatedExprTree,
    NumberLiteral, Origin, QuoteMode, ReadPolicy, SourceId, Span, Symbol, Trivia,
};

use crate::{
    BinaryBase64CodecLib,
    base64::{decode_base64, encode_base64},
};

fn cx() -> sim_kernel::Cx {
    let mut cx = sim_kernel::Cx::new(Arc::new(EagerPolicy), Arc::new(DefaultFactory));
    sim_test_support::register_core_classes(&mut cx);
    let binary = sim_codec_binary::BinaryCodecLib::new(cx.registry_mut().fresh_codec_id());
    cx.load_lib(&binary).unwrap();
    let binary_base64 = BinaryBase64CodecLib::new(cx.registry_mut().fresh_codec_id());
    cx.load_lib(&binary_base64).unwrap();
    cx
}

fn symbol() -> Symbol {
    Symbol::qualified("codec", "binary-base64")
}

fn corpus() -> Vec<Expr> {
    vec![
        Expr::Nil,
        Expr::Bool(true),
        Expr::Number(NumberLiteral {
            domain: Symbol::qualified("numbers", "f64"),
            canonical: "42.5".to_owned(),
        }),
        Expr::Symbol(Symbol::qualified("math", "pi")),
        Expr::String("line\n\"quoted\"".to_owned()),
        Expr::Bytes(vec![0, 1, 2, 0xff]),
        Expr::List(vec![
            Expr::Symbol(Symbol::new("f")),
            Expr::String("x".to_owned()),
            Expr::Bool(false),
        ]),
        Expr::Map(vec![
            (Expr::Symbol(Symbol::new("b")), Expr::Bool(false)),
            (Expr::Symbol(Symbol::new("a")), Expr::Bool(true)),
        ]),
        Expr::Set(vec![
            Expr::String("z".to_owned()),
            Expr::String("a".to_owned()),
        ]),
        Expr::Quote {
            mode: QuoteMode::Syntax,
            expr: Box::new(Expr::Extension {
                tag: Symbol::qualified("demo", "escape"),
                payload: Box::new(Expr::Annotated {
                    expr: Box::new(Expr::Vector(vec![Expr::Bool(true)])),
                    annotations: vec![(
                        Symbol::qualified("meta", "origin"),
                        Expr::String("matrix".to_owned()),
                    )],
                }),
            }),
        },
    ]
}

#[test]
fn codec_registers() {
    let cx = cx();
    assert!(cx.registry().codec_by_symbol(&symbol()).is_some());
}

#[test]
fn base64_uses_standard_padded_alphabet() {
    assert_eq!(encode_base64(&[]), "");
    assert_eq!(encode_base64(&[0xfb]), "+w==");
    assert_eq!(encode_base64(&[0xfb, 0xef]), "++8=");
    assert_eq!(encode_base64(&[0xfb, 0xef, 0xff]), "++//");
    assert_eq!(
        decode_base64(sim_kernel::CodecId(1), "++8=").unwrap(),
        vec![0xfb, 0xef]
    );
}

#[test]
fn encoder_returns_text_with_no_line_breaks() {
    let mut cx = cx();
    let output = encode_with_codec(
        &mut cx,
        &symbol(),
        &Expr::Bytes((0..=255).collect()),
        EncodeOptions::default(),
    )
    .unwrap();
    let Output::Text(text) = output else {
        panic!("binary-base64 must produce text output");
    };
    assert!(!text.contains('\n'));
    assert!(!text.contains('\r'));
    assert!(text.as_bytes().iter().all(u8::is_ascii));
}

#[test]
fn full_expr_surface_roundtrips() {
    let mut cx = cx();
    for expr in corpus() {
        let decoded = sim_test_support::roundtrip(&mut cx, "binary-base64", &expr);
        assert!(
            decoded.canonical_eq(&expr),
            "decoded {decoded:?} from {expr:?}"
        );
    }
}

#[test]
fn datum_roundtrip_preserves_content_id() {
    let mut cx = cx();
    let datum = sample_datum();
    let content_id = datum.content_id().unwrap();

    let output = encode_datum_with_codec(&mut cx, &symbol(), &datum, EncodeOptions::default())
        .unwrap()
        .into_text()
        .unwrap();
    let decoded = decode_datum_with_codec(
        &mut cx,
        &symbol(),
        Input::Text(output),
        ReadPolicy::default(),
    )
    .unwrap();

    assert_eq!(decoded, datum);
    assert_eq!(decoded.content_id().unwrap(), content_id);
}

#[test]
fn default_decode_returns_datum_even_in_eval_position() {
    let mut cx = cx();
    let datum = sample_datum();
    let output = encode_datum_with_codec(&mut cx, &symbol(), &datum, EncodeOptions::default())
        .unwrap()
        .into_text()
        .unwrap();

    let decoded = decode_default_with_codec(
        &mut cx,
        &symbol(),
        Input::Text(output),
        ReadPolicy::default(),
        DecodePosition::Eval,
    )
    .unwrap();

    assert_eq!(decoded, DecodedForm::Datum(datum));
}

#[test]
fn emitted_text_is_base64_of_binary_frame() {
    let mut cx = cx();
    let expr = Expr::String("wire".to_owned());
    let base64_output = encode_with_codec(&mut cx, &symbol(), &expr, EncodeOptions::default())
        .unwrap()
        .into_text()
        .unwrap();
    let binary_output = encode_with_codec(
        &mut cx,
        &Symbol::qualified("codec", "binary"),
        &expr,
        EncodeOptions::default(),
    )
    .unwrap();
    let Output::Bytes(binary_bytes) = binary_output else {
        panic!("binary codec must produce bytes");
    };
    assert_eq!(
        decode_base64(sim_kernel::CodecId(9), &base64_output).unwrap(),
        binary_bytes
    );
}

#[test]
fn decode_accepts_ascii_whitespace_around_base64() {
    let mut cx = cx();
    let text = encode_with_codec(
        &mut cx,
        &symbol(),
        &Expr::String("space".to_owned()),
        EncodeOptions::default(),
    )
    .unwrap()
    .into_text()
    .unwrap();
    let spaced = format!(" \n{}\t\r", text);
    let decoded = decode_with_codec(
        &mut cx,
        &symbol(),
        Input::Text(spaced),
        ReadPolicy::default(),
    )
    .unwrap();
    assert_eq!(decoded, Expr::String("space".to_owned()));
}

#[test]
fn malformed_base64_returns_codec_error() {
    let mut cx = cx();
    for text in ["@@==", "abc", "ab=c", "abcd====", "Zh=="] {
        let err = decode_with_codec(
            &mut cx,
            &symbol(),
            Input::Text(text.to_owned()),
            ReadPolicy::default(),
        )
        .unwrap_err();
        match err {
            sim_kernel::Error::CodecError { message, .. } => {
                assert!(message.contains("base64"), "{message}");
            }
            other => panic!("unexpected error {other:?}"),
        }
    }
}

fn sample_datum() -> Datum {
    Datum::Map(vec![
        (
            Datum::Symbol(Symbol::new("codec")),
            Datum::Symbol(Symbol::qualified("codec", "binary-base64")),
        ),
        (
            Datum::Symbol(Symbol::new("payload")),
            Datum::List(vec![Datum::Bool(false), Datum::Bytes(vec![0, 1, 2, 3])]),
        ),
    ])
}

#[test]
fn malformed_decoded_binary_returns_codec_error() {
    let mut cx = cx();
    let err = decode_with_codec(
        &mut cx,
        &symbol(),
        Input::Text(encode_base64(b"BAD!")),
        ReadPolicy::default(),
    )
    .unwrap_err();
    match err {
        sim_kernel::Error::CodecError { message, .. } => {
            assert!(message.contains("magic mismatch"));
        }
        other => panic!("unexpected error {other:?}"),
    }
}

#[test]
fn bytes_input_must_be_utf8() {
    let mut cx = cx();
    let err = decode_with_codec(
        &mut cx,
        &symbol(),
        Input::Bytes(vec![0xff]),
        ReadPolicy::default(),
    )
    .unwrap_err();
    match err {
        sim_kernel::Error::CodecError { message, .. } => {
            assert!(message.contains("not valid UTF-8"));
        }
        other => panic!("unexpected error {other:?}"),
    }
}

#[test]
fn located_expr_roundtrips_with_origin() {
    let mut cx = cx();
    let located = LocatedExpr {
        expr: Expr::String("wire".to_owned()),
        origin: Some(Origin {
            codec: sim_kernel::CodecId(3),
            source: SourceId("cache.simb64".to_owned()),
            span: Span { start: 10, end: 14 },
            trivia: vec![
                Trivia::Whitespace(" ".to_owned()),
                Trivia::BlockComment("/*x*/".to_owned()),
            ],
        }),
    };

    let encoded = encode_located_with_codec(
        &mut cx,
        &symbol(),
        &located,
        EncodeOptions {
            lossless_origin: true,
            ..Default::default()
        },
    )
    .unwrap();
    let decoded = sim_codec::decode_located_with_codec(
        &mut cx,
        &symbol(),
        match encoded {
            Output::Text(text) => Input::Text(text),
            Output::Bytes(bytes) => Input::Bytes(bytes),
        },
        ReadPolicy::default(),
        "ignored.simb64",
    )
    .unwrap();
    assert_eq!(decoded, located);
}

#[test]
fn tree_roundtrips_nested_origins() {
    let mut cx = cx();
    let tree = LocatedExprTree {
        expr: Expr::List(vec![Expr::String("x".to_owned())]),
        origin: Some(Origin {
            codec: sim_kernel::CodecId(7),
            source: SourceId("root".to_owned()),
            span: Span { start: 0, end: 3 },
            trivia: vec![Trivia::Whitespace(" ".to_owned())],
        }),
        children: vec![LocatedExprTree::without_children(
            Expr::String("x".to_owned()),
            Some(Origin {
                codec: sim_kernel::CodecId(7),
                source: SourceId("child".to_owned()),
                span: Span { start: 1, end: 2 },
                trivia: vec![Trivia::LineComment("; child".to_owned())],
            }),
        )],
    };

    let encoded = encode_tree_with_codec(
        &mut cx,
        &symbol(),
        &tree,
        EncodeOptions {
            lossless_origin: true,
            ..Default::default()
        },
    )
    .unwrap();
    let decoded = decode_tree_with_codec(
        &mut cx,
        &symbol(),
        match encoded {
            Output::Text(text) => Input::Text(text),
            Output::Bytes(bytes) => Input::Bytes(bytes),
        },
        ReadPolicy::default(),
        "ignored.simb64",
    )
    .unwrap();
    assert_eq!(decoded, tree);
}

#[test]
fn decode_enforces_underlying_binary_limits() {
    let mut cx = cx();
    let text = encode_with_codec(
        &mut cx,
        &symbol(),
        &Expr::String("wire".repeat(8)),
        EncodeOptions::default(),
    )
    .unwrap()
    .into_text()
    .unwrap();
    let err = decode_with_codec_and_limits(
        &mut cx,
        &symbol(),
        Input::Text(text),
        ReadPolicy::default(),
        DecodeLimits {
            max_string_bytes: 4,
            ..DecodeLimits::default()
        },
    )
    .unwrap_err();
    match err {
        sim_kernel::Error::CodecError { message, .. } => {
            assert!(message.contains("string exceeds decode limit"));
        }
        other => panic!("unexpected error {other:?}"),
    }
}

#[test]
fn decode_rejects_oversized_whitespace_heavy_input_before_allocation() {
    let mut cx = cx();
    // A tiny amount of real base64 buried in a large whitespace pad. The raw
    // input length (>> max_input_bytes) must be rejected before the wrapper
    // allocates the whitespace-stripping buffer or the decoded frame buffer.
    let padded = format!("{}QQ=={}", " ".repeat(4096), "\n".repeat(4096));
    let err = decode_with_codec_and_limits(
        &mut cx,
        &symbol(),
        Input::Text(padded),
        ReadPolicy::default(),
        DecodeLimits {
            max_input_bytes: 64,
            ..DecodeLimits::default()
        },
    )
    .unwrap_err();
    match err {
        sim_kernel::Error::CodecError { message, .. } => {
            assert!(
                message.contains("input bytes limit exceeded"),
                "unexpected message {message:?}"
            );
        }
        other => panic!("unexpected error {other:?}"),
    }
}