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 | d117a85 | 2006-07-13 13:20:19 +0000 | [diff] [blame] | 62 | #define RECODE(foo, bar) recode(__LINE__, foo, bar) |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 63 | |
| 64 | static void |
vlm | d117a85 | 2006-07-13 13:20:19 +0000 | [diff] [blame] | 65 | recode(int lineno, char *time_str, const char *expect) { |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 66 | int frac_value, frac_digits; |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 67 | GeneralizedTime_t gt; |
| 68 | struct tm tm; |
| 69 | time_t tloc; |
vlm | d117a85 | 2006-07-13 13:20:19 +0000 | [diff] [blame] | 70 | char *tz; |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 71 | |
| 72 | gt.buf = (uint8_t *)time_str; |
| 73 | gt.size = strlen(time_str); |
| 74 | |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 75 | tloc = asn_GT2time_frac(>, &frac_value, &frac_digits, &tm, 1); |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 76 | assert(tloc != -1); |
| 77 | |
| 78 | gt.buf = 0; |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 79 | asn_time2GT_frac(>, &tm, frac_value, frac_digits, 1); |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 80 | assert(gt.buf); |
| 81 | |
vlm | d117a85 | 2006-07-13 13:20:19 +0000 | [diff] [blame] | 82 | tz = getenv("TZ"); |
| 83 | printf("%d: [%s] (%ld) => [%s] == [%s] (%d, %d) (TZ=%s)\n", |
| 84 | lineno, time_str, (long)tloc, gt.buf, |
| 85 | expect, frac_value, frac_digits, |
| 86 | tz ? tz : ""); |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 87 | |
| 88 | assert(strcmp((char *)gt.buf, expect) == 0); |
| 89 | FREEMEM(gt.buf); |
| 90 | } |
| 91 | |
vlm | 9d53193 | 2005-07-04 01:44:01 +0000 | [diff] [blame] | 92 | static void |
| 93 | check_fractions() { |
| 94 | GeneralizedTime_t *gt = 0; |
| 95 | struct tm tm; |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 96 | int fv, fd; |
| 97 | time_t tloc; |
vlm | 9d53193 | 2005-07-04 01:44:01 +0000 | [diff] [blame] | 98 | |
| 99 | memset(&tm, 0, sizeof tm); |
| 100 | tm.tm_year = 70; |
| 101 | tm.tm_mday = 1; |
| 102 | |
| 103 | gt = asn_time2GT_frac(gt, &tm, -1, -1, 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, 0, 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, 0, -1, 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, -1, 0, 1); |
| 119 | assert(gt); |
| 120 | printf("[%s]\n", gt->buf); |
| 121 | assert(strcmp((char *)gt->buf, "19700101000000Z") == 0); |
| 122 | |
| 123 | gt = asn_time2GT_frac(gt, &tm, 10, 0, 1); |
| 124 | assert(gt); |
| 125 | printf("[%s]\n", gt->buf); |
| 126 | assert(strcmp((char *)gt->buf, "19700101000000Z") == 0); |
| 127 | |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 128 | /* Normalization should happen prior to calling the _frac() */ |
| 129 | gt = asn_time2GT_frac(gt, &tm, 55, 2, 1); |
| 130 | assert(gt); |
| 131 | printf("[%s]\n", gt->buf); |
| 132 | assert(strcmp((char *)gt->buf, "19700101000000.55Z") == 0); |
| 133 | |
| 134 | gt = asn_time2GT_frac(gt, &tm, 5, 2, 1); |
| 135 | assert(gt); |
| 136 | printf("[%s]\n", gt->buf); |
| 137 | assert(strcmp((char *)gt->buf, "19700101000000.05Z") == 0); |
| 138 | |
vlm | 9d53193 | 2005-07-04 01:44:01 +0000 | [diff] [blame] | 139 | /* Normalization should happen prior calling the _frac() */ |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 140 | gt = asn_time2GT_frac(gt, &tm, 900, 2, 1); |
vlm | 9d53193 | 2005-07-04 01:44:01 +0000 | [diff] [blame] | 141 | assert(gt); |
| 142 | printf("[%s]\n", gt->buf); |
| 143 | assert(strcmp((char *)gt->buf, "19700101000000Z") == 0); |
| 144 | |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 145 | gt = asn_time2GT_frac(gt, &tm, 90, 2, 1); |
vlm | 9d53193 | 2005-07-04 01:44:01 +0000 | [diff] [blame] | 146 | assert(gt); |
| 147 | printf("[%s]\n", gt->buf); |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 148 | assert(strcmp((char *)gt->buf, "19700101000000.9Z") == 0); |
vlm | baf858b | 2005-07-04 02:29:36 +0000 | [diff] [blame] | 149 | |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 150 | tloc = asn_GT2time_prec(gt, &fv, 0, 0, 1); |
| 151 | assert(tloc == 0); |
| 152 | assert(fv == 0); |
vlm | 9d53193 | 2005-07-04 01:44:01 +0000 | [diff] [blame] | 153 | |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 154 | tloc = asn_GT2time_prec(gt, &fv, 1, 0, 1); |
| 155 | assert(tloc == 0); |
| 156 | assert(fv == 9); |
vlm | 9d53193 | 2005-07-04 01:44:01 +0000 | [diff] [blame] | 157 | |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 158 | tloc = asn_GT2time_prec(gt, &fv, 2, 0, 1); |
| 159 | assert(tloc == 0); |
| 160 | assert(fv == 90); |
vlm | 008a048 | 2005-07-04 02:20:26 +0000 | [diff] [blame] | 161 | |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 162 | tloc = asn_GT2time_frac(gt, &fv, &fd, 0, 1); |
| 163 | assert(tloc == 0); |
| 164 | assert(fv == 9); |
| 165 | assert(fd == 1); |
vlm | baf858b | 2005-07-04 02:29:36 +0000 | [diff] [blame] | 166 | |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 167 | gt->buf[gt->size-1] = '0'; |
| 168 | gt->buf[gt->size++] = 'Z'; |
| 169 | gt->buf[gt->size] = '\0'; |
vlm | 008a048 | 2005-07-04 02:20:26 +0000 | [diff] [blame] | 170 | |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 171 | tloc = asn_GT2time_frac(gt, &fv, &fd, 0, 1); |
| 172 | assert(tloc == 0); |
| 173 | assert(fd == 2); |
| 174 | assert(fv == 90); |
vlm | baf858b | 2005-07-04 02:29:36 +0000 | [diff] [blame] | 175 | |
vlm | aa11696 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 176 | tloc = asn_GT2time_prec(gt, &fv, 1, 0, 1); |
| 177 | assert(tloc == 0); |
| 178 | assert(fv == 9); |
| 179 | |
| 180 | tloc = asn_GT2time_prec(gt, &fv, 100, 0, 1); |
| 181 | assert(tloc == 0); |
| 182 | assert(fv == 0); |
vlm | 008a048 | 2005-07-04 02:20:26 +0000 | [diff] [blame] | 183 | |
vlm | 9d53193 | 2005-07-04 01:44:01 +0000 | [diff] [blame] | 184 | FREEMEM(gt->buf); |
| 185 | FREEMEM(gt); |
| 186 | } |
| 187 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 188 | int |
| 189 | main(int ac, char **av) { |
vlm | d117a85 | 2006-07-13 13:20:19 +0000 | [diff] [blame] | 190 | char *tz = getenv("TZ"); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 191 | |
vlm | d3da851 | 2004-08-19 13:26:54 +0000 | [diff] [blame] | 192 | (void)av; |
| 193 | |
vlm | d117a85 | 2006-07-13 13:20:19 +0000 | [diff] [blame] | 194 | printf("TZ = [%s]\n", tz ? tz : ""); |
| 195 | |
vlm | 9d53193 | 2005-07-04 01:44:01 +0000 | [diff] [blame] | 196 | check_fractions(); |
| 197 | |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 198 | recognize("200401250", -1, 0); |
| 199 | recognize("2004012509300", -1, 0); |
| 200 | recognize("20040125093000-", -1, 0); |
| 201 | recognize("20040125093007-0", -1, 0); |
| 202 | recognize("20040125093007-080", -1, 0); |
| 203 | recognize("200401250930.01Z", -1, 0); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 204 | |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 205 | /* These six are from X.690:11.7.5 */ |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 206 | recognize("19920520240000Z", -1, 0); /* midnight represented incorrectly */ |
| 207 | recognize("19920622123421.0Z", 709216461, 0); /* spurious trailing zeros */ |
| 208 | recognize("19920722132100.30Z", 711811260, 0); /* spurious trailing zeros */ |
| 209 | recognize("19920521000000Z", 706406400, 0); |
| 210 | recognize("19920622123421Z", 709216461, 0); |
| 211 | recognize("19920722132100.3Z", 711811260, 0); |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 212 | |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 213 | recognize("20040125093007Z", 1075023007, 0); |
| 214 | recognize("20040125093007+00", 1075023007, 0); |
| 215 | recognize("20040125093007.01+0000", 1075023007, 0); |
| 216 | recognize("20040125093007,1+0000", 1075023007, 0); |
| 217 | recognize("20040125093007-0800", 1075051807, 0); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 218 | |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 219 | recognize("19920722132100.123000123Z", 711811260, 0); |
| 220 | recognize("19920722132100.1230000123Z", 711811260, 0); |
| 221 | recognize("19920722132100.12300000123Z", 711811260, 0); |
| 222 | |
| 223 | encode(1075023007, "20040125093007Z", 1); |
vlm | 0f8e5c1 | 2004-08-07 04:16:42 +0000 | [diff] [blame] | 224 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 225 | if(ac > 1) { |
| 226 | /* These will be valid only inside PST time zone */ |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 227 | recognize("20040125093007", 1075051807, 0); |
| 228 | recognize("200401250930", 1075051800, 0); |
| 229 | recognize("20040125093000,01", 1075051800, 0); |
| 230 | recognize("20040125093000,1234", 1075051800, 0); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 231 | |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 232 | encode(1075023007, "20040125013007-0800", 0); |
vlm | d117a85 | 2006-07-13 13:20:19 +0000 | [diff] [blame] | 233 | RECODE("20050702123312", "20050702193312Z"); |
vlm | 0f8e5c1 | 2004-08-07 04:16:42 +0000 | [diff] [blame] | 234 | } |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 235 | |
vlm | d117a85 | 2006-07-13 13:20:19 +0000 | [diff] [blame] | 236 | #if defined(sun) || defined(__sun) || defined(_sun_) || defined(__solaris__) |
| 237 | printf("Solaris does not have a decent timegm() function.\n"); |
| 238 | #else /* !solaris */ |
| 239 | RECODE("20050702123312Z", "20050702123312Z"); |
| 240 | RECODE("20050702123312+01", "20050702113312Z"); |
| 241 | RECODE("20050702123312,0+01", "20050702113312Z"); |
| 242 | RECODE("20050702123312,1+01", "20050702113312.1Z"); |
| 243 | RECODE("20050702123312.01+01", "20050702113312.01Z"); |
| 244 | RECODE("20050702123312.00+01", "20050702113312Z"); |
| 245 | RECODE("20050702123312.30+01", "20050702113312.3Z"); |
| 246 | RECODE("20050702123312,30000+01", "20050702113312.3Z"); |
| 247 | RECODE("20050702123312,300000000+01", "20050702113312.3Z"); |
| 248 | RECODE("20050702123312.123456+01", "20050702113312.123456Z"); |
| 249 | RECODE("20050702123312.1234567+01", "20050702113312.123456Z"); |
| 250 | RECODE("20050702123312.12345678+01", "20050702113312.123456Z"); |
| 251 | RECODE("20050702123312.123456789+01", "20050702113312.123456Z"); |
| 252 | RECODE("20050702123312.2000000000+01", "20050702113312.2Z"); |
| 253 | RECODE("20050702123312.3000000000+01", "20050702113312.3Z"); |
| 254 | RECODE("20050702123312.4000000000+01", "20050702113312.4Z"); |
| 255 | RECODE("20050702123312.5000000000+01", "20050702113312.5Z"); |
| 256 | RECODE("20050702123312.5000000001+01", "20050702113312.5Z"); |
| 257 | RECODE("20050702123312.5000010001+01", "20050702113312.500001Z"); |
| 258 | RECODE("20050702123312.5000001001+01", "20050702113312.5Z"); |
| 259 | RECODE("20050702123312.000001+01", "20050702113312.000001Z"); |
| 260 | RECODE("20050702123312.0000001Z", "20050702123312Z"); |
| 261 | RECODE("20050702123312.0080010+1056", "20050702013712.008001Z"); |
| 262 | #endif |
vlm | 5ea810e | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 263 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 264 | return 0; |
| 265 | } |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 266 | |
| 267 | /* |
| 268 | * Dummy function. |
| 269 | */ |
| 270 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 271 | asn_enc_rval_t |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 272 | 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] | 273 | asn_enc_rval_t erval; |
vlm | d3da851 | 2004-08-19 13:26:54 +0000 | [diff] [blame] | 274 | |
| 275 | (void)td; |
| 276 | (void)ptr; |
| 277 | (void)tag_mode; |
| 278 | (void)tag; |
| 279 | (void)cb; |
| 280 | (void)app_key; |
| 281 | |
vlm | dcb9abb | 2005-11-13 09:49:05 +0000 | [diff] [blame] | 282 | memset(&erval, 0, sizeof(erval)); |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 283 | return erval; |
| 284 | } |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 285 | |
| 286 | asn_enc_rval_t |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 287 | 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] | 288 | asn_enc_rval_t erval; |
| 289 | |
| 290 | (void)td; |
| 291 | (void)ptr; |
| 292 | (void)ilevel; |
| 293 | (void)flags; |
| 294 | (void)cb; |
| 295 | (void)app_key; |
| 296 | |
vlm | dcb9abb | 2005-11-13 09:49:05 +0000 | [diff] [blame] | 297 | memset(&erval, 0, sizeof(erval)); |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 298 | return erval; |
| 299 | } |