diff options
| author | Laytan Laats <laytanlaats@hotmail.com> | 2024-10-25 20:37:55 +0200 |
|---|---|---|
| committer | Laytan Laats <laytanlaats@hotmail.com> | 2024-10-25 20:37:55 +0200 |
| commit | 793ef6012b5a7844cee392d51999cb566af4a787 (patch) | |
| tree | 8193053aeb17e2e16e23e32a795aa5278020aa19 /tests/core/encoding | |
| parent | bcf2b93c6e7bf6e4d94685f2c841d6243bceb4c3 (diff) | |
encoding/cbor: handle binary having more fields than the struct by discarding
Diffstat (limited to 'tests/core/encoding')
| -rw-r--r-- | tests/core/encoding/cbor/test_core_cbor.odin | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/tests/core/encoding/cbor/test_core_cbor.odin b/tests/core/encoding/cbor/test_core_cbor.odin index 45f47505a..bd0eb0003 100644 --- a/tests/core/encoding/cbor/test_core_cbor.odin +++ b/tests/core/encoding/cbor/test_core_cbor.odin @@ -383,6 +383,57 @@ test_lying_length_array :: proc(t: ^testing.T) { } @(test) +test_unmarshal_map_into_struct_partially :: proc(t: ^testing.T) { + + // Tests that when unmarshalling into a struct that has less fields than the binary map has fields, + // the additional fields in the binary are skipped and the rest of the binary is correctly decoded. + + Foo :: struct { + bar: struct { + hello: string, + world: string, + }, + baz: int, + } + + Foo_More :: struct { + bar: struct { + hello: string, + world: string, + hellope: string, + }, + baz: int, + } + more := Foo_More{ + bar = { + hello = "hello", + world = "world", + hellope = "hellope", + }, + baz = 4, + } + + more_bin, err := cbor.marshal(more) + testing.expect_value(t, err, nil) + + less := Foo{ + bar = { + hello = "hello", + world = "world", + }, + baz = 4, + } + less_out: Foo + uerr := cbor.unmarshal(string(more_bin), &less_out) + testing.expect_value(t, uerr, nil) + testing.expect_value(t, less, less_out) + + delete(more_bin) + delete(less_out.bar.hello) + delete(less_out.bar.world) +} + +@(test) test_decode_unsigned :: proc(t: ^testing.T) { expect_decoding(t, "\x00", "0", u8) expect_decoding(t, "\x01", "1", u8) |