| 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 | import os |
| 7 | import time |
| 8 | |
| 9 | fn test_serialize_utctime_basic() ! { |
| 10 | inp := '191215190210Z' |
| 11 | |
| 12 | exp := [u8(0x17), 0x0D, 49, 57, 49, 50, 49, 53, 49, 57, 48, 50, 49, 48, 90] |
| 13 | |
| 14 | ut := UtcTime.new(inp)! |
| 15 | out := encode(ut)! |
| 16 | |
| 17 | assert out == exp |
| 18 | |
| 19 | // back |
| 20 | back, pos := UtcTime.decode(out)! |
| 21 | assert back.tag().tag_number() == int(TagType.utctime) |
| 22 | assert back == ut |
| 23 | assert back.value == inp |
| 24 | } |
| 25 | |
| 26 | fn test_create_utctime_from_std_time() ! { |
| 27 | now := time.new(year: 2024, month: 11, day: 13, hour: 17, minute: 45, second: 50) |
| 28 | |
| 29 | utb := UtcTime.from_time(now)! |
| 30 | utc := UtcTime.from_time(now)! |
| 31 | |
| 32 | assert utb.value == utc.value |
| 33 | |
| 34 | tt := utc.into_utctime()! |
| 35 | assert now == tt |
| 36 | |
| 37 | inp := '191215190210Z' |
| 38 | t_inp := UtcTime.new(inp)! |
| 39 | t_inp_utc := t_inp.into_utctime()! |
| 40 | |
| 41 | // time to UtcTime back |
| 42 | tinp_back := UtcTime.from_time(t_inp_utc)! |
| 43 | assert tinp_back.value == inp |
| 44 | } |
| 45 | |
| 46 | fn test_create_utctime_from_std_time_with_negative_offset() ! { |
| 47 | tz := os.getenv('TZ') |
| 48 | os.setenv('TZ', 'utc 1', true) |
| 49 | |
| 50 | defer { |
| 51 | os.setenv('TZ', tz, true) |
| 52 | } |
| 53 | |
| 54 | now := time.new(year: 2024, month: 11, day: 13, hour: 17, minute: 45, second: 50) |
| 55 | UtcTime.from_time(now)! |
| 56 | } |
| 57 | |
| 58 | fn test_serialize_utctime_error_without_z() ! { |
| 59 | // this input does not contains zulu 'Z' part |
| 60 | inp := '191215190210' |
| 61 | |
| 62 | exp := [u8(0x17), 0x0D, 49, 57, 49, 50, 49, 53, 49, 57, 48, 50, 49, 48] |
| 63 | |
| 64 | ut := UtcTime.new(inp) or { |
| 65 | assert err == error('UtcTime: fail on validate utctime') |
| 66 | return |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | fn test_serialize_utctime_error_month() ! { |
| 71 | // the month part is > 12 |
| 72 | inp := '191815190210Z' |
| 73 | |
| 74 | exp := [u8(0x17), 0x0D, 49, 57, 49, 56, 49, 53, 49, 57, 48, 50, 49, 48] |
| 75 | |
| 76 | ut := UtcTime.new(inp) or { |
| 77 | assert err == error('UtcTime: fail on validate utctime') |
| 78 | return |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | fn test_serialize_utctime_error_day() ! { |
| 83 | // the day part is > 30 |
| 84 | inp := '191235190210Z' |
| 85 | |
| 86 | exp := [u8(0x17), 0x0D, 0x31, 0x39, 0x31, 0x32, 0x31, 0x32, 0x31, 0x39, 0x30, 0x32, 0x31, 0x30, |
| 87 | 0x5A] |
| 88 | |
| 89 | ut := UtcTime.new(inp) or { |
| 90 | assert err == error('UtcTime: fail on validate utctime') |
| 91 | return |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | fn test_serialize_decode_generalizedtime() ! { |
| 96 | s := '20100102030405Z' |
| 97 | |
| 98 | exp := [u8(0x18), 0x0f, 50, 48, 49, 48, 48, 49, 48, 50, 48, 51, 48, 52, 48, 53, 90] |
| 99 | |
| 100 | gt := GeneralizedTime.new(s)! |
| 101 | out := encode(gt)! |
| 102 | assert out == exp |
| 103 | |
| 104 | // back |
| 105 | back, pos := GeneralizedTime.decode(out)! |
| 106 | |
| 107 | assert back == gt |
| 108 | assert back.value == s |
| 109 | assert back.tag().tag_number() == int(TagType.generalizedtime) |
| 110 | } |
| 111 | |
| 112 | fn test_create_generalizedtime_from_std_time() ! { |
| 113 | now := time.new(year: 2024, month: 11, day: 13, hour: 17, minute: 45, second: 50) |
| 114 | |
| 115 | gtb := GeneralizedTime.from_time(now)! |
| 116 | gtc := GeneralizedTime.from_time(now)! |
| 117 | |
| 118 | assert gtb.value == gtc.value |
| 119 | assert gtb.value == '20241113174550Z' |
| 120 | assert gtc.value == '20241113174550Z' |
| 121 | |
| 122 | tt := gtc.into_utctime()! |
| 123 | assert now == tt |
| 124 | |
| 125 | s := '20100102030405Z' |
| 126 | gt := GeneralizedTime.new(s)! |
| 127 | |
| 128 | g_utc := gt.into_utctime()! |
| 129 | g_utc_back := GeneralizedTime.from_time(g_utc)! |
| 130 | |
| 131 | assert g_utc_back.value == s |
| 132 | } |
| 133 | |
| 134 | fn test_create_generalizedtime_from_std_time_with_negative_offset() ! { |
| 135 | tz := os.getenv('TZ') |
| 136 | os.setenv('TZ', 'utc 1', true) |
| 137 | |
| 138 | defer { |
| 139 | os.setenv('TZ', tz, true) |
| 140 | } |
| 141 | |
| 142 | now := time.new(year: 2024, month: 11, day: 13, hour: 17, minute: 45, second: 50) |
| 143 | GeneralizedTime.from_time(now)! |
| 144 | } |
| 145 | |