v2 / vlib / x / encoding / asn1 / element_encode_test.v
22 lines · 18 sloc · 841 bytes · fdc49dc51a0d7e83abd5b383afaaab3f2793f2cf
Raw
1module asn1
2
3// taken examples from https://letsencrypt.org/id/docs/a-warm-welcome-to-asn1-and-der/#explicit-vs-implicit
4fn test_encode_with_or_without_options() ! {
5 // Utf8String with tag == 12 (0c)
6 obj := Utf8String.new('hi')!
7
8 // without options
9 out_1 := encode(obj)!
10 normal := [u8(0x0C), 0x02, 0x68, 0x69]
11 assert out_1 == normal
12
13 // with implicit definded as [5] IMPLICIT UTF8String was serialized into 85 02 68 69
14 impl_expected := [u8(0x85), 0x02, 0x68, 0x69]
15 out_2 := encode_with_options(obj, 'context_specific:5;implicit;inner:12')!
16 assert out_2 == impl_expected
17
18 // as explicit tagging defined as [5] EXPLICIT UTF8String encoded into A5 04 0C 02 68 69
19 expl_expected := [u8(0xA5), 0x04, 0x0C, 0x02, 0x68, 0x69]
20 out_3 := encode_with_options(obj, 'context_specific:5;explicit;inner:0x0c')!
21 assert out_3 == expl_expected
22}
23