test: add test_base64.c
This commit is contained in:
		
							parent
							
								
									4c730f50f8
								
							
						
					
					
						commit
						c244d425ca
					
				
					 8 changed files with 193 additions and 41 deletions
				
			
		| 
						 | 
				
			
			@ -1,4 +1,4 @@
 | 
			
		|||
SUBDIRS = lib tools
 | 
			
		||||
SUBDIRS = lib tools tests
 | 
			
		||||
 | 
			
		||||
if BUILD_FUSE
 | 
			
		||||
SUBDIRS  += fuse
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,2 +1,2 @@
 | 
			
		|||
noinst_LIBRARIES = libstpdfs.a
 | 
			
		||||
libstpdfs_a_SOURCES = block.c superblock.c codec/base64.c
 | 
			
		||||
libstpdfs_a_SOURCES = block.c superblock.c codec/base64.c compression/lzp.c
 | 
			
		||||
| 
						 | 
				
			
			@ -11,7 +11,7 @@ base64_encode(char *dest, const char *src, size_t srclen)
 | 
			
		|||
	uint8_t a, b, c;
 | 
			
		||||
	uint32_t triple;
 | 
			
		||||
 | 
			
		||||
	encodedlen = 4 * ((srclen + 2) / 3) + 4;
 | 
			
		||||
	encodedlen = 4 * ((srclen + 2) / 3);
 | 
			
		||||
 | 
			
		||||
	if (dest == NULL)
 | 
			
		||||
	{
 | 
			
		||||
| 
						 | 
				
			
			@ -39,5 +39,6 @@ base64_encode(char *dest, const char *src, size_t srclen)
 | 
			
		|||
			dest[encodedlen - 2] = '='; 
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	dest[encodedlen] = 0;
 | 
			
		||||
	return (encodedlen);
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1,35 +1,5 @@
 | 
			
		|||
/*
 | 
			
		||||
        ; hash(h, x) (h = (h << 4) ^ x)
 | 
			
		||||
 | 
			
		||||
        section '.code' code
 | 
			
		||||
 | 
			
		||||
        ; xor hash, hash
 | 
			
		||||
        ; xor mask, mask
 | 
			
		||||
        ; j = 1
 | 
			
		||||
        ; while (insz > 0)
 | 
			
		||||
        ; {
 | 
			
		||||
        ;    if (ecx == 8)
 | 
			
		||||
        ;    {
 | 
			
		||||
        ;       mov [out], mask
 | 
			
		||||
        ;       xor ecx, ecx
 | 
			
		||||
        ;       xor mask, mask
 | 
			
		||||
        ;       j = 1;
 | 
			
		||||
        ;    }
 | 
			
		||||
        ;    c = in[inpos++]
 | 
			
		||||
        ;    if (c == table[hash])
 | 
			
		||||
        ;    {
 | 
			
		||||
        ;        mask |= 1 << ecx
 | 
			
		||||
        ;    }
 | 
			
		||||
        ;    else
 | 
			
		||||
        ;    {
 | 
			
		||||
        ;        table[hash] = c
 | 
			
		||||
        ;        out[j] = c;
 | 
			
		||||
        ;    }
 | 
			
		||||
        ;    HASH(hash, c)
 | 
			
		||||
        ;    ecx++;
 | 
			
		||||
        ; }
 | 
			
		||||
*/
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
 | 
			
		||||
#define LZP_HASH_ORDER 16
 | 
			
		||||
#define LZP_HASH_SIZE  (1 << LZP_HASH_ORDER)
 | 
			
		||||
| 
						 | 
				
			
			@ -37,21 +7,110 @@
 | 
			
		|||
#define HASH(h, x) ((h << 4) ^ x)
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
lzp_compress()
 | 
			
		||||
lzp_compress(uint8_t *out, size_t *outsz, const uint8_t *in, size_t insz)
 | 
			
		||||
{
 | 
			
		||||
	uint32_t hash = 0;
 | 
			
		||||
	uint8_t c;
 | 
			
		||||
	uint16_t hash = 0;
 | 
			
		||||
	uint32_t mask = 0;
 | 
			
		||||
	char c;
 | 
			
		||||
	uint8_t table[LZP_HASH_SIZE] = { 0 };
 | 
			
		||||
	size_t i, j;
 | 
			
		||||
	size_t inpos;
 | 
			
		||||
	size_t outpos;
 | 
			
		||||
	uint8_t buff[9];
 | 
			
		||||
 | 
			
		||||
	while (1)
 | 
			
		||||
	inpos = 0;
 | 
			
		||||
	outpos = 0;
 | 
			
		||||
	while (inpos < insz)
 | 
			
		||||
	{
 | 
			
		||||
		hash = HASH(hash, c);
 | 
			
		||||
		j = 1;
 | 
			
		||||
		for (i = 0; i < 8; i++)
 | 
			
		||||
		{
 | 
			
		||||
			if (inpos >= insz)
 | 
			
		||||
			{
 | 
			
		||||
				break;
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			c = in[inpos++];
 | 
			
		||||
 | 
			
		||||
			if (table[hash] == c)
 | 
			
		||||
			{
 | 
			
		||||
				mask |= 1 << i;
 | 
			
		||||
			}
 | 
			
		||||
			else
 | 
			
		||||
			{
 | 
			
		||||
				table[hash] = c;
 | 
			
		||||
				buff[j++] = c;
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			hash = HASH(hash, c);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if (i > 0)
 | 
			
		||||
		{
 | 
			
		||||
			buff[0] =  mask;
 | 
			
		||||
			if (out != NULL)
 | 
			
		||||
			{
 | 
			
		||||
				memcpy(out + outpos, buff, j);
 | 
			
		||||
			}
 | 
			
		||||
			outpos += j;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	*outsz = outpos;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
lzp_decompress()
 | 
			
		||||
lzp_decompress(uint8_t *out, size_t *outsz, const uint8_t *in, size_t insz)
 | 
			
		||||
{
 | 
			
		||||
	
 | 
			
		||||
	uint8_t c;
 | 
			
		||||
	uint16_t hash = 0;
 | 
			
		||||
	uint32_t mask = 0;
 | 
			
		||||
	uint8_t table[LZP_HASH_SIZE] = { 0 };
 | 
			
		||||
	size_t i, j;
 | 
			
		||||
	size_t inpos;
 | 
			
		||||
	size_t outpos;
 | 
			
		||||
	uint8_t buff[9];
 | 
			
		||||
 | 
			
		||||
	inpos = 0;
 | 
			
		||||
	outpos = 0;
 | 
			
		||||
	while (inpos < insz)
 | 
			
		||||
	{
 | 
			
		||||
		j = 0;
 | 
			
		||||
		if (inpos >= insz)
 | 
			
		||||
		{
 | 
			
		||||
			break;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		mask = in[inpos++];
 | 
			
		||||
 | 
			
		||||
		for (i = 0; i < 8; i++)
 | 
			
		||||
		{
 | 
			
		||||
			if ((mask & (1 << i)) != 0)
 | 
			
		||||
			{
 | 
			
		||||
				c = table[hash];
 | 
			
		||||
			}
 | 
			
		||||
			else
 | 
			
		||||
			{
 | 
			
		||||
				if (inpos >= insz)
 | 
			
		||||
				{
 | 
			
		||||
					break;
 | 
			
		||||
				}
 | 
			
		||||
				c = in[inpos++];
 | 
			
		||||
				table[hash] = c;
 | 
			
		||||
			}
 | 
			
		||||
			buff[j++] = c;
 | 
			
		||||
			hash = HASH(hash, c);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if (j > 0)
 | 
			
		||||
		{
 | 
			
		||||
			if (out != NULL)
 | 
			
		||||
			{
 | 
			
		||||
				memcpy(out + outpos, buff, j);
 | 
			
		||||
			}
 | 
			
		||||
			outpos += j;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	*outsz = outpos;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										0
									
								
								lib/xchacha12.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								lib/xchacha12.c
									
										
									
									
									
										Normal file
									
								
							
							
								
								
									
										10
									
								
								tests/Makefile.am
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								tests/Makefile.am
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,10 @@
 | 
			
		|||
AM_CPPFLAGS = -I$(top_srcdir)/lib
 | 
			
		||||
 | 
			
		||||
TESTS = test_lzp test_base64
 | 
			
		||||
check_PROGRAMS = test_lzp test_base64
 | 
			
		||||
 | 
			
		||||
LDADD = -lcmocka ../lib/libstpdfs.a
 | 
			
		||||
 | 
			
		||||
test_lzp_SOURCES = test_lzp.c
 | 
			
		||||
 | 
			
		||||
test_base64_SOURCES = test_base64.c
 | 
			
		||||
							
								
								
									
										57
									
								
								tests/test_base64.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										57
									
								
								tests/test_base64.c
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,57 @@
 | 
			
		|||
#include <stdarg.h>
 | 
			
		||||
#include <stddef.h>
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#include <setjmp.h>
 | 
			
		||||
#include <cmocka.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
 | 
			
		||||
const char *simple_enc = "SGkh";
 | 
			
		||||
const char *simple_dec = "Hi!";
 | 
			
		||||
 | 
			
		||||
const char *with_pad_enc = "bGlnaHQgdw==";
 | 
			
		||||
const char *with_pad_dec = "light w";
 | 
			
		||||
 | 
			
		||||
size_t base64_encode(char *dest, const char *src, size_t srclen);
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
test_encode(void **state)
 | 
			
		||||
{
 | 
			
		||||
	size_t len;
 | 
			
		||||
	char enc[14];
 | 
			
		||||
 | 
			
		||||
	len = base64_encode(enc, simple_dec, strlen(simple_dec));
 | 
			
		||||
 | 
			
		||||
	assert_int_equal(strlen(simple_enc), len);
 | 
			
		||||
	assert_string_equal(simple_enc, enc);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
test_encode_with_padding(void **state)
 | 
			
		||||
{
 | 
			
		||||
	size_t len;
 | 
			
		||||
	char enc[14];
 | 
			
		||||
 | 
			
		||||
	len = base64_encode(enc, with_pad_dec, strlen(with_pad_dec));
 | 
			
		||||
 | 
			
		||||
	assert_int_equal(strlen(with_pad_enc), len);
 | 
			
		||||
	assert_string_equal(with_pad_enc, enc);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
test_decode(void **state)
 | 
			
		||||
{
 | 
			
		||||
	(void)state;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int
 | 
			
		||||
main(void)
 | 
			
		||||
{
 | 
			
		||||
	const struct CMUnitTest tests[] = {
 | 
			
		||||
		cmocka_unit_test(test_encode),
 | 
			
		||||
		cmocka_unit_test(test_encode_with_padding),
 | 
			
		||||
		cmocka_unit_test(test_decode),
 | 
			
		||||
	};
 | 
			
		||||
 
 | 
			
		||||
	return cmocka_run_group_tests(tests, NULL, NULL);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										25
									
								
								tests/test_lzp.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								tests/test_lzp.c
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,25 @@
 | 
			
		|||
#include <stdarg.h>
 | 
			
		||||
#include <stddef.h>
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#include <setjmp.h>
 | 
			
		||||
#include <cmocka.h>
 | 
			
		||||
 | 
			
		||||
static const char *uncompress_data =  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
 | 
			
		||||
									 "abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc"
 | 
			
		||||
									 "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccabcabcabcabcabcabcabcabcabcabcabcabcabcabc";
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
test_lzp(void **state)
 | 
			
		||||
{
 | 
			
		||||
	(void)state;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int
 | 
			
		||||
main(void)
 | 
			
		||||
{
 | 
			
		||||
	const struct CMUnitTest tests[] = {
 | 
			
		||||
		cmocka_unit_test(test_lzp),
 | 
			
		||||
	};
 | 
			
		||||
 
 | 
			
		||||
	return cmocka_run_group_tests(tests, NULL, NULL);
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		
		Reference in a new issue