From 067ad59444fc30fe7ef97c6bb9126d6e9b0c39b8 Mon Sep 17 00:00:00 2001 From: Dimitri Lozeve Date: Thu, 4 Jun 2020 10:53:27 +0200 Subject: [PATCH] Add test for challenge 1 (hex to base64) --- ex01/main.c | 2 +- tests/main.c | 42 +++++++++++++++++++++++++++++++----------- utils.c | 5 ++--- utils.h | 3 +-- 4 files changed, 35 insertions(+), 17 deletions(-) diff --git a/ex01/main.c b/ex01/main.c index 81830f1..849bb30 100644 --- a/ex01/main.c +++ b/ex01/main.c @@ -10,7 +10,7 @@ int main(int argc, char *argv[argc + 1]) { unsigned char buf[512] = {0}; size_t len = hex_to_bytes(buf, argv[1]); - unsigned char out[512] = {'\0'}; + char out[512] = {'\0'}; bytes_to_base64(out, buf, len); printf("%s\n", out); diff --git a/tests/main.c b/tests/main.c index ed09292..dcdc364 100644 --- a/tests/main.c +++ b/tests/main.c @@ -1,7 +1,7 @@ #define MUNIT_ENABLE_ASSERT_ALIASES #include "munit.h" -#include #include "utils.h" +#include static MunitResult test_hex_to_byte(const MunitParameter params[], void *data) { (void)params; @@ -14,18 +14,38 @@ static MunitResult test_hex_to_byte(const MunitParameter params[], void *data) { return MUNIT_OK; } +static MunitResult test_hex_to_base64(const MunitParameter params[], + void *data) { + (void)params; + (void)data; + const char *input = "49276d206b696c6c696e6720796f757220627261696e206c696b6520" + "6120706f69736f6e6f7573206d757368726f6f6d"; + + unsigned char buf[512] = {0}; + size_t len = hex_to_bytes(buf, input); + + assert_size(len, ==, 48); + + char out[512] = {'\0'}; + bytes_to_base64(out, buf, len); + + assert_size(strlen(out), ==, 64); + assert_string_equal( + out, "SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t"); + + return MUNIT_OK; +} + static MunitTest test_suite_tests[] = { - {"/hex_to_byte", test_hex_to_byte, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL}, + {"/hex_to_byte", test_hex_to_byte, NULL, NULL, MUNIT_TEST_OPTION_NONE, + NULL}, + {"/hex_to_base64", test_hex_to_base64, NULL, NULL, MUNIT_TEST_OPTION_NONE, + NULL}, {NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL}}; -static const MunitSuite test_suite = { - (char*) "", - test_suite_tests, - NULL, - 1, - MUNIT_SUITE_OPTION_NONE -}; - -int main(int argc, char* argv[MUNIT_ARRAY_PARAM(argc + 1)]) { +static const MunitSuite test_suite = {(char *)"", test_suite_tests, NULL, 1, + MUNIT_SUITE_OPTION_NONE}; + +int main(int argc, char *argv[MUNIT_ARRAY_PARAM(argc + 1)]) { return munit_suite_main(&test_suite, NULL, argc, argv); } diff --git a/utils.c b/utils.c index c79eb32..20e506e 100644 --- a/utils.c +++ b/utils.c @@ -36,9 +36,8 @@ size_t hex_to_bytes(unsigned char *out, const char hex[static 1]) { return strlen(hex) / 2 + strlen(hex) % 2; } -unsigned char *bytes_to_base64(unsigned char *out, - const unsigned char in[static 1], size_t len) { - static const unsigned char base64_table[65] = +char *bytes_to_base64(char *out, const unsigned char in[static 1], size_t len) { + static const char base64_table[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; for (size_t i = 0; i < len - 2; i += 3) { diff --git a/utils.h b/utils.h index dd090fb..8bff75f 100644 --- a/utils.h +++ b/utils.h @@ -5,8 +5,7 @@ unsigned char hex_to_byte(const char c); size_t hex_to_bytes(unsigned char *out, const char hex[static 1]); -unsigned char *bytes_to_base64(unsigned char *out, - const unsigned char in[static 1], size_t len); +char *bytes_to_base64(char *out, const unsigned char in[static 1], size_t len); double frequency_score(unsigned char *buf, size_t len); unsigned int hamming(const char *s1, const char *s2);