1
0
mirror of https://gitlab.xiph.org/xiph/icecast-server.git synced 2024-09-22 04:15:54 -04:00

Update: Added tests for MD5 handling code

This commit is contained in:
Philipp Schafft 2023-02-12 19:51:10 +00:00
parent f741bd131d
commit c7c4a63b66
2 changed files with 84 additions and 0 deletions

View File

@ -33,5 +33,11 @@ ctest_string_renderer_test_LDADD = \
icecast-string_renderer.o
check_PROGRAMS += ctest_string_renderer.test
ctest_crypt_test_SOURCES = tests/ctest_crypt.c
ctest_crypt_test_LDADD = \
icecast-util_string.o \
icecast-md5.o
check_PROGRAMS += ctest_crypt.test
# Add all programs to TESTS
TESTS = $(check_PROGRAMS)

78
src/tests/ctest_crypt.c Normal file
View File

@ -0,0 +1,78 @@
/* Icecast
*
* This program is distributed under the GNU General Public License, version 2.
* A copy of this license is included with this source.
*
* Copyright 2018-2023, Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>,
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <igloo/tap.h>
#include "../md5.h"
#include "../util_string.h"
void test_md5_hash(const char *in, const char *expect, bool positive)
{
struct MD5Context context;
unsigned char digest[16];
char *out;
MD5Init(&context);
MD5Update(&context, (const unsigned char *)in, strlen(in));
MD5Final(digest, &context);
out = util_bin_to_hex(digest, 16);
if (positive) {
igloo_tap_test("md5 positive vector", strcmp(out, expect) == 0);
} else {
igloo_tap_test("md5 negative vector", strcmp(out, expect) != 0);
}
free(out);
}
static void test_md5(void)
{
struct vector {
const char *in;
const char *out;
};
static const struct vector table_pos[] = {
{"", "d41d8cd98f00b204e9800998ecf8427e"},
{"\n", "68b329da9893e34099c7d8ad5cb9c940"}
};
static const struct vector table_neg[] = {
{"XXX", "d41d8cd98f00b204e9800998ecf8427e"},
{"YYY", "1234567890abcdef1234567890abcdef"}
};
size_t i;
for (i = 0; i < (sizeof(table_pos)/sizeof(*table_pos)); i++) {
test_md5_hash(table_pos[i].in, table_pos[i].out, true);
}
for (i = 0; i < (sizeof(table_neg)/sizeof(*table_neg)); i++) {
test_md5_hash(table_neg[i].in, table_neg[i].out, false);
}
}
int main (void)
{
igloo_tap_init();
igloo_tap_exit_on(igloo_TAP_EXIT_ON_FIN, NULL);
igloo_tap_group_run("md5 vectors", test_md5);
igloo_tap_fin();
return EXIT_FAILURE; // return failure as we should never reach this point!
}