From 14e2390ef808e6f606136769b9d3d9653e2a8e2f Mon Sep 17 00:00:00 2001 From: Gregory Date: Sat, 19 Jan 2019 18:16:00 +0200 Subject: [PATCH] base64 init context --- Makefile | 12 ++++++++++-- inc/ft_base64.h | 31 +++++++++++++++++++++++++++++++ inc/tests.h | 31 ++++++++++++++++++++++++------- src/base64/ft_base64_init.c | 20 ++++++++++++++++++++ t/base64_tests.c | 18 ++++++++++++++++++ t/md5_tests.c | 14 +++++++------- t/tests.c | 23 +++++++++++++++-------- 7 files changed, 125 insertions(+), 24 deletions(-) create mode 100644 inc/ft_base64.h create mode 100644 src/base64/ft_base64_init.c create mode 100644 t/base64_tests.c diff --git a/Makefile b/Makefile index 0aabdcd0..7fa21d8c 100644 --- a/Makefile +++ b/Makefile @@ -20,6 +20,7 @@ ROOT := $(shell pwd) SRC_DIR := $(ROOT)/src/ MD5_DIR := $(SRC_DIR)/md5/ SHA_DIR := $(SRC_DIR)/sha/ +B64_DIR := $(SRC_DIR)/base64/ OBJ_DIR := $(ROOT)/obj/ INC_DIR := $(ROOT)/inc/ LIB_DIR := $(ROOT)/lib/ @@ -63,6 +64,7 @@ SHA_SRC = ft_sha256_init.c \ ft_sha224_final.c \ ft_sha224_digest_string.c +BASE64_SRC = ft_base64_init.c SRC = main.c \ ft_ssl_init.c \ @@ -81,7 +83,8 @@ SRC = main.c \ ft_ssl_usage.c SRC += $(MD5_SRC) \ - $(SHA_SRC) + $(SHA_SRC) \ + $(BASE64_SRC) # project object files OBJ = $(addprefix $(OBJ_DIR), $(SRC:.c=.o)) @@ -97,11 +100,15 @@ MD5_TESTS += $(MD5_SRC) SHA_TESTS = sha_tests.c SHA_TESTS += $(SHA_SRC) +BASE64_TESTS = base64_tests.c +BASE64_TESTS += $(BASE64_SRC) + TEST_SRC = tests.c \ munit.c TEST_SRC += $(MD5_TESTS) \ - $(SHA_TESTS) + $(SHA_TESTS) \ + $(BASE64_TESTS) TEST_OBJ = $(addprefix $(OBJ_DIR), $(TEST_SRC:.c=.o)) @@ -190,6 +197,7 @@ multi: vpath %.c $(SRC_DIR) \ $(MD5_DIR) \ $(SHA_DIR) \ + $(B64_DIR) \ $(TST_DIR) \ $(MUINUT_DIR) diff --git a/inc/ft_base64.h b/inc/ft_base64.h new file mode 100644 index 00000000..1ccdf067 --- /dev/null +++ b/inc/ft_base64.h @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_base64.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/01/19 17:58:39 by gtertysh #+# #+# */ +/* Updated: 2019/01/19 17:59:19 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_BASE64_H +# define FT_BASE64_H + +# include + +# define FT_BASE64_BLOCK_SIZE 24 + +typedef uint64_t t_byte8; +typedef unsigned char t_byte1; + +typedef struct s_base64_ctx +{ + t_byte8 block_bit_index; + t_byte1 block[FT_BASE64_BLOCK_SIZE]; +} t_base64_ctx; + +void ft_base64_init(t_base64_ctx *ctx); + +#endif diff --git a/inc/tests.h b/inc/tests.h index 00c2f9f1..eb64b718 100644 --- a/inc/tests.h +++ b/inc/tests.h @@ -15,21 +15,29 @@ # define MUNIT_ENABLE_ASSERT_ALIASES # include "munit.h" +# include "tests_macros.h" -MunitResult should_init_ctx(const MunitParameter test_params[], +/* +** MD5 +*/ +MunitResult should_init_md5_ctx(const MunitParameter test_params[], void *test_data); -MunitResult update_should_change_count(const MunitParameter test_params[], +MunitResult md5_update_change_count(const MunitParameter test_params[], void *test_data); -MunitResult decode_string_to_int(const MunitParameter test_params[], +MunitResult md5_decode_string_to_int(const MunitParameter test_params[], void *test_data); -MunitResult encode_bits_to_string(const MunitParameter test_params[], +MunitResult md5_encode_bits_to_string(const MunitParameter test_params[], void *test_data); -MunitResult encode_register(const MunitParameter test_params[], +MunitResult md5_encode_register(const MunitParameter test_params[], void *test_data); -MunitResult create_digest(const MunitParameter test_params[], +MunitResult md5_create_digest(const MunitParameter test_params[], void *test_data); -MunitResult create_string(const MunitParameter test_params[], +MunitResult md5_create_string(const MunitParameter test_params[], void *test_data); + +/* +** SHA256 +*/ MunitResult should_init_ctx_sha256(const MunitParameter test_params[], void *test_data); MunitResult decode_string_to_int_sha256(const MunitParameter test_params[], @@ -48,6 +56,10 @@ MunitResult compute_digest_sha256(const MunitParameter test_params[], void *test_data); MunitResult create_digest_string_sha256(const MunitParameter test_params[], void *test_data); + +/* +** SHA224 +*/ MunitResult should_init_ctx_sha224(const MunitParameter test_params[], void *test_data); MunitResult compute_digest_sha224(const MunitParameter test_params[], @@ -55,4 +67,9 @@ MunitResult compute_digest_sha224(const MunitParameter test_params[], MunitResult create_digest_string_sha224(const MunitParameter test_params[], void *test_data); +/* +** BASE64 +*/ +MunitResult should_init_base64_ctx(const MunitParameter test_params[], + void *test_data); #endif diff --git a/src/base64/ft_base64_init.c b/src/base64/ft_base64_init.c new file mode 100644 index 00000000..fdc599d0 --- /dev/null +++ b/src/base64/ft_base64_init.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_base64_init.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/01/19 17:57:47 by gtertysh #+# #+# */ +/* Updated: 2019/01/19 17:58:16 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_base64.h" +#include "libft.h" + +void ft_base64_init(t_base64_ctx *ctx) +{ + ctx->block_bit_index = 0; + ft_bzero(ctx->block, FT_BASE64_BLOCK_SIZE); +} diff --git a/t/base64_tests.c b/t/base64_tests.c new file mode 100644 index 00000000..82808900 --- /dev/null +++ b/t/base64_tests.c @@ -0,0 +1,18 @@ +#include "tests.h" +#include "tests_macros.h" +#include "ft_base64.h" + +TEST_RESULT should_init_base64_ctx(TEST_PARAMS, TEST_DATA) +{ + UNUSED(test_params); + UNUSED(test_data); + t_base64_ctx ctx; + + ft_base64_init(&ctx); + + munit_assert_true(ctx.block_bit_index == 0); + for (int i = 0; i < FT_BASE64_BLOCK_SIZE; i++) + munit_assert_uchar(ctx.block[i], ==, 0); + + return MUNIT_OK; +} \ No newline at end of file diff --git a/t/md5_tests.c b/t/md5_tests.c index 8b9989c0..00840cc8 100644 --- a/t/md5_tests.c +++ b/t/md5_tests.c @@ -15,7 +15,7 @@ #include "ft_md5.h" #include "libft.h" -TEST_RESULT should_init_ctx(TEST_PARAMS, TEST_DATA) +TEST_RESULT should_init_md5_ctx(TEST_PARAMS, TEST_DATA) { UNUSED(test_params); UNUSED(test_data); @@ -34,7 +34,7 @@ TEST_RESULT should_init_ctx(TEST_PARAMS, TEST_DATA) return MUNIT_OK; } -TEST_RESULT decode_string_to_int(TEST_PARAMS, TEST_DATA) +TEST_RESULT md5_decode_string_to_int(TEST_PARAMS, TEST_DATA) { UNUSED(test_params); UNUSED(test_data); @@ -56,7 +56,7 @@ TEST_RESULT decode_string_to_int(TEST_PARAMS, TEST_DATA) return MUNIT_OK; } -TEST_RESULT update_should_change_count(TEST_PARAMS, TEST_DATA) +TEST_RESULT md5_update_change_count(TEST_PARAMS, TEST_DATA) { UNUSED(test_params); UNUSED(test_data); @@ -72,7 +72,7 @@ TEST_RESULT update_should_change_count(TEST_PARAMS, TEST_DATA) return MUNIT_OK; } -TEST_RESULT encode_bits_to_string(TEST_PARAMS, TEST_DATA) +TEST_RESULT md5_encode_bits_to_string(TEST_PARAMS, TEST_DATA) { UNUSED(test_params); UNUSED(test_data); @@ -101,7 +101,7 @@ TEST_RESULT encode_bits_to_string(TEST_PARAMS, TEST_DATA) return MUNIT_OK; } -TEST_RESULT encode_register(TEST_PARAMS, TEST_DATA) +TEST_RESULT md5_encode_register(TEST_PARAMS, TEST_DATA) { UNUSED(test_params); UNUSED(test_data); @@ -127,7 +127,7 @@ TEST_RESULT encode_register(TEST_PARAMS, TEST_DATA) return MUNIT_OK; } -TEST_RESULT create_digest(TEST_PARAMS, TEST_DATA) +TEST_RESULT md5_create_digest(TEST_PARAMS, TEST_DATA) { UNUSED(test_params); UNUSED(test_data); @@ -168,7 +168,7 @@ TEST_RESULT create_digest(TEST_PARAMS, TEST_DATA) return MUNIT_OK; } -TEST_RESULT create_string(TEST_PARAMS, TEST_DATA) +TEST_RESULT md5_create_string(TEST_PARAMS, TEST_DATA) { UNUSED(test_params); UNUSED(test_data); diff --git a/t/tests.c b/t/tests.c index ebdc72c2..d896c553 100644 --- a/t/tests.c +++ b/t/tests.c @@ -14,13 +14,13 @@ #include "tests_macros.h" MunitTest g_md5_tests[] = { - IT("/init_ctx", should_init_ctx, NULL, NULL, 0, NULL), - IT("/updates_ctx_count", update_should_change_count, NULL, NULL, 0, NULL), - IT("/decode_string_to_int", decode_string_to_int, NULL, NULL, 0, NULL), - IT("/encode_bits_to_string", encode_bits_to_string, NULL, NULL, 0, NULL), - IT("/encode_register", encode_register, NULL, NULL, 0, NULL), - IT("/creates_digest", create_digest, NULL, NULL, 0, NULL), - IT("/creates_string", create_string, NULL, NULL, 0, NULL), + IT("/init_ctx", should_init_md5_ctx, NULL, NULL, 0, NULL), + IT("/updates_ctx_count", md5_update_change_count, NULL, NULL, 0, NULL), + IT("/decode_string_to_int", md5_decode_string_to_int, NULL, NULL, 0, NULL), + IT("/encode_to_string", md5_encode_bits_to_string, NULL, NULL, 0, NULL), + IT("/encode_register", md5_encode_register, NULL, NULL, 0, NULL), + IT("/creates_digest", md5_create_digest, NULL, NULL, 0, NULL), + IT("/creates_string", md5_create_string, NULL, NULL, 0, NULL), END_IT }; @@ -37,11 +37,18 @@ MunitTest g_sha_tests[] = { IT("/compute_digest_224", compute_digest_sha224, NULL, NULL, 0, NULL), IT("/creates_string_256", create_digest_string_sha256, NULL, NULL, 0, NULL), IT("/creates_string_224", create_digest_string_sha224, NULL, NULL, 0, NULL), - END_IT}; + END_IT +}; + +MunitTest g_base64_tests[] = { + IT("/init_ctx", should_init_base64_ctx, NULL, NULL, 0, NULL), + END_IT +}; static const MunitSuite g_ft_ssl_suites[] = { {(char *)"/md5_suite", g_md5_tests, NULL, 1, MUNIT_SUITE_OPTION_NONE}, {(char *)"/sha_suite", g_sha_tests, NULL, 1, MUNIT_SUITE_OPTION_NONE}, + {(char *)"/base64_suite", g_base64_tests, NULL, 1, MUNIT_SUITE_OPTION_NONE}, {NULL, NULL, NULL, 0, MUNIT_SUITE_OPTION_NONE} };