| 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. |
| 4 | module asn1 |
| 5 | |
| 6 | // BITSTRING Test |
| 7 | // |
| 8 | struct BSTest { |
| 9 | inp []u8 |
| 10 | pad u8 |
| 11 | out BitString |
| 12 | err IError |
| 13 | } |
| 14 | |
| 15 | fn test_new_bitstring() ! { |
| 16 | ds := [ |
| 17 | BSTest{'abc'.bytes(), 8, BitString{}, error('BitString: bad pad bits or zero length')}, |
| 18 | BSTest{''.bytes(), 2, BitString{}, error('BitString: bad pad bits or zero length')}, |
| 19 | BSTest{[u8(0xff)], 1, BitString{}, error('BitString: bad args')}, |
| 20 | BSTest{[u8(0xff)], 0, BitString{ |
| 21 | data: [u8(0xff)] |
| 22 | pad: 0 |
| 23 | }, none}, |
| 24 | BSTest{[u8(0xfe)], 0, BitString{ |
| 25 | data: [u8(0xfe)] |
| 26 | pad: 0 |
| 27 | }, none}, |
| 28 | BSTest{[u8(0x8e)], 0, BitString{ |
| 29 | data: [u8(0x8e)] |
| 30 | pad: 0 |
| 31 | }, none}, |
| 32 | BSTest{[u8(0x88)], 0, BitString{ |
| 33 | data: [u8(0x88)] |
| 34 | pad: 0 |
| 35 | }, none}, |
| 36 | BSTest{[u8(0x03), 0x02, 0x00, 0x20], 0, BitString{ |
| 37 | data: [u8(0x03), 0x02, 0x00, 0x20] |
| 38 | pad: 0 |
| 39 | }, none}, |
| 40 | BSTest{[u8(0x03), 0x02, 0x05, 0x80], 5, BitString{ |
| 41 | data: [u8(0x03), 0x02, 0x05, 0x80] |
| 42 | pad: 5 |
| 43 | }, none}, |
| 44 | ] |
| 45 | for c in ds { |
| 46 | out := BitString.new_with_pad(c.inp, c.pad) or { |
| 47 | assert err == c.err |
| 48 | continue |
| 49 | } |
| 50 | |
| 51 | assert out == c.out |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | struct BSParse { |
| 56 | inp []u8 |
| 57 | src []u8 |
| 58 | pad u8 |
| 59 | err IError |
| 60 | } |
| 61 | |
| 62 | fn test_serialize_and_decode_bitstring() ! { |
| 63 | ds := [ |
| 64 | // from rust-asn1 |
| 65 | BSParse{[u8(0x03), 0x01, 0x00], ''.bytes(), 0, none}, |
| 66 | BSParse{[u8(0x03), 0x02, 0x07, 0x00], [u8(0)], 7, none}, |
| 67 | BSParse{[u8(0x03), 0x02, 0x07, 0x80], [u8(0x80)], 7, none}, |
| 68 | BSParse{[u8(0x03), 0x03, 0x04, 0x81, 0xf0], [u8(0x81), 0xf0], 4, none}, |
| 69 | BSParse{[u8(0x03), 0x00], ''.bytes(), 0, error('BitString: zero length bit string')}, |
| 70 | // bad length |
| 71 | BSParse{[u8(0x03), 0x02, 0x07, 0x01], [u8(0x81)], 0, error('BitString: bad args')}, |
| 72 | // bad args |
| 73 | BSParse{[u8(0x03), 0x02, 0x07, 0x40], [u8(0x81)], 0, error('BitString: bad args')}, |
| 74 | // bad args |
| 75 | BSParse{[u8(0x03), 0x02, 0x08, 0x00], [u8(0x81)], 0, error('BitString: bad pad bits or zero length')}, |
| 76 | // bad args |
| 77 | ] |
| 78 | for i, c in ds { |
| 79 | bs, idx := BitString.decode(c.inp) or { |
| 80 | assert err == c.err |
| 81 | continue |
| 82 | } |
| 83 | |
| 84 | assert bs.tag().tag_number() == int(TagType.bitstring) |
| 85 | |
| 86 | // b := new_bitstring_with_pad(c.src, c.pad)! |
| 87 | b := BitString.new_with_pad(c.src, c.pad)! |
| 88 | // b is Encoder, so lets smart cast it to real |
| 89 | assert bs == b |
| 90 | |
| 91 | // back |
| 92 | s := encode(b)! |
| 93 | |
| 94 | assert s == c.inp |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // This data is taken from https://learn.microsoft.com/en-us/windows/win32/seccertenroll/about-bit-string |
| 99 | // 0299: 03 81 81 ; BIT_STRING (81 Bytes) |
| 100 | // 029c: 00 |
| 101 | // 029d: 47 eb 99 5a df 9e 70 0d fb a7 31 32 c1 5f 5c 24 |
| 102 | // 02ad: c2 e0 bf c6 24 af 15 66 0e b8 6a 2e ab 2b c4 97 |
| 103 | // 02bd: 1f e3 cb dc 63 a5 25 ec c7 b4 28 61 66 36 a1 31 |
| 104 | // 02cd: 1b bf dd d0 fc bf 17 94 90 1d e5 5e c7 11 5e c9 |
| 105 | // 02dd: 55 9f eb a3 3e 14 c7 99 a6 cb ba a1 46 0f 39 d4 |
| 106 | // 02ed: 44 c4 c8 4b 76 0e 20 5d 6d a9 34 9e d4 d5 87 42 |
| 107 | // 02fd: eb 24 26 51 14 90 b4 0f 06 5e 52 88 32 7a 95 20 |
| 108 | // 030d: a0 fd f7 e5 7d 60 dd 72 68 9b f5 7b 05 8f 6d 1e |
| 109 | fn test_bitstring_from_bytes_arrays() ! { |
| 110 | data := [u8(0x03), 0x81, 0x81, 0x00, 0x47, 0xeb, 0x99, 0x5a, 0xdf, 0x9e, 0x70, 0x0d, 0xfb, |
| 111 | 0xa7, 0x31, 0x32, 0xc1, 0x5f, 0x5c, 0x24, 0xc2, 0xe0, 0xbf, 0xc6, 0x24, 0xaf, 0x15, 0x66, |
| 112 | 0x0e, 0xb8, 0x6a, 0x2e, 0xab, 0x2b, 0xc4, 0x97, 0x1f, 0xe3, 0xcb, 0xdc, 0x63, 0xa5, 0x25, |
| 113 | 0xec, 0xc7, 0xb4, 0x28, 0x61, 0x66, 0x36, 0xa1, 0x31, 0x1b, 0xbf, 0xdd, 0xd0, 0xfc, 0xbf, |
| 114 | 0x17, 0x94, 0x90, 0x1d, 0xe5, 0x5e, 0xc7, 0x11, 0x5e, 0xc9, 0x55, 0x9f, 0xeb, 0xa3, 0x3e, |
| 115 | 0x14, 0xc7, 0x99, 0xa6, 0xcb, 0xba, 0xa1, 0x46, 0x0f, 0x39, 0xd4, 0x44, 0xc4, 0xc8, 0x4b, |
| 116 | 0x76, 0x0e, 0x20, 0x5d, 0x6d, 0xa9, 0x34, 0x9e, 0xd4, 0xd5, 0x87, 0x42, 0xeb, 0x24, 0x26, |
| 117 | 0x51, 0x14, 0x90, 0xb4, 0x0f, 0x06, 0x5e, 0x52, 0x88, 0x32, 0x7a, 0x95, 0x20, 0xa0, 0xfd, |
| 118 | 0xf7, 0xe5, 0x7d, 0x60, 0xdd, 0x72, 0x68, 0x9b, 0xf5, 0x7b, 0x05, 0x8f, 0x6d, 0x1e] |
| 119 | |
| 120 | bs, idx := BitString.decode(data)! |
| 121 | assert idx == data.len |
| 122 | |
| 123 | assert bs.tag().tag_number() == int(TagType.bitstring) |
| 124 | assert bs.bytes_len() == 0x81 |
| 125 | assert bs.pad == 0x00 |
| 126 | |
| 127 | // serializes bitstring back |
| 128 | bs_back := encode(bs)! |
| 129 | assert bs_back == data |
| 130 | |
| 131 | // with decode |
| 132 | bsd := decode(data)! |
| 133 | assert bsd.tag() == bs.tag() |
| 134 | assert bsd.payload()! == bs.payload()! |
| 135 | |
| 136 | // with parse |
| 137 | mut p := Parser.new(data) |
| 138 | bs_parse := BitString.parse(mut p)! |
| 139 | assert bs_parse.tag() == bs.tag() |
| 140 | assert bs_parse.payload()! == bs.payload()! |
| 141 | } |
| 142 | |
| 143 | // test for BitString from binary string |
| 144 | struct BinaryStringTest { |
| 145 | bits string |
| 146 | out []u8 |
| 147 | err IError |
| 148 | } |
| 149 | |
| 150 | const binstring_data = [ |
| 151 | // empty bit string |
| 152 | BinaryStringTest{'', [u8(0x03), 0x01, 0x00], none}, |
| 153 | // malformed bit string |
| 154 | BinaryStringTest{'aaa', []u8{}, error('not valid bit string')}, |
| 155 | // taken examples from internet |
| 156 | BinaryStringTest{'011010001', [u8(0x03), 0x03, 0x07, 0x68, 0x80], none}, |
| 157 | BinaryStringTest{'101100001001', [u8(0x03), 0x03, 0x04, 0xb0, 0x90], none}, |
| 158 | BinaryStringTest{'011011100101110111', [u8(0x03), 0x04, 0x06, 0x6e, 0x5d, 0xc0], none}, |
| 159 | ] |
| 160 | |
| 161 | fn test_bitstring_from_binary_string() ! { |
| 162 | for item in binstring_data { |
| 163 | bs := BitString.from_binary_string(item.bits) or { |
| 164 | assert err == item.err |
| 165 | continue |
| 166 | } |
| 167 | out := encode(bs)! |
| 168 | assert out == item.out |
| 169 | } |
| 170 | } |
| 171 | |