v2 / vlib / x / encoding / asn1 / element_decode_test.v
29 lines · 23 sloc · 1.18 KB · fdc49dc51a0d7e83abd5b383afaaab3f2793f2cf
Raw
1// Copyright (c) 2022, 2024 blackshirt. All rights reserved.
2// Use of this source code is governed by a MIT License
3// that can be found in the LICENSE file.
4module asn1
5
6// examples data was taken examples from https://letsencrypt.org/id/docs/a-warm-welcome-to-asn1-and-der/#explicit-vs-implicit
7fn test_decode_element_with_or_without_options() ! {
8 // Utf8String with tag == 12 (0c)
9 original_obj := Utf8String.new('hi')!
10
11 // without options
12 normal := [u8(0x0C), 0x02, 0x68, 0x69]
13 obj_1 := decode(normal)!
14 assert obj_1.equal(original_obj)
15
16 // with implicit definded as [5] IMPLICIT UTF8String was serialized into 85 02 68 69
17 implicit_data := [u8(0x85), 0x02, 0x68, 0x69]
18 obj_2 := decode_with_options(implicit_data, 'context_specific:5;implicit;inner:12')!
19
20 assert obj_2.equal(original_obj)
21 // dump(obj_2) // Output: obj_2: asn1.Element(Utf8String: (hi))
22
23 // as explicit tagging defined as [5] EXPLICIT UTF8String encoded into A5 04 0C 02 68 69
24 explicit_data := [u8(0xA5), 0x04, 0x0C, 0x02, 0x68, 0x69]
25 obj_3 := decode_with_options(explicit_data, 'context_specific:5;explicit;inner:0x0c')!
26
27 assert obj_3.equal(original_obj)
28 // dump(obj_3) // output: obj_3: asn1.Element(Utf8String: (hi))
29}
30