Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 1 | #define __ASN_INTERNAL_TEST_MODE__ |
Lev Walkin | 4421266 | 2004-08-19 13:26:54 +0000 | [diff] [blame] | 2 | #include <GeneralizedTime.c> |
| 3 | #include <constraints.c> |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 4 | |
| 5 | static void |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 6 | recognize(char *time_str, time_t expect, int as_gmt) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 7 | GeneralizedTime_t gt; |
| 8 | struct tm tm; |
| 9 | time_t tloc; |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 10 | long fv, fb; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 11 | |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 12 | gt.buf = (uint8_t *)time_str; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 13 | gt.size = strlen(time_str); |
| 14 | |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 15 | tloc = asn_GT2time_frac(>, &fv, &fb, &tm, as_gmt); |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 16 | printf("%s: [%s] -> %ld == %ld\n", |
| 17 | as_gmt?"GMT":"ofs", time_str, (long)tloc, (long)expect); |
Lev Walkin | a460ba3 | 2004-10-20 15:40:04 +0000 | [diff] [blame] | 18 | |
| 19 | if(tloc != -1) { |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 20 | printf("\t%04d-%02d-%02dT%02d:%02d:%02d(.%ld/%ld)%+03ld%02ld\n", |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 21 | 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, |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 27 | fv, fb, |
Lev Walkin | a460ba3 | 2004-10-20 15:40:04 +0000 | [diff] [blame] | 28 | (GMTOFF(tm) / 3600), |
| 29 | labs(GMTOFF(tm) % 3600) |
| 30 | ); |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 31 | assert(fb < 100 || (fb % 100) == 0); |
Lev Walkin | a460ba3 | 2004-10-20 15:40:04 +0000 | [diff] [blame] | 32 | } |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 33 | assert(tloc == expect); |
| 34 | |
Lev Walkin | a460ba3 | 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 |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 38 | |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 39 | if(!as_gmt) recognize(time_str, expect, 1); |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | static void |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 43 | encode(time_t tloc, const char *expect, int force_gmt) { |
Lev Walkin | 9900636 | 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); |
Lev Walkin | 535612a | 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)); |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 57 | } else { |
| 58 | assert(!expect); |
| 59 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 62 | |
| 63 | static void |
| 64 | recode(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(>, &frac_value, &frac_base, &tm, 1); |
| 74 | assert(tloc != -1); |
| 75 | |
| 76 | gt.buf = 0; |
| 77 | asn_time2GT_frac(>, &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 | |
Lev Walkin | 4b4c146 | 2005-07-04 01:44:01 +0000 | [diff] [blame] | 86 | static void |
| 87 | check_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); |
Lev Walkin | 88e0a77 | 2005-07-04 02:29:36 +0000 | [diff] [blame] | 129 | 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); |
Lev Walkin | 4b4c146 | 2005-07-04 01:44:01 +0000 | [diff] [blame] | 134 | 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 | |
Lev Walkin | 10dd97c | 2005-07-04 02:20:26 +0000 | [diff] [blame] | 141 | 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 | |
Lev Walkin | 88e0a77 | 2005-07-04 02:29:36 +0000 | [diff] [blame] | 146 | 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 | |
Lev Walkin | 10dd97c | 2005-07-04 02:20:26 +0000 | [diff] [blame] | 151 | 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); |
Lev Walkin | 88e0a77 | 2005-07-04 02:29:36 +0000 | [diff] [blame] | 159 | 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); |
Lev Walkin | 10dd97c | 2005-07-04 02:20:26 +0000 | [diff] [blame] | 165 | |
Lev Walkin | 4b4c146 | 2005-07-04 01:44:01 +0000 | [diff] [blame] | 166 | FREEMEM(gt->buf); |
| 167 | FREEMEM(gt); |
| 168 | } |
| 169 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 170 | int |
| 171 | main(int ac, char **av) { |
| 172 | |
Lev Walkin | 4421266 | 2004-08-19 13:26:54 +0000 | [diff] [blame] | 173 | (void)av; |
| 174 | |
Lev Walkin | 4b4c146 | 2005-07-04 01:44:01 +0000 | [diff] [blame] | 175 | check_fractions(); |
| 176 | |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 177 | 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); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 183 | |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 184 | /* These six are from X.690:11.7.5 */ |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 185 | 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); |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 191 | |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 192 | 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); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 197 | |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 198 | recognize("19920722132100.123000123Z", 711811260, 0); |
| 199 | recognize("19920722132100.1230000123Z", 711811260, 0); |
| 200 | recognize("19920722132100.12300000123Z", 711811260, 0); |
| 201 | |
| 202 | encode(1075023007, "20040125093007Z", 1); |
Lev Walkin | 91d40d4 | 2004-08-07 04:16:42 +0000 | [diff] [blame] | 203 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 204 | if(ac > 1) { |
| 205 | /* These will be valid only inside PST time zone */ |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 206 | recognize("20040125093007", 1075051807, 0); |
| 207 | recognize("200401250930", 1075051800, 0); |
| 208 | recognize("20040125093000,01", 1075051800, 0); |
| 209 | recognize("20040125093000,1234", 1075051800, 0); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 210 | |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 211 | encode(1075023007, "20040125013007-0800", 0); |
| 212 | recode("20050702123312", "20050702193312Z"); |
Lev Walkin | 91d40d4 | 2004-08-07 04:16:42 +0000 | [diff] [blame] | 213 | } |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 214 | |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 215 | 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 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 232 | return 0; |
| 233 | } |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 234 | |
| 235 | /* |
| 236 | * Dummy function. |
| 237 | */ |
| 238 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 239 | asn_enc_rval_t |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 240 | 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) { |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 241 | asn_enc_rval_t erval; |
Lev Walkin | 4421266 | 2004-08-19 13:26:54 +0000 | [diff] [blame] | 242 | |
| 243 | (void)td; |
| 244 | (void)ptr; |
| 245 | (void)tag_mode; |
| 246 | (void)tag; |
| 247 | (void)cb; |
| 248 | (void)app_key; |
| 249 | |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 250 | return erval; |
| 251 | } |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 252 | |
| 253 | asn_enc_rval_t |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 254 | 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) { |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 255 | 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 | } |