aboutsummaryrefslogtreecommitdiffstats
path: root/skeletons/tests/check-GeneralizedTime.c
blob: b56fa0b9ebcc60c1fdc0373305ce8431d06ab32b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#define	__ASN_INTERNAL_TEST_MODE__
#include <GeneralizedTime.c>
#include <constraints.c>
#include <math.h>	/* for pow(3) */

static void
recognize(char *time_str, time_t expect, int as_gmt) {
	GeneralizedTime_t gt;
	struct tm tm;
	time_t tloc;
	int fv, fp;

	gt.buf = (uint8_t *)time_str;
	gt.size = strlen(time_str);

	tloc = asn_GT2time_frac(&gt, &fv, &fp, &tm, as_gmt);
	printf("%s: [%s] -> %ld == %ld\n",
		as_gmt?"GMT":"ofs", time_str, (long)tloc, (long)expect);

	if(tloc != -1) {
		printf("\t%04d-%02d-%02dT%02d:%02d:%02d.%f(%d/%d)%+03ld%02ld\n",
		tm.tm_year + 1900,
		tm.tm_mon + 1,
		tm.tm_mday,
		tm.tm_hour,
		tm.tm_min,
		tm.tm_sec,
		(double)fv * pow(0.1, fp), fv, fp,
		(GMTOFF(tm) / 3600),
		labs(GMTOFF(tm) % 3600)
		);
	}
	assert(tloc == expect);

#ifdef	HAVE_TM_GMTOFF
	assert(tloc == -1 || as_gmt == 0 || GMTOFF(tm) == 0);
#endif

	if(!as_gmt) recognize(time_str, expect, 1);
}

static void
encode(time_t tloc, const char *expect, int force_gmt) {
	GeneralizedTime_t *gt;
	struct tm tm, *tmp;

	tmp = localtime_r(&tloc, &tm);
	assert(tmp);

	gt = asn_time2GT(0, &tm, force_gmt);
	if(gt) {
		assert(expect);
		printf("[%s] vs [%s] (%d)\n",
			gt->buf, expect, force_gmt);
		assert(gt->size == (int)strlen((char *)gt->buf));
		assert(!strcmp((char *)gt->buf, expect));
	} else {
		assert(!expect);
	}
}

#define	RECODE(foo, bar)	recode(__LINE__, foo, bar)

static void
recode(int lineno, char *time_str, const char *expect) {
	int frac_value, frac_digits;
	GeneralizedTime_t gt;
	struct tm tm;
	time_t tloc;
	char *tz;

	gt.buf = (uint8_t *)time_str;
	gt.size = strlen(time_str);

	tloc = asn_GT2time_frac(&gt, &frac_value, &frac_digits, &tm, 1);
	assert(tloc != -1);

	gt.buf = 0;
	asn_time2GT_frac(&gt, &tm, frac_value, frac_digits, 1);
	assert(gt.buf);

	tz = getenv("TZ");
	printf("%d: [%s] (%ld) => [%s] == [%s] (%d, %d) (TZ=%s)\n",
		lineno, time_str, (long)tloc, gt.buf,
		expect, frac_value, frac_digits,
		tz ? tz : "");

	assert(strcmp((char *)gt.buf, expect) == 0);
	FREEMEM(gt.buf);
}

static void
check_fractions() {
	GeneralizedTime_t *gt = 0;
	struct tm tm;
	int fv, fd;
	time_t tloc;

	memset(&tm, 0, sizeof tm);
	tm.tm_year = 70;
	tm.tm_mday = 1;

	gt = asn_time2GT_frac(gt, &tm, -1, -1, 1);
	assert(gt);
	printf("[%s]\n", gt->buf);
	assert(strcmp((char *)gt->buf, "19700101000000Z") == 0);

	gt = asn_time2GT_frac(gt, &tm, 0, 0, 1);
	assert(gt);
	printf("[%s]\n", gt->buf);
	assert(strcmp((char *)gt->buf, "19700101000000Z") == 0);

	gt = asn_time2GT_frac(gt, &tm, 0, -1, 1);
	assert(gt);
	printf("[%s]\n", gt->buf);
	assert(strcmp((char *)gt->buf, "19700101000000Z") == 0);

	gt = asn_time2GT_frac(gt, &tm, -1, 0, 1);
	assert(gt);
	printf("[%s]\n", gt->buf);
	assert(strcmp((char *)gt->buf, "19700101000000Z") == 0);

	gt = asn_time2GT_frac(gt, &tm, 10, 0, 1);
	assert(gt);
	printf("[%s]\n", gt->buf);
	assert(strcmp((char *)gt->buf, "19700101000000Z") == 0);

	/* Normalization should happen prior to calling the _frac() */
	gt = asn_time2GT_frac(gt, &tm, 55, 2, 1);
	assert(gt);
	printf("[%s]\n", gt->buf);
	assert(strcmp((char *)gt->buf, "19700101000000.55Z") == 0);

	gt = asn_time2GT_frac(gt, &tm, 5, 2, 1);
	assert(gt);
	printf("[%s]\n", gt->buf);
	assert(strcmp((char *)gt->buf, "19700101000000.05Z") == 0);

	/* Normalization should happen prior calling the _frac() */
	gt = asn_time2GT_frac(gt, &tm, 900, 2, 1);
	assert(gt);
	printf("[%s]\n", gt->buf);
	assert(strcmp((char *)gt->buf, "19700101000000Z") == 0);

	gt = asn_time2GT_frac(gt, &tm, 90, 2, 1);
	assert(gt);
	printf("[%s]\n", gt->buf);
	assert(strcmp((char *)gt->buf, "19700101000000.9Z") == 0);

	tloc = asn_GT2time_prec(gt, &fv, 0, 0, 1);
	assert(tloc == 0);
	assert(fv == 0);

	tloc = asn_GT2time_prec(gt, &fv, 1, 0, 1);
	assert(tloc == 0);
	assert(fv == 9);

	tloc = asn_GT2time_prec(gt, &fv, 2, 0, 1);
	assert(tloc == 0);
	assert(fv == 90);

	tloc = asn_GT2time_frac(gt, &fv, &fd, 0, 1);
	assert(tloc == 0);
	assert(fv == 9);
	assert(fd == 1);

	gt->buf[gt->size-1] = '0';
	gt->buf[gt->size++] = 'Z';
	gt->buf[gt->size] = '\0';

	tloc = asn_GT2time_frac(gt, &fv, &fd, 0, 1);
	assert(tloc == 0);
	assert(fd == 2);
	assert(fv == 90);

	tloc = asn_GT2time_prec(gt, &fv, 1, 0, 1);
	assert(tloc == 0);
	assert(fv == 9);

	tloc = asn_GT2time_prec(gt, &fv, 100, 0, 1);
	assert(tloc == 0);
	assert(fv == 0);

	FREEMEM(gt->buf);
	FREEMEM(gt);
}

int
main(int ac, char **av) {
	char *tz = getenv("TZ");

	(void)av;

	printf("TZ = [%s]\n", tz ? tz : "");

	check_fractions();

	recognize("200401250", -1, 0);
	recognize("2004012509300", -1, 0);
	recognize("20040125093000-", -1, 0);
	recognize("20040125093007-0", -1, 0);
	recognize("20040125093007-080", -1, 0);
	recognize("200401250930.01Z", -1, 0);

	/* These six are from X.690:11.7.5 */
	recognize("19920520240000Z", -1, 0);  /* midnight represented incorrectly */
	recognize("19920622123421.0Z", 709216461, 0);  /* spurious trailing zeros */
	recognize("19920722132100.30Z", 711811260, 0); /* spurious trailing zeros */
	recognize("19920521000000Z", 706406400, 0);
	recognize("19920622123421Z", 709216461, 0);
	recognize("19920722132100.3Z", 711811260, 0);

	recognize("20040125093007Z", 1075023007, 0);
	recognize("20040125093007+00", 1075023007, 0);
	recognize("20040125093007.01+0000", 1075023007, 0);
	recognize("20040125093007,1+0000", 1075023007, 0);
	recognize("20040125093007-0800", 1075051807, 0);

	recognize("19920722132100.123000123Z", 711811260, 0);
	recognize("19920722132100.1230000123Z", 711811260, 0);
	recognize("19920722132100.12300000123Z", 711811260, 0);

	encode(1075023007, "20040125093007Z", 1);

	if(ac > 1) {
		/* These will be valid only inside PST time zone */
		recognize("20040125093007", 1075051807, 0);
		recognize("200401250930", 1075051800, 0);
		recognize("20040125093000,01", 1075051800, 0);
		recognize("20040125093000,1234", 1075051800, 0);

		encode(1075023007, "20040125013007-0800", 0);
		RECODE("20050702123312", "20050702193312Z");
	}

#if defined(sun) || defined(__sun) || defined(_sun_) || defined(__solaris__)
	printf("Solaris does not have a decent timegm() function.\n");
#else	/* !solaris */
	RECODE("20050702123312Z", "20050702123312Z");
	RECODE("20050702123312+01", "20050702113312Z");
	RECODE("20050702123312,0+01", "20050702113312Z");
	RECODE("20050702123312,1+01", "20050702113312.1Z");
	RECODE("20050702123312.01+01", "20050702113312.01Z");
	RECODE("20050702123312.00+01", "20050702113312Z");
	RECODE("20050702123312.30+01", "20050702113312.3Z");
	RECODE("20050702123312,30000+01", "20050702113312.3Z");
	RECODE("20050702123312,300000000+01", "20050702113312.3Z");
	RECODE("20050702123312.123456+01", "20050702113312.123456Z");
	RECODE("20050702123312.1234567+01", "20050702113312.123456Z");
	RECODE("20050702123312.12345678+01", "20050702113312.123456Z");
	RECODE("20050702123312.123456789+01", "20050702113312.123456Z");
	RECODE("20050702123312.2000000000+01", "20050702113312.2Z");
	RECODE("20050702123312.3000000000+01", "20050702113312.3Z");
	RECODE("20050702123312.4000000000+01", "20050702113312.4Z");
	RECODE("20050702123312.5000000000+01", "20050702113312.5Z");
	RECODE("20050702123312.5000000001+01", "20050702113312.5Z");
	RECODE("20050702123312.5000010001+01", "20050702113312.500001Z");
	RECODE("20050702123312.5000001001+01", "20050702113312.5Z");
	RECODE("20050702123312.000001+01", "20050702113312.000001Z");
	RECODE("20050702123312.0000001Z", "20050702123312Z");
	RECODE("20050702123312.0080010+1056", "20050702013712.008001Z");
#endif

	return 0;
}

/*
 * Dummy function.
 */

asn_enc_rval_t
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) {
	asn_enc_rval_t erval;

	(void)td;
	(void)ptr;
	(void)tag_mode;
	(void)tag;
	(void)cb;
	(void)app_key;

	memset(&erval, 0, sizeof(erval));
	return erval;
}

asn_enc_rval_t
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) {
	asn_enc_rval_t erval;

	(void)td;
	(void)ptr;
	(void)ilevel;
	(void)flags;
	(void)cb;
	(void)app_key;

	memset(&erval, 0, sizeof(erval));
	return erval;
}