Ticket refs #5832: Improve date time validation

This commit is contained in:
Amin Ben Ramdhane 2021-08-27 16:22:08 +01:00
parent 59da1ee4ec
commit d285f7df07
3 changed files with 43 additions and 4 deletions

View file

@ -946,8 +946,8 @@ static char *check_value_by_type(char *value, int type)
}
break;
case DMT_TIME:
if (!strptime(buf, (len == 27) ? "%Y-%m-%dT%H:%M:%S." : "%Y-%m-%dT%H:%M:%SZ", &tm))
return (len == 27) ? "0001-01-01T00:00:00.000000Z" : "0001-01-01T00:00:00Z";
if (!strptime(buf, "%Y-%m-%dT%H:%M:%S", &tm))
return "0001-01-01T00:00:00Z";
break;
default:
break;

View file

@ -1297,10 +1297,29 @@ int dm_validate_long(char *value, struct range_args r_args[], int r_args_size)
int dm_validate_dateTime(char *value)
{
/* check format */
/*
* Allowed format:
* XXXX-XX-XXTXX:XX:XXZ
* XXXX-XX-XXTXX:XX:XX.XXXZ
* XXXX-XX-XXTXX:XX:XX.XXXXXXZ
*/
char *p = NULL;
struct tm tm;
if (!(strptime(value, "%Y-%m-%dT%H:%M:%SZ", &tm)))
int m;
p = strptime(value, "%Y-%m-%dT%H:%M:%SZ", &tm);
if (p && *p == '\0')
return 0;
p = strptime(value, "%Y-%m-%dT%H:%M:%S.", &tm);
if (!p || *p == '\0' || value[strlen(value) - 1] != 'Z')
return -1;
int num_parsed = sscanf(p, "%dZ", &m);
if (num_parsed != 1 || (strlen(p) != 7 && strlen(p) != 4))
return -1;
return 0;
}

View file

@ -764,9 +764,29 @@ static void test_bbf_api_validate(void **state)
assert_int_equal(validate, -1);
// dm_validate_dateTime: test with wrong value
validate = dm_validate_dateTime("2021-12-31T20:53:99.12Z");
assert_int_equal(validate, -1);
// dm_validate_dateTime: test with wrong value
validate = dm_validate_dateTime("2021-12-31T20:53:99+01:00Z");
assert_int_equal(validate, -1);
// dm_validate_dateTime: test with wrong value
validate = dm_validate_dateTime("2021-12-31T20:53:99.12");
assert_int_equal(validate, -1);
// dm_validate_dateTime: test with correct value
validate = dm_validate_dateTime("2021-12-31T20:53:01Z");
assert_int_equal(validate, 0);
// dm_validate_dateTime: test with correct value
validate = dm_validate_dateTime("2021-12-31T20:53:01.125Z");
assert_int_equal(validate, 0);
// dm_validate_dateTime: test with correct value
validate = dm_validate_dateTime("2021-12-31T20:53:01.125345Z");
assert_int_equal(validate, 0);
/*
* Test of dm_validate_hexBinary function