v2 / vlib / x / encoding / asn1 / boolean_test.v
43 lines · 38 sloc · 1.29 KB · 58fc4dead559901cad648fb695f31d0f6de9945a
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 BooleanTest {
7 inp []u8
8 out bool
9 err IError
10}
11
12fn test_encode_decode_boolean_in_der_rule() {
13 bd := [
14 BooleanTest{[u8(1), 0x01, 0xff], true, none},
15 BooleanTest{[u8(1), 0x01, 0x00], false, none},
16 BooleanTest{[u8(1), 0x01, 0x10], false, error('Boolean: in DER, other than 0xff is not allowed for true value')}, // invalid value
17 BooleanTest{[u8(1), 0x02, 0x00], false, error('Boolean: should have length 1')}, // bad length
18 BooleanTest{[u8(1), 0x00, 0x00], false, error('Boolean: should have length 1')}, // bad length
19 BooleanTest{[u8(2), 0x01, 0x00], false, error('Unexpected non-boolean tag')}, // bad tag number
20 ]
21 for c in bd {
22 out, _ := Boolean.decode(c.inp) or {
23 assert err == c.err
24 continue
25 }
26 // out.value is now u8, call .value() instead
27 assert out.value() == c.out
28 }
29}
30
31fn test_parse_boolean_with_parser() ! {
32 data := [u8(0x01), 0x01, 0xff]
33 mut p := Parser.new(data)
34
35 // This is fails too lookup
36 // out := p.read_element[Boolean]()!
37 // assert out.str() == 'Boolean (TRUE)'
38
39 // THis is ok
40 mut p2 := Parser.new(data)
41 out_3th := Boolean.parse(mut p2)!
42 assert out_3th.str() == 'Boolean (TRUE)'
43}
44