diff --git a/channels/sip/config_parser.c b/channels/sip/config_parser.c index 68abafee00..cf040a928d 100644 --- a/channels/sip/config_parser.c +++ b/channels/sip/config_parser.c @@ -263,7 +263,7 @@ AST_TEST_DEFINE(sip_parse_register_line_test) reg->callid_valid != FALSE || reg->ocseq != INITIAL_CSEQ) { - ast_str_append(&args->ast_test_error_str, 0, "Test 1: simple config failed\n"); + ast_test_status_update(test, "Test 1: simple config failed\n"); res = AST_TEST_FAIL; } ast_string_field_free_memory(reg); @@ -290,7 +290,7 @@ AST_TEST_DEFINE(sip_parse_register_line_test) reg->callid_valid != FALSE || reg->ocseq != INITIAL_CSEQ) { - ast_str_append(&args->ast_test_error_str, 0, "Test 2: add secret failed\n"); + ast_test_status_update(test, "Test 2: add secret failed\n"); res = AST_TEST_FAIL; } ast_string_field_free_memory(reg); @@ -317,7 +317,7 @@ AST_TEST_DEFINE(sip_parse_register_line_test) reg->callid_valid != FALSE || reg->ocseq != INITIAL_CSEQ) { - ast_str_append(&args->ast_test_error_str, 0, "Test 3: add userdomain and authuser failed\n"); + ast_test_status_update(test, "Test 3: add userdomain and authuser failed\n"); res = AST_TEST_FAIL; } ast_string_field_free_memory(reg); @@ -344,7 +344,7 @@ AST_TEST_DEFINE(sip_parse_register_line_test) reg->callid_valid != FALSE || reg->ocseq != INITIAL_CSEQ) { - ast_str_append(&args->ast_test_error_str, 0, "Test 4: add callback extension failed\n"); + ast_test_status_update(test, "Test 4: add callback extension failed\n"); res = AST_TEST_FAIL; } ast_string_field_free_memory(reg); @@ -371,7 +371,7 @@ AST_TEST_DEFINE(sip_parse_register_line_test) reg->callid_valid != FALSE || reg->ocseq != INITIAL_CSEQ) { - ast_str_append(&args->ast_test_error_str, 0, "Test 5: add transport failed\n"); + ast_test_status_update(test, "Test 5: add transport failed\n"); res = AST_TEST_FAIL; } ast_string_field_free_memory(reg); @@ -398,7 +398,7 @@ AST_TEST_DEFINE(sip_parse_register_line_test) reg->callid_valid != FALSE || reg->ocseq != INITIAL_CSEQ) { - ast_str_append(&args->ast_test_error_str, 0, "Test 6: change to tls transport and add expiry failed\n"); + ast_test_status_update(test, "Test 6: change to tls transport and add expiry failed\n"); res = AST_TEST_FAIL; } ast_string_field_free_memory(reg); @@ -425,7 +425,7 @@ AST_TEST_DEFINE(sip_parse_register_line_test) reg->callid_valid != FALSE || reg->ocseq != INITIAL_CSEQ) { - ast_str_append(&args->ast_test_error_str, 0, "Test 7, change transport to tcp, add custom port, and add peer failed.\n"); + ast_test_status_update(test, "Test 7, change transport to tcp, add custom port, and add peer failed.\n"); res = AST_TEST_FAIL; } ast_string_field_free_memory(reg); @@ -452,7 +452,7 @@ AST_TEST_DEFINE(sip_parse_register_line_test) reg->callid_valid != FALSE || reg->ocseq != INITIAL_CSEQ) { - ast_str_append(&args->ast_test_error_str, 0, "Test 8, remove transport failed.\n"); + ast_test_status_update(test, "Test 8, remove transport failed.\n"); res = AST_TEST_FAIL; } ast_string_field_free_memory(reg); @@ -462,8 +462,8 @@ AST_TEST_DEFINE(sip_parse_register_line_test) if (!(reg = ast_calloc_with_stringfields(1, struct sip_registry, 256))) { goto alloc_fail; } else if (!sip_parse_register_line(reg, default_expiry, reg9, 1)) { - - ast_str_append(&args->ast_test_error_str, 0, "Test 9, missing domain, expected to fail but did not.\n"); + ast_test_status_update(test, + "Test 9, missing domain, expected to fail but did not.\n"); res = AST_TEST_FAIL; } ast_string_field_free_memory(reg); @@ -473,8 +473,8 @@ AST_TEST_DEFINE(sip_parse_register_line_test) if (!(reg = ast_calloc_with_stringfields(1, struct sip_registry, 256))) { goto alloc_fail; } else if (!sip_parse_register_line(reg, default_expiry, reg10, 1)) { - - ast_str_append(&args->ast_test_error_str, 0, "Test 10, missing user expected to fail but did not\n"); + ast_test_status_update(test, + "Test 10, missing user expected to fail but did not\n"); res = AST_TEST_FAIL; } ast_string_field_free_memory(reg); @@ -482,8 +482,8 @@ AST_TEST_DEFINE(sip_parse_register_line_test) /* ---Test reg 11, no registry object, expected to fail--- */ if (!sip_parse_register_line(NULL, default_expiry, reg1, 1)) { - - ast_str_append(&args->ast_test_error_str, 0, "Test 11, no registery object, expected to fail but did not.\n"); + ast_test_status_update(test, + "Test 11, no registery object, expected to fail but did not.\n"); res = AST_TEST_FAIL; } @@ -492,7 +492,8 @@ AST_TEST_DEFINE(sip_parse_register_line_test) goto alloc_fail; } else if (!sip_parse_register_line(reg, default_expiry, NULL, 1)) { - ast_str_append(&args->ast_test_error_str, 0, "Test 11, NULL register line expected to fail but did not.\n"); + ast_test_status_update(test, + "Test 11, NULL register line expected to fail but did not.\n"); res = AST_TEST_FAIL; } ast_string_field_free_memory(reg); @@ -502,7 +503,7 @@ AST_TEST_DEFINE(sip_parse_register_line_test) return res; alloc_fail: - ast_str_set(&args->ast_test_error_str, 0, "Out of memory. \n"); + ast_test_status_update(test, "Out of memory. \n"); return res; } @@ -582,60 +583,54 @@ AST_TEST_DEFINE(sip_parse_host_line_test) /* test 1, simple host */ sip_parse_host(host1, 1, &host, &port, &transport); if (port != STANDARD_SIP_PORT || - ast_strlen_zero(host) || strcmp(host, "www.blah.com") || - transport != SIP_TRANSPORT_UDP) { - - ast_str_append(&args->ast_test_error_str, 0, "Test 1: simple host failed.\n"); + ast_strlen_zero(host) || strcmp(host, "www.blah.com") || + transport != SIP_TRANSPORT_UDP) { + ast_test_status_update(test, "Test 1: simple host failed.\n"); res = AST_TEST_FAIL; } /* test 2, add tcp transport */ sip_parse_host(host2, 1, &host, &port, &transport); if (port != STANDARD_SIP_PORT || - ast_strlen_zero(host) || strcmp(host, "www.blah.com") || - transport != SIP_TRANSPORT_TCP) { - - ast_str_append(&args->ast_test_error_str, 0, "Test 2: tcp host failed.\n"); + ast_strlen_zero(host) || strcmp(host, "www.blah.com") || + transport != SIP_TRANSPORT_TCP) { + ast_test_status_update(test, "Test 2: tcp host failed.\n"); res = AST_TEST_FAIL; } /* test 3, add tls transport */ sip_parse_host(host3, 1, &host, &port, &transport); if (port != STANDARD_TLS_PORT || - ast_strlen_zero(host) || strcmp(host, "10.10.10.10") || - transport != SIP_TRANSPORT_TLS) { - - ast_str_append(&args->ast_test_error_str, 0, "Test 3: tls host failed. \n"); + ast_strlen_zero(host) || strcmp(host, "10.10.10.10") || + transport != SIP_TRANSPORT_TLS) { + ast_test_status_update(test, "Test 3: tls host failed. \n"); res = AST_TEST_FAIL; } /* test 4, add custom port with tls */ sip_parse_host(host4, 1, &host, &port, &transport); - if (port != 1234 || - ast_strlen_zero(host) || strcmp(host, "10.10.10.10") || - transport != SIP_TRANSPORT_TLS) { - - ast_str_append(&args->ast_test_error_str, 0, "Test 4: tls host with custom port failed.\n"); + if (port != 1234 || ast_strlen_zero(host) || + strcmp(host, "10.10.10.10") || + transport != SIP_TRANSPORT_TLS) { + ast_test_status_update(test, "Test 4: tls host with custom port failed.\n"); res = AST_TEST_FAIL; } /* test 5, simple host with custom port */ sip_parse_host(host5, 1, &host, &port, &transport); - if (port != 1234 || - ast_strlen_zero(host) || strcmp(host, "10.10.10.10") || - transport != SIP_TRANSPORT_UDP) { - - ast_str_append(&args->ast_test_error_str, 0, "Test 5: simple host with custom port failed.\n"); + if (port != 1234 || ast_strlen_zero(host) || + strcmp(host, "10.10.10.10") || + transport != SIP_TRANSPORT_UDP) { + ast_test_status_update(test, "Test 5: simple host with custom port failed.\n"); res = AST_TEST_FAIL; } - return res; /* test 6, expected failure with NULL input */ - if (sip_parse_host(NULL, 1, &host, &port, &transport)) { - - ast_str_append(&args->ast_test_error_str, 0, "Test 6: expected error on NULL input did not occur.\n"); + if (!sip_parse_host(NULL, 1, &host, &port, &transport)) { + ast_test_status_update(test, "Test 6: expected error on NULL input did not occur.\n"); res = AST_TEST_FAIL; } + return res; } diff --git a/channels/sip/reqresp_parser.c b/channels/sip/reqresp_parser.c index 6fec362cc4..fe6ce0e1c4 100644 --- a/channels/sip/reqresp_parser.c +++ b/channels/sip/reqresp_parser.c @@ -139,66 +139,62 @@ AST_TEST_DEFINE(sip_parse_uri_test) /* Test 1, simple URI */ name = pass = domain = port = transport = NULL; if (parse_uri(uri1, "sip:,sips:", &name, &pass, &domain, &port, &transport) || - strcmp(name, "name") || - !ast_strlen_zero(pass) || - strcmp(domain, "host") || - !ast_strlen_zero(port) || - !ast_strlen_zero(transport)) { - - ast_str_append(&args->ast_test_error_str, 0, "Test 1: simple uri failed. \n"); + strcmp(name, "name") || + !ast_strlen_zero(pass) || + strcmp(domain, "host") || + !ast_strlen_zero(port) || + !ast_strlen_zero(transport)) { + ast_test_status_update(test, "Test 1: simple uri failed. \n"); res = AST_TEST_FAIL; } /* Test 2, add tcp transport */ name = pass = domain = port = transport = NULL; if (parse_uri(uri2, "sip:,sips:", &name, &pass, &domain, &port, &transport) || - strcmp(name, "name") || - !ast_strlen_zero(pass) || - strcmp(domain, "host") || - !ast_strlen_zero(port) || - strcmp(transport, "tcp")) { - - ast_str_append(&args->ast_test_error_str, 0, "Test 2: uri with addtion of tcp transport failed. \n"); + strcmp(name, "name") || + !ast_strlen_zero(pass) || + strcmp(domain, "host") || + !ast_strlen_zero(port) || + strcmp(transport, "tcp")) { + ast_test_status_update(test, "Test 2: uri with addtion of tcp transport failed. \n"); res = AST_TEST_FAIL; } /* Test 3, add secret */ name = pass = domain = port = transport = NULL; if (parse_uri(uri3, "sip:,sips:", &name, &pass, &domain, &port, &transport) || - strcmp(name, "name") || - strcmp(pass, "secret") || - strcmp(domain, "host") || - !ast_strlen_zero(port) || - strcmp(transport, "tcp")) { - - ast_str_append(&args->ast_test_error_str, 0, "Test 3: uri with addition of secret failed.\n"); + strcmp(name, "name") || + strcmp(pass, "secret") || + strcmp(domain, "host") || + !ast_strlen_zero(port) || + strcmp(transport, "tcp")) { + ast_test_status_update(test, "Test 3: uri with addition of secret failed.\n"); res = AST_TEST_FAIL; } /* Test 4, add port and unparsed header field*/ name = pass = domain = port = transport = NULL; if (parse_uri(uri4, "sip:,sips:", &name, &pass, &domain, &port, &transport) || - strcmp(name, "name") || - strcmp(pass, "secret") || - strcmp(domain, "host") || - strcmp(port, "port") || - strcmp(transport, "tcp")) { - - ast_str_append(&args->ast_test_error_str, 0, "Test 4: add port and unparsed header field failed.\n"); + strcmp(name, "name") || + strcmp(pass, "secret") || + strcmp(domain, "host") || + strcmp(port, "port") || + strcmp(transport, "tcp")) { + ast_test_status_update(test, "Test 4: add port and unparsed header field failed.\n"); res = AST_TEST_FAIL; } /* Test 5, verify parse_uri does not crash when given a NULL uri */ name = pass = domain = port = transport = NULL; if (!parse_uri(NULL, "sip:,sips:", &name, &pass, &domain, &port, &transport)) { - ast_str_append(&args->ast_test_error_str, 0, "Test 5: passing a NULL uri failed.\n"); + ast_test_status_update(test, "Test 5: passing a NULL uri failed.\n"); res = AST_TEST_FAIL; } /* Test 6, verify parse_uri does not crash when given a NULL output parameters */ name = pass = domain = port = transport = NULL; if (parse_uri(uri4, "sip:,sips:", NULL, NULL, NULL, NULL, NULL)) { - ast_str_append(&args->ast_test_error_str, 0, "Test 6: passing NULL output parameters failed.\n"); + ast_test_status_update(test, "Test 6: passing NULL output parameters failed.\n"); res = AST_TEST_FAIL; } @@ -339,46 +335,41 @@ AST_TEST_DEFINE(get_calleridname_test) /* quoted-text with backslash escaped quote */ after_dname = get_calleridname(in1, dname, sizeof(dname)); - ast_test_status_update(&args->status_update, "display-name1: %s\nafter: %s\n", dname, after_dname); + ast_test_status_update(test, "display-name1: %s\nafter: %s\n", dname, after_dname); if (strcmp(dname, " quoted-text internal \" quote ")) { - ast_test_status_update(&args->status_update, "display-name1 test failed\n"); - ast_str_append(&args->ast_test_error_str, 0, "quoted-text with internal backslash decode failed. \n"); + ast_test_status_update(test, "display-name1 test failed\n"); res = AST_TEST_FAIL; } /* token text */ after_dname = get_calleridname(in2, dname, sizeof(dname)); - ast_test_status_update(&args->status_update, "display-name2: %s\nafter: %s\n", dname, after_dname); + ast_test_status_update(test, "display-name2: %s\nafter: %s\n", dname, after_dname); if (strcmp(dname, "token text with no quotes")) { - ast_test_status_update(&args->status_update, "display-name2 test failed\n"); - ast_str_append(&args->ast_test_error_str, 0, "token text with decode failed. \n"); + ast_test_status_update(test, "display-name2 test failed\n"); res = AST_TEST_FAIL; } /* quoted-text buffer overflow */ after_dname = get_calleridname(overflow1, dname, sizeof(dname)); - ast_test_status_update(&args->status_update, "overflow display-name1: %s\nafter: %s\n", dname, after_dname); + ast_test_status_update(test, "overflow display-name1: %s\nafter: %s\n", dname, after_dname); if (*dname != '\0' && after_dname != overflow1) { - ast_test_status_update(&args->status_update, "overflow display-name1 test failed\n"); - ast_str_append(&args->ast_test_error_str, 0, "quoted-text buffer overflow check failed. \n"); + ast_test_status_update(test, "overflow display-name1 test failed\n"); res = AST_TEST_FAIL; } /* quoted-text buffer with no terminating end quote */ after_dname = get_calleridname(noendquote, dname, sizeof(dname)); - ast_test_status_update(&args->status_update, "noendquote display-name1: %s\nafter: %s\n", dname, after_dname); + ast_test_status_update(test, "noendquote display-name1: %s\nafter: %s\n", dname, after_dname); if (*dname != '\0' && after_dname != noendquote) { - ast_test_status_update(&args->status_update, "no end quote for quoted-text display-name failed\n"); - ast_str_append(&args->ast_test_error_str, 0, "quoted-text buffer check no terminating end quote failed. \n"); + ast_test_status_update(test, "no end quote for quoted-text display-name failed\n"); res = AST_TEST_FAIL; } /* addr-spec rather than display-name. */ after_dname = get_calleridname(addrspec, dname, sizeof(dname)); - ast_test_status_update(&args->status_update, "noendquote display-name1: %s\nafter: %s\n", dname, after_dname); + ast_test_status_update(test, "noendquote display-name1: %s\nafter: %s\n", dname, after_dname); if (*dname != '\0' && after_dname != addrspec) { - ast_test_status_update(&args->status_update, "detection of addr-spec failed\n"); - ast_str_append(&args->ast_test_error_str, 0, "detection of addr-spec failed. \n"); + ast_test_status_update(test, "detection of addr-spec failed\n"); res = AST_TEST_FAIL; } diff --git a/funcs/func_math.c b/funcs/func_math.c index 2164ff261c..de9120d5b3 100644 --- a/funcs/func_math.c +++ b/funcs/func_math.c @@ -473,8 +473,7 @@ AST_TEST_DEFINE(test_MATH_function) break; } - ast_test_status_update(&args->status_update, "Testing MATH() substitution ...\n"); - ast_str_reset(args->ast_test_error_str); + ast_test_status_update(test, "Testing MATH() substitution ...\n"); if (!(expr = ast_str_create(16)) || !(result = ast_str_create(16))) { if (expr) { @@ -489,14 +488,16 @@ AST_TEST_DEFINE(test_MATH_function) ast_str_set(&expr, 0, "${MATH(170 AND 63,i)}"); ast_str_substitute_variables(&result, 0, NULL, ast_str_buffer(expr)); if (strcmp(ast_str_buffer(result), "42") != 0) { - ast_str_append(&args->ast_test_error_str, 0, "Expected result '42' not returned! ('%s')\n", ast_str_buffer(result)); + ast_test_status_update(test, "Expected result '42' not returned! ('%s')\n", + ast_str_buffer(result)); res = AST_TEST_FAIL; } ast_str_set(&expr, 0, "${MATH(170AND63,i)}"); ast_str_substitute_variables(&result, 0, NULL, ast_str_buffer(expr)); if (strcmp(ast_str_buffer(result), "42") != 0) { - ast_str_append(&args->ast_test_error_str, 0, "Expected result '42' not returned! ('%s')\n", ast_str_buffer(result)); + ast_test_status_update(test, "Expected result '42' not returned! ('%s')\n", + ast_str_buffer(result)); res = AST_TEST_FAIL; } diff --git a/include/asterisk/test.h b/include/asterisk/test.h index 82610cdd46..f97df80d75 100644 --- a/include/asterisk/test.h +++ b/include/asterisk/test.h @@ -1,9 +1,10 @@ /* * Asterisk -- An open source telephony toolkit. * - * Copyright (C) 2009, Digium, Inc. + * Copyright (C) 2009-2010, Digium, Inc. * * David Vossel + * Russell Bryant * * See http://www.asterisk.org for more information about * the Asterisk project. Please do not directly contact @@ -23,6 +24,7 @@ * For an overview on how to use the test API, see \ref AstUnitTestAPI * * \author David Vossel + * \author Russell Bryant */ #ifndef _AST_TEST_H_ @@ -46,7 +48,7 @@ Each defined test has three arguments avaliable to it's test code. \param struct ast_test_info *info \param enum ast_test_command cmd - \param struct ast_test_args *args + \param struct ast_test *test While these arguments are not visible they are passed to every test function defined using the AST_TEST_DEFINE macro. @@ -72,7 +74,7 @@ . . if (fail) { \\ the following is just some example logic - ast_str_set(&args->ast_test_error_str, 0 , "an error occured because..."); + ast_test_status_update(test, "an error occured because..."); res = AST_RESULT_FAIL; } else { res = AST_RESULT_PASS @@ -81,10 +83,8 @@ } \endcode - Every callback function is passed an ast_test_args object which contains - an ast_str allowing the function to provide an optional short description of - what went wrong if the test failed. This is done by writing to - args->ast_test_error_str. + Details of the test execution, especially failure details, should be provided + by using the ast_test_status_update() function. \subsection RegisterTest Register a Test @@ -117,13 +117,13 @@ /*! Macros used for defining and registering a test */ #ifdef TEST_FRAMEWORK -#define AST_TEST_DEFINE(hdr) static enum ast_test_result_state hdr(struct ast_test_info *info, enum ast_test_command cmd, struct ast_test_args *args) +#define AST_TEST_DEFINE(hdr) static enum ast_test_result_state hdr(struct ast_test_info *info, enum ast_test_command cmd, struct ast_test *test) #define AST_TEST_REGISTER(cb) ast_test_register(cb) #define AST_TEST_UNREGISTER(cb) ast_test_unregister(cb) #else -#define AST_TEST_DEFINE(hdr) static enum ast_test_result_state attribute_unused hdr(struct ast_test_info *info, enum ast_test_command cmd, struct ast_test_args *args) +#define AST_TEST_DEFINE(hdr) static enum ast_test_result_state attribute_unused hdr(struct ast_test_info *info, enum ast_test_command cmd, struct ast_test *test) #define AST_TEST_REGISTER(cb) #define AST_TEST_UNREGISTER(cb) #define ast_test_status_update(a,b,c...) @@ -142,34 +142,23 @@ enum ast_test_command { }; /*! - * This struct is passed to ast_test_status_update() providing a place to push - * the update to. In the future this structure may expand beyond simply being - * a wrapper for cli args to including other status update options as well. + * \brief An Asterisk unit test. + * + * This is an opaque type. */ -struct ast_test_status_args { - /*! pointer to cli arg used for updating status */ - struct ast_cli_args *cli; -}; +struct ast_test; /*! - * tools made available to the callback function during test execution - */ -struct ast_test_args { - struct ast_str *ast_test_error_str; /*! optional error str to describe error result */ - struct ast_test_status_args status_update; -}; - -/*! - * Contains all the initilization information required to store a new test definition + * \brief Contains all the initialization information required to store a new test definition */ struct ast_test_info { - /*! name of test, unique to category */ + /*! \brief name of test, unique to category */ const char *name; - /*! test category */ + /*! \brief test category */ const char *category; - /*! optional short summary of test */ + /*! \brief optional short summary of test */ const char *summary; - /*! optional brief detailed description of test */ + /*! \brief optional brief detailed description of test */ const char *description; }; @@ -182,7 +171,8 @@ struct ast_test_info { * \retval AST_TEST_PASS for pass * \retval AST_TEST_FAIL for failure */ -typedef enum ast_test_result_state (ast_test_cb_t)(struct ast_test_info *info, enum ast_test_command cmd, struct ast_test_args *args); +typedef enum ast_test_result_state (ast_test_cb_t)(struct ast_test_info *info, + enum ast_test_command cmd, struct ast_test *test); /*! * \brief unregisters a test with the test framework @@ -207,13 +197,19 @@ int ast_test_register(ast_test_cb_t *cb); /*! * \brief update test's status during testing. * - * \param ast_test_status_args defines everywhere the update should go. + * \param test currently executing test * * \retval 0 success * \retval -1 failure */ -int ast_test_status_update(struct ast_test_status_args *args, const char *fmt, ...) -__attribute__((format(printf, 2, 3))); +int __ast_test_status_update(const char *file, const char *func, int line, + struct ast_test *test, const char *fmt, ...) + __attribute__((format(printf, 5, 6))); + +/*! + * \ref __ast_test_status_update() + */ +#define ast_test_status_update(t, f, ...) __ast_test_status_update(__FILE__, __PRETTY_FUNCTION__, __LINE__, (t), (f), ## __VA_ARGS__) #endif /* TEST_FRAMEWORK */ #endif /* _AST_TEST_H */ diff --git a/include/asterisk/utils.h b/include/asterisk/utils.h index 1ea9a6085e..a0208e7f20 100644 --- a/include/asterisk/utils.h +++ b/include/asterisk/utils.h @@ -758,8 +758,4 @@ int ast_str_to_eid(struct ast_eid *eid, const char *s); */ int ast_eid_cmp(const struct ast_eid *eid1, const struct ast_eid *eid2); -/*! - * \brief Registers util api unit tests - */ -void ast_utils_register_tests(void); #endif /* _ASTERISK_UTILS_H */ diff --git a/main/test.c b/main/test.c index c4b023f370..e6bbaeb995 100644 --- a/main/test.c +++ b/main/test.c @@ -1,9 +1,10 @@ /* * Asterisk -- An open source telephony toolkit. * - * Copyright (C) 2009, Digium, Inc. + * Copyright (C) 2009-2010, Digium, Inc. * * David Vossel + * Russell Bryant * * See http://www.asterisk.org for more information about * the Asterisk project. Please do not directly contact @@ -16,11 +17,12 @@ * at the top of the source tree. */ -/*! \file - * +/*! + * \file * \brief Unit Test Framework * * \author David Vossel + * \author Russell Bryant */ #include "asterisk.h" @@ -43,14 +45,24 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$"); /*! This array corresponds to the values defined in the ast_test_state enum */ static const char * const test_result2str[] = { [AST_TEST_NOT_RUN] = "NOT RUN", - [AST_TEST_PASS] = "PASS", - [AST_TEST_FAIL] = "FAIL", + [AST_TEST_PASS] = "PASS", + [AST_TEST_FAIL] = "FAIL", }; /*! holds all the information pertaining to a single defined test */ struct ast_test { struct ast_test_info info; /*!< holds test callback information */ - struct ast_test_args args; /*!< function callback arguments */ + /*! + * \brief Test defined status output from last execution + */ + struct ast_str *status_str; + /*! + * \brief CLI arguments, if tests being run from the CLI + * + * If this is set, status updates from the tests will be sent to the + * CLI in addition to being saved off in status_str. + */ + struct ast_cli_args *cli; enum ast_test_result_state state; /*!< current test state */ unsigned int time; /*!< time in ms test took */ ast_test_cb_t *cb; /*!< test callback function */ @@ -77,23 +89,18 @@ enum test_mode { /*! List of registered test definitions */ static AST_LIST_HEAD_STATIC(tests, ast_test); -/*! static function prototypes */ static struct ast_test *test_alloc(ast_test_cb_t *cb); static struct ast_test *test_free(struct ast_test *test); static int test_insert(struct ast_test *test); static struct ast_test *test_remove(ast_test_cb_t *cb); static int test_cat_cmp(const char *cat1, const char *cat2); -int ast_test_status_update(struct ast_test_status_args *args, const char *fmt, ...) +int __ast_test_status_update(const char *file, const char *func, int line, + struct ast_test *test, const char *fmt, ...) { struct ast_str *buf = NULL; va_list ap; - /* it is not an error if no cli args exist. */ - if (!args->cli) { - return 0; - } - if (!(buf = ast_str_create(128))) { return -1; } @@ -102,9 +109,16 @@ int ast_test_status_update(struct ast_test_status_args *args, const char *fmt, . ast_str_set_va(&buf, 0, fmt, ap); va_end(ap); - ast_cli(args->cli->fd, "%s", ast_str_buffer(buf)); + if (test->cli) { + ast_cli(test->cli->fd, "[%s:%s:%d]: %s", + file, func, line, ast_str_buffer(buf)); + } + + ast_str_append(&test->status_str, 0, "[%s:%s:%d]: %s", + file, func, line, ast_str_buffer(buf)); ast_free(buf); + return 0; } @@ -112,18 +126,15 @@ int ast_test_register(ast_test_cb_t *cb) { struct ast_test *test; - /* verify data.*/ if (!cb) { ast_log(LOG_WARNING, "Attempted to register test without all required information\n"); return -1; } - /* create test object */ if (!(test = test_alloc(cb))) { return -1; } - /* insert into list */ if (test_insert(test)) { test_free(test); return -1; @@ -136,12 +147,10 @@ int ast_test_unregister(ast_test_cb_t *cb) { struct ast_test *test; - /* find test and remove */ if (!(test = test_remove(cb))) { return -1; /* not found */ } - /* free test object */ test_free(test); return 0; @@ -158,16 +167,11 @@ static void test_execute(struct ast_test *test) { struct timeval begin; - /* clear any previous error results before starting */ - ast_str_reset(test->args.ast_test_error_str); - /* get start time */ + ast_str_reset(test->status_str); + begin = ast_tvnow(); - /* the callback gets the pointer to the pointer of the error buf */ - test->state = test->cb(&test->info, TEST_EXECUTE, &test->args); - /* record the total time the test took */ + test->state = test->cb(&test->info, TEST_EXECUTE, test); test->time = ast_tvdiff_ms(ast_tvnow(), begin); - /* clear any status update args that may have been set */ - memset(&test->args.status_update, 0, sizeof(struct ast_test_status_args)); } static void test_xml_entry(struct ast_test *test, FILE *f) @@ -183,7 +187,7 @@ static void test_xml_entry(struct ast_test *test, FILE *f) if (test->state == AST_TEST_FAIL) { fprintf(f, "\t\t%s\n", - S_OR(ast_str_buffer(test->args.ast_test_error_str), "NA")); + S_OR(ast_str_buffer(test->status_str), "NA")); fprintf(f, "\t\n"); } @@ -200,12 +204,12 @@ static void test_txt_entry(struct ast_test *test, FILE *f) fprintf(f, "Summary: %s\n", test->info.summary); fprintf(f, "Description: %s\n", test->info.description); fprintf(f, "Result: %s\n", test_result2str[test->state]); - if (test->state == AST_TEST_FAIL) { - fprintf(f, "Error Description: %s\n", S_OR(ast_str_buffer(test->args.ast_test_error_str), "NA")); - } if (test->state != AST_TEST_NOT_RUN) { fprintf(f, "Time: %d\n", test->time); } + if (test->state == AST_TEST_FAIL) { + fprintf(f, "Error Description: %s\n\n", S_OR(ast_str_buffer(test->status_str), "NA")); + } } /*! @@ -266,11 +270,13 @@ static int test_execute_multiple(const char *name, const char *category, struct } /* set the test status update argument. it is ok if cli is NULL */ - test->args.status_update.cli = cli; + test->cli = cli; /* execute the test and save results */ test_execute(test); + test->cli = NULL; + /* update execution specific counts here */ last_results.last_time += test->time; if (test->state == AST_TEST_PASS) { @@ -285,12 +291,11 @@ static int test_execute_multiple(const char *name, const char *category, struct (test->state == AST_TEST_FAIL) ? COLOR_RED : COLOR_GREEN, 0, sizeof(result_buf)); - ast_cli(cli->fd, "END %s - %s Time: %dms Result: %s %s\n", + ast_cli(cli->fd, "END %s - %s Time: %dms Result: %s\n", test->info.category, test->info.name, test->time, - result_buf, - ast_str_buffer(test->args.ast_test_error_str)); + result_buf); } } @@ -432,46 +437,20 @@ done: * \internal * \brief adds test to container sorted first by category then by name * - * \return 0 on success, -1 on failure + * \retval 0 success + * \retval -1 failure */ static int test_insert(struct ast_test *test) { - struct ast_test *cur = NULL; - int res = 0; - int i = 0; - int inserted = 0; - /* This is a slow operation that may need to be optimized in the future * as the test framework expands. At the moment we are doing string * comparisons on every item within the list to insert in sorted order. */ - AST_LIST_LOCK(&tests); - AST_LIST_TRAVERSE_SAFE_BEGIN(&tests, cur, entry) { - if ((i = strcmp(test->info.category, cur->info.category)) < 0) { - AST_LIST_INSERT_BEFORE_CURRENT(test, entry); - inserted = 1; - break; - } else if (!i) { /* same category, now insert by name within that category*/ - if ((i = strcmp(test->info.name, cur->info.name)) < 0) { - AST_LIST_INSERT_BEFORE_CURRENT(test, entry); - inserted = 1; - break; - } else if (!i) { - /* Error, duplicate found */ - res = -1; - break; - } - } - } - AST_LIST_TRAVERSE_SAFE_END; - - if (!inserted && !res) { - AST_LIST_INSERT_TAIL(&tests, test, entry); - inserted = 1; - } + AST_LIST_LOCK(&tests); + AST_LIST_INSERT_SORTALPHA(&tests, test, entry, info.category); AST_LIST_UNLOCK(&tests); - return res; + return 0; } /*! @@ -501,7 +480,8 @@ static struct ast_test *test_remove(ast_test_cb_t *cb) * \brief compares two test categories to determine if cat1 resides in cat2 * \internal * - * \return 0 if true + * \retval 0 true + * \retval non-zero false */ static int test_cat_cmp(const char *cat1, const char *cat2) @@ -524,8 +504,8 @@ static int test_cat_cmp(const char *cat1, const char *cat2) } /*! - * \brief frees a ast_test object and all it's data members * \internal + * \brief free an ast_test object and all it's data members */ static struct ast_test *test_free(struct ast_test *test) { @@ -533,7 +513,7 @@ static struct ast_test *test_free(struct ast_test *test) return NULL; } - ast_free(test->args.ast_test_error_str); + ast_free(test->status_str); ast_free(test); return NULL; @@ -541,7 +521,7 @@ static struct ast_test *test_free(struct ast_test *test) /*! * \internal - * \brief allocates an ast_test object. + * \brief allocate an ast_test object. */ static struct ast_test *test_alloc(ast_test_cb_t *cb) { @@ -553,14 +533,32 @@ static struct ast_test *test_alloc(ast_test_cb_t *cb) test->cb = cb; - test->cb(&test->info, TEST_INIT, &test->args); + test->cb(&test->info, TEST_INIT, test); + + if (ast_strlen_zero(test->info.name)) { + ast_log(LOG_WARNING, "Test has no name, test registration refused.\n"); + return test_free(test); + } + + if (ast_strlen_zero(test->info.category)) { + ast_log(LOG_WARNING, "Test %s has no category, test registration refused.\n", + test->info.name); + return test_free(test); + } - if (ast_strlen_zero(test->info.name) || - ast_strlen_zero(test->info.category) || - ast_strlen_zero(test->info.summary) || - ast_strlen_zero(test->info.description) || - !(test->args.ast_test_error_str = ast_str_create(128))) { + if (ast_strlen_zero(test->info.summary)) { + ast_log(LOG_WARNING, "Test %s/%s has no summary, test registration refused.\n", + test->info.category, test->info.name); + return test_free(test); + } + if (ast_strlen_zero(test->info.description)) { + ast_log(LOG_WARNING, "Test %s/%s has no description, test registration refused.\n", + test->info.category, test->info.name); + return test_free(test); + } + + if (!(test->status_str = ast_str_create(128))) { return test_free(test); } @@ -570,7 +568,7 @@ static struct ast_test *test_alloc(ast_test_cb_t *cb) /* CLI commands */ static char *test_cli_show_registered(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) { -#define FORMAT "%-30.30s %-25.25s %-40.40s %-13.13s\n" +#define FORMAT "%-25.25s %-30.30s %-40.40s %-13.13s\n" static const char * const option1[] = { "all", "category", NULL }; static const char * const option2[] = { "name", NULL }; struct ast_test *test = NULL; @@ -601,19 +599,22 @@ static char *test_cli_show_registered(struct ast_cli_entry *e, int cmd, struct a ((a->argc == 7) && strcmp(a->argv[5], "name"))) { return CLI_SHOWUSAGE; } - ast_cli(a->fd, FORMAT, "Name", "Category", "Summary", "Test Result"); + ast_cli(a->fd, FORMAT, "Category", "Name", "Summary", "Test Result"); + ast_cli(a->fd, FORMAT, "--------", "----", "-------", "-----------"); AST_LIST_LOCK(&tests); AST_LIST_TRAVERSE(&tests, test, entry) { if ((a->argc == 4) || ((a->argc == 5) && !test_cat_cmp(test->info.category, a->argv[4])) || ((a->argc == 7) && !strcmp(test->info.category, a->argv[4]) && !strcmp(test->info.name, a->argv[6]))) { - ast_cli(a->fd, FORMAT, test->info.name, test->info.category, test->info.summary, test_result2str[test->state]); - count ++; + ast_cli(a->fd, FORMAT, test->info.category, test->info.name, + test->info.summary, test_result2str[test->state]); + count++; } } AST_LIST_UNLOCK(&tests); - ast_cli(a->fd, "%d Registered Tests Matched\n", count); + ast_cli(a->fd, FORMAT, "--------", "----", "-------", "-----------"); + ast_cli(a->fd, "\n%d Registered Tests Matched\n", count); default: return NULL; } @@ -625,6 +626,7 @@ static char *test_cli_execute_registered(struct ast_cli_entry *e, int cmd, struc { static const char * const option1[] = { "all", "category", NULL }; static const char * const option2[] = { "name", NULL }; + switch (cmd) { case CLI_INIT: e->command = "test execute"; @@ -681,7 +683,7 @@ static char *test_cli_execute_registered(struct ast_cli_entry *e, int cmd, struc static char *test_cli_show_results(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) { -#define FORMAT_RES_ALL "%s%s %-30.30s %-25.25s %-40.40s\n" +#define FORMAT_RES_ALL "%s%s %-30.30s %-25.25s\n" static const char * const option1[] = { "all", "failed", "passed", NULL }; char result_buf[32] = { 0 }; struct ast_test *test = NULL; @@ -718,7 +720,7 @@ static char *test_cli_show_results(struct ast_cli_entry *e, int cmd, struct ast_ return CLI_SHOWUSAGE; } - ast_cli(a->fd, FORMAT_RES_ALL, "Result", "", "Name", "Category", "Error Description"); + ast_cli(a->fd, FORMAT_RES_ALL, "Result", "", "Name", "Category"); AST_LIST_LOCK(&tests); AST_LIST_TRAVERSE(&tests, test, entry) { if (test->state == AST_TEST_NOT_RUN) { @@ -735,8 +737,7 @@ static char *test_cli_show_results(struct ast_cli_entry *e, int cmd, struct ast_ result_buf, " ", test->info.name, - test->info.category, - (test->state == AST_TEST_FAIL) ? S_OR(ast_str_buffer(test->args.ast_test_error_str), "Not Avaliable") : ""); + test->info.category); } } AST_LIST_UNLOCK(&tests); diff --git a/tests/test_ast_format_str_reduce.c b/tests/test_ast_format_str_reduce.c index 0c24ab2d13..8dd1f500fc 100644 --- a/tests/test_ast_format_str_reduce.c +++ b/tests/test_ast_format_str_reduce.c @@ -94,12 +94,14 @@ AST_TEST_DEFINE(ast_format_str_reduce_test_1) for (i = 0; test_strings[i][0]; i++) { c = ast_strdupa(test_strings[i][0]); if (!(c = ast_format_str_reduce(c))) { - ast_str_set(&args->ast_test_error_str, 0, "Error running ast_format_str_reduce() on string '%s'\n", test_strings[i][0]); + ast_test_status_update(test, "Error running ast_format_str_reduce() on string '%s'\n", + test_strings[i][0]); return AST_TEST_FAIL; } if (strcmp(test_strings[i][1], c)) { - ast_str_set(&args->ast_test_error_str, 0, "Format string '%s' reduced to '%s'. Expected '%s'\n", test_strings[i][0], c, test_strings[i][1]); + ast_test_status_update(test, "Format string '%s' reduced to '%s'. Expected '%s'\n", + test_strings[i][0], c, test_strings[i][1]); return AST_TEST_FAIL; } } @@ -107,7 +109,9 @@ AST_TEST_DEFINE(ast_format_str_reduce_test_1) for (i = 0; fail_strings[i]; i++) { c = ast_strdupa(fail_strings[i]); if ((c = ast_format_str_reduce(c))) { - ast_str_set(&args->ast_test_error_str, 0, "ast_format_str_reduce() succeded on string '%s' with result '%s', but we expected it to fail\n", fail_strings[i], c); + ast_test_status_update(test, "ast_format_str_reduce() succeded on string '%s' " + "with result '%s', but we expected it to fail\n", + fail_strings[i], c); return AST_TEST_FAIL; } } diff --git a/tests/test_heap.c b/tests/test_heap.c index 420bd4f7bf..a8dd13e0a1 100644 --- a/tests/test_heap.c +++ b/tests/test_heap.c @@ -80,8 +80,6 @@ AST_TEST_DEFINE(heap_test_1) return AST_TEST_FAIL; } - ast_test_status_update(&args->status_update, "pushing nodes\n"); - ast_heap_push(h, &nodes[0]); ast_heap_push(h, &nodes[1]); @@ -90,22 +88,25 @@ AST_TEST_DEFINE(heap_test_1) obj = ast_heap_pop(h); if (obj->val != 3) { + ast_test_status_update(test, "expected 3, got %ld\n", obj->val); return AST_TEST_FAIL; } - ast_test_status_update(&args->status_update, "popping nodes\n"); obj = ast_heap_pop(h); if (obj->val != 2) { + ast_test_status_update(test, "expected 2, got %ld\n", obj->val); return AST_TEST_FAIL; } obj = ast_heap_pop(h); if (obj->val != 1) { + ast_test_status_update(test, "expected 1, got %ld\n", obj->val); return AST_TEST_FAIL; } obj = ast_heap_pop(h); if (obj) { + ast_test_status_update(test, "got unexpected object\n"); return AST_TEST_FAIL; } @@ -117,10 +118,10 @@ AST_TEST_DEFINE(heap_test_1) AST_TEST_DEFINE(heap_test_2) { struct ast_heap *h = NULL; - static const unsigned int one_million = 1000000; + static const unsigned int total = 100000; struct node *nodes = NULL; struct node *node; - unsigned int i = one_million; + unsigned int i = total; long last = LONG_MAX; long cur; enum ast_test_result_state res = AST_TEST_PASS; @@ -130,13 +131,18 @@ AST_TEST_DEFINE(heap_test_2) info->name = "heap_test_2"; info->category = "main/heap/"; info->summary = "load test"; - info->description = "Push a million random elements on to a heap,verify that the heap has been properly constructed, and then ensure that the elements are come back off in the proper order"; + info->description = + "Push one hundred thousand random elements on to a heap, " + "verify that the heap has been properly constructed, " + "and then ensure that the elements are come back off " + "in the proper order." + "\n"; return AST_TEST_NOT_RUN; case TEST_EXECUTE: break; } - if (!(nodes = ast_malloc(one_million * sizeof(*node)))) { + if (!(nodes = ast_malloc(total * sizeof(*node)))) { res = AST_TEST_FAIL; goto return_cleanup; } @@ -160,7 +166,7 @@ AST_TEST_DEFINE(heap_test_2) while ((node = ast_heap_pop(h))) { cur = node->val; if (cur > last) { - ast_str_set(&args->ast_test_error_str, 0, "i: %u, cur: %ld, last: %ld\n", i, cur, last); + ast_test_status_update(test, "i: %u, cur: %ld, last: %ld\n", i, cur, last); res = AST_TEST_FAIL; goto return_cleanup; } @@ -168,8 +174,8 @@ AST_TEST_DEFINE(heap_test_2) i++; } - if (i != one_million) { - ast_str_set(&args->ast_test_error_str, 0, "Stopped popping off after only getting %u nodes\n", i); + if (i != total) { + ast_test_status_update(test, "Stopped popping off after only getting %u nodes\n", i); res = AST_TEST_FAIL; goto return_cleanup; } diff --git a/tests/test_md5.c b/tests/test_md5.c deleted file mode 100644 index f8b884913a..0000000000 --- a/tests/test_md5.c +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Asterisk -- An open source telephony toolkit. - * - * Copyright (C) 2010, Digium, Inc. - * - * Russell Bryant - * - * 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 MD5 test - * - * \author Russell Bryant - * - * \ingroup tests - */ - -/*** MODULEINFO - TEST_FRAMEWORK - ***/ - -#include "asterisk.h" - -ASTERISK_FILE_VERSION(__FILE__, "$Revision$") - -#include "asterisk/utils.h" -#include "asterisk/module.h" -#include "asterisk/test.h" - -AST_TEST_DEFINE(md5_test) -{ - static const struct { - const char *input; - const char *expected_output; - } tests[] = { - { "apples", "daeccf0ad3c1fc8c8015205c332f5b42" }, - { "bananas", "ec121ff80513ae58ed478d5c5787075b" }, - { "reallylongstringaboutgoatcheese", "0a2d9280d37e2e37545cfef6e7e4e890" }, - }; - enum ast_test_result_state res = AST_TEST_PASS; - int i; - - switch (cmd) { - case TEST_INIT: - info->name = "md5_test"; - info->category = "main/"; - info->summary = "MD5 test"; - info->description = - "This test exercises MD5 calculations.\n" - ""; - return AST_TEST_NOT_RUN; - case TEST_EXECUTE: - break; - } - - ast_test_status_update(&args->status_update, "Testing MD5 ...\n"); - - for (i = 0; i < ARRAY_LEN(tests); i++) { - char md5_hash[32]; - ast_md5_hash(md5_hash, tests[i].input); - if (strcasecmp(md5_hash, tests[i].expected_output)) { - ast_test_status_update(&args->status_update, - "input: '%s' hash: '%s' expected hash: '%s'\n", - tests[i].input, md5_hash, tests[i].expected_output); - ast_str_append(&args->ast_test_error_str, 0, - "input: '%s' hash: '%s' expected hash: '%s'\n", - tests[i].input, md5_hash, tests[i].expected_output); - res = AST_TEST_FAIL; - } - } - - return res; -} - -static int unload_module(void) -{ - AST_TEST_UNREGISTER(md5_test); - return 0; -} - -static int load_module(void) -{ - AST_TEST_REGISTER(md5_test); - return AST_MODULE_LOAD_SUCCESS; -} - -AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "MD5 Test"); diff --git a/tests/test_sched.c b/tests/test_sched.c index b9e23d81e5..6cb9f61cb0 100644 --- a/tests/test_sched.c +++ b/tests/test_sched.c @@ -64,7 +64,7 @@ AST_TEST_DEFINE(sched_test_order) } if (!(con = sched_context_create())) { - ast_str_set(&args->ast_test_error_str, 0, + ast_test_status_update(test, "Test failed - could not create scheduler context\n"); return AST_TEST_FAIL; } @@ -73,79 +73,79 @@ AST_TEST_DEFINE(sched_test_order) * of ast_sched_wait() looks appropriate at each step along the way. */ if ((wait = ast_sched_wait(con)) != -1) { - ast_str_set(&args->ast_test_error_str, 0, + ast_test_status_update(test, "ast_sched_wait() should have returned -1, returned '%d'\n", wait); goto return_cleanup; } if ((id1 = ast_sched_add(con, 100000, sched_cb, NULL)) == -1) { - ast_str_set(&args->ast_test_error_str, 0, "Failed to add scheduler entry\n"); + ast_test_status_update(test, "Failed to add scheduler entry\n"); goto return_cleanup; } if ((wait = ast_sched_wait(con)) > 100000) { - ast_str_set(&args->ast_test_error_str, 0, + ast_test_status_update(test, "ast_sched_wait() should have returned <= 100000, returned '%d'\n", wait); goto return_cleanup; } if ((id2 = ast_sched_add(con, 10000, sched_cb, NULL)) == -1) { - ast_str_set(&args->ast_test_error_str, 0, "Failed to add scheduler entry\n"); + ast_test_status_update(test, "Failed to add scheduler entry\n"); goto return_cleanup; } if ((wait = ast_sched_wait(con)) > 10000) { - ast_str_set(&args->ast_test_error_str, 0, + ast_test_status_update(test, "ast_sched_wait() should have returned <= 10000, returned '%d'\n", wait); goto return_cleanup; } if ((id3 = ast_sched_add(con, 1000, sched_cb, NULL)) == -1) { - ast_str_set(&args->ast_test_error_str, 0, "Failed to add scheduler entry\n"); + ast_test_status_update(test, "Failed to add scheduler entry\n"); goto return_cleanup; } if ((wait = ast_sched_wait(con)) > 1000) { - ast_str_set(&args->ast_test_error_str, 0, + ast_test_status_update(test, "ast_sched_wait() should have returned <= 1000, returned '%d'\n", wait); goto return_cleanup; } if (ast_sched_del(con, id3) == -1) { - ast_str_set(&args->ast_test_error_str, 0, "Failed to remove scheduler entry\n"); + ast_test_status_update(test, "Failed to remove scheduler entry\n"); goto return_cleanup; } if ((wait = ast_sched_wait(con)) <= 1000) { - ast_str_set(&args->ast_test_error_str, 0, + ast_test_status_update(test, "ast_sched_wait() should have returned > 1000, returned '%d'\n", wait); goto return_cleanup; } if (ast_sched_del(con, id2) == -1) { - ast_str_set(&args->ast_test_error_str, 0, "Failed to remove scheduler entry\n"); + ast_test_status_update(test, "Failed to remove scheduler entry\n"); goto return_cleanup; } if ((wait = ast_sched_wait(con)) <= 10000) { - ast_str_set(&args->ast_test_error_str, 0, + ast_test_status_update(test, "ast_sched_wait() should have returned > 10000, returned '%d'\n", wait); goto return_cleanup; } if (ast_sched_del(con, id1) == -1) { - ast_str_set(&args->ast_test_error_str, 0, "Failed to remove scheduler entry\n"); + ast_test_status_update(test, "Failed to remove scheduler entry\n"); goto return_cleanup; } if ((wait = ast_sched_wait(con)) != -1) { - ast_str_set(&args->ast_test_error_str, 0, + ast_test_status_update(test, "ast_sched_wait() should have returned -1, returned '%d'\n", wait); goto return_cleanup; diff --git a/tests/test_sha1.c b/tests/test_sha1.c deleted file mode 100644 index 62d62959e0..0000000000 --- a/tests/test_sha1.c +++ /dev/null @@ -1,100 +0,0 @@ -/* - * Asterisk -- An open source telephony toolkit. - * - * Copyright (C) 2010, Digium, Inc. - * - * Russell Bryant - * - * 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 SHA1 test - * - * \author Russell Bryant - * - * \ingroup tests - */ - -/*** MODULEINFO - TEST_FRAMEWORK - ***/ - -#include "asterisk.h" - -ASTERISK_FILE_VERSION(__FILE__, "$Revision$") - -#include "asterisk/utils.h" -#include "asterisk/module.h" -#include "asterisk/test.h" - -AST_TEST_DEFINE(sha1_test) -{ - static const struct { - const char *input; - const char *expected_output; - } tests[] = { - { "giraffe", - "fac8f1a31d2998734d6a5253e49876b8e6a08239" }, - { "platypus", - "1dfb21b7a4d35e90d943e3a16107ccbfabd064d5" }, - { "ParastratiosphecomyiaStratiosphecomyioides", - "58af4e8438676f2bd3c4d8df9e00ee7fe06945bb" }, - }; - enum ast_test_result_state res = AST_TEST_PASS; - int i; - - switch (cmd) { - case TEST_INIT: - info->name = "sha1_test"; - info->category = "main/"; - info->summary = "SHA1 test"; - info->description = - "This test exercises SHA1 calculations.\n" - ""; - return AST_TEST_NOT_RUN; - case TEST_EXECUTE: - break; - } - - ast_test_status_update(&args->status_update, "Testing SHA1 ...\n"); - - for (i = 0; i < ARRAY_LEN(tests); i++) { - char sha1_hash[64]; - ast_sha1_hash(sha1_hash, tests[i].input); - if (strcasecmp(sha1_hash, tests[i].expected_output)) { - ast_test_status_update(&args->status_update, - "input: '%s' hash: '%s' expected hash: '%s'\n", - tests[i].input, sha1_hash, tests[i].expected_output); - ast_str_append(&args->ast_test_error_str, 0, - "input: '%s' hash: '%s' expected hash: '%s'\n", - tests[i].input, sha1_hash, tests[i].expected_output); - res = AST_TEST_FAIL; - } - } - - return res; -} - -static int unload_module(void) -{ - AST_TEST_UNREGISTER(sha1_test); - return 0; -} - -static int load_module(void) -{ - AST_TEST_REGISTER(sha1_test); - return AST_MODULE_LOAD_SUCCESS; -} - -AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "SHA1 Test"); diff --git a/tests/test_skel.c b/tests/test_skel.c index 10ae097b36..c1ce5e844d 100644 --- a/tests/test_skel.c +++ b/tests/test_skel.c @@ -55,10 +55,10 @@ AST_TEST_DEFINE(sample_test) break; } - ast_test_status_update(&args->status_update, "Executing sample test.\n"); + ast_test_status_update(test, "Executing sample test...\n"); if (!(ptr = ast_malloc(8))) { - ast_str_set(&args->ast_test_error_str, 0, "ast_malloc() failed\n"); + ast_test_status_update(test, "ast_malloc() failed\n"); return AST_TEST_FAIL; } diff --git a/tests/test_substitution.c b/tests/test_substitution.c index 6fdcfa1acc..b1ffdf64b7 100644 --- a/tests/test_substitution.c +++ b/tests/test_substitution.c @@ -44,23 +44,20 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include "asterisk/threadstorage.h" #include "asterisk/test.h" -static enum ast_test_result_state test_chan_integer(void *status, struct ast_str **err, +static enum ast_test_result_state test_chan_integer(struct ast_test *test, struct ast_channel *c, int *ifield, const char *expression) { int i, okay = 1, value1 = -1, value2 = -1; char workspace[4096]; struct ast_str *str = ast_str_create(16); - ast_test_status_update(status, "Testing '%s' . . . . . %s\n", expression, okay ? "passed" : "FAILED"); + ast_test_status_update(test, "Testing '%s' . . . . . %s\n", expression, okay ? "passed" : "FAILED"); for (i = 0; i < 256; i++) { *ifield = i; ast_str_substitute_variables(&str, 0, c, expression); pbx_substitute_variables_helper(c, expression, workspace, sizeof(workspace)); if (sscanf(workspace, "%d", &value1) != 1 || value1 != i || sscanf(ast_str_buffer(str), "%d", &value2) != 1 || value2 != i) { - ast_test_status_update(status, "%s != %s and/or %d != %d != %d\n", ast_str_buffer(str), workspace, value1, value2, i); - ast_str_set(err, 0, "%s: '%s' != '%s' and/or %d != %d != %d\n", - __PRETTY_FUNCTION__, ast_str_buffer(str), - workspace, value1, value2, i); + ast_test_status_update(test, "%s != %s and/or %d != %d != %d\n", ast_str_buffer(str), workspace, value1, value2, i); okay = 0; } } @@ -70,7 +67,7 @@ static enum ast_test_result_state test_chan_integer(void *status, struct ast_str return okay ? AST_TEST_PASS : AST_TEST_FAIL; } -static enum ast_test_result_state test_chan_string(void *status, struct ast_str **err, +static enum ast_test_result_state test_chan_string(struct ast_test *test, struct ast_channel *c, char *cfield, size_t cfieldsize, const char *expression) { @@ -83,11 +80,10 @@ static enum ast_test_result_state test_chan_string(void *status, struct ast_str ast_copy_string(cfield, values[i], cfieldsize); ast_str_substitute_variables(&str, 0, c, expression); pbx_substitute_variables_helper(c, expression, workspace, sizeof(workspace)); - ast_test_status_update(status, "Testing '%s' . . . . . %s\n", expression, okay ? "passed" : "FAILED"); + ast_test_status_update(test, "Testing '%s' . . . . . %s\n", + expression, okay ? "passed" : "FAILED"); if (strcmp(cfield, ast_str_buffer(str)) != 0 || strcmp(cfield, workspace) != 0) { - ast_test_status_update(status, "%s != %s != %s\n", cfield, ast_str_buffer(str), workspace); - ast_str_set(err, 0, "%s: '%s' != '%s' != '%s'\n", - __PRETTY_FUNCTION__, cfield, ast_str_buffer(str), workspace); + ast_test_status_update(test, "%s != %s != %s\n", cfield, ast_str_buffer(str), workspace); okay = 0; } } @@ -97,7 +93,7 @@ static enum ast_test_result_state test_chan_string(void *status, struct ast_str return okay ? AST_TEST_PASS : AST_TEST_FAIL; } -static enum ast_test_result_state test_chan_variable(void *status, struct ast_str **err, +static enum ast_test_result_state test_chan_variable(struct ast_test *test, struct ast_channel *c, const char *varname) { const char *values[] = { "one", "three", "reallylongdinosaursoundingthingwithwordsinit" }; @@ -111,11 +107,11 @@ static enum ast_test_result_state test_chan_variable(void *status, struct ast_st pbx_builtin_setvar_helper(c, varname, values[i]); ast_str_substitute_variables(&str, 0, c, ast_str_buffer(var)); pbx_substitute_variables_helper(c, ast_str_buffer(var), workspace, sizeof(workspace)); - ast_test_status_update(status, "Testing '%s' . . . . . %s\n", ast_str_buffer(var), okay ? "passed" : "FAILED"); + ast_test_status_update(test, "Testing '%s' . . . . . %s\n", + ast_str_buffer(var), okay ? "passed" : "FAILED"); if (strcmp(values[i], ast_str_buffer(str)) != 0 || strcmp(values[i], workspace) != 0) { - ast_test_status_update(status, "%s != %s != %s\n", values[i], ast_str_buffer(str), workspace); - ast_str_set(err, 0, "%s: Variable: '%s' ... '%s' != '%s' != '%s'\n", varname, - __PRETTY_FUNCTION__, values[i], ast_str_buffer(str), workspace); + ast_test_status_update(test, "%s != %s != %s\n", + values[i], ast_str_buffer(str), workspace); okay = 0; } } @@ -126,7 +122,7 @@ static enum ast_test_result_state test_chan_variable(void *status, struct ast_st return okay ? AST_TEST_PASS : AST_TEST_FAIL; } -static enum ast_test_result_state test_chan_function(void *status, struct ast_str **err, +static enum ast_test_result_state test_chan_function(struct ast_test *test, struct ast_channel *c, const char *expression) { int okay = 1; @@ -135,12 +131,11 @@ static enum ast_test_result_state test_chan_function(void *status, struct ast_st ast_str_substitute_variables(&str, 0, c, expression); pbx_substitute_variables_helper(c, expression, workspace, sizeof(workspace)); - ast_test_status_update(status, "Testing '%s' . . . . . %s\n", expression, okay ? "passed" : "FAILED"); + ast_test_status_update(test, "Testing '%s' . . . . . %s\n", + expression, okay ? "passed" : "FAILED"); if (strcmp(workspace, ast_str_buffer(str)) != 0) { - ast_test_status_update(status, "test_chan_function, expr: '%s' ... %s != %s\n", + ast_test_status_update(test, "test_chan_function, expr: '%s' ... %s != %s\n", expression, ast_str_buffer(str), workspace); - ast_str_set(err, 0, "%s: expr: '%s' ... '%s' != '%s'\n", - __PRETTY_FUNCTION__, expression, ast_str_buffer(str), workspace); okay = 0; } @@ -149,7 +144,7 @@ static enum ast_test_result_state test_chan_function(void *status, struct ast_st return okay ? AST_TEST_PASS : AST_TEST_FAIL; } -static enum ast_test_result_state test_2way_function(void *status, struct ast_str **err, +static enum ast_test_result_state test_2way_function(struct ast_test *test, struct ast_channel *c, const char *encode1, const char *encode2, const char *decode1, const char *decode2) { @@ -163,14 +158,13 @@ static enum ast_test_result_state test_2way_function(void *status, struct ast_st okay = !strcmp(ast_str_buffer(str), "foobarbaz"); - ast_test_status_update(status, "Testing '%s%s' and '%s%s' . . . . . %s\n", + ast_test_status_update(test, "Testing '%s%s' and '%s%s' . . . . . %s\n", encode1, encode2, decode1, decode2, okay ? "passed" : "FAILED"); if (!okay) { - ast_test_status_update(status, " '%s' != 'foobarbaz'\n", ast_str_buffer(str)); - ast_str_set(err, 0, "%s: '%s' != 'foobarbaz'\n", - __PRETTY_FUNCTION__, ast_str_buffer(str)); + ast_test_status_update(test, " '%s' != 'foobarbaz'\n", + ast_str_buffer(str)); } ast_free(str); @@ -178,7 +172,7 @@ static enum ast_test_result_state test_2way_function(void *status, struct ast_st return okay ? AST_TEST_PASS : AST_TEST_FAIL; } -static enum ast_test_result_state test_expected_result(void *status, struct ast_str **err, +static enum ast_test_result_state test_expected_result(struct ast_test *test, struct ast_channel *c, const char *expression, const char *result) { struct ast_str *str = ast_str_create(16); @@ -187,14 +181,12 @@ static enum ast_test_result_state test_expected_result(void *status, struct ast_ ast_str_substitute_variables(&str, 0, c, expression); okay = !strcmp(ast_str_buffer(str), result); - ast_test_status_update(status, "Testing '%s' ('%s') == '%s' . . . . . %s\n", + ast_test_status_update(test, "Testing '%s' ('%s') == '%s' . . . . . %s\n", ast_str_buffer(str), expression, result, okay ? "passed" : "FAILED"); if (!okay) { - ast_test_status_update(status, "test_expected_result: '%s' != '%s'\n", - ast_str_buffer(str), result); - ast_str_set(err, 0, "%s: '%s' != '%s'\n", __PRETTY_FUNCTION__, + ast_test_status_update(test, "test_expected_result: '%s' != '%s'\n", ast_str_buffer(str), result); } @@ -222,43 +214,43 @@ AST_TEST_DEFINE(test_substitution) break; } - ast_test_status_update(&args->status_update, "Testing variable substitution ...\n"); + ast_test_status_update(test, "Testing variable substitution ...\n"); c = ast_channel_alloc(0, 0, "", "", "", "", "", "", 0, "Test/substitution"); #define TEST(t) if (t == AST_TEST_FAIL) { res = AST_TEST_FAIL; } - TEST(test_chan_integer(&args->status_update, &args->ast_test_error_str, c, &c->cid.cid_pres, "${CALLINGPRES}")); - TEST(test_chan_integer(&args->status_update, &args->ast_test_error_str, c, &c->cid.cid_ani2, "${CALLINGANI2}")); - TEST(test_chan_integer(&args->status_update, &args->ast_test_error_str, c, &c->cid.cid_ton, "${CALLINGTON}")); - TEST(test_chan_integer(&args->status_update, &args->ast_test_error_str, c, &c->cid.cid_tns, "${CALLINGTNS}")); - TEST(test_chan_integer(&args->status_update, &args->ast_test_error_str, c, &c->hangupcause, "${HANGUPCAUSE}")); - TEST(test_chan_integer(&args->status_update, &args->ast_test_error_str, c, &c->priority, "${PRIORITY}")); - TEST(test_chan_string(&args->status_update, &args->ast_test_error_str, c, c->context, sizeof(c->context), "${CONTEXT}")); - TEST(test_chan_string(&args->status_update, &args->ast_test_error_str, c, c->exten, sizeof(c->exten), "${EXTEN}")); - TEST(test_chan_variable(&args->status_update, &args->ast_test_error_str, c, "CHANNEL(language)")); - TEST(test_chan_variable(&args->status_update, &args->ast_test_error_str, c, "CHANNEL(musicclass)")); - TEST(test_chan_variable(&args->status_update, &args->ast_test_error_str, c, "CHANNEL(parkinglot)")); - TEST(test_chan_variable(&args->status_update, &args->ast_test_error_str, c, "CALLERID(name)")); - TEST(test_chan_variable(&args->status_update, &args->ast_test_error_str, c, "CURLOPT(proxyuserpwd)")); - TEST(test_chan_variable(&args->status_update, &args->ast_test_error_str, c, "CDR(foo)")); - TEST(test_chan_variable(&args->status_update, &args->ast_test_error_str, c, "ENV(foo)")); - TEST(test_chan_variable(&args->status_update, &args->ast_test_error_str, c, "GLOBAL(foo)")); - TEST(test_chan_variable(&args->status_update, &args->ast_test_error_str, c, "GROUP()")); - TEST(test_2way_function(&args->status_update, &args->ast_test_error_str, c, "${AES_ENCRYPT(abcdefghijklmnop,", ")}", "${AES_DECRYPT(abcdefghijklmnop,", ")}")); - TEST(test_2way_function(&args->status_update, &args->ast_test_error_str, c, "${BASE64_ENCODE(", ")}", "${BASE64_DECODE(", ")}")); + TEST(test_chan_integer(test, c, &c->cid.cid_pres, "${CALLINGPRES}")); + TEST(test_chan_integer(test, c, &c->cid.cid_ani2, "${CALLINGANI2}")); + TEST(test_chan_integer(test, c, &c->cid.cid_ton, "${CALLINGTON}")); + TEST(test_chan_integer(test, c, &c->cid.cid_tns, "${CALLINGTNS}")); + TEST(test_chan_integer(test, c, &c->hangupcause, "${HANGUPCAUSE}")); + TEST(test_chan_integer(test, c, &c->priority, "${PRIORITY}")); + TEST(test_chan_string(test, c, c->context, sizeof(c->context), "${CONTEXT}")); + TEST(test_chan_string(test, c, c->exten, sizeof(c->exten), "${EXTEN}")); + TEST(test_chan_variable(test, c, "CHANNEL(language)")); + TEST(test_chan_variable(test, c, "CHANNEL(musicclass)")); + TEST(test_chan_variable(test, c, "CHANNEL(parkinglot)")); + TEST(test_chan_variable(test, c, "CALLERID(name)")); + TEST(test_chan_variable(test, c, "CURLOPT(proxyuserpwd)")); + TEST(test_chan_variable(test, c, "CDR(foo)")); + TEST(test_chan_variable(test, c, "ENV(foo)")); + TEST(test_chan_variable(test, c, "GLOBAL(foo)")); + TEST(test_chan_variable(test, c, "GROUP()")); + TEST(test_2way_function(test, c, "${AES_ENCRYPT(abcdefghijklmnop,", ")}", "${AES_DECRYPT(abcdefghijklmnop,", ")}")); + TEST(test_2way_function(test, c, "${BASE64_ENCODE(", ")}", "${BASE64_DECODE(", ")}")); pbx_builtin_setvar_helper(c, "foo", "123"); pbx_builtin_setvar_helper(c, "bar", "foo"); pbx_builtin_setvar_helper(c, "baz", "fo"); - TEST(test_expected_result(&args->status_update, &args->ast_test_error_str, c, "${foo}${foo}", "123123")); - TEST(test_expected_result(&args->status_update, &args->ast_test_error_str, c, "A${foo}A${foo}A", "A123A123A")); - TEST(test_expected_result(&args->status_update, &args->ast_test_error_str, c, "A${${bar}}A", "A123A")); - TEST(test_expected_result(&args->status_update, &args->ast_test_error_str, c, "A${${baz}o}A", "A123A")); - TEST(test_expected_result(&args->status_update, &args->ast_test_error_str, c, "A${${baz}o:1}A", "A23A")); - TEST(test_expected_result(&args->status_update, &args->ast_test_error_str, c, "A${${baz}o:1:1}A", "A2A")); - TEST(test_expected_result(&args->status_update, &args->ast_test_error_str, c, "A${${baz}o:1:-1}A", "A2A")); - TEST(test_expected_result(&args->status_update, &args->ast_test_error_str, c, "A${${baz}o:-1:1}A", "A3A")); - TEST(test_expected_result(&args->status_update, &args->ast_test_error_str, c, "A${${baz}o:-2:1}A", "A2A")); - TEST(test_expected_result(&args->status_update, &args->ast_test_error_str, c, "A${${baz}o:-2:-1}A", "A2A")); + TEST(test_expected_result(test, c, "${foo}${foo}", "123123")); + TEST(test_expected_result(test, c, "A${foo}A${foo}A", "A123A123A")); + TEST(test_expected_result(test, c, "A${${bar}}A", "A123A")); + TEST(test_expected_result(test, c, "A${${baz}o}A", "A123A")); + TEST(test_expected_result(test, c, "A${${baz}o:1}A", "A23A")); + TEST(test_expected_result(test, c, "A${${baz}o:1:1}A", "A2A")); + TEST(test_expected_result(test, c, "A${${baz}o:1:-1}A", "A2A")); + TEST(test_expected_result(test, c, "A${${baz}o:-1:1}A", "A3A")); + TEST(test_expected_result(test, c, "A${${baz}o:-2:1}A", "A2A")); + TEST(test_expected_result(test, c, "A${${baz}o:-2:-1}A", "A2A")); #undef TEST /* For testing dialplan functions */ @@ -276,7 +268,7 @@ AST_TEST_DEFINE(test_substitution) if (acf->read && acf->read2) { char expression[80]; snprintf(expression, sizeof(expression), "${%s(foo)}", cmd); - if (AST_TEST_FAIL == test_chan_function(&args->status_update, &args->ast_test_error_str,c, expression)) { + if (AST_TEST_FAIL == test_chan_function(test, c, expression)) { res = AST_TEST_FAIL; } } diff --git a/tests/test_utils.c b/tests/test_utils.c index 92db72a089..b79da926f9 100644 --- a/tests/test_utils.c +++ b/tests/test_utils.c @@ -4,6 +4,7 @@ * Copyright (C) 2010, Digium, Inc. * * David Vossel + * Russell Bryant * * See http://www.asterisk.org for more information about * the Asterisk project. Please do not directly contact @@ -16,11 +17,12 @@ * at the top of the source tree. */ -/*! \file - * +/*! + * \file * \brief Unit Tests for utils API * * \author David Vossel + * \author Russell Bryant */ /*** MODULEINFO @@ -54,60 +56,148 @@ AST_TEST_DEFINE(uri_encode_decode_test) break; } - ast_test_status_update(&args->status_update, "Input before executing ast_uri_encode:\n%s\n", in); - ast_test_status_update(&args->status_update, "Output expected for ast_uri_encode with enabling do_special_char:\n%s\n", expected1); - ast_test_status_update(&args->status_update, "Output expected for ast_uri_encode with out enabling do_special_char:\n%s\n\n", expected2); + ast_test_status_update(test, "Input before executing ast_uri_encode:\n%s\n", in); + ast_test_status_update(test, "Output expected for ast_uri_encode with enabling do_special_char: %s\n", expected1); + ast_test_status_update(test, "Output expected for ast_uri_encode with out enabling do_special_char: %s\n\n", expected2); /* Test with do_special_char enabled */ ast_uri_encode(in, out, sizeof(out), 1); - ast_test_status_update(&args->status_update, "Output after enabling do_special_char:\n%s\n", out); + ast_test_status_update(test, "Output after enabling do_special_char:\n%s\n", out); if (strcmp(expected1, out)) { - ast_test_status_update(&args->status_update, "ENCODE DOES NOT MATCH EXPECTED, FAIL\n"); - ast_str_append(&args->ast_test_error_str, 0, "enable do_special_char test encode failed: \n"); + ast_test_status_update(test, "ENCODE DOES NOT MATCH EXPECTED, FAIL\n"); res = AST_TEST_FAIL; } /* Verify uri decode matches original */ ast_uri_decode(out); if (strcmp(in, out)) { - ast_test_status_update(&args->status_update, "Decoded string did not match original input\n\n"); - ast_str_append(&args->ast_test_error_str, 0, "enable do_special_char test decode failed: \n"); + ast_test_status_update(test, "Decoded string did not match original input\n"); res = AST_TEST_FAIL; } else { - ast_test_status_update(&args->status_update, "Decoded string matched original input\n\n"); + ast_test_status_update(test, "Decoded string matched original input\n"); } /* Test with do_special_char disabled */ out[0] = '\0'; ast_uri_encode(in, out, sizeof(out), 0); - ast_test_status_update(&args->status_update, "Output after disabling do_special_char:\n%s\n", out); + ast_test_status_update(test, "Output after disabling do_special_char: %s\n", out); if (strcmp(expected2, out)) { - ast_test_status_update(&args->status_update, "ENCODE DOES NOT MATCH EXPECTED, FAIL\n"); - ast_str_append(&args->ast_test_error_str, 0, "no do_special_char test encode failed: \n"); + ast_test_status_update(test, "ENCODE DOES NOT MATCH EXPECTED, FAIL\n"); res = AST_TEST_FAIL; } /* Verify uri decode matches original */ ast_uri_decode(out); if (strcmp(in, out)) { - ast_test_status_update(&args->status_update, "Decoded string did not match original input\n\n"); - ast_str_append(&args->ast_test_error_str, 0, "no do_special_char test decode failed\n"); + ast_test_status_update(test, "Decoded string did not match original input\n"); res = AST_TEST_FAIL; } else { - ast_test_status_update(&args->status_update, "Decoded string matched original input\n\n"); + ast_test_status_update(test, "Decoded string matched original input\n"); + } + + return res; +} + +AST_TEST_DEFINE(md5_test) +{ + static const struct { + const char *input; + const char *expected_output; + } tests[] = { + { "apples", "daeccf0ad3c1fc8c8015205c332f5b42" }, + { "bananas", "ec121ff80513ae58ed478d5c5787075b" }, + { "reallylongstringaboutgoatcheese", "0a2d9280d37e2e37545cfef6e7e4e890" }, + }; + enum ast_test_result_state res = AST_TEST_PASS; + int i; + + switch (cmd) { + case TEST_INIT: + info->name = "md5_test"; + info->category = "main/utils/"; + info->summary = "MD5 test"; + info->description = + "This test exercises MD5 calculations." + ""; + return AST_TEST_NOT_RUN; + case TEST_EXECUTE: + break; + } + + ast_test_status_update(test, "Testing MD5 ...\n"); + + for (i = 0; i < ARRAY_LEN(tests); i++) { + char md5_hash[32]; + ast_md5_hash(md5_hash, tests[i].input); + if (strcasecmp(md5_hash, tests[i].expected_output)) { + ast_test_status_update(test, + "input: '%s' hash: '%s' expected hash: '%s'\n", + tests[i].input, md5_hash, tests[i].expected_output); + res = AST_TEST_FAIL; + } } + + return res; +} + +AST_TEST_DEFINE(sha1_test) +{ + static const struct { + const char *input; + const char *expected_output; + } tests[] = { + { "giraffe", + "fac8f1a31d2998734d6a5253e49876b8e6a08239" }, + { "platypus", + "1dfb21b7a4d35e90d943e3a16107ccbfabd064d5" }, + { "ParastratiosphecomyiaStratiosphecomyioides", + "58af4e8438676f2bd3c4d8df9e00ee7fe06945bb" }, + }; + enum ast_test_result_state res = AST_TEST_PASS; + int i; + + switch (cmd) { + case TEST_INIT: + info->name = "sha1_test"; + info->category = "main/utils/"; + info->summary = "SHA1 test"; + info->description = + "This test exercises SHA1 calculations." + ""; + return AST_TEST_NOT_RUN; + case TEST_EXECUTE: + break; + } + + ast_test_status_update(test, "Testing SHA1 ...\n"); + + for (i = 0; i < ARRAY_LEN(tests); i++) { + char sha1_hash[64]; + ast_sha1_hash(sha1_hash, tests[i].input); + if (strcasecmp(sha1_hash, tests[i].expected_output)) { + ast_test_status_update(test, + "input: '%s' hash: '%s' expected hash: '%s'\n", + tests[i].input, sha1_hash, tests[i].expected_output); + res = AST_TEST_FAIL; + } + } + return res; } static int unload_module(void) { AST_TEST_UNREGISTER(uri_encode_decode_test); + AST_TEST_UNREGISTER(md5_test); + AST_TEST_UNREGISTER(sha1_test); return 0; } static int load_module(void) { AST_TEST_REGISTER(uri_encode_decode_test); + AST_TEST_REGISTER(md5_test); + AST_TEST_REGISTER(sha1_test); return AST_MODULE_LOAD_SUCCESS; }