blob: b56fa0b9ebcc60c1fdc0373305ce8431d06ab32b [file] [log] [blame]
vlm5ea810e2005-07-03 05:32:40 +00001#define __ASN_INTERNAL_TEST_MODE__
vlmd3da8512004-08-19 13:26:54 +00002#include <GeneralizedTime.c>
3#include <constraints.c>
vlmaa116962005-07-04 12:21:51 +00004#include <math.h> /* for pow(3) */
vlmfa67ddc2004-06-03 03:38:44 +00005
6static void
vlm5ea810e2005-07-03 05:32:40 +00007recognize(char *time_str, time_t expect, int as_gmt) {
vlmfa67ddc2004-06-03 03:38:44 +00008 GeneralizedTime_t gt;
9 struct tm tm;
10 time_t tloc;
vlmaa116962005-07-04 12:21:51 +000011 int fv, fp;
vlmfa67ddc2004-06-03 03:38:44 +000012
vlm5ea810e2005-07-03 05:32:40 +000013 gt.buf = (uint8_t *)time_str;
vlmfa67ddc2004-06-03 03:38:44 +000014 gt.size = strlen(time_str);
15
vlmaa116962005-07-04 12:21:51 +000016 tloc = asn_GT2time_frac(&gt, &fv, &fp, &tm, as_gmt);
vlm81057a82004-08-07 03:52:26 +000017 printf("%s: [%s] -> %ld == %ld\n",
18 as_gmt?"GMT":"ofs", time_str, (long)tloc, (long)expect);
vlm6534a8d2004-10-20 15:40:04 +000019
20 if(tloc != -1) {
vlmaa116962005-07-04 12:21:51 +000021 printf("\t%04d-%02d-%02dT%02d:%02d:%02d.%f(%d/%d)%+03ld%02ld\n",
vlmfa67ddc2004-06-03 03:38:44 +000022 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,
vlmaa116962005-07-04 12:21:51 +000028 (double)fv * pow(0.1, fp), fv, fp,
vlm6534a8d2004-10-20 15:40:04 +000029 (GMTOFF(tm) / 3600),
30 labs(GMTOFF(tm) % 3600)
31 );
32 }
vlm81057a82004-08-07 03:52:26 +000033 assert(tloc == expect);
34
vlm6534a8d2004-10-20 15:40:04 +000035#ifdef HAVE_TM_GMTOFF
36 assert(tloc == -1 || as_gmt == 0 || GMTOFF(tm) == 0);
37#endif
vlm81057a82004-08-07 03:52:26 +000038
vlm5ea810e2005-07-03 05:32:40 +000039 if(!as_gmt) recognize(time_str, expect, 1);
vlm81057a82004-08-07 03:52:26 +000040}
41
42static void
vlm5ea810e2005-07-03 05:32:40 +000043encode(time_t tloc, const char *expect, int force_gmt) {
vlm81057a82004-08-07 03:52:26 +000044 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);
vlm5ea810e2005-07-03 05:32:40 +000055 assert(gt->size == (int)strlen((char *)gt->buf));
56 assert(!strcmp((char *)gt->buf, expect));
vlm81057a82004-08-07 03:52:26 +000057 } else {
58 assert(!expect);
59 }
vlmfa67ddc2004-06-03 03:38:44 +000060}
61
vlmd117a852006-07-13 13:20:19 +000062#define RECODE(foo, bar) recode(__LINE__, foo, bar)
vlm5ea810e2005-07-03 05:32:40 +000063
64static void
vlmd117a852006-07-13 13:20:19 +000065recode(int lineno, char *time_str, const char *expect) {
vlmaa116962005-07-04 12:21:51 +000066 int frac_value, frac_digits;
vlm5ea810e2005-07-03 05:32:40 +000067 GeneralizedTime_t gt;
68 struct tm tm;
69 time_t tloc;
vlmd117a852006-07-13 13:20:19 +000070 char *tz;
vlm5ea810e2005-07-03 05:32:40 +000071
72 gt.buf = (uint8_t *)time_str;
73 gt.size = strlen(time_str);
74
vlmaa116962005-07-04 12:21:51 +000075 tloc = asn_GT2time_frac(&gt, &frac_value, &frac_digits, &tm, 1);
vlm5ea810e2005-07-03 05:32:40 +000076 assert(tloc != -1);
77
78 gt.buf = 0;
vlmaa116962005-07-04 12:21:51 +000079 asn_time2GT_frac(&gt, &tm, frac_value, frac_digits, 1);
vlm5ea810e2005-07-03 05:32:40 +000080 assert(gt.buf);
81
vlmd117a852006-07-13 13:20:19 +000082 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 : "");
vlm5ea810e2005-07-03 05:32:40 +000087
88 assert(strcmp((char *)gt.buf, expect) == 0);
89 FREEMEM(gt.buf);
90}
91
vlm9d531932005-07-04 01:44:01 +000092static void
93check_fractions() {
94 GeneralizedTime_t *gt = 0;
95 struct tm tm;
vlmaa116962005-07-04 12:21:51 +000096 int fv, fd;
97 time_t tloc;
vlm9d531932005-07-04 01:44:01 +000098
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
vlmaa116962005-07-04 12:21:51 +0000128 /* 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
vlm9d531932005-07-04 01:44:01 +0000139 /* Normalization should happen prior calling the _frac() */
vlmaa116962005-07-04 12:21:51 +0000140 gt = asn_time2GT_frac(gt, &tm, 900, 2, 1);
vlm9d531932005-07-04 01:44:01 +0000141 assert(gt);
142 printf("[%s]\n", gt->buf);
143 assert(strcmp((char *)gt->buf, "19700101000000Z") == 0);
144
vlmaa116962005-07-04 12:21:51 +0000145 gt = asn_time2GT_frac(gt, &tm, 90, 2, 1);
vlm9d531932005-07-04 01:44:01 +0000146 assert(gt);
147 printf("[%s]\n", gt->buf);
vlmaa116962005-07-04 12:21:51 +0000148 assert(strcmp((char *)gt->buf, "19700101000000.9Z") == 0);
vlmbaf858b2005-07-04 02:29:36 +0000149
vlmaa116962005-07-04 12:21:51 +0000150 tloc = asn_GT2time_prec(gt, &fv, 0, 0, 1);
151 assert(tloc == 0);
152 assert(fv == 0);
vlm9d531932005-07-04 01:44:01 +0000153
vlmaa116962005-07-04 12:21:51 +0000154 tloc = asn_GT2time_prec(gt, &fv, 1, 0, 1);
155 assert(tloc == 0);
156 assert(fv == 9);
vlm9d531932005-07-04 01:44:01 +0000157
vlmaa116962005-07-04 12:21:51 +0000158 tloc = asn_GT2time_prec(gt, &fv, 2, 0, 1);
159 assert(tloc == 0);
160 assert(fv == 90);
vlm008a0482005-07-04 02:20:26 +0000161
vlmaa116962005-07-04 12:21:51 +0000162 tloc = asn_GT2time_frac(gt, &fv, &fd, 0, 1);
163 assert(tloc == 0);
164 assert(fv == 9);
165 assert(fd == 1);
vlmbaf858b2005-07-04 02:29:36 +0000166
vlmaa116962005-07-04 12:21:51 +0000167 gt->buf[gt->size-1] = '0';
168 gt->buf[gt->size++] = 'Z';
169 gt->buf[gt->size] = '\0';
vlm008a0482005-07-04 02:20:26 +0000170
vlmaa116962005-07-04 12:21:51 +0000171 tloc = asn_GT2time_frac(gt, &fv, &fd, 0, 1);
172 assert(tloc == 0);
173 assert(fd == 2);
174 assert(fv == 90);
vlmbaf858b2005-07-04 02:29:36 +0000175
vlmaa116962005-07-04 12:21:51 +0000176 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);
vlm008a0482005-07-04 02:20:26 +0000183
vlm9d531932005-07-04 01:44:01 +0000184 FREEMEM(gt->buf);
185 FREEMEM(gt);
186}
187
vlmfa67ddc2004-06-03 03:38:44 +0000188int
189main(int ac, char **av) {
vlmd117a852006-07-13 13:20:19 +0000190 char *tz = getenv("TZ");
vlmfa67ddc2004-06-03 03:38:44 +0000191
vlmd3da8512004-08-19 13:26:54 +0000192 (void)av;
193
vlmd117a852006-07-13 13:20:19 +0000194 printf("TZ = [%s]\n", tz ? tz : "");
195
vlm9d531932005-07-04 01:44:01 +0000196 check_fractions();
197
vlm5ea810e2005-07-03 05:32:40 +0000198 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);
vlmfa67ddc2004-06-03 03:38:44 +0000204
vlm81057a82004-08-07 03:52:26 +0000205 /* These six are from X.690:11.7.5 */
vlm5ea810e2005-07-03 05:32:40 +0000206 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);
vlm81057a82004-08-07 03:52:26 +0000212
vlm5ea810e2005-07-03 05:32:40 +0000213 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);
vlmfa67ddc2004-06-03 03:38:44 +0000218
vlm5ea810e2005-07-03 05:32:40 +0000219 recognize("19920722132100.123000123Z", 711811260, 0);
220 recognize("19920722132100.1230000123Z", 711811260, 0);
221 recognize("19920722132100.12300000123Z", 711811260, 0);
222
223 encode(1075023007, "20040125093007Z", 1);
vlm0f8e5c12004-08-07 04:16:42 +0000224
vlmfa67ddc2004-06-03 03:38:44 +0000225 if(ac > 1) {
226 /* These will be valid only inside PST time zone */
vlm5ea810e2005-07-03 05:32:40 +0000227 recognize("20040125093007", 1075051807, 0);
228 recognize("200401250930", 1075051800, 0);
229 recognize("20040125093000,01", 1075051800, 0);
230 recognize("20040125093000,1234", 1075051800, 0);
vlmfa67ddc2004-06-03 03:38:44 +0000231
vlm5ea810e2005-07-03 05:32:40 +0000232 encode(1075023007, "20040125013007-0800", 0);
vlmd117a852006-07-13 13:20:19 +0000233 RECODE("20050702123312", "20050702193312Z");
vlm0f8e5c12004-08-07 04:16:42 +0000234 }
vlm81057a82004-08-07 03:52:26 +0000235
vlmd117a852006-07-13 13:20:19 +0000236#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
vlm5ea810e2005-07-03 05:32:40 +0000263
vlmfa67ddc2004-06-03 03:38:44 +0000264 return 0;
265}
vlm81057a82004-08-07 03:52:26 +0000266
267/*
268 * Dummy function.
269 */
270
vlm39ba4c42004-09-22 16:06:28 +0000271asn_enc_rval_t
vlmef6355b2004-09-29 13:26:15 +0000272OCTET_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) {
vlm39ba4c42004-09-22 16:06:28 +0000273 asn_enc_rval_t erval;
vlmd3da8512004-08-19 13:26:54 +0000274
275 (void)td;
276 (void)ptr;
277 (void)tag_mode;
278 (void)tag;
279 (void)cb;
280 (void)app_key;
281
vlmdcb9abb2005-11-13 09:49:05 +0000282 memset(&erval, 0, sizeof(erval));
vlm81057a82004-08-07 03:52:26 +0000283 return erval;
284}
vlm39ba4c42004-09-22 16:06:28 +0000285
286asn_enc_rval_t
vlm9de248e2004-10-20 15:50:55 +0000287OCTET_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) {
vlm39ba4c42004-09-22 16:06:28 +0000288 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
vlmdcb9abb2005-11-13 09:49:05 +0000297 memset(&erval, 0, sizeof(erval));
vlm39ba4c42004-09-22 16:06:28 +0000298 return erval;
299}