| 1 | module asn1 |
| 2 | |
| 3 | struct StringOption { |
| 4 | src string |
| 5 | cls string |
| 6 | tagnum int |
| 7 | mode string |
| 8 | inner string |
| 9 | optional bool |
| 10 | has_default bool |
| 11 | err IError |
| 12 | } |
| 13 | |
| 14 | fn test_parse_string_option() ! { |
| 15 | data := [ |
| 16 | // should parseable |
| 17 | StringOption{'application:20;explicit;inner:5', 'application', 20, 'explicit', '5', false, false, none}, |
| 18 | StringOption{'private:0x20;implicit;inner:5', 'private', 32, 'implicit', '5', false, false, none}, |
| 19 | StringOption{'context_specific:0x20;implicit;inner:5', 'context_specific', 32, 'implicit', '5', false, false, none}, |
| 20 | StringOption{'private:0x20;implicit;inner:5; optional', 'private', 32, 'implicit', '5', true, false, none}, |
| 21 | StringOption{'private:0x20;implicit;inner:5; has_default', 'private', 32, 'implicit', '5', false, true, none}, |
| 22 | StringOption{'private:0x20;implicit;inner:5; optional; has_default', 'private', 32, 'implicit', '5', true, true, none}, |
| 23 | // not parseable |
| 24 | // Without mode or inner |
| 25 | StringOption{'application:20', 'application', 0, '', '5', false, false, error('Invalid zonk or uncorerct mode value')}, |
| 26 | StringOption{'application:20; inner:4', 'application', 0, '', '0', false, false, error('Invalid zonk or uncorerct mode value')}, |
| 27 | StringOption{'application:20; implicit', 'application', 0, '', '0', false, false, error('You provides incorrect inner number')}, |
| 28 | StringOption{'implicit;inner:33', '', -1, 'implicit', '33', false, false, error('You provides incorrect inner number')}, |
| 29 | ] |
| 30 | for i, item in data { |
| 31 | // dump(i) |
| 32 | fo := FieldOptions.from_string(item.src) or { |
| 33 | assert err == item.err |
| 34 | continue |
| 35 | } |
| 36 | assert fo.cls == item.cls |
| 37 | assert fo.tagnum == item.tagnum |
| 38 | assert fo.optional == item.optional |
| 39 | assert fo.has_default == item.has_default |
| 40 | assert fo.mode == item.mode |
| 41 | assert fo.inner == item.inner |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | struct TagMarker { |
| 46 | attr string |
| 47 | cls string |
| 48 | num int |
| 49 | err IError |
| 50 | } |
| 51 | |
| 52 | fn test_tag_marker_parsing() ! { |
| 53 | data := [ |
| 54 | // normal |
| 55 | TagMarker{'application:100', 'application', 100, none}, |
| 56 | TagMarker{'context_specific:100', 'context_specific', 100, none}, |
| 57 | // normal with hex number |
| 58 | TagMarker{'private:0x54', 'private', 0x54, none}, |
| 59 | // normal with spaces should be allowed |
| 60 | TagMarker{'application: 0x20', 'application', 0x20, none}, |
| 61 | TagMarker{' application : 0x20 ', 'application', 0x20, none}, |
| 62 | // universal should not allowed |
| 63 | TagMarker{'universal:0x5f', 'universal', 0x5f, error('not a tag marker')}, |
| 64 | // bad tag key should error |
| 65 | TagMarker{'embuh: 0x20', '', 0x20, error('not a tag marker')}, |
| 66 | TagMarker{'private_embuh: 0x20', '', 0x20, error('bad tag name')}, |
| 67 | // key without number also error |
| 68 | TagMarker{'private_embuh', '', 0, error('bad tag marker length')}, |
| 69 | TagMarker{'private:', 'private', 0, error('bad tag number')}, |
| 70 | TagMarker{'private:bb', 'private', 0, error('bad tag number')}, |
| 71 | ] |
| 72 | for item in data { |
| 73 | k, v := parse_tag_marker(item.attr) or { |
| 74 | assert err == item.err |
| 75 | continue |
| 76 | } |
| 77 | assert k == item.cls |
| 78 | assert v.int() == item.num |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | struct ModeMarker { |
| 83 | attr string |
| 84 | value string |
| 85 | err IError |
| 86 | } |
| 87 | |
| 88 | fn test_mode_marker_parsing() ! { |
| 89 | data := [ |
| 90 | // the normal right thing |
| 91 | ModeMarker{'explicit', 'explicit', none}, |
| 92 | ModeMarker{'implicit', 'implicit', none}, |
| 93 | // with spaces is allowed |
| 94 | ModeMarker{' implicit ', 'implicit', none}, |
| 95 | ModeMarker{' explicit ', 'explicit', none}, |
| 96 | // bad key or value |
| 97 | ModeMarker{'xx_implicit', '', error('not mode marker')}, |
| 98 | ModeMarker{'implicitkey', '', error('bad mode value')}, |
| 99 | ModeMarker{'exoplicit implicit', '', error('not mode marker')}, |
| 100 | ] |
| 101 | for i, item in data { |
| 102 | // dump(i) |
| 103 | v := parse_mode_marker(item.attr) or { |
| 104 | assert err == item.err |
| 105 | continue |
| 106 | } |
| 107 | assert valid_mode_value(v) == true |
| 108 | assert v == item.value |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | struct InnerMarker { |
| 113 | src string |
| 114 | result string |
| 115 | err IError |
| 116 | } |
| 117 | |
| 118 | fn test_for_inner_tag_marker() ! { |
| 119 | data := [InnerMarker{'', '', error('not inner tag marker')}, |
| 120 | InnerMarker{'inner:0', '0', none}, InnerMarker{'inner:12', '12', none}, |
| 121 | InnerMarker{'inner:private,true,14', 'private,true,14', none}, |
| 122 | InnerMarker{'inner:universal,true,14', 'universal,true,14', none}, |
| 123 | InnerMarker{'inner:invalid,true,14', '', error('Your first ext inner is not extended cls')}] |
| 124 | for i, item in data { |
| 125 | // dump(i) |
| 126 | k, v := parse_inner_tag_marker(item.src) or { |
| 127 | assert err == item.err |
| 128 | continue |
| 129 | } |
| 130 | |
| 131 | assert v == item.result |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | struct HasDefaultMarker { |
| 136 | attr string |
| 137 | err IError |
| 138 | } |
| 139 | |
| 140 | fn test_has_default_marker_parsing() ! { |
| 141 | data := [ |
| 142 | HasDefaultMarker{'has_default', none}, |
| 143 | HasDefaultMarker{' has_default ', none}, |
| 144 | HasDefaultMarker{'', error('not has_default marker')}, |
| 145 | HasDefaultMarker{'has_default:', error('bad has_default marker')}, |
| 146 | HasDefaultMarker{'has_defaultaa', error('bad has_default marker')}, |
| 147 | ] |
| 148 | for item in data { |
| 149 | s := parse_default_marker(item.attr) or { |
| 150 | assert err == item.err |
| 151 | continue |
| 152 | } |
| 153 | assert valid_default_marker(s) == true |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | struct OptionalMarker { |
| 158 | attr string |
| 159 | valid bool |
| 160 | present bool |
| 161 | err IError |
| 162 | } |
| 163 | |
| 164 | fn test_optional_marker_parsing() ! { |
| 165 | data := [ |
| 166 | // exactly matching key |
| 167 | OptionalMarker{'optional', true, false, none}, |
| 168 | OptionalMarker{'optional: present ', true, true, none}, |
| 169 | // matching key contains spaces is allowed |
| 170 | OptionalMarker{'optional ', true, false, none}, |
| 171 | OptionalMarker{' optional ', true, false, none}, |
| 172 | OptionalMarker{'optional:present ', true, true, none}, |
| 173 | // contains another key is not allowed |
| 174 | OptionalMarker{'optional: true ', false, false, error('Non optional presence bit marker')}, |
| 175 | OptionalMarker{'optional-- ', false, false, error('bad optional key')}, |
| 176 | // this should not allowed |
| 177 | OptionalMarker{'', false, false, error('not optional marker')}, |
| 178 | OptionalMarker{'optional_aaa', false, false, error('bad optional key')}, |
| 179 | OptionalMarker{'opt', false, false, error('not optional marker')}, |
| 180 | OptionalMarker{'xx_optional_ ', false, false, error('not optional marker')}, |
| 181 | ] |
| 182 | for i, item in data { |
| 183 | // dump(i) |
| 184 | key, prs := parse_optional_marker(item.attr) or { |
| 185 | assert err == item.err |
| 186 | continue |
| 187 | } |
| 188 | assert valid_optional_key(key) == item.valid |
| 189 | assert valid_optional_present_bit_marker(prs) == item.present |
| 190 | } |
| 191 | } |
| 192 | |