blob: 9eabb69fa6f0a5ad4ee99d0f9f6072f524c1333d [file] [log] [blame]
Lev Walkin535612a2005-07-03 05:32:40 +00001#define __ASN_INTERNAL_TEST_MODE__
Lev Walkin44212662004-08-19 13:26:54 +00002#include <GeneralizedTime.c>
3#include <constraints.c>
Lev Walkin3a522782005-07-04 12:21:51 +00004#include <math.h> /* for pow(3) */
Lev Walkinf15320b2004-06-03 03:38:44 +00005
6static void
Lev Walkin535612a2005-07-03 05:32:40 +00007recognize(char *time_str, time_t expect, int as_gmt) {
Lev Walkinf15320b2004-06-03 03:38:44 +00008 GeneralizedTime_t gt;
9 struct tm tm;
10 time_t tloc;
Lev Walkin3a522782005-07-04 12:21:51 +000011 int fv, fp;
Lev Walkinf15320b2004-06-03 03:38:44 +000012
Lev Walkin535612a2005-07-03 05:32:40 +000013 gt.buf = (uint8_t *)time_str;
Lev Walkinf15320b2004-06-03 03:38:44 +000014 gt.size = strlen(time_str);
15
Lev Walkin3a522782005-07-04 12:21:51 +000016 tloc = asn_GT2time_frac(&gt, &fv, &fp, &tm, as_gmt);
Lev Walkin99006362004-08-07 03:52:26 +000017 printf("%s: [%s] -> %ld == %ld\n",
18 as_gmt?"GMT":"ofs", time_str, (long)tloc, (long)expect);
Lev Walkina460ba32004-10-20 15:40:04 +000019
20 if(tloc != -1) {
Lev Walkin3a522782005-07-04 12:21:51 +000021 printf("\t%04d-%02d-%02dT%02d:%02d:%02d.%f(%d/%d)%+03ld%02ld\n",
Lev Walkinf15320b2004-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,
Lev Walkin3a522782005-07-04 12:21:51 +000028 (double)fv * pow(0.1, fp), fv, fp,
Lev Walkina460ba32004-10-20 15:40:04 +000029 (GMTOFF(tm) / 3600),
30 labs(GMTOFF(tm) % 3600)
31 );
32 }
Lev Walkin99006362004-08-07 03:52:26 +000033 assert(tloc == expect);
34
Lev Walkina460ba32004-10-20 15:40:04 +000035#ifdef HAVE_TM_GMTOFF
36 assert(tloc == -1 || as_gmt == 0 || GMTOFF(tm) == 0);
37#endif
Lev Walkin99006362004-08-07 03:52:26 +000038
Lev Walkin535612a2005-07-03 05:32:40 +000039 if(!as_gmt) recognize(time_str, expect, 1);
Lev Walkin99006362004-08-07 03:52:26 +000040}
41
42static void
Lev Walkin535612a2005-07-03 05:32:40 +000043encode(time_t tloc, const char *expect, int force_gmt) {
Lev Walkin99006362004-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);
Lev Walkin535612a2005-07-03 05:32:40 +000055 assert(gt->size == (int)strlen((char *)gt->buf));
56 assert(!strcmp((char *)gt->buf, expect));
Lev Walkin99006362004-08-07 03:52:26 +000057 } else {
58 assert(!expect);
59 }
Lev Walkinf15320b2004-06-03 03:38:44 +000060}
61
Lev Walkin535612a2005-07-03 05:32:40 +000062
63static void
64recode(char *time_str, const char *expect) {
Lev Walkin3a522782005-07-04 12:21:51 +000065 int frac_value, frac_digits;
Lev Walkin535612a2005-07-03 05:32:40 +000066 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
Lev Walkin3a522782005-07-04 12:21:51 +000073 tloc = asn_GT2time_frac(&gt, &frac_value, &frac_digits, &tm, 1);
Lev Walkin535612a2005-07-03 05:32:40 +000074 assert(tloc != -1);
75
76 gt.buf = 0;
Lev Walkin3a522782005-07-04 12:21:51 +000077 asn_time2GT_frac(&gt, &tm, frac_value, frac_digits, 1);
Lev Walkin535612a2005-07-03 05:32:40 +000078 assert(gt.buf);
79
Lev Walkin3a522782005-07-04 12:21:51 +000080 printf("[%s] => [%s] == [%s] (%d, %d)\n",
81 time_str, gt.buf, expect, frac_value, frac_digits);
Lev Walkin535612a2005-07-03 05:32:40 +000082
83 assert(strcmp((char *)gt.buf, expect) == 0);
84 FREEMEM(gt.buf);
85}
86
Lev Walkin4b4c1462005-07-04 01:44:01 +000087static void
88check_fractions() {
89 GeneralizedTime_t *gt = 0;
90 struct tm tm;
Lev Walkin3a522782005-07-04 12:21:51 +000091 int fv, fd;
92 time_t tloc;
Lev Walkin4b4c1462005-07-04 01:44:01 +000093
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
Lev Walkin3a522782005-07-04 12:21:51 +0000123 /* 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
Lev Walkin4b4c1462005-07-04 01:44:01 +0000134 /* Normalization should happen prior calling the _frac() */
Lev Walkin3a522782005-07-04 12:21:51 +0000135 gt = asn_time2GT_frac(gt, &tm, 900, 2, 1);
Lev Walkin4b4c1462005-07-04 01:44:01 +0000136 assert(gt);
137 printf("[%s]\n", gt->buf);
138 assert(strcmp((char *)gt->buf, "19700101000000Z") == 0);
139
Lev Walkin3a522782005-07-04 12:21:51 +0000140 gt = asn_time2GT_frac(gt, &tm, 90, 2, 1);
Lev Walkin4b4c1462005-07-04 01:44:01 +0000141 assert(gt);
142 printf("[%s]\n", gt->buf);
Lev Walkin3a522782005-07-04 12:21:51 +0000143 assert(strcmp((char *)gt->buf, "19700101000000.9Z") == 0);
Lev Walkin88e0a772005-07-04 02:29:36 +0000144
Lev Walkin3a522782005-07-04 12:21:51 +0000145 tloc = asn_GT2time_prec(gt, &fv, 0, 0, 1);
146 assert(tloc == 0);
147 assert(fv == 0);
Lev Walkin4b4c1462005-07-04 01:44:01 +0000148
Lev Walkin3a522782005-07-04 12:21:51 +0000149 tloc = asn_GT2time_prec(gt, &fv, 1, 0, 1);
150 assert(tloc == 0);
151 assert(fv == 9);
Lev Walkin4b4c1462005-07-04 01:44:01 +0000152
Lev Walkin3a522782005-07-04 12:21:51 +0000153 tloc = asn_GT2time_prec(gt, &fv, 2, 0, 1);
154 assert(tloc == 0);
155 assert(fv == 90);
Lev Walkin10dd97c2005-07-04 02:20:26 +0000156
Lev Walkin3a522782005-07-04 12:21:51 +0000157 tloc = asn_GT2time_frac(gt, &fv, &fd, 0, 1);
158 assert(tloc == 0);
159 assert(fv == 9);
160 assert(fd == 1);
Lev Walkin88e0a772005-07-04 02:29:36 +0000161
Lev Walkin3a522782005-07-04 12:21:51 +0000162 gt->buf[gt->size-1] = '0';
163 gt->buf[gt->size++] = 'Z';
164 gt->buf[gt->size] = '\0';
Lev Walkin10dd97c2005-07-04 02:20:26 +0000165
Lev Walkin3a522782005-07-04 12:21:51 +0000166 tloc = asn_GT2time_frac(gt, &fv, &fd, 0, 1);
167 assert(tloc == 0);
168 assert(fd == 2);
169 assert(fv == 90);
Lev Walkin88e0a772005-07-04 02:29:36 +0000170
Lev Walkin3a522782005-07-04 12:21:51 +0000171 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);
Lev Walkin10dd97c2005-07-04 02:20:26 +0000178
Lev Walkin4b4c1462005-07-04 01:44:01 +0000179 FREEMEM(gt->buf);
180 FREEMEM(gt);
181}
182
Lev Walkinf15320b2004-06-03 03:38:44 +0000183int
184main(int ac, char **av) {
185
Lev Walkin44212662004-08-19 13:26:54 +0000186 (void)av;
187
Lev Walkin4b4c1462005-07-04 01:44:01 +0000188 check_fractions();
189
Lev Walkin535612a2005-07-03 05:32:40 +0000190 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);
Lev Walkinf15320b2004-06-03 03:38:44 +0000196
Lev Walkin99006362004-08-07 03:52:26 +0000197 /* These six are from X.690:11.7.5 */
Lev Walkin535612a2005-07-03 05:32:40 +0000198 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);
Lev Walkin99006362004-08-07 03:52:26 +0000204
Lev Walkin535612a2005-07-03 05:32:40 +0000205 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);
Lev Walkinf15320b2004-06-03 03:38:44 +0000210
Lev Walkin535612a2005-07-03 05:32:40 +0000211 recognize("19920722132100.123000123Z", 711811260, 0);
212 recognize("19920722132100.1230000123Z", 711811260, 0);
213 recognize("19920722132100.12300000123Z", 711811260, 0);
214
215 encode(1075023007, "20040125093007Z", 1);
Lev Walkin91d40d42004-08-07 04:16:42 +0000216
Lev Walkinf15320b2004-06-03 03:38:44 +0000217 if(ac > 1) {
218 /* These will be valid only inside PST time zone */
Lev Walkin535612a2005-07-03 05:32:40 +0000219 recognize("20040125093007", 1075051807, 0);
220 recognize("200401250930", 1075051800, 0);
221 recognize("20040125093000,01", 1075051800, 0);
222 recognize("20040125093000,1234", 1075051800, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000223
Lev Walkin535612a2005-07-03 05:32:40 +0000224 encode(1075023007, "20040125013007-0800", 0);
225 recode("20050702123312", "20050702193312Z");
Lev Walkin91d40d42004-08-07 04:16:42 +0000226 }
Lev Walkin99006362004-08-07 03:52:26 +0000227
Lev Walkin535612a2005-07-03 05:32:40 +0000228 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");
Lev Walkin3a522782005-07-04 12:21:51 +0000241 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");
Lev Walkin535612a2005-07-03 05:32:40 +0000248 recode("20050702123312.000001+01", "20050702113312.000001Z");
249 recode("20050702123312.0000001Z", "20050702123312Z");
250 recode("20050702123312.0080010+1056", "20050702013712.008001Z");
251
Lev Walkinf15320b2004-06-03 03:38:44 +0000252 return 0;
253}
Lev Walkin99006362004-08-07 03:52:26 +0000254
255/*
256 * Dummy function.
257 */
258
Lev Walkina9cc46e2004-09-22 16:06:28 +0000259asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000260OCTET_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) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000261 asn_enc_rval_t erval;
Lev Walkin44212662004-08-19 13:26:54 +0000262
263 (void)td;
264 (void)ptr;
265 (void)tag_mode;
266 (void)tag;
267 (void)cb;
268 (void)app_key;
269
Lev Walkinabde4f42005-11-13 09:49:05 +0000270 memset(&erval, 0, sizeof(erval));
Lev Walkin99006362004-08-07 03:52:26 +0000271 return erval;
272}
Lev Walkina9cc46e2004-09-22 16:06:28 +0000273
274asn_enc_rval_t
Lev Walkindc06f6b2004-10-20 15:50:55 +0000275OCTET_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) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000276 asn_enc_rval_t erval;
277
278 (void)td;
279 (void)ptr;
280 (void)ilevel;
281 (void)flags;
282 (void)cb;
283 (void)app_key;
284
Lev Walkinabde4f42005-11-13 09:49:05 +0000285 memset(&erval, 0, sizeof(erval));
Lev Walkina9cc46e2004-09-22 16:06:28 +0000286 return erval;
287}