blob: c39904e7e7860d2ff6021d1fd57a2769a5226fbc [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);
129 assert(strcmp((char *)gt->buf, "19700101000000.5Z") == 0);
130
131 gt = asn_time2GT_frac(gt, &tm, -10, 20, 1);
132 assert(gt);
133 printf("[%s]\n", gt->buf);
134 assert(strcmp((char *)gt->buf, "19700101000000Z") == 0);
135
136 FREEMEM(gt->buf);
137 FREEMEM(gt);
138}
139
vlmfa67ddc2004-06-03 03:38:44 +0000140int
141main(int ac, char **av) {
142
vlmd3da8512004-08-19 13:26:54 +0000143 (void)av;
144
vlm9d531932005-07-04 01:44:01 +0000145 check_fractions();
146
vlm5ea810e2005-07-03 05:32:40 +0000147 recognize("200401250", -1, 0);
148 recognize("2004012509300", -1, 0);
149 recognize("20040125093000-", -1, 0);
150 recognize("20040125093007-0", -1, 0);
151 recognize("20040125093007-080", -1, 0);
152 recognize("200401250930.01Z", -1, 0);
vlmfa67ddc2004-06-03 03:38:44 +0000153
vlm81057a82004-08-07 03:52:26 +0000154 /* These six are from X.690:11.7.5 */
vlm5ea810e2005-07-03 05:32:40 +0000155 recognize("19920520240000Z", -1, 0); /* midnight represented incorrectly */
156 recognize("19920622123421.0Z", 709216461, 0); /* spurious trailing zeros */
157 recognize("19920722132100.30Z", 711811260, 0); /* spurious trailing zeros */
158 recognize("19920521000000Z", 706406400, 0);
159 recognize("19920622123421Z", 709216461, 0);
160 recognize("19920722132100.3Z", 711811260, 0);
vlm81057a82004-08-07 03:52:26 +0000161
vlm5ea810e2005-07-03 05:32:40 +0000162 recognize("20040125093007Z", 1075023007, 0);
163 recognize("20040125093007+00", 1075023007, 0);
164 recognize("20040125093007.01+0000", 1075023007, 0);
165 recognize("20040125093007,1+0000", 1075023007, 0);
166 recognize("20040125093007-0800", 1075051807, 0);
vlmfa67ddc2004-06-03 03:38:44 +0000167
vlm5ea810e2005-07-03 05:32:40 +0000168 recognize("19920722132100.123000123Z", 711811260, 0);
169 recognize("19920722132100.1230000123Z", 711811260, 0);
170 recognize("19920722132100.12300000123Z", 711811260, 0);
171
172 encode(1075023007, "20040125093007Z", 1);
vlm0f8e5c12004-08-07 04:16:42 +0000173
vlmfa67ddc2004-06-03 03:38:44 +0000174 if(ac > 1) {
175 /* These will be valid only inside PST time zone */
vlm5ea810e2005-07-03 05:32:40 +0000176 recognize("20040125093007", 1075051807, 0);
177 recognize("200401250930", 1075051800, 0);
178 recognize("20040125093000,01", 1075051800, 0);
179 recognize("20040125093000,1234", 1075051800, 0);
vlmfa67ddc2004-06-03 03:38:44 +0000180
vlm5ea810e2005-07-03 05:32:40 +0000181 encode(1075023007, "20040125013007-0800", 0);
182 recode("20050702123312", "20050702193312Z");
vlm0f8e5c12004-08-07 04:16:42 +0000183 }
vlm81057a82004-08-07 03:52:26 +0000184
vlm5ea810e2005-07-03 05:32:40 +0000185 recode("20050702123312Z", "20050702123312Z");
186 recode("20050702123312+01", "20050702113312Z");
187 recode("20050702123312,0+01", "20050702113312Z");
188 recode("20050702123312,1+01", "20050702113312.1Z");
189 recode("20050702123312.01+01", "20050702113312.01Z");
190 recode("20050702123312.00+01", "20050702113312Z");
191 recode("20050702123312.30+01", "20050702113312.3Z");
192 recode("20050702123312,30000+01", "20050702113312.3Z");
193 recode("20050702123312,300000000+01", "20050702113312.3Z");
194 recode("20050702123312.123456+01", "20050702113312.123456Z");
195 recode("20050702123312.1234567+01", "20050702113312.123456Z");
196 recode("20050702123312.12345678+01", "20050702113312.123456Z");
197 recode("20050702123312.123456789+01", "20050702113312.123456Z");
198 recode("20050702123312.000001+01", "20050702113312.000001Z");
199 recode("20050702123312.0000001Z", "20050702123312Z");
200 recode("20050702123312.0080010+1056", "20050702013712.008001Z");
201
vlmfa67ddc2004-06-03 03:38:44 +0000202 return 0;
203}
vlm81057a82004-08-07 03:52:26 +0000204
205/*
206 * Dummy function.
207 */
208
vlm39ba4c42004-09-22 16:06:28 +0000209asn_enc_rval_t
vlmef6355b2004-09-29 13:26:15 +0000210OCTET_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 +0000211 asn_enc_rval_t erval;
vlmd3da8512004-08-19 13:26:54 +0000212
213 (void)td;
214 (void)ptr;
215 (void)tag_mode;
216 (void)tag;
217 (void)cb;
218 (void)app_key;
219
vlm81057a82004-08-07 03:52:26 +0000220 return erval;
221}
vlm39ba4c42004-09-22 16:06:28 +0000222
223asn_enc_rval_t
vlm9de248e2004-10-20 15:50:55 +0000224OCTET_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 +0000225 asn_enc_rval_t erval;
226
227 (void)td;
228 (void)ptr;
229 (void)ilevel;
230 (void)flags;
231 (void)cb;
232 (void)app_key;
233
234 return erval;
235}