[asterisk-commits] dlee: branch dlee/ari-event-remodel r392103 - in /team/dlee/ari-event-remodel...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Jun 17 15:44:13 CDT 2013


Author: dlee
Date: Mon Jun 17 15:44:12 2013
New Revision: 392103

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=392103
Log:
Test date validations

Added:
    team/dlee/ari-event-remodel/tests/test_ari_model.c   (with props)
Modified:
    team/dlee/ari-event-remodel/res/res_ari_model.c

Modified: team/dlee/ari-event-remodel/res/res_ari_model.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ari-event-remodel/res/res_ari_model.c?view=diff&rev=392103&r1=392102&r2=392103
==============================================================================
--- team/dlee/ari-event-remodel/res/res_ari_model.c (original)
+++ team/dlee/ari-event-remodel/res/res_ari_model.c Mon Jun 17 15:44:12 2013
@@ -42,14 +42,14 @@
 static regex_t date_regex;
 
 /* Regex for YYYY-MM-DD */
-#define REGEX_YMD "[0-9]{4}-[0-9]{2}-[0-9]{2}"
+#define REGEX_YMD "[0-9]{4}-[01][0-9]-[0-3][0-9]"
 
 /* Regex for hh:mm(:ss(.s)); seconds and subseconds optional
  * Handles the probably impossible case of a leap second, too */
 #define REGEX_HMS "[0-2][0-9]:[0-5][0-9](:[0-6][0-9](.[0-9]+)?)?"
 
 /* Regex for timezone */
-#define REGEX_TZ "(Z|[-+][0-2]:[0-59])"
+#define REGEX_TZ "(Z|[-+][0-2][0-9]:[0-5][0-9])"
 
 /* REGEX for ISO 8601, the time specifier optional */
 #define ISO8601_PATTERN "^" REGEX_YMD "(T" REGEX_HMS REGEX_TZ ")?$"
@@ -139,7 +139,7 @@
 	str = ast_json_string_get(json);
 	ast_assert(str != NULL);
 	if (regexec(&date_regex, str, 0, NULL, 0) != 0) {
-		ast_log(LOG_ERROR, "Date field is malformed: %s\n", str);
+		ast_log(LOG_ERROR, "Date field is malformed: '%s'\n", str);
 		return 0;
 	}
 	return 1;

Added: team/dlee/ari-event-remodel/tests/test_ari_model.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ari-event-remodel/tests/test_ari_model.c?view=auto&rev=392103
==============================================================================
--- team/dlee/ari-event-remodel/tests/test_ari_model.c (added)
+++ team/dlee/ari-event-remodel/tests/test_ari_model.c Mon Jun 17 15:44:12 2013
@@ -1,0 +1,357 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2013, Digium, Inc.
+ *
+ * David M. Lee, II <dlee at digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*!
+ * \file
+ * \brief Test the native ARI JSON validators.
+ *
+ * \author David M. Lee, II <dlee at digium.com>
+ */
+
+/*** MODULEINFO
+	<depend>TEST_FRAMEWORK</depend>
+	<depend>res_ari_model</depend>
+	<support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include "asterisk/utils.h"
+#include "asterisk/module.h"
+#include "asterisk/test.h"
+#include "../res/stasis_http/ari_model.h"
+
+AST_TEST_DEFINE(validate_byte)
+{
+	RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
+	RAII_VAR(struct ast_json *, str, NULL, ast_json_unref);
+	int res;
+
+	switch (cmd) {
+	case TEST_INIT:
+		info->name = __func__;
+		info->category = "/ari/validators/";
+		info->summary = "Test byte validation";
+		info->description =
+			"Test byte validation";
+		return AST_TEST_NOT_RUN;
+	case TEST_EXECUTE:
+		break;
+	}
+
+	uut = ast_json_integer_create(-128);
+	ast_test_validate(test, NULL != uut);
+	ast_test_validate(test, ari_validate_byte(uut));
+
+	res = ast_json_integer_set(uut, 0);
+	ast_test_validate(test, 0 == res);
+	ast_test_validate(test, ari_validate_byte(uut));
+
+	res = ast_json_integer_set(uut, 255);
+	ast_test_validate(test, 0 == res);
+	ast_test_validate(test, ari_validate_byte(uut));
+
+	res = ast_json_integer_set(uut, -129);
+	ast_test_validate(test, 0 == res);
+	ast_test_validate(test, !ari_validate_byte(uut));
+
+	res = ast_json_integer_set(uut, 256);
+	ast_test_validate(test, 0 == res);
+	ast_test_validate(test, !ari_validate_byte(uut));
+
+	str = ast_json_string_create("not a byte");
+	ast_test_validate(test, NULL != str);
+	ast_test_validate(test, !ari_validate_byte(str));
+
+	/* Even if the string has an integral value */
+	res = ast_json_string_set(str, "0");
+	ast_test_validate(test, 0 == res);
+	ast_test_validate(test, !ari_validate_byte(str));
+
+	ast_test_validate(test, !ari_validate_byte(ast_json_null()));
+
+	return AST_TEST_PASS;
+}
+
+AST_TEST_DEFINE(validate_boolean)
+{
+	RAII_VAR(struct ast_json *, str, NULL, ast_json_unref);
+	int res;
+
+	switch (cmd) {
+	case TEST_INIT:
+		info->name = __func__;
+		info->category = "/ari/validators/";
+		info->summary = "Test byte validation";
+		info->description =
+			"Test byte validation";
+		return AST_TEST_NOT_RUN;
+	case TEST_EXECUTE:
+		break;
+	}
+
+	ast_test_validate(test, ari_validate_boolean(ast_json_true()));
+	ast_test_validate(test, ari_validate_boolean(ast_json_false()));
+
+	str = ast_json_string_create("not a bool");
+	ast_test_validate(test, NULL != str);
+	ast_test_validate(test, !ari_validate_boolean(str));
+
+	/* Even if the string has a boolean value */
+	res = ast_json_string_set(str, "true");
+	ast_test_validate(test, 0 == res);
+	ast_test_validate(test, !ari_validate_boolean(str));
+
+	/* Even if the string has a boolean text in it */
+	res = ast_json_string_set(str, "true");
+	ast_test_validate(test, 0 == res);
+	ast_test_validate(test, !ari_validate_boolean(str));
+
+	ast_test_validate(test, !ari_validate_boolean(ast_json_null()));
+
+	return AST_TEST_PASS;
+}
+
+AST_TEST_DEFINE(validate_int)
+{
+	RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
+	RAII_VAR(struct ast_json *, str, NULL, ast_json_unref);
+	int res;
+
+	switch (cmd) {
+	case TEST_INIT:
+		info->name = __func__;
+		info->category = "/ari/validators/";
+		info->summary = "Test int validation";
+		info->description =
+			"Test int validation";
+		return AST_TEST_NOT_RUN;
+	case TEST_EXECUTE:
+		break;
+	}
+
+	uut = ast_json_integer_create(-2147483648);
+	ast_test_validate(test, NULL != uut);
+	ast_test_validate(test, ari_validate_int(uut));
+
+	res = ast_json_integer_set(uut, 0);
+	ast_test_validate(test, 0 == res);
+	ast_test_validate(test, ari_validate_int(uut));
+
+	res = ast_json_integer_set(uut, 2147483647);
+	ast_test_validate(test, 0 == res);
+	ast_test_validate(test, ari_validate_int(uut));
+
+	res = ast_json_integer_set(uut, -2147483649LL);
+	ast_test_validate(test, 0 == res);
+	ast_test_validate(test, !ari_validate_int(uut));
+
+	res = ast_json_integer_set(uut, 2147483648LL);
+	ast_test_validate(test, 0 == res);
+	ast_test_validate(test, !ari_validate_int(uut));
+
+	str = ast_json_string_create("not a int");
+	ast_test_validate(test, NULL != str);
+	ast_test_validate(test, !ari_validate_int(str));
+
+	/* Even if the string has an integral value */
+	res = ast_json_string_set(str, "0");
+	ast_test_validate(test, 0 == res);
+	ast_test_validate(test, !ari_validate_int(str));
+
+	ast_test_validate(test, !ari_validate_int(ast_json_null()));
+
+	return AST_TEST_PASS;
+}
+
+AST_TEST_DEFINE(validate_long)
+{
+	RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
+	RAII_VAR(struct ast_json *, str, NULL, ast_json_unref);
+	int res;
+
+	switch (cmd) {
+	case TEST_INIT:
+		info->name = __func__;
+		info->category = "/ari/validators/";
+		info->summary = "Test long validation";
+		info->description =
+			"Test long validation";
+		return AST_TEST_NOT_RUN;
+	case TEST_EXECUTE:
+		break;
+	}
+
+	uut = ast_json_integer_create(0);
+	ast_test_validate(test, NULL != uut);
+	ast_test_validate(test, ari_validate_long(uut));
+
+	str = ast_json_string_create("not a long");
+	ast_test_validate(test, NULL != str);
+	ast_test_validate(test, !ari_validate_long(str));
+
+	/* Even if the string has an integral value */
+	res = ast_json_string_set(str, "0");
+	ast_test_validate(test, 0 == res);
+	ast_test_validate(test, !ari_validate_long(str));
+
+	ast_test_validate(test, !ari_validate_long(ast_json_null()));
+
+	return AST_TEST_PASS;
+}
+
+AST_TEST_DEFINE(validate_string)
+{
+	RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
+	RAII_VAR(struct ast_json *, str, NULL, ast_json_unref);
+	int res;
+
+	switch (cmd) {
+	case TEST_INIT:
+		info->name = __func__;
+		info->category = "/ari/validators/";
+		info->summary = "Test string validation";
+		info->description =
+			"Test string validation";
+		return AST_TEST_NOT_RUN;
+	case TEST_EXECUTE:
+		break;
+	}
+
+	uut = ast_json_string_create("text");
+	ast_test_validate(test, NULL != uut);
+	ast_test_validate(test, ari_validate_string(uut));
+
+	res = ast_json_string_set(uut, "");
+	ast_test_validate(test, 0 == res);
+	ast_test_validate(test, ari_validate_string(uut));
+
+	ast_test_validate(test, !ari_validate_string(ast_json_null()));
+
+	return AST_TEST_PASS;
+}
+
+AST_TEST_DEFINE(validate_date)
+{
+	RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);
+	RAII_VAR(struct ast_json *, str, NULL, ast_json_unref);
+	enum ast_test_result_state test_res;
+	int res;
+	int i;
+	const char *valid_dates[] = {
+		"2013-06-17",
+		"2013-06-17T23:59Z",
+		"2013-06-17T23:59:59Z",
+		"2013-06-30T23:59:61Z",
+		"2013-06-17T23:59:59.999999Z",
+		"2013-06-17T23:59-06:00",
+		"2013-06-17T23:59:59-06:00",
+		"2013-06-30T23:59:61-06:00",
+		"2013-06-17T23:59:59.999999-06:00",
+		"2013-06-17T23:59+06:30",
+		"2013-06-17T23:59:59+06:30",
+		"2013-06-30T23:59:61+06:30",
+		"2013-06-17T23:59:59.999999+06:30",
+		"9999-12-31T23:59:61.999999Z",
+	};
+
+	const char *invalid_dates[] = {
+		"",
+		"Not a date",
+		"2013-06-17T", /* Missing time */
+		"2013-06-17T23:59:59.Z", /* Missing subsecond */
+		"2013-06-17T23:59", /* Missing timezone */
+		"2013-06-17T23:59:59.999999", /* Missing timezone */
+		"9999-99-31T23:59:61.999999Z", /* Invalid month */
+		"9999-12-99T23:59:61.999999Z", /* Invalid day */
+		"9999-12-31T99:59:61.999999Z", /* Invalid hour */
+		"9999-12-31T23:99:61.999999Z", /* Invalid minute */
+		"9999-12-31T23:59:99.999999Z", /* Invalid second */
+		"2013-06-17T23:59:59.999999-99:00", /* Invalid timezone */
+		"2013-06-17T23:59:59.999999-06:99", /* Invalid timezone */
+	};
+
+	switch (cmd) {
+	case TEST_INIT:
+		info->name = __func__;
+		info->category = "/ari/validators/";
+		info->summary = "Test date validation";
+		info->description =
+			"Test date validation";
+		return AST_TEST_NOT_RUN;
+	case TEST_EXECUTE:
+		break;
+	}
+
+	uut = ast_json_string_create("");
+	ast_test_validate(test, NULL != uut);
+
+	/* Instead of using ast_test_validate, we'll collect the results from
+	 * several test cases, since we have so many */
+	test_res = AST_TEST_PASS;
+	for (i = 0; i < ARRAY_LEN(valid_dates); ++i) {
+		res = ast_json_string_set(uut, valid_dates[i]);
+		ast_test_validate(test, 0 == res);
+		if (!ari_validate_date(uut)) {
+			ast_test_status_update(test,
+				"Expected '%s' to be a valid date\n",
+				valid_dates[i]);
+			test_res = AST_TEST_FAIL;
+		}
+	}
+
+	for (i = 0; i < ARRAY_LEN(invalid_dates); ++i) {
+		res = ast_json_string_set(uut, invalid_dates[i]);
+		ast_test_validate(test, 0 == res);
+		if (ari_validate_date(uut)) {
+			ast_test_status_update(test,
+				"Expected '%s' to be an invalid date\n",
+				invalid_dates[i]);
+			test_res = AST_TEST_FAIL;
+		}
+	}
+
+	ast_test_validate(test, !ari_validate_string(ast_json_null()));
+
+	return test_res;
+}
+static int unload_module(void)
+{
+	AST_TEST_UNREGISTER(validate_byte);
+	AST_TEST_UNREGISTER(validate_boolean);
+	AST_TEST_UNREGISTER(validate_int);
+	AST_TEST_UNREGISTER(validate_long);
+	AST_TEST_UNREGISTER(validate_string);
+	AST_TEST_UNREGISTER(validate_date);
+	return 0;
+}
+
+static int load_module(void)
+{
+	AST_TEST_REGISTER(validate_byte);
+	AST_TEST_REGISTER(validate_boolean);
+	AST_TEST_REGISTER(validate_int);
+	AST_TEST_REGISTER(validate_long);
+	AST_TEST_REGISTER(validate_string);
+	AST_TEST_REGISTER(validate_date);
+	return AST_MODULE_LOAD_SUCCESS;
+}
+
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Skeleton (sample) Test");

Propchange: team/dlee/ari-event-remodel/tests/test_ari_model.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/dlee/ari-event-remodel/tests/test_ari_model.c
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/dlee/ari-event-remodel/tests/test_ari_model.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain




More information about the asterisk-commits mailing list