Commit dd01a5e5 authored by Lev Walkin's avatar Lev Walkin

OER support for UTCTime and GeneralizedTime, also fuzz-testing

parent 2ed11835
...@@ -176,7 +176,7 @@ static asn_per_constraints_t asn_DEF_GeneralizedTime_per_constraints = { ...@@ -176,7 +176,7 @@ static asn_per_constraints_t asn_DEF_GeneralizedTime_per_constraints = {
asn_TYPE_operation_t asn_OP_GeneralizedTime = { asn_TYPE_operation_t asn_OP_GeneralizedTime = {
OCTET_STRING_free, OCTET_STRING_free,
GeneralizedTime_print, GeneralizedTime_print,
OCTET_STRING_compare, /* Does not normalize time zones! */ GeneralizedTime_compare,
OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */ OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
GeneralizedTime_encode_der, GeneralizedTime_encode_der,
OCTET_STRING_decode_xer_utf8, OCTET_STRING_decode_xer_utf8,
...@@ -185,8 +185,8 @@ asn_TYPE_operation_t asn_OP_GeneralizedTime = { ...@@ -185,8 +185,8 @@ asn_TYPE_operation_t asn_OP_GeneralizedTime = {
0, 0,
0, 0,
#else #else
0, OCTET_STRING_decode_oer,
0, OCTET_STRING_encode_oer,
#endif /* ASN_DISABLE_OER_SUPPORT */ #endif /* ASN_DISABLE_OER_SUPPORT */
#ifdef ASN_DISABLE_PER_SUPPORT #ifdef ASN_DISABLE_PER_SUPPORT
0, 0,
...@@ -739,7 +739,7 @@ GeneralizedTime_random_fill(const asn_TYPE_descriptor_t *td, void **sptr, ...@@ -739,7 +739,7 @@ GeneralizedTime_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
(void)constraints; (void)constraints;
if(max_length < sizeof("yyyymmddhhmmss")) { if(max_length < sizeof("yyyymmddhhmmss") && !*sptr) {
return result_skipped; return result_skipped;
} }
...@@ -754,3 +754,55 @@ GeneralizedTime_random_fill(const asn_TYPE_descriptor_t *td, void **sptr, ...@@ -754,3 +754,55 @@ GeneralizedTime_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
return result_ok; return result_ok;
} }
int
GeneralizedTime_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
const void *bptr) {
const GeneralizedTime_t *a = aptr;
const GeneralizedTime_t *b = bptr;
(void)td;
if(a && b) {
int afrac_value, afrac_digits;
int bfrac_value, bfrac_digits;
time_t at = asn_GT2time_frac(a, &afrac_value, &afrac_digits, 0, 0);
time_t bt = asn_GT2time_frac(b, &bfrac_value, &bfrac_digits, 0, 0);
if(at < bt) {
return -1;
} else if(at > bt) {
return 1;
} else if(afrac_digits == bfrac_digits) {
if(afrac_value == bfrac_value) {
return 0;
}
if(afrac_value < bfrac_value) {
return -1;
} else {
return 1;
}
} else if(afrac_digits == 0) {
return -1;
} else if(bfrac_digits == 0) {
return 1;
} else {
double afrac = (double)afrac_value / afrac_digits;
double bfrac = (double)bfrac_value / bfrac_digits;
if(afrac < bfrac) {
return -1;
} else if(afrac > bfrac) {
return 1;
} else {
return 0;
}
}
} else if(!a && !b) {
return 0;
} else if(!a) {
return -1;
} else {
return 1;
}
}
...@@ -17,13 +17,13 @@ extern asn_TYPE_descriptor_t asn_DEF_GeneralizedTime; ...@@ -17,13 +17,13 @@ extern asn_TYPE_descriptor_t asn_DEF_GeneralizedTime;
extern asn_TYPE_operation_t asn_OP_GeneralizedTime; extern asn_TYPE_operation_t asn_OP_GeneralizedTime;
asn_struct_print_f GeneralizedTime_print; asn_struct_print_f GeneralizedTime_print;
asn_struct_compare_f GeneralizedTime_compare;
asn_constr_check_f GeneralizedTime_constraint; asn_constr_check_f GeneralizedTime_constraint;
der_type_encoder_f GeneralizedTime_encode_der; der_type_encoder_f GeneralizedTime_encode_der;
xer_type_encoder_f GeneralizedTime_encode_xer; xer_type_encoder_f GeneralizedTime_encode_xer;
asn_random_fill_f GeneralizedTime_random_fill; asn_random_fill_f GeneralizedTime_random_fill;
#define GeneralizedTime_free OCTET_STRING_free #define GeneralizedTime_free OCTET_STRING_free
#define GeneralizedTime_compare OCTET_STRING_compare
#define GeneralizedTime_decode_ber OCTET_STRING_decode_ber #define GeneralizedTime_decode_ber OCTET_STRING_decode_ber
#define GeneralizedTime_decode_xer OCTET_STRING_decode_xer_utf8 #define GeneralizedTime_decode_xer OCTET_STRING_decode_xer_utf8
#define GeneralizedTime_decode_uper OCTET_STRING_decode_uper #define GeneralizedTime_decode_uper OCTET_STRING_decode_uper
......
...@@ -31,7 +31,7 @@ static asn_per_constraints_t asn_DEF_UTCTime_constraints = { ...@@ -31,7 +31,7 @@ static asn_per_constraints_t asn_DEF_UTCTime_constraints = {
asn_TYPE_operation_t asn_OP_UTCTime = { asn_TYPE_operation_t asn_OP_UTCTime = {
OCTET_STRING_free, OCTET_STRING_free,
UTCTime_print, UTCTime_print,
OCTET_STRING_compare, /* Does not deal with time zones. */ UTCTime_compare,
OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */ OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
OCTET_STRING_encode_der, /* Implemented in terms of OCTET STRING */ OCTET_STRING_encode_der, /* Implemented in terms of OCTET STRING */
OCTET_STRING_decode_xer_utf8, OCTET_STRING_decode_xer_utf8,
...@@ -40,8 +40,8 @@ asn_TYPE_operation_t asn_OP_UTCTime = { ...@@ -40,8 +40,8 @@ asn_TYPE_operation_t asn_OP_UTCTime = {
0, 0,
0, 0,
#else #else
0, OCTET_STRING_decode_oer,
0, OCTET_STRING_encode_oer,
#endif /* ASN_DISABLE_OER_SUPPORT */ #endif /* ASN_DISABLE_OER_SUPPORT */
#ifdef ASN_DISABLE_PER_SUPPORT #ifdef ASN_DISABLE_PER_SUPPORT
0, 0,
...@@ -109,7 +109,7 @@ UTCTime_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, ...@@ -109,7 +109,7 @@ UTCTime_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
ASN__ENCODE_FAILED; ASN__ENCODE_FAILED;
/* Fractions are not allowed in UTCTime */ /* Fractions are not allowed in UTCTime */
ut = asn_time2GT(0, 0, 1); ut = asn_time2UT(0, &tm, 1);
if(!ut) ASN__ENCODE_FAILED; if(!ut) ASN__ENCODE_FAILED;
rv = OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags, rv = OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags,
...@@ -210,7 +210,7 @@ UTCTime_random_fill(const asn_TYPE_descriptor_t *td, void **sptr, ...@@ -210,7 +210,7 @@ UTCTime_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
(void)constraints; (void)constraints;
if(max_length < sizeof("yymmddhhmmss")) { if(max_length < sizeof("yymmddhhmmss") && !*sptr) {
return result_skipped; return result_skipped;
} }
...@@ -225,3 +225,31 @@ UTCTime_random_fill(const asn_TYPE_descriptor_t *td, void **sptr, ...@@ -225,3 +225,31 @@ UTCTime_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
return result_ok; return result_ok;
} }
int
UTCTime_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
const void *bptr) {
const GeneralizedTime_t *a = aptr;
const GeneralizedTime_t *b = bptr;
(void)td;
if(a && b) {
time_t at = asn_UT2time(a, 0, 0);
time_t bt = asn_UT2time(b, 0, 0);
if(at < bt) {
return -1;
} else if(at > bt) {
return 1;
} else {
return 0;
}
} else if(!a && !b) {
return 0;
} else if(!a) {
return -1;
} else {
return 1;
}
}
...@@ -17,12 +17,12 @@ extern asn_TYPE_descriptor_t asn_DEF_UTCTime; ...@@ -17,12 +17,12 @@ extern asn_TYPE_descriptor_t asn_DEF_UTCTime;
extern asn_TYPE_operation_t asn_OP_UTCTime; extern asn_TYPE_operation_t asn_OP_UTCTime;
asn_struct_print_f UTCTime_print; asn_struct_print_f UTCTime_print;
asn_struct_compare_f UTCTime_compare;
asn_constr_check_f UTCTime_constraint; asn_constr_check_f UTCTime_constraint;
xer_type_encoder_f UTCTime_encode_xer; xer_type_encoder_f UTCTime_encode_xer;
asn_random_fill_f UTCTime_random_fill; asn_random_fill_f UTCTime_random_fill;
#define UTCTime_free OCTET_STRING_free #define UTCTime_free OCTET_STRING_free
#define UTCTime_compare OCTET_STRING_compare
#define UTCTime_decode_ber OCTET_STRING_decode_ber #define UTCTime_decode_ber OCTET_STRING_decode_ber
#define UTCTime_encode_der OCTET_STRING_encode_der #define UTCTime_encode_der OCTET_STRING_encode_der
#define UTCTime_decode_xer OCTET_STRING_decode_xer_utf8 #define UTCTime_decode_xer OCTET_STRING_decode_xer_utf8
......
...@@ -34,6 +34,8 @@ TESTS += bundles/07-VisibleString-bundle.txt ...@@ -34,6 +34,8 @@ TESTS += bundles/07-VisibleString-bundle.txt
TESTS += bundles/08-OBJECT-IDENTIFIER-bundle.txt TESTS += bundles/08-OBJECT-IDENTIFIER-bundle.txt
TESTS += bundles/09-RELATIVE-OID-bundle.txt TESTS += bundles/09-RELATIVE-OID-bundle.txt
TESTS += bundles/10-UTF8String-bundle.txt TESTS += bundles/10-UTF8String-bundle.txt
TESTS += bundles/11-UTCTime-bundle.txt
TESTS += bundles/12-GeneralizedTime-bundle.txt
EXTRA_DIST = \ EXTRA_DIST = \
random-test-driver.c \ random-test-driver.c \
......
...@@ -188,6 +188,21 @@ check_fractions() { ...@@ -188,6 +188,21 @@ check_fractions() {
FREEMEM(gt); FREEMEM(gt);
} }
static void
compare(int lineno, int cmp_control, const char *astr, const char *bstr) {
GeneralizedTime_t a = {(uint8_t *)strdup(astr), strlen(astr)};
GeneralizedTime_t b = {(uint8_t *)strdup(bstr), strlen(bstr)};
int cmp_result = asn_DEF_GeneralizedTime.op->compare_struct(
&asn_DEF_GeneralizedTime, &a, &b);
if(cmp_result != cmp_control) {
fprintf(stderr, "%03d: [%s] == [%s] = %d, expected %d\n", lineno, astr,
bstr, cmp_result, cmp_control);
assert(cmp_result == cmp_control);
}
ASN_STRUCT_RESET(asn_DEF_GeneralizedTime, &a);
ASN_STRUCT_RESET(asn_DEF_GeneralizedTime, &b);
}
int int
main(int ac, char **av) { main(int ac, char **av) {
char *tz = getenv("TZ"); char *tz = getenv("TZ");
...@@ -264,6 +279,16 @@ main(int ac, char **av) { ...@@ -264,6 +279,16 @@ main(int ac, char **av) {
RECODE("20050702123312.0080010+1056", "20050702013712.008001Z"); RECODE("20050702123312.0080010+1056", "20050702013712.008001Z");
#endif #endif
compare(__LINE__, 0, "20040125093007", "20040125093007");
compare(__LINE__, 0, "20040125093007-0000", "20040125093007Z");
compare(__LINE__, 1, "20040125093008", "20040125093007");
compare(__LINE__, 1, "20040125093008-0000", "20040125093007-0000");
compare(__LINE__, 0, "20040125093008-0000", "20040125093008-0000");
compare(__LINE__, 1, "20040125093008-0000", "20040125093007Z");
compare(__LINE__, 0, "20040125093007-0000", "20040125093007+0000");
compare(__LINE__, 1, "20040125093007-0030", "20040125093007Z");
compare(__LINE__, -1, "20040125093007+0030", "20040125093007Z");
return 0; return 0;
} }
...@@ -33,6 +33,21 @@ check(char *time_str, time_t sample, int as_gmt) { ...@@ -33,6 +33,21 @@ check(char *time_str, time_t sample, int as_gmt) {
if(as_gmt) check(time_str, sample, as_gmt); if(as_gmt) check(time_str, sample, as_gmt);
} }
static void
compare(int lineno, int cmp_control, const char *astr, const char *bstr) {
UTCTime_t a = {(uint8_t *)strdup(astr), strlen(astr)};
UTCTime_t b = {(uint8_t *)strdup(bstr), strlen(bstr)};
int cmp_result =
asn_DEF_UTCTime.op->compare_struct(&asn_DEF_UTCTime, &a, &b);
if(cmp_result != cmp_control) {
fprintf(stderr, "%03d: [%s] == [%s] = %d, expected %d\n", lineno, astr,
bstr, cmp_result, cmp_control);
assert(cmp_result == cmp_control);
}
ASN_STRUCT_RESET(asn_DEF_UTCTime, &a);
ASN_STRUCT_RESET(asn_DEF_UTCTime, &b);
}
int int
main(int ac, char **av) { main(int ac, char **av) {
...@@ -57,6 +72,16 @@ main(int ac, char **av) { ...@@ -57,6 +72,16 @@ main(int ac, char **av) {
check("040125093000,1234", 1075051800, 0); check("040125093000,1234", 1075051800, 0);
} }
compare(__LINE__, 0, "040125093007", "040125093007");
compare(__LINE__, 0, "040125093007-0000", "040125093007Z");
compare(__LINE__, 1, "040125093008", "040125093007");
compare(__LINE__, 1, "040125093008-0000", "040125093007-0000");
compare(__LINE__, 0, "040125093008-0000", "040125093008-0000");
compare(__LINE__, 1, "040125093008-0000", "040125093007Z");
compare(__LINE__, 0, "040125093007-0000", "040125093007+0000");
compare(__LINE__, 1, "040125093007-0030", "040125093007Z");
compare(__LINE__, -1, "040125093007+0030", "040125093007Z");
return 0; return 0;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment