vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 1 | #define __ASN_INTERNAL_TEST_MODE__ |
vlm | d3da851 | 2004-08-19 13:26:54 +0000 | [diff] [blame] | 2 | #include <GeneralizedTime.c> |
| 3 | #include <constraints.c> |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 4 | #include <math.h> /* for pow(3) */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 5 | |
| 6 | static void |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 7 | recognize(char *time_str, time_t expect, int as_gmt) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 8 | GeneralizedTime_t gt; |
| 9 | struct tm tm; |
| 10 | time_t tloc; |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 11 | int fv, fp; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 12 | |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 13 | gt.buf = (uint8_t *)time_str; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 14 | gt.size = strlen(time_str); |
| 15 | |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 16 | tloc = asn_GT2time_frac(>, &fv, &fp, &tm, as_gmt); |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 17 | printf("%s: [%s] -> %ld == %ld\n", |
| 18 | as_gmt?"GMT":"ofs", time_str, (long)tloc, (long)expect); |
vlm | 6534a8d | 2004-10-20 15:40:04 +0000 | [diff] [blame] | 19 | |
| 20 | if(tloc != -1) { |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 21 | printf("\t%04d-%02d-%02dT%02d:%02d:%02d.%f(%d/%d)%+03ld%02ld\n", |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 22 | tm.tm_year + 1900, |
| 23 | tm.tm_mon + 1, |
| 24 | tm.tm_mday, |
| 25 | tm.tm_hour, |
| 26 | tm.tm_min, |
| 27 | tm.tm_sec, |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 28 | (double)fv * pow(0.1, fp), fv, fp, |
vlm | 6534a8d | 2004-10-20 15:40:04 +0000 | [diff] [blame] | 29 | (GMTOFF(tm) / 3600), |
| 30 | labs(GMTOFF(tm) % 3600) |
| 31 | ); |
| 32 | } |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 33 | assert(tloc == expect); |
| 34 | |
vlm | 6534a8d | 2004-10-20 15:40:04 +0000 | [diff] [blame] | 35 | #ifdef HAVE_TM_GMTOFF |
| 36 | assert(tloc == -1 || as_gmt == 0 || GMTOFF(tm) == 0); |
| 37 | #endif |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 38 | |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 39 | if(!as_gmt) recognize(time_str, expect, 1); |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | static void |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 43 | encode(time_t tloc, const char *expect, int force_gmt) { |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 44 | GeneralizedTime_t *gt; |
| 45 | struct tm tm, *tmp; |
| 46 | |
| 47 | tmp = localtime_r(&tloc, &tm); |
| 48 | assert(tmp); |
| 49 | |
| 50 | gt = asn_time2GT(0, &tm, force_gmt); |
| 51 | if(gt) { |
| 52 | assert(expect); |
| 53 | printf("[%s] vs [%s] (%d)\n", |
| 54 | gt->buf, expect, force_gmt); |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 55 | assert(gt->size == (int)strlen((char *)gt->buf)); |
| 56 | assert(!strcmp((char *)gt->buf, expect)); |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 57 | } else { |
| 58 | assert(!expect); |
| 59 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 60 | } |
| 61 | |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 62 | |
| 63 | static void |
| 64 | recode(char *time_str, const char *expect) { |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 65 | int frac_value, frac_digits; |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 66 | GeneralizedTime_t gt; |
| 67 | struct tm tm; |
| 68 | time_t tloc; |
| 69 | |
| 70 | gt.buf = (uint8_t *)time_str; |
| 71 | gt.size = strlen(time_str); |
| 72 | |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 73 | tloc = asn_GT2time_frac(>, &frac_value, &frac_digits, &tm, 1); |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 74 | assert(tloc != -1); |
| 75 | |
| 76 | gt.buf = 0; |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 77 | asn_time2GT_frac(>, &tm, frac_value, frac_digits, 1); |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 78 | assert(gt.buf); |
| 79 | |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 80 | printf("[%s] => [%s] == [%s] (%d, %d)\n", |
| 81 | time_str, gt.buf, expect, frac_value, frac_digits); |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 82 | |
| 83 | assert(strcmp((char *)gt.buf, expect) == 0); |
| 84 | FREEMEM(gt.buf); |
| 85 | } |
| 86 | |
vlm | 9d53193 | 2005-07-04 01:44:01 +0000 | [diff] [blame] | 87 | static void |
| 88 | check_fractions() { |
| 89 | GeneralizedTime_t *gt = 0; |
| 90 | struct tm tm; |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 91 | int fv, fd; |
| 92 | time_t tloc; |
vlm | 9d53193 | 2005-07-04 01:44:01 +0000 | [diff] [blame] | 93 | |
| 94 | memset(&tm, 0, sizeof tm); |
| 95 | tm.tm_year = 70; |
| 96 | tm.tm_mday = 1; |
| 97 | |
| 98 | gt = asn_time2GT_frac(gt, &tm, -1, -1, 1); |
| 99 | assert(gt); |
| 100 | printf("[%s]\n", gt->buf); |
| 101 | assert(strcmp((char *)gt->buf, "19700101000000Z") == 0); |
| 102 | |
| 103 | gt = asn_time2GT_frac(gt, &tm, 0, 0, 1); |
| 104 | assert(gt); |
| 105 | printf("[%s]\n", gt->buf); |
| 106 | assert(strcmp((char *)gt->buf, "19700101000000Z") == 0); |
| 107 | |
| 108 | gt = asn_time2GT_frac(gt, &tm, 0, -1, 1); |
| 109 | assert(gt); |
| 110 | printf("[%s]\n", gt->buf); |
| 111 | assert(strcmp((char *)gt->buf, "19700101000000Z") == 0); |
| 112 | |
| 113 | gt = asn_time2GT_frac(gt, &tm, -1, 0, 1); |
| 114 | assert(gt); |
| 115 | printf("[%s]\n", gt->buf); |
| 116 | assert(strcmp((char *)gt->buf, "19700101000000Z") == 0); |
| 117 | |
| 118 | gt = asn_time2GT_frac(gt, &tm, 10, 0, 1); |
| 119 | assert(gt); |
| 120 | printf("[%s]\n", gt->buf); |
| 121 | assert(strcmp((char *)gt->buf, "19700101000000Z") == 0); |
| 122 | |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 123 | /* Normalization should happen prior to calling the _frac() */ |
| 124 | gt = asn_time2GT_frac(gt, &tm, 55, 2, 1); |
| 125 | assert(gt); |
| 126 | printf("[%s]\n", gt->buf); |
| 127 | assert(strcmp((char *)gt->buf, "19700101000000.55Z") == 0); |
| 128 | |
| 129 | gt = asn_time2GT_frac(gt, &tm, 5, 2, 1); |
| 130 | assert(gt); |
| 131 | printf("[%s]\n", gt->buf); |
| 132 | assert(strcmp((char *)gt->buf, "19700101000000.05Z") == 0); |
| 133 | |
vlm | 9d53193 | 2005-07-04 01:44:01 +0000 | [diff] [blame] | 134 | /* Normalization should happen prior calling the _frac() */ |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 135 | gt = asn_time2GT_frac(gt, &tm, 900, 2, 1); |
vlm | 9d53193 | 2005-07-04 01:44:01 +0000 | [diff] [blame] | 136 | assert(gt); |
| 137 | printf("[%s]\n", gt->buf); |
| 138 | assert(strcmp((char *)gt->buf, "19700101000000Z") == 0); |
| 139 | |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 140 | gt = asn_time2GT_frac(gt, &tm, 90, 2, 1); |
vlm | 9d53193 | 2005-07-04 01:44:01 +0000 | [diff] [blame] | 141 | assert(gt); |
| 142 | printf("[%s]\n", gt->buf); |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 143 | assert(strcmp((char *)gt->buf, "19700101000000.9Z") == 0); |
vlm | baf858b | 2005-07-04 02:29:36 +0000 | [diff] [blame] | 144 | |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 145 | tloc = asn_GT2time_prec(gt, &fv, 0, 0, 1); |
| 146 | assert(tloc == 0); |
| 147 | assert(fv == 0); |
vlm | 9d53193 | 2005-07-04 01:44:01 +0000 | [diff] [blame] | 148 | |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 149 | tloc = asn_GT2time_prec(gt, &fv, 1, 0, 1); |
| 150 | assert(tloc == 0); |
| 151 | assert(fv == 9); |
vlm | 9d53193 | 2005-07-04 01:44:01 +0000 | [diff] [blame] | 152 | |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 153 | tloc = asn_GT2time_prec(gt, &fv, 2, 0, 1); |
| 154 | assert(tloc == 0); |
| 155 | assert(fv == 90); |
vlm | 008a048 | 2005-07-04 02:20:26 +0000 | [diff] [blame] | 156 | |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 157 | tloc = asn_GT2time_frac(gt, &fv, &fd, 0, 1); |
| 158 | assert(tloc == 0); |
| 159 | assert(fv == 9); |
| 160 | assert(fd == 1); |
vlm | baf858b | 2005-07-04 02:29:36 +0000 | [diff] [blame] | 161 | |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 162 | gt->buf[gt->size-1] = '0'; |
| 163 | gt->buf[gt->size++] = 'Z'; |
| 164 | gt->buf[gt->size] = '\0'; |
vlm | 008a048 | 2005-07-04 02:20:26 +0000 | [diff] [blame] | 165 | |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 166 | tloc = asn_GT2time_frac(gt, &fv, &fd, 0, 1); |
| 167 | assert(tloc == 0); |
| 168 | assert(fd == 2); |
| 169 | assert(fv == 90); |
vlm | baf858b | 2005-07-04 02:29:36 +0000 | [diff] [blame] | 170 | |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 171 | tloc = asn_GT2time_prec(gt, &fv, 1, 0, 1); |
| 172 | assert(tloc == 0); |
| 173 | assert(fv == 9); |
| 174 | |
| 175 | tloc = asn_GT2time_prec(gt, &fv, 100, 0, 1); |
| 176 | assert(tloc == 0); |
| 177 | assert(fv == 0); |
vlm | 008a048 | 2005-07-04 02:20:26 +0000 | [diff] [blame] | 178 | |
vlm | 9d53193 | 2005-07-04 01:44:01 +0000 | [diff] [blame] | 179 | FREEMEM(gt->buf); |
| 180 | FREEMEM(gt); |
| 181 | } |
| 182 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 183 | int |
| 184 | main(int ac, char **av) { |
| 185 | |
vlm | d3da851 | 2004-08-19 13:26:54 +0000 | [diff] [blame] | 186 | (void)av; |
| 187 | |
vlm | 9d53193 | 2005-07-04 01:44:01 +0000 | [diff] [blame] | 188 | check_fractions(); |
| 189 | |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 190 | recognize("200401250", -1, 0); |
| 191 | recognize("2004012509300", -1, 0); |
| 192 | recognize("20040125093000-", -1, 0); |
| 193 | recognize("20040125093007-0", -1, 0); |
| 194 | recognize("20040125093007-080", -1, 0); |
| 195 | recognize("200401250930.01Z", -1, 0); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 196 | |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 197 | /* These six are from X.690:11.7.5 */ |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 198 | recognize("19920520240000Z", -1, 0); /* midnight represented incorrectly */ |
| 199 | recognize("19920622123421.0Z", 709216461, 0); /* spurious trailing zeros */ |
| 200 | recognize("19920722132100.30Z", 711811260, 0); /* spurious trailing zeros */ |
| 201 | recognize("19920521000000Z", 706406400, 0); |
| 202 | recognize("19920622123421Z", 709216461, 0); |
| 203 | recognize("19920722132100.3Z", 711811260, 0); |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 204 | |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 205 | recognize("20040125093007Z", 1075023007, 0); |
| 206 | recognize("20040125093007+00", 1075023007, 0); |
| 207 | recognize("20040125093007.01+0000", 1075023007, 0); |
| 208 | recognize("20040125093007,1+0000", 1075023007, 0); |
| 209 | recognize("20040125093007-0800", 1075051807, 0); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 210 | |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 211 | recognize("19920722132100.123000123Z", 711811260, 0); |
| 212 | recognize("19920722132100.1230000123Z", 711811260, 0); |
| 213 | recognize("19920722132100.12300000123Z", 711811260, 0); |
| 214 | |
| 215 | encode(1075023007, "20040125093007Z", 1); |
vlm | 0f8e5c1 | 2004-08-07 04:16:42 +0000 | [diff] [blame] | 216 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 217 | if(ac > 1) { |
| 218 | /* These will be valid only inside PST time zone */ |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 219 | recognize("20040125093007", 1075051807, 0); |
| 220 | recognize("200401250930", 1075051800, 0); |
| 221 | recognize("20040125093000,01", 1075051800, 0); |
| 222 | recognize("20040125093000,1234", 1075051800, 0); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 223 | |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 224 | encode(1075023007, "20040125013007-0800", 0); |
| 225 | recode("20050702123312", "20050702193312Z"); |
vlm | 0f8e5c1 | 2004-08-07 04:16:42 +0000 | [diff] [blame] | 226 | } |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 227 | |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 228 | recode("20050702123312Z", "20050702123312Z"); |
| 229 | recode("20050702123312+01", "20050702113312Z"); |
| 230 | recode("20050702123312,0+01", "20050702113312Z"); |
| 231 | recode("20050702123312,1+01", "20050702113312.1Z"); |
| 232 | recode("20050702123312.01+01", "20050702113312.01Z"); |
| 233 | recode("20050702123312.00+01", "20050702113312Z"); |
| 234 | recode("20050702123312.30+01", "20050702113312.3Z"); |
| 235 | recode("20050702123312,30000+01", "20050702113312.3Z"); |
| 236 | recode("20050702123312,300000000+01", "20050702113312.3Z"); |
| 237 | recode("20050702123312.123456+01", "20050702113312.123456Z"); |
| 238 | recode("20050702123312.1234567+01", "20050702113312.123456Z"); |
| 239 | recode("20050702123312.12345678+01", "20050702113312.123456Z"); |
| 240 | recode("20050702123312.123456789+01", "20050702113312.123456Z"); |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 241 | recode("20050702123312.2000000000+01", "20050702113312.2Z"); |
| 242 | recode("20050702123312.3000000000+01", "20050702113312.3Z"); |
| 243 | recode("20050702123312.4000000000+01", "20050702113312.4Z"); |
| 244 | recode("20050702123312.5000000000+01", "20050702113312.5Z"); |
| 245 | recode("20050702123312.5000000001+01", "20050702113312.5Z"); |
| 246 | recode("20050702123312.5000010001+01", "20050702113312.500001Z"); |
| 247 | recode("20050702123312.5000001001+01", "20050702113312.5Z"); |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 248 | recode("20050702123312.000001+01", "20050702113312.000001Z"); |
| 249 | recode("20050702123312.0000001Z", "20050702123312Z"); |
| 250 | recode("20050702123312.0080010+1056", "20050702013712.008001Z"); |
| 251 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 252 | return 0; |
| 253 | } |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 254 | |
| 255 | /* |
| 256 | * Dummy function. |
| 257 | */ |
| 258 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 259 | asn_enc_rval_t |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 260 | OCTET_STRING_encode_der(asn_TYPE_descriptor_t *td, void *ptr, int tag_mode, ber_tlv_tag_t tag, asn_app_consume_bytes_f *cb, void *app_key) { |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 261 | asn_enc_rval_t erval; |
vlm | d3da851 | 2004-08-19 13:26:54 +0000 | [diff] [blame] | 262 | |
| 263 | (void)td; |
| 264 | (void)ptr; |
| 265 | (void)tag_mode; |
| 266 | (void)tag; |
| 267 | (void)cb; |
| 268 | (void)app_key; |
| 269 | |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 270 | return erval; |
| 271 | } |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 272 | |
| 273 | asn_enc_rval_t |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 274 | OCTET_STRING_encode_xer_utf8(asn_TYPE_descriptor_t *td, void *ptr, int ilevel, enum xer_encoder_flags_e flags, asn_app_consume_bytes_f *cb, void *app_key) { |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 275 | asn_enc_rval_t erval; |
| 276 | |
| 277 | (void)td; |
| 278 | (void)ptr; |
| 279 | (void)ilevel; |
| 280 | (void)flags; |
| 281 | (void)cb; |
| 282 | (void)app_key; |
| 283 | |
| 284 | return erval; |
| 285 | } |