| 1 | module asn1 |
| 2 | |
| 3 | type MyOct = string |
| 4 | |
| 5 | fn (mo MyOct) tag() Tag { |
| 6 | return default_octetstring_tag |
| 7 | } |
| 8 | |
| 9 | fn (mo MyOct) payload() ![]u8 { |
| 10 | return mo.bytes() |
| 11 | } |
| 12 | |
| 13 | type MyStr = string |
| 14 | |
| 15 | fn (ms MyStr) tag() Tag { |
| 16 | return default_utf8string_tag |
| 17 | } |
| 18 | |
| 19 | fn (ms MyStr) payload() ![]u8 { |
| 20 | return ms.bytes() |
| 21 | } |
| 22 | |
| 23 | struct TestStruct { |
| 24 | n int |
| 25 | a MyOct |
| 26 | b MyStr |
| 27 | } |
| 28 | |
| 29 | fn (t TestStruct) tag() Tag { |
| 30 | return Tag{.universal, true, int(TagType.sequence)} // 0x30 |
| 31 | } |
| 32 | |
| 33 | fn (t TestStruct) payload() ![]u8 { |
| 34 | kd := KeyDefault(map[string]Element{}) |
| 35 | out := make_payload[TestStruct](t, kd)! |
| 36 | return out |
| 37 | } |
| 38 | |
| 39 | fn test_struct_make_payload() ! { |
| 40 | st := TestStruct{ |
| 41 | a: MyOct('aku') |
| 42 | b: MyStr('dia') |
| 43 | } |
| 44 | // TestStruct is sequence |
| 45 | out := encode(st)! |
| 46 | expected := [u8(0x30), 0x0a, (0x04), 0x03, 0x61, 0x6b, 0x75, (0x0c), 0x03, 0x64, 0x69, 0x61] |
| 47 | // without field option passed, its should be expected value |
| 48 | assert out == expected |
| 49 | } |
| 50 | |
| 51 | fn test_into_optional() ! { |
| 52 | el := Boolean.new(true) |
| 53 | orig_expected := [u8(0x01), 0x01, 0xff] |
| 54 | |
| 55 | without_option := encode(el)! |
| 56 | assert without_option == orig_expected |
| 57 | |
| 58 | // marked this element as optional, make its serialized into empty bytes |
| 59 | with_option_1 := encode_with_options(el, 'optional')! |
| 60 | assert with_option_1 == []u8{} |
| 61 | } |
| 62 | |
| 63 | // test for wrapping functionality |
| 64 | struct WrapperTest { |
| 65 | attr string |
| 66 | err IError |
| 67 | out []u8 |
| 68 | } |
| 69 | |
| 70 | fn test_wrapping_functionality() ! { |
| 71 | // raw boolean element |
| 72 | elem := Boolean.new(true) |
| 73 | orig_expected := [u8(0x01), 0x01, 0xff] |
| 74 | |
| 75 | data := [ |
| 76 | WrapperTest{'', none, orig_expected}, |
| 77 | // Tag{.contex_specific, true, 1} = 0b1010_0001 |
| 78 | WrapperTest{'context_specific:1; explicit; inner:1', none, [ |
| 79 | u8(0xa1), |
| 80 | 0x03, |
| 81 | 0x01, |
| 82 | 0x01, |
| 83 | 0xff, |
| 84 | ]}, |
| 85 | // Tag{.contex_specific, false, 1} // 0b1000_0001 = 0x81 = 129 |
| 86 | WrapperTest{'context_specific:1; implicit; inner:1', none, [ |
| 87 | u8(0x81), // |
| 88 | 0x01, |
| 89 | 0xff, |
| 90 | ]}, |
| 91 | // |
| 92 | WrapperTest{'context_specific:1;inner:2', error('Invalid zonk or uncorerct mode value'), [ |
| 93 | u8(0xa1), |
| 94 | 0x01, |
| 95 | 0xff, |
| 96 | ]}, |
| 97 | WrapperTest{'application:5;explicit;inner:1', none, [ |
| 98 | u8(0x65), |
| 99 | 0x03, |
| 100 | 0x01, |
| 101 | 0x01, |
| 102 | 0xff, |
| 103 | ]}, |
| 104 | // same as above, but with implicit mode, 0b_01_0_00101 |
| 105 | WrapperTest{'application:5;implicit;inner:1', none, [ |
| 106 | u8(0x45), |
| 107 | 0x01, |
| 108 | 0xff, |
| 109 | ]}, |
| 110 | // marked as optional would not be encoded, |
| 111 | WrapperTest{'application:5; optional;inner:2', error('Invalid zonk or uncorerct mode value'), []u8{}}, |
| 112 | WrapperTest{'application:5; explicit;inner:2', none, [ |
| 113 | u8(0x65), |
| 114 | 0x03, |
| 115 | 0x01, |
| 116 | 0x01, |
| 117 | 0xff, |
| 118 | ]}, |
| 119 | // wrapped into universal is error |
| 120 | WrapperTest{'universal:50; explicit; inner:3', error('You have not provides correct options marker'), orig_expected}, |
| 121 | // marked as an optional |
| 122 | WrapperTest{'private:10; optional', error('Invalid zonk or uncorerct mode value'), []u8{}}, |
| 123 | WrapperTest{'application:5;implicit', error('You provides incorrect inner number'), []u8{}}, |
| 124 | // 0b_11_0_00101 = c5 |
| 125 | WrapperTest{'private:5; implicit; inner:1', none, [ |
| 126 | u8(0xC5), |
| 127 | 0x01, |
| 128 | 0xff, |
| 129 | ]}, |
| 130 | ] |
| 131 | for i, item in data { |
| 132 | out := encode_with_options(elem, item.attr) or { |
| 133 | assert err == item.err |
| 134 | continue |
| 135 | } |
| 136 | assert out == item.out |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | // test for ElementList |
| 141 | fn test_for_element_list() ! { |
| 142 | mut fields := ElementList([]Element{}) |
| 143 | |
| 144 | a := Boolean.new(true) |
| 145 | b := Boolean.new(false) |
| 146 | |
| 147 | fields << a |
| 148 | fields << b |
| 149 | |
| 150 | fields_payload := fields.payload()! |
| 151 | |
| 152 | expected := [u8(0x01), 0x01, 0xff, u8(0x01), 1, 0] |
| 153 | assert fields_payload == expected |
| 154 | assert fields.encoded_len() == 6 |
| 155 | |
| 156 | els := ElementList.from_bytes(expected)! |
| 157 | assert els.len == 2 |
| 158 | assert els[0] is Boolean |
| 159 | assert els[1] is Boolean |
| 160 | } |
| 161 | |