libswarm-ng

C implementation of BMT hasher, Swarmhash and Single Owner Chunk for swarm
git clone git://git.defalsify.org/libswarm-ng.git
Log | Files | Refs | Submodules | README

commit b20ad5ca1e26ecdf9175949a2ead6d770995041f
parent 650c5a87edf4582a78f2fc9046d0ee5d8db516e9
Author: nolash <dev@holbrook.no>
Date:   Wed, 22 Sep 2021 22:33:16 +0200

Fix addition not assign bug in data chunk length for serialization

Diffstat:
MMakefile.dev | 7++++---
Msrc/chunk.c | 2+-
Mtest/check_bmt.c | 20++++++++++----------
Mtest/check_file.c | 66+++++++++++++++++++++++++++++++++---------------------------------
4 files changed, 48 insertions(+), 47 deletions(-)

diff --git a/Makefile.dev b/Makefile.dev @@ -67,9 +67,9 @@ build_lib: build .PHONY: test clean check_base: build build_check - #LD_LIBRARY_PATH=./build/:./build/test CK_FORK=no build/test/check_bmt + LD_LIBRARY_PATH=./build/:./build/test CK_FORK=no build/test/check_bmt LD_LIBRARY_PATH=./build/:./build/test CK_FORK=no build/test/check_chunk - #LD_LIBRARY_PATH=./build/:./build/test CK_FORK=no build/test/check_file + LD_LIBRARY_PATH=./build/:./build/test CK_FORK=no build/test/check_file check_keystore: build build_check_keystore LD_LIBRARY_PATH=./build/:./build/test CK_FORK=no build/test/check_keystore @@ -77,7 +77,8 @@ check_keystore: build build_check_keystore check_soc: build build_check_soc LD_LIBRARY_PATH=./build/:./build/test CK_FORK=no build/test/check_soc -check: check_base check_keystore check_soc +# TODO: file test is flaky, check_base should be first instead +check: check_keystore check_soc check_base test: check diff --git a/src/chunk.c b/src/chunk.c @@ -13,7 +13,7 @@ unsigned char* chunk_serialize(const swarm_chunk_t *chunk, unsigned char *z, siz crsr += SWARM_DATA_LENGTH_TYPESIZE; memcpy(z + crsr, chunk->payload, chunk->payload_sz); - *sz += crsr + chunk->payload_sz; + *sz = crsr + chunk->payload_sz; return z; } diff --git a/test/check_bmt.c b/test/check_bmt.c @@ -23,7 +23,7 @@ END_TEST START_TEST(check_bmt_sum_foo) { bmt_t bmt_context; char *input = "foo"; - char v_chk[_SWARM_WORD_SIZE]; + char v_chk[SWARM_WORD_SIZE]; char input_length = 3; int r; @@ -32,7 +32,7 @@ START_TEST(check_bmt_sum_foo) { ck_assert_int_eq(r, 0); hex2bin(HASH_OF_FOO, v_chk); - ck_assert_mem_eq(bmt_context.buf, v_chk, _SWARM_WORD_SIZE); + ck_assert_mem_eq(bmt_context.buf, v_chk, SWARM_WORD_SIZE); } END_TEST @@ -43,12 +43,12 @@ START_TEST(check_bmt_sum_vector) { int i; int lengths[] = { - _SWARM_WORD_SIZE - 1, - _SWARM_WORD_SIZE, - _SWARM_WORD_SIZE + 1, - _SWARM_WORD_SIZE * 2 - 1, - _SWARM_WORD_SIZE * 2, - _SWARM_WORD_SIZE * 2 + 1, + SWARM_WORD_SIZE - 1, + SWARM_WORD_SIZE, + SWARM_WORD_SIZE + 1, + SWARM_WORD_SIZE * 2 - 1, + SWARM_WORD_SIZE * 2, + SWARM_WORD_SIZE * 2 + 1, //SWARM_BLOCK_SIZE - 1, SWARM_BLOCK_SIZE, }; @@ -62,7 +62,7 @@ START_TEST(check_bmt_sum_vector) { //"", "c10090961e7682a10890c334d759a28426647141213abda93b096b892824d2ef", }; - char v_chk[_SWARM_WORD_SIZE]; + char v_chk[SWARM_WORD_SIZE]; char buf[SWARM_BLOCK_SIZE]; struct block_generator bg; @@ -79,7 +79,7 @@ START_TEST(check_bmt_sum_vector) { ck_assert_int_eq(r, 0); hex2bin(vectors[i], v_chk); - ck_assert_mem_eq(bmt_context.buf, v_chk, _SWARM_WORD_SIZE); + ck_assert_mem_eq(bmt_context.buf, v_chk, SWARM_WORD_SIZE); } } END_TEST diff --git a/test/check_file.c b/test/check_file.c @@ -28,7 +28,7 @@ void file_callback(const char *hash, const char *data, const size_t data_length, sprintf(filename, "h%d", file_callback_serial); fd = openat(file_callback_dir_fd, filename, O_CREAT | O_EXCL | O_RDWR | O_CLOEXEC | O_SYNC); ck_assert_int_ge(fd, 2); - write(fd, hash, _SWARM_WORD_SIZE); + write(fd, hash, SWARM_WORD_SIZE); close(fd); ck_assert_str_eq((char*)callback_static, callback_static_foo); @@ -37,7 +37,7 @@ void file_callback(const char *hash, const char *data, const size_t data_length, START_TEST(check_file_init) { - ck_assert_int_eq(_SWARM_BATCH_SIZE, 128); + ck_assert_int_eq(SWARM_BATCH_SIZE, 128); filehash_t fh; int r; @@ -45,7 +45,7 @@ START_TEST(check_file_init) { filehash_init(&fh); ck_assert_ptr_eq(fh.ptr[SWARM_LEVELS-1], fh.buf); - ck_assert_int_eq(fh.spans[1], _SWARM_BATCH_SIZE * SWARM_BLOCK_SIZE); + ck_assert_int_eq(fh.spans[1], SWARM_BATCH_SIZE * SWARM_BLOCK_SIZE); } END_TEST @@ -80,16 +80,16 @@ START_TEST(check_file_write_batch) { filehash_init(&fh); - for (i = 0; i < _SWARM_BATCH_SIZE; i++) { + for (i = 0; i < SWARM_BATCH_SIZE; i++) { r = block_generate(&bg, buf, SWARM_BLOCK_SIZE); ck_assert_int_eq(r, SWARM_BLOCK_SIZE); r = filehash_write(&fh, buf, SWARM_BLOCK_SIZE); ck_assert_int_eq(r, SWARM_BLOCK_SIZE); } - ck_assert_int_eq(fh.writes[0], _SWARM_BATCH_SIZE); - ck_assert_int_eq(fh.length, _SWARM_BATCH_SIZE * SWARM_BLOCK_SIZE); + ck_assert_int_eq(fh.writes[0], SWARM_BATCH_SIZE); + ck_assert_int_eq(fh.length, SWARM_BATCH_SIZE * SWARM_BLOCK_SIZE); ck_assert_ptr_eq(fh.ptr[0], fh.ptr[1]); - ck_assert_ptr_eq(fh.ptr[1], fh.buf + _SWARM_WORD_SIZE); + ck_assert_ptr_eq(fh.ptr[1], fh.buf + SWARM_WORD_SIZE); } END_TEST @@ -124,7 +124,7 @@ START_TEST(check_file_callback_intermediate) { int r; struct block_generator bg; - char buf[SWARM_BLOCK_SIZE * (_SWARM_BATCH_SIZE + 1)]; + char buf[SWARM_BLOCK_SIZE * (SWARM_BATCH_SIZE + 1)]; char cbdir_template[] = "swarmfile_test_callback_intermediate_XXXXXX"; char *cbdir; char cbfilename[1024]; @@ -142,9 +142,9 @@ START_TEST(check_file_callback_intermediate) { bg.v = 0; bg.m = 255; - r = block_generate(&bg, buf, SWARM_BLOCK_SIZE * (_SWARM_BATCH_SIZE + 1)); - ck_assert_int_eq(r, SWARM_BLOCK_SIZE * (_SWARM_BATCH_SIZE + 1)); - for (i = 0; i < _SWARM_BATCH_SIZE + 1; i++) { + r = block_generate(&bg, buf, SWARM_BLOCK_SIZE * (SWARM_BATCH_SIZE + 1)); + ck_assert_int_eq(r, SWARM_BLOCK_SIZE * (SWARM_BATCH_SIZE + 1)); + for (i = 0; i < SWARM_BATCH_SIZE + 1; i++) { r = filehash_write(&fh, buf + i * SWARM_BLOCK_SIZE, SWARM_BLOCK_SIZE); ck_assert_int_eq(r, SWARM_BLOCK_SIZE); @@ -167,23 +167,23 @@ START_TEST(check_file_callback_intermediate) { cbfd = open(cbfilename, O_RDONLY); ck_assert_int_gt(cbfd, 2); - r = read(cbfd, cbbuf, _SWARM_WORD_SIZE); - ck_assert_int_eq(r, _SWARM_WORD_SIZE); - ck_assert_mem_eq(cbbuf, fh.buf, _SWARM_WORD_SIZE); + r = read(cbfd, cbbuf, SWARM_WORD_SIZE); + ck_assert_int_eq(r, SWARM_WORD_SIZE); + ck_assert_mem_eq(cbbuf, fh.buf, SWARM_WORD_SIZE); close(cbfd); r = filehash_sum(&fh); - ck_assert_int_eq(r, SWARM_BLOCK_SIZE * (_SWARM_BATCH_SIZE + 1)); + ck_assert_int_eq(r, SWARM_BLOCK_SIZE * (SWARM_BATCH_SIZE + 1)); sprintf(cbfilename, "%s/h130", cbdir); cbfd = open(cbfilename, O_RDONLY); ck_assert_int_gt(cbfd, 2); - r = read(cbfd, cbbuf, _SWARM_WORD_SIZE); - ck_assert_mem_eq(cbbuf, fh.buf, _SWARM_WORD_SIZE); + r = read(cbfd, cbbuf, SWARM_WORD_SIZE); + ck_assert_mem_eq(cbbuf, fh.buf, SWARM_WORD_SIZE); - r = hex2bin("b8e1804e37a064d28d161ab5f256cc482b1423d5cd0a6b30fde7b0f51ece9199", cbbuf + _SWARM_WORD_SIZE); - ck_assert_int_eq(r, _SWARM_WORD_SIZE); - ck_assert_mem_eq(cbbuf, cbbuf + _SWARM_WORD_SIZE, _SWARM_WORD_SIZE); + r = hex2bin("b8e1804e37a064d28d161ab5f256cc482b1423d5cd0a6b30fde7b0f51ece9199", cbbuf + SWARM_WORD_SIZE); + ck_assert_int_eq(r, SWARM_WORD_SIZE); + ck_assert_mem_eq(cbbuf, cbbuf + SWARM_WORD_SIZE, SWARM_WORD_SIZE); close(cbfd); close(file_callback_dir_fd); @@ -200,24 +200,24 @@ START_TEST(check_file_vectors) { int whole; int part; int writes; - char v_chk[_SWARM_WORD_SIZE]; + char v_chk[SWARM_WORD_SIZE]; struct block_generator bg; char buf[SWARM_BLOCK_SIZE]; int lengths[] = { SWARM_BLOCK_SIZE, - SWARM_BLOCK_SIZE + _SWARM_WORD_SIZE - 1, - SWARM_BLOCK_SIZE + _SWARM_WORD_SIZE, - SWARM_BLOCK_SIZE + (_SWARM_WORD_SIZE * 2) - 1, - SWARM_BLOCK_SIZE + (_SWARM_WORD_SIZE * 2), + SWARM_BLOCK_SIZE + SWARM_WORD_SIZE - 1, + SWARM_BLOCK_SIZE + SWARM_WORD_SIZE, + SWARM_BLOCK_SIZE + (SWARM_WORD_SIZE * 2) - 1, + SWARM_BLOCK_SIZE + (SWARM_WORD_SIZE * 2), SWARM_BLOCK_SIZE * 2, - SWARM_BLOCK_SIZE * _SWARM_BATCH_SIZE, - SWARM_BLOCK_SIZE * _SWARM_BATCH_SIZE + _SWARM_WORD_SIZE - 1, - SWARM_BLOCK_SIZE * _SWARM_BATCH_SIZE + _SWARM_WORD_SIZE, - SWARM_BLOCK_SIZE * _SWARM_BATCH_SIZE + (_SWARM_WORD_SIZE * 2), - SWARM_BLOCK_SIZE * (_SWARM_BATCH_SIZE + 1), - SWARM_BLOCK_SIZE * (_SWARM_BATCH_SIZE + 2), - SWARM_BLOCK_SIZE * _SWARM_BATCH_SIZE * _SWARM_BATCH_SIZE, + SWARM_BLOCK_SIZE * SWARM_BATCH_SIZE, + SWARM_BLOCK_SIZE * SWARM_BATCH_SIZE + SWARM_WORD_SIZE - 1, + SWARM_BLOCK_SIZE * SWARM_BATCH_SIZE + SWARM_WORD_SIZE, + SWARM_BLOCK_SIZE * SWARM_BATCH_SIZE + (SWARM_WORD_SIZE * 2), + SWARM_BLOCK_SIZE * (SWARM_BATCH_SIZE + 1), + SWARM_BLOCK_SIZE * (SWARM_BATCH_SIZE + 2), + SWARM_BLOCK_SIZE * SWARM_BATCH_SIZE * SWARM_BATCH_SIZE, }; char *vectors[] = { @@ -268,7 +268,7 @@ START_TEST(check_file_vectors) { ck_assert_int_eq(r, lengths[i]); r = hex2bin(vectors[i], v_chk); - ck_assert_mem_eq(fh.buf, v_chk, _SWARM_WORD_SIZE); + ck_assert_mem_eq(fh.buf, v_chk, SWARM_WORD_SIZE); } } END_TEST