change base64 tests

This commit is contained in:
Gregory 2019-02-02 19:13:36 +02:00
parent c2eb5e5b3a
commit d6826a38c7
12 changed files with 122 additions and 96 deletions

View file

@ -66,7 +66,7 @@ SHA_SRC = ft_sha256_init.c \
BASE64_SRC = ft_base64_init.c \
ft_base64_fill_buffer.c \
ft_base64_encode_buffer.c
ft_base64_encode.c
SRC = main.c \
ft_ssl_init.c \

View file

@ -15,7 +15,7 @@
# include <stdint.h>
# define FT_BASE64_BLOCK_SIZE 3
# define FT_BASE64_GLUE_BLOCK_SIZE 3
# define FT_BASE64_CHARS_SIZE 4
# define FT_BASE64_ALPHABET_LENGTH 64
@ -24,7 +24,8 @@ typedef unsigned char t_byte1;
typedef struct s_base64_ctx
{
t_byte1 block[FT_BASE64_BLOCK_SIZE];
t_byte8 glue_block_bytes_count;
t_byte1 glue_block[FT_BASE64_GLUE_BLOCK_SIZE];
t_byte1 alphabet[FT_BASE64_ALPHABET_LENGTH];
t_byte1 chars[FT_BASE64_CHARS_SIZE];
} t_base64_ctx;
@ -32,6 +33,6 @@ typedef struct s_base64_ctx
void ft_base64_init(t_base64_ctx *ctx);
void ft_base64_fill_buffer(
t_base64_ctx *ctx, t_byte1 *data, t_byte8 size);
void ft_base64_encode_buffer(t_base64_ctx *ctx, t_byte8 len);
void ft_base64_encode(t_base64_ctx *ctx, t_byte1 *data);
#endif

View file

@ -13,9 +13,18 @@
#ifndef FT_SSL_H
# define FT_SSL_H
# include <stddef.h>
# define FT_SSL_ALGS_COUNT 3
# define FT_SSL_BUFFER_SIZE 1024
typedef enum e_alg_type
{
DIGEST,
CIPHER,
END
} t_alg_type;
typedef struct s_ft_ssl t_ft_ssl;
typedef void (*t_process_stdin)(t_ft_ssl *ft_ssl);
@ -38,6 +47,7 @@ typedef struct s_alorithm
t_process_stdin process_stdin;
t_process_file process_file;
t_process_string process_string;
t_alg_type type;
} t_algorithm;
struct s_ft_ssl
@ -46,6 +56,7 @@ struct s_ft_ssl
t_process_stdin process_stdin;
t_process_file process_file;
t_process_string process_string;
t_alg_type type;
};
void ft_ssl_usage(void);

View file

@ -75,7 +75,6 @@ MunitResult should_init_base64_ctx(const MunitParameter test_params[],
MunitResult should_fill_base64_buffer(const MunitParameter test_params[],
void *test_data);
MunitResult should_clean_base64_buffer(const MunitParameter [], void *);
MunitResult should_encode_base64_buffer(const MunitParameter test_params[],
MunitResult should_encode_base64_data(const MunitParameter test_params[],
void *test_data);
#endif

View file

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_base64_encode.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/02/02 18:26:32 by gtertysh #+# #+# */
/* Updated: 2019/02/02 18:26:51 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include "ft_base64.h"
void ft_base64_encode(t_base64_ctx *ctx, t_byte1 *data)
{
t_byte1 first_char;
t_byte1 second_char;
t_byte1 third_char;
t_byte1 fourth_char;
first_char = (data[0] >> 2) & 0x3f;
second_char = ((data[0] << 4) & 0x30) | ((data[1] >> 4) & 0xf);
third_char = ((data[1] << 2) & 0x3c) | ((data[2] >> 6) & 0x3);
fourth_char = data[2] & 0x3F;
ctx->chars[0] = ctx->alphabet[first_char];
ctx->chars[1] = ctx->alphabet[second_char];
ctx->chars[2] = ctx->alphabet[third_char];
ctx->chars[3] = ctx->alphabet[fourth_char];
}

View file

@ -1,27 +0,0 @@
#include "libft.h"
#include "ft_base64.h"
void ft_base64_encode_buffer(t_base64_ctx *ctx, t_byte8 len)
{
t_byte1 first_char;
t_byte1 second_char;
t_byte1 third_char;
t_byte1 fourth_char;
first_char = (ctx->block[0] >> 2) & 0x3f;
second_char = ((ctx->block[0] << 4) & 0x30) | ((ctx->block[1] >> 4) & 0xf);
third_char = ((ctx->block[1] << 2) & 0x3c) | ((ctx->block[2] >> 6) & 0x3);
fourth_char = ctx->block[2] & 0x3F;
ctx->chars[0] = ctx->alphabet[first_char];
ctx->chars[1] = ctx->alphabet[second_char];
if (len < 2)
ctx->chars[2] = '=';
else
ctx->chars[2] = ctx->alphabet[third_char];
if (len < 3)
ctx->chars[3] = '=';
else
ctx->chars[3] = ctx->alphabet[fourth_char];
ft_bzero(ctx->block, FT_BASE64_BLOCK_SIZE);
}

View file

@ -1,7 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_base64_fill_buffer.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/02/02 18:27:34 by gtertysh #+# #+# */
/* Updated: 2019/02/02 18:27:39 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include "ft_base64.h"
void ft_base64_fill_buffer(t_base64_ctx *ctx, t_byte1 *data, t_byte8 size)
{
ft_memcpy(ctx->block, data, size);
ft_memcpy(ctx->glue_block, data, size);
ctx->glue_block_bytes_count = size;
}

View file

@ -6,7 +6,7 @@
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/19 17:57:47 by gtertysh #+# #+# */
/* Updated: 2019/01/19 17:58:16 by gtertysh ### ########.fr */
/* Updated: 2019/02/02 18:29:20 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,14 +15,10 @@
void ft_base64_init(t_base64_ctx *ctx)
{
char capital_case[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char lower_case[] = "abcdefghijklmnopqrstuvwxyz";
char rest[] = "0123456789+/";
ft_bzero(ctx->block, FT_BASE64_BLOCK_SIZE);
ctx->glue_block_bytes_count = 0;
ft_bzero(ctx->glue_block, FT_BASE64_GLUE_BLOCK_SIZE);
ft_bzero(ctx->chars, FT_BASE64_CHARS_SIZE);
ft_memcpy(ctx->alphabet, capital_case, 26);
ft_memcpy(ctx->alphabet + 26, lower_case, 26);
ft_memcpy(ctx->alphabet + 52, rest, 12);
ft_memcpy(ctx->alphabet, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 26);
ft_memcpy(ctx->alphabet + 26, "abcdefghijklmnopqrstuvwxyz", 26);
ft_memcpy(ctx->alphabet + 52, "0123456789+/", 12);
}

View file

@ -13,16 +13,30 @@
#include "ft_ssl.h"
#include "libft.h"
static t_algorithm *init_algorithms(void)
{
static t_algorithm algs[FT_SSL_ALGS_COUNT] = {
{"md5", ft_ssl_md5_stdin, ft_ssl_md5_file, ft_ssl_md5_string},
{"sha256", ft_ssl_sha256_stdin, ft_ssl_sha256_file, ft_ssl_sha256_string},
{"sha224", ft_ssl_sha224_stdin, ft_ssl_sha224_file, ft_ssl_sha224_string}
};
return (algs);
}
t_algorithm g_algorithms[] = {
{
"md5",
ft_ssl_md5_stdin,
ft_ssl_md5_file,
ft_ssl_md5_string,
DIGEST
},
{
"sha256",
ft_ssl_sha256_stdin,
ft_ssl_sha256_file,
ft_ssl_sha256_string,
DIGEST
},
{
"sha224",
ft_ssl_sha224_stdin,
ft_ssl_sha224_file,
ft_ssl_sha224_string,
DIGEST
},
{NULL, NULL, NULL, NULL, END}
};
static void init_flags(t_ft_ssl *ft_ssl)
{
@ -48,9 +62,11 @@ void ft_ssl_init(char *alg_name, t_ft_ssl *ft_ssl)
i = 0;
alg = 0;
init_flags(ft_ssl);
alg_walker = init_algorithms();
while (i < FT_SSL_ALGS_COUNT)
alg_walker = g_algorithms;
while (alg_walker)
{
if (alg_walker[i].type == END)
break ;
if (ft_strcmp(alg_name, alg_walker[i].name) == 0)
{
alg = &alg_walker[i];

View file

@ -15,9 +15,13 @@
void ft_ssl_usage(void)
{
ft_putstr("\n\nusage: \n");
ft_putstr("ft_ssl algorithm [-p|-q|-r] [[-s string...] [file...]]\n\n");
ft_putstr("algorithms:\n");
ft_putstr("md5 sha256 sha224\n\n\n");
ft_putstr("Message Digest commands:\n");
ft_putstr("md5\nsha256\nsha224\n\n");
ft_putstr("Digest command usage:\n");
ft_putstr("ft_ssl digest [-p|-q|-r] [[-s string...] [file...]]\n\n");
ft_putstr("Cipher commands:\n");
ft_putstr("base64\n");
ft_putstr("Cipher command usage:\n");
ft_putstr("ft_ssl cipher [-p|-q|-r] [[-s string...] [file...]]\n\n");
exit(1);
}

View file

@ -7,17 +7,19 @@ TEST_RESULT should_init_base64_ctx(TEST_PARAMS, TEST_DATA)
UNUSED(test_params);
UNUSED(test_data);
t_base64_ctx ctx;
char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\
abcdefghijklmnopqrstuvwxyz0123456789+/";
ft_base64_init(&ctx);
for (int i = 0; i < FT_BASE64_BLOCK_SIZE; i++)
munit_assert_uchar(ctx.block[i], ==, 0);
for (int i = 0; i < FT_BASE64_GLUE_BLOCK_SIZE; i++)
munit_assert_uchar(ctx.glue_block[i], ==, 0);
for (int i = 0; i < FT_BASE64_CHARS_SIZE; i++)
munit_assert_uchar(ctx.chars[i], ==, 0);
munit_assert_string_equal(alphabet, (char *)ctx.alphabet);
munit_assert_true(ctx.glue_block_bytes_count == 0);
return MUNIT_OK;
}
@ -31,14 +33,17 @@ TEST_RESULT should_fill_base64_buffer(TEST_PARAMS, TEST_DATA)
ft_base64_init(&ctx);
t_byte1 data[] = "123";
ft_base64_fill_buffer(&ctx, data, 3);
ft_base64_fill_buffer(&ctx, (t_byte1 *)"123", 3);
munit_assert_memory_equal(3, ctx.glue_block, "123");
munit_assert_true(ctx.glue_block_bytes_count == 3);
munit_assert_memory_equal(3, ctx.block, data);
ft_base64_fill_buffer(&ctx, (t_byte1 *)"22", 2);
munit_assert_memory_equal(2, ctx.glue_block, "22");
munit_assert_true(ctx.glue_block_bytes_count == 2);
return MUNIT_OK;
}
TEST_RESULT should_clean_base64_buffer(TEST_PARAMS, TEST_DATA)
TEST_RESULT should_encode_base64_data(TEST_PARAMS, TEST_DATA)
{
UNUSED(test_params);
UNUSED(test_data);
@ -47,32 +52,10 @@ TEST_RESULT should_clean_base64_buffer(TEST_PARAMS, TEST_DATA)
ft_base64_init(&ctx);
ft_base64_fill_buffer(&ctx, (t_byte1 *)"Man", 3);
ft_base64_encode_buffer(&ctx, 3);
for (int i = 0; i < FT_BASE64_BLOCK_SIZE; i++)
munit_assert_uchar(ctx.block[i], ==, 0);
return MUNIT_OK;
}
TEST_RESULT should_encode_base64_buffer(TEST_PARAMS, TEST_DATA)
{
UNUSED(test_params);
UNUSED(test_data);
t_base64_ctx ctx;
ft_base64_init(&ctx);
ft_base64_fill_buffer(&ctx, (t_byte1 *)"Man", 3);
ft_base64_encode_buffer(&ctx, 3);
ft_base64_encode(&ctx, (t_byte1 *)"Man");
munit_assert_string_equal((char *)ctx.chars, "TWFu");
ft_base64_fill_buffer(&ctx, (t_byte1 *)"Ma", 2);
ft_base64_encode_buffer(&ctx, 2);
munit_assert_string_equal((char *)ctx.chars, "TWE=");
ft_base64_fill_buffer(&ctx, (t_byte1 *)"M", 1);
ft_base64_encode_buffer(&ctx, 1);
munit_assert_string_equal((char *)ctx.chars, "TQ==");
ft_base64_encode(&ctx, (t_byte1 *)"LOL");
munit_assert_string_equal((char *)ctx.chars, "TE9M");
return MUNIT_OK;
}

View file

@ -43,8 +43,7 @@ MunitTest g_sha_tests[] = {
MunitTest g_base64_tests[] = {
IT("/init_ctx", should_init_base64_ctx, NULL, NULL, 0, NULL),
IT("/fills_buffer", should_fill_base64_buffer, NULL, NULL, 0, NULL),
IT("/cleans_buffer", should_clean_base64_buffer, NULL, NULL, 0, NULL),
IT("/encodes_buffer", should_encode_base64_buffer, NULL, NULL, 0, NULL),
IT("/encodes_data", should_encode_base64_data, NULL, NULL, 0, NULL),
END_IT
};