v2 / vlib / x / encoding / asn1 / printablestring_test.v
54 lines · 44 sloc · 1.46 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// Tests case for PrintableString
7fn test_encode_printablestring_basic() ! {
8 s := 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
9
10 ps := PrintableString.new(s)!
11 buf := encode(ps)!
12
13 mut out := [u8(0x13)]
14 length := [u8(0x81), u8(s.len)]
15 out << length
16 out << s.bytes()
17 // dump(out)
18 assert out == buf
19
20 psback, _ := PrintableString.decode(buf)!
21 assert psback.tag().tag_number() == int(TagType.printablestring)
22 assert psback.tag().tag_class() == .universal
23
24 assert psback.value == s
25}
26
27struct EncodingTest[T] {
28 input T
29 exp []u8
30}
31
32fn test_encode_printablestring_generic() {
33 data := [
34 // from dart asn1lib test
35 EncodingTest[string]{'TheTestString', [u8(0x13), 13, 84, 104, 101, 84, 101, 115, 116, 83,
36 116, 114, 105, 110, 103]},
37 EncodingTest[string]{'Test User 1', [u8(0x13), 0x0b, 84, 101, 115, 116, 32, 85, 115, 101,
38 114, 32, 49]},
39 ]
40
41 for t in data {
42 ps := PrintableString.new(string(t.input))!
43 out := encode(ps)!
44 // out := serialize_printablestring(string(t.input))!
45 assert out == t.exp
46
47 // decode back
48 psback, _ := PrintableString.decode(out)!
49
50 assert psback.value == t.input
51 assert psback.tag().tag_number() == int(TagType.printablestring)
52 assert psback.tag().is_constructed() == false
53 }
54}
55