v2 / vlib / x / encoding / asn1 / ia5string_test.v
43 lines · 38 sloc · 1.03 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
6struct IA5StringTest {
7 src string
8 out []u8
9 err IError
10}
11
12fn test_ia5string_handling() ! {
13 data := [
14 IA5StringTest{'test', [u8(22), 4, 116, 101, 115, 116], none},
15 IA5StringTest{'abc', '\x16\x03abc'.bytes(), none},
16 IA5StringTest{`🚀`.str(), []u8{}, error('IA5String: contains non-ascii chars')},
17 IA5StringTest{')', '\x16\x01)'.bytes(), none},
18 IA5StringTest{'\x13\x03ab\x00', []u8{}, error('IA5String: contains non-ascii chars')},
19 ]
20
21 for c in data {
22 s := IA5String.new(c.src) or {
23 assert err == c.err
24 continue
25 }
26 out := encode(s) or {
27 assert err == c.err
28 continue
29 }
30 assert out == c.out
31
32 // unpack back
33 mut p := Parser.new(out)
34 ret := IA5String.parse(mut p) or {
35 assert err == c.err
36 continue
37 }
38
39 assert ret.tag().tag_number() == 22
40 assert ret.tag().tag_class() == TagClass.universal
41 assert ret.tag().is_constructed() == false
42 }
43}
44