| 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 | fn test_encode_decode_numericstring_basic() { |
| 7 | str := '98' |
| 8 | exp := [u8(0x12), 0x02, 57, 56] |
| 9 | |
| 10 | ns := NumericString.new(str)! |
| 11 | out := encode(ns)! |
| 12 | assert out == exp |
| 13 | |
| 14 | // decode back |
| 15 | nsback, _ := NumericString.decode(out)! |
| 16 | |
| 17 | assert nsback.tag().tag_class() == .universal |
| 18 | assert nsback.tag().is_constructed() == false |
| 19 | assert nsback.tag().tag_number() == int(TagType.numericstring) |
| 20 | assert nsback.value == str |
| 21 | } |
| 22 | |
| 23 | struct NumericalTest { |
| 24 | inp string |
| 25 | exp_length int |
| 26 | exp_bytelength []u8 |
| 27 | exp_values []u8 |
| 28 | exp_out []u8 |
| 29 | err IError |
| 30 | } |
| 31 | |
| 32 | fn test_encode_decode_numericstring_advanced() ! { |
| 33 | // maps string to repeat |
| 34 | m := { |
| 35 | '1': 1 |
| 36 | '2': 10 |
| 37 | '3': 128 |
| 38 | '4': 256 |
| 39 | '5': 65536 // its too long to repeat |
| 40 | //'6': 16777215 |
| 41 | } |
| 42 | mut exp := []NumericalTest{} |
| 43 | for k, v in m { |
| 44 | s := k.repeat(v) // strings.repeat_string(k, v) |
| 45 | b := s.bytes() |
| 46 | ln := Length.new(b.len)! |
| 47 | mut dst := []u8{} |
| 48 | ln.encode(mut dst)! |
| 49 | |
| 50 | d := NumericalTest{ |
| 51 | inp: s |
| 52 | exp_length: dst.len |
| 53 | exp_bytelength: dst |
| 54 | exp_values: b |
| 55 | err: none |
| 56 | } |
| 57 | |
| 58 | exp << d |
| 59 | } |
| 60 | |
| 61 | for c in exp { |
| 62 | mut exp_out := [u8(TagType.numericstring)] |
| 63 | exp_out << c.exp_bytelength |
| 64 | exp_out << c.exp_values |
| 65 | ns := NumericString.new(c.inp) or { |
| 66 | assert err == c.err |
| 67 | continue |
| 68 | } |
| 69 | out := encode(ns)! |
| 70 | assert out == exp_out |
| 71 | |
| 72 | // decode back |
| 73 | back, _ := NumericString.decode(out)! |
| 74 | |
| 75 | assert back.value == c.inp |
| 76 | } |
| 77 | } |
| 78 | |