blob: 4b9f266160b0c32d7aad140c330cc3f5fac6bdee [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>
vlmfa67ddc2004-06-03 03:38:44 +00004
5static void
vlm5ea810e2005-07-03 05:32:40 +00006recognize(char *time_str, time_t expect, int as_gmt) {
vlmfa67ddc2004-06-03 03:38:44 +00007 GeneralizedTime_t gt;
8 struct tm tm;
9 time_t tloc;
vlm5ea810e2005-07-03 05:32:40 +000010 long fv, fb;
vlmfa67ddc2004-06-03 03:38:44 +000011
vlm5ea810e2005-07-03 05:32:40 +000012 gt.buf = (uint8_t *)time_str;
vlmfa67ddc2004-06-03 03:38:44 +000013 gt.size = strlen(time_str);
14
vlm5ea810e2005-07-03 05:32:40 +000015 tloc = asn_GT2time_frac(&gt, &fv, &fb, &tm, as_gmt);
vlm81057a82004-08-07 03:52:26 +000016 printf("%s: [%s] -> %ld == %ld\n",
17 as_gmt?"GMT":"ofs", time_str, (long)tloc, (long)expect);
vlm6534a8d2004-10-20 15:40:04 +000018
19 if(tloc != -1) {
vlm5ea810e2005-07-03 05:32:40 +000020 printf("\t%04d-%02d-%02dT%02d:%02d:%02d(.%ld/%ld)%+03ld%02ld\n",
vlmfa67ddc2004-06-03 03:38:44 +000021 tm.tm_year + 1900,
22 tm.tm_mon + 1,
23 tm.tm_mday,
24 tm.tm_hour,
25 tm.tm_min,
26 tm.tm_sec,
vlm5ea810e2005-07-03 05:32:40 +000027 fv, fb,
vlm6534a8d2004-10-20 15:40:04 +000028 (GMTOFF(tm) / 3600),
29 labs(GMTOFF(tm) % 3600)
30 );
vlm5ea810e2005-07-03 05:32:40 +000031 assert(fb < 100 || (fb % 100) == 0);
vlm6534a8d2004-10-20 15:40:04 +000032 }
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
vlm5ea810e2005-07-03 05:32:40 +000062
63static void
64recode(char *time_str, const char *expect) {
65 long frac_value, frac_base;
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
73 tloc = asn_GT2time_frac(&gt, &frac_value, &frac_base, &tm, 1);
74 assert(tloc != -1);
75
76 gt.buf = 0;
77 asn_time2GT_frac(&gt, &tm, frac_value, frac_base, 1);
78 assert(gt.buf);
79
80 printf("[%s] => [%s] == [%s]\n", time_str, gt.buf, expect);
81
82 assert(strcmp((char *)gt.buf, expect) == 0);
83 FREEMEM(gt.buf);
84}
85
vlm9d531932005-07-04 01:44:01 +000086static void
87check_fractions() {
88 GeneralizedTime_t *gt = 0;
89 struct tm tm;
90
91 memset(&tm, 0, sizeof tm);
92 tm.tm_year = 70;
93 tm.tm_mday = 1;
94
95 gt = asn_time2GT_frac(gt, &tm, -1, -1, 1);
96 assert(gt);
97 printf("[%s]\n", gt->buf);
98 assert(strcmp((char *)gt->buf, "19700101000000Z") == 0);
99
100 gt = asn_time2GT_frac(gt, &tm, 0, 0, 1);
101 assert(gt);
102 printf("[%s]\n", gt->buf);
103 assert(strcmp((char *)gt->buf, "19700101000000Z") == 0);
104
105 gt = asn_time2GT_frac(gt, &tm, 0, -1, 1);
106 assert(gt);
107 printf("[%s]\n", gt->buf);
108 assert(strcmp((char *)gt->buf, "19700101000000Z") == 0);
109
110 gt = asn_time2GT_frac(gt, &tm, -1, 0, 1);
111 assert(gt);
112 printf("[%s]\n", gt->buf);
113 assert(strcmp((char *)gt->buf, "19700101000000Z") == 0);
114
115 gt = asn_time2GT_frac(gt, &tm, 10, 0, 1);
116 assert(gt);
117 printf("[%s]\n", gt->buf);
118 assert(strcmp((char *)gt->buf, "19700101000000Z") == 0);
119
120 /* Normalization should happen prior calling the _frac() */
121 gt = asn_time2GT_frac(gt, &tm, 55, 10, 1);
122 assert(gt);
123 printf("[%s]\n", gt->buf);
124 assert(strcmp((char *)gt->buf, "19700101000000Z") == 0);
125
126 gt = asn_time2GT_frac(gt, &tm, 10, 20, 1);
127 assert(gt);
128 printf("[%s]\n", gt->buf);
vlmbaf858b2005-07-04 02:29:36 +0000129 assert(strcmp((char *)gt->buf, "19700101000000Z") == 0);
130
131 gt = asn_time2GT_frac(gt, &tm, 10000000, 20000000, 1);
132 assert(gt);
133 printf("[%s]\n", gt->buf);
vlm9d531932005-07-04 01:44:01 +0000134 assert(strcmp((char *)gt->buf, "19700101000000.5Z") == 0);
135
136 gt = asn_time2GT_frac(gt, &tm, -10, 20, 1);
137 assert(gt);
138 printf("[%s]\n", gt->buf);
139 assert(strcmp((char *)gt->buf, "19700101000000Z") == 0);
140
vlm008a0482005-07-04 02:20:26 +0000141 gt = asn_time2GT_frac(gt, &tm, 98, 99, 1);
142 assert(gt);
143 printf("[%s]\n", gt->buf);
144 assert(strcmp((char *)gt->buf, "19700101000000Z") == 0);
145
vlmbaf858b2005-07-04 02:29:36 +0000146 gt = asn_time2GT_frac(gt, &tm, 988, 999, 1);
147 assert(gt);
148 printf("[%s]\n", gt->buf);
149 assert(strcmp((char *)gt->buf, "19700101000000Z") == 0);
150
vlm008a0482005-07-04 02:20:26 +0000151 gt = asn_time2GT_frac(gt, &tm, 90, 91, 1);
152 assert(gt);
153 printf("[%s]\n", gt->buf);
154 assert(strcmp((char *)gt->buf, "19700101000000Z") == 0);
155
156 gt = asn_time2GT_frac(gt, &tm, 89, 91, 1);
157 assert(gt);
158 printf("[%s]\n", gt->buf);
vlmbaf858b2005-07-04 02:29:36 +0000159 assert(strcmp((char *)gt->buf, "19700101000000Z") == 0);
160
161 gt = asn_time2GT_frac(gt, &tm, 89000000, 91000000, 1);
162 assert(gt);
163 printf("[%s]\n", gt->buf);
164 assert(strcmp((char *)gt->buf, "19700101000000.978021Z") == 0);
vlm008a0482005-07-04 02:20:26 +0000165
vlm9d531932005-07-04 01:44:01 +0000166 FREEMEM(gt->buf);
167 FREEMEM(gt);
168}
169
vlmfa67ddc2004-06-03 03:38:44 +0000170int
171main(int ac, char **av) {
172
vlmd3da8512004-08-19 13:26:54 +0000173 (void)av;
174
vlm9d531932005-07-04 01:44:01 +0000175 check_fractions();
176
vlm5ea810e2005-07-03 05:32:40 +0000177 recognize("200401250", -1, 0);
178 recognize("2004012509300", -1, 0);
179 recognize("20040125093000-", -1, 0);
180 recognize("20040125093007-0", -1, 0);
181 recognize("20040125093007-080", -1, 0);
182 recognize("200401250930.01Z", -1, 0);
vlmfa67ddc2004-06-03 03:38:44 +0000183
vlm81057a82004-08-07 03:52:26 +0000184 /* These six are from X.690:11.7.5 */
vlm5ea810e2005-07-03 05:32:40 +0000185 recognize("19920520240000Z", -1, 0); /* midnight represented incorrectly */
186 recognize("19920622123421.0Z", 709216461, 0); /* spurious trailing zeros */
187 recognize("19920722132100.30Z", 711811260, 0); /* spurious trailing zeros */
188 recognize("19920521000000Z", 706406400, 0);
189 recognize("19920622123421Z", 709216461, 0);
190 recognize("19920722132100.3Z", 711811260, 0);
vlm81057a82004-08-07 03:52:26 +0000191
vlm5ea810e2005-07-03 05:32:40 +0000192 recognize("20040125093007Z", 1075023007, 0);
193 recognize("20040125093007+00", 1075023007, 0);
194 recognize("20040125093007.01+0000", 1075023007, 0);
195 recognize("20040125093007,1+0000", 1075023007, 0);
196 recognize("20040125093007-0800", 1075051807, 0);
vlmfa67ddc2004-06-03 03:38:44 +0000197
vlm5ea810e2005-07-03 05:32:40 +0000198 recognize("19920722132100.123000123Z", 711811260, 0);
199 recognize("19920722132100.1230000123Z", 711811260, 0);
200 recognize("19920722132100.12300000123Z", 711811260, 0);
201
202 encode(1075023007, "20040125093007Z", 1);
vlm0f8e5c12004-08-07 04:16:42 +0000203
vlmfa67ddc2004-06-03 03:38:44 +0000204 if(ac > 1) {
205 /* These will be valid only inside PST time zone */
vlm5ea810e2005-07-03 05:32:40 +0000206 recognize("20040125093007", 1075051807, 0);
207 recognize("200401250930", 1075051800, 0);
208 recognize("20040125093000,01", 1075051800, 0);
209 recognize("20040125093000,1234", 1075051800, 0);
vlmfa67ddc2004-06-03 03:38:44 +0000210
vlm5ea810e2005-07-03 05:32:40 +0000211 encode(1075023007, "20040125013007-0800", 0);
212 recode("20050702123312", "20050702193312Z");
vlm0f8e5c12004-08-07 04:16:42 +0000213 }
vlm81057a82004-08-07 03:52:26 +0000214
vlm5ea810e2005-07-03 05:32:40 +0000215 recode("20050702123312Z", "20050702123312Z");
216 recode("20050702123312+01", "20050702113312Z");
217 recode("20050702123312,0+01", "20050702113312Z");
218 recode("20050702123312,1+01", "20050702113312.1Z");
219 recode("20050702123312.01+01", "20050702113312.01Z");
220 recode("20050702123312.00+01", "20050702113312Z");
221 recode("20050702123312.30+01", "20050702113312.3Z");
222 recode("20050702123312,30000+01", "20050702113312.3Z");
223 recode("20050702123312,300000000+01", "20050702113312.3Z");
224 recode("20050702123312.123456+01", "20050702113312.123456Z");
225 recode("20050702123312.1234567+01", "20050702113312.123456Z");
226 recode("20050702123312.12345678+01", "20050702113312.123456Z");
227 recode("20050702123312.123456789+01", "20050702113312.123456Z");
228 recode("20050702123312.000001+01", "20050702113312.000001Z");
229 recode("20050702123312.0000001Z", "20050702123312Z");
230 recode("20050702123312.0080010+1056", "20050702013712.008001Z");
231
vlmfa67ddc2004-06-03 03:38:44 +0000232 return 0;
233}
vlm81057a82004-08-07 03:52:26 +0000234
235/*
236 * Dummy function.
237 */
238
vlm39ba4c42004-09-22 16:06:28 +0000239asn_enc_rval_t
vlmef6355b2004-09-29 13:26:15 +0000240OCTET_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 +0000241 asn_enc_rval_t erval;
vlmd3da8512004-08-19 13:26:54 +0000242
243 (void)td;
244 (void)ptr;
245 (void)tag_mode;
246 (void)tag;
247 (void)cb;
248 (void)app_key;
249
vlm81057a82004-08-07 03:52:26 +0000250 return erval;
251}
vlm39ba4c42004-09-22 16:06:28 +0000252
253asn_enc_rval_t
vlm9de248e2004-10-20 15:50:55 +0000254OCTET_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 +0000255 asn_enc_rval_t erval;
256
257 (void)td;
258 (void)ptr;
259 (void)ilevel;
260 (void)flags;
261 (void)cb;
262 (void)app_key;
263
264 return erval;
265}