Add an MD5 test module.
[asterisk/asterisk.git] / tests / test_md5.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2010, Digium, Inc.
5  *
6  * Russell Bryant <russell@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*!
20  * \file
21  * \brief MD5 test
22  *
23  * \author Russell Bryant <russell@digium.com>
24  *
25  * \ingroup tests
26  */
27
28 /*** MODULEINFO
29         <depend>TEST_FRAMEWORK</depend>
30  ***/
31
32 #include "asterisk.h"
33
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35
36 #include "asterisk/utils.h"
37 #include "asterisk/module.h"
38 #include "asterisk/test.h"
39 #include "asterisk/md5.h"
40
41 AST_TEST_DEFINE(md5_test)
42 {
43         static const struct {
44                 const char *input;
45                 const char *expected_output;
46         } tests[] = {
47                 { "apples",                          "daeccf0ad3c1fc8c8015205c332f5b42" },
48                 { "bananas",                         "ec121ff80513ae58ed478d5c5787075b" },
49                 { "reallylongstringaboutgoatcheese", "0a2d9280d37e2e37545cfef6e7e4e890" },
50         };
51         enum ast_test_result_state res = AST_TEST_PASS;
52         int i;
53
54         switch (cmd) {
55         case TEST_INIT:
56                 info->name = "md5_test";
57                 info->category = "main/";
58                 info->summary = "MD5 test";
59                 info->description =
60                         "This test exercises MD5 calculations.\n"
61                         "";
62                 return AST_TEST_NOT_RUN;
63         case TEST_EXECUTE:
64                 break;
65         }
66
67         ast_test_status_update(&args->status_update, "Testing MD5 ...\n");
68
69         for (i = 0; i < ARRAY_LEN(tests); i++) {
70                 char md5_hash[32];
71                 ast_md5_hash(md5_hash, tests[i].input);
72                 if (strcasecmp(md5_hash, tests[i].expected_output)) {
73                         ast_test_status_update(&args->status_update,
74                                         "input: '%s'  hash: '%s'  expected hash: '%s'\n",
75                                         tests[i].input, md5_hash, tests[i].expected_output);
76                         ast_str_append(&args->ast_test_error_str, 0,
77                                         "input: '%s'  hash: '%s'  expected hash: '%s'\n",
78                                         tests[i].input, md5_hash, tests[i].expected_output);
79                         res = AST_TEST_FAIL;
80                 }
81         }
82
83         return res;
84 }
85
86 static int unload_module(void)
87 {
88         AST_TEST_UNREGISTER(md5_test);
89         return 0;
90 }
91
92 static int load_module(void)
93 {
94         AST_TEST_REGISTER(md5_test);
95         return AST_MODULE_LOAD_SUCCESS;
96 }
97
98 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "MD5 Test");