From 5f2cffdd8258ebe07410f8f55623c4a711722cd2 Mon Sep 17 00:00:00 2001 From: SEK1RO Date: Mon, 23 Sep 2024 21:20:06 +0300 Subject: [PATCH 1/6] submodule(distrib/arch): v1.0.1 --- distrib/arch | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/distrib/arch b/distrib/arch index 0dcfd06..a829d97 160000 --- a/distrib/arch +++ b/distrib/arch @@ -1 +1 @@ -Subproject commit 0dcfd063edcc7c7675e092da3c7d6130578ec727 +Subproject commit a829d9746a263ee0f95de1114b4a14e8fa6541ae From 5396abe20267b6397118c9ea5ee1fb7c410070d3 Mon Sep 17 00:00:00 2001 From: SEK1RO Date: Thu, 26 Sep 2024 12:00:23 +0300 Subject: [PATCH 2/6] feat(cover): gcov, gcovr --- .gitignore | 1 + Makefile | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 9bcc349..c36cc14 100644 --- a/.gitignore +++ b/.gitignore @@ -43,6 +43,7 @@ bin obj lib doc +cov # IDE .vscode diff --git a/Makefile b/Makefile index d1b68f5..d6cf51c 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ DEBUG ?= false USRDIR ?= /usr .PHONY: build i install uni uninstall\ - tools library tests docs clean + tools library tests docs cov clean LIB = basen OBJS =\ @@ -59,6 +59,8 @@ DIRS =\ ${OBJDIR}\ ${OBJDIR}/hash\ ${LIBDIR}\ + doc\ + cov build: library tools @@ -80,8 +82,19 @@ uninstall: docs: doxygen Doxyfile +cover: ${DIRS} ${patsubst %, ${BINDIR}/%${-g}-cov, ${TESTS}} + ${patsubst %, ./${BINDIR}/%${-g}-cov;, ${TESTS}} + gcovr --html-nested cov/index.html + gcovr + +${OBJDIR}/%${-g}-cov.o: ${SRCDIR}/%.cpp ${INCDIR}/${LIB}/%.hpp + ${CC} -o $@ -c $< -I${INCDIR} ${-l} ${CFLAGS} --coverage + +${BINDIR}/%${-g}-cov: ${TESTDIR}/%.cpp ${patsubst %, ${OBJDIR}/%${-g}-cov.o, ${OBJS}} + ${CC} -o $@ $^ -I${INCDIR} ${-l} -lgtest ${CFLAGS} --coverage + clean: - rm -rf ${OBJDIR}/* ${LIBDIR}/* ${BINDIR}/* + rm -rf ${DIRS} ifneq (${OBJS},) From 119bca6c8769e3da0b53a7b7aef073b40fdb559e Mon Sep 17 00:00:00 2001 From: SEK1RO Date: Thu, 26 Sep 2024 12:06:51 +0300 Subject: [PATCH 3/6] fix(docs.yml): on realease published --- .github/workflows/docs.yml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index 4a85f6f..883a6f6 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -1,10 +1,8 @@ name: Deploy documentation on: - push: - branches: ["main"] - - workflow_dispatch: + release: + types: [published] permissions: contents: read From 6d689cb9b3855b02133da3536d0de04cd5487de6 Mon Sep 17 00:00:00 2001 From: SEK1RO Date: Thu, 26 Sep 2024 12:26:07 +0300 Subject: [PATCH 4/6] fix(README.md): coverage --- README.md | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index bd72246..8d9b0fe 100644 --- a/README.md +++ b/README.md @@ -30,18 +30,6 @@ Uninstall: ``` sudo make uni USRDIR=(Your installation dir) ``` -For build with with debug flags: -``` -make -j $(nproc) DEBUG= -``` -For build tests (needed gtest package as dependency): -``` -make tests -j $(nproc) DEBUG= -``` -And also flag for dynamic linking (if possible): -``` -make tools -j $(nproc) SHARED= -``` ## Documentation @@ -56,4 +44,17 @@ Now we would like to implement the following features: - BCH - Bech32 +For build with with debug flags: +``` +make -j $(nproc) DEBUG= +``` +For build tests (needed gtest package as dependency): +``` +make tests -j $(nproc) DEBUG= +``` +For generating coverage: +``` +make cover -j $(nproc) DEBUG= +``` + [⬆️ Contents](#contents) From b0e645679d15a86aa8b74d6d03f4e48bcbcb7171 Mon Sep 17 00:00:00 2001 From: SEK1RO Date: Thu, 26 Sep 2024 17:17:01 +0300 Subject: [PATCH 5/6] fix(hex): coverage --- Makefile | 3 +-- test/test-hex.cpp | 39 +++++++++++++++++++++------------------ 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/Makefile b/Makefile index d6cf51c..525492d 100644 --- a/Makefile +++ b/Makefile @@ -84,8 +84,7 @@ docs: cover: ${DIRS} ${patsubst %, ${BINDIR}/%${-g}-cov, ${TESTS}} ${patsubst %, ./${BINDIR}/%${-g}-cov;, ${TESTS}} - gcovr --html-nested cov/index.html - gcovr + gcovr --html-nested cov/index.html --txt --exclude-throw-branches ${OBJDIR}/%${-g}-cov.o: ${SRCDIR}/%.cpp ${INCDIR}/${LIB}/%.hpp ${CC} -o $@ -c $< -I${INCDIR} ${-l} ${CFLAGS} --coverage diff --git a/test/test-hex.cpp b/test/test-hex.cpp index bd84488..fb8dc79 100644 --- a/test/test-hex.cpp +++ b/test/test-hex.cpp @@ -1,21 +1,28 @@ +#include + #include #include using namespace hex; +TEST(hex, isValid) +{ + std::vector> tests = { + {true, "1234"}, + {false, "!@/"}, + }; + for (auto it : tests) + EXPECT_EQ(it.first, isValid(it.second)); +} TEST(hex, encode) { std::vector data = {0x74, 0x65, 0x73, 0x74}; EXPECT_EQ("74657374", encode(data)); - try - { - std::string str = ""; - encode(data.data(), data.size(), str.data(), str.size()); - } - catch (const std::exception &e) - { - EXPECT_STREQ(e.what(), "hex::encode: not enough allocated length"); - } + + std::string str = ""; + EXPECT_THROW(encode(data.data(), std::numeric_limits::max(), str.data(), str.size()), std::overflow_error); + EXPECT_THROW(encode(data.data(), data.size(), str.data(), str.size()), std::length_error); + EXPECT_NO_THROW(encode(data.data(), 0, str.data(), str.size())); } TEST(hex, encode_1e7) { @@ -26,15 +33,11 @@ TEST(hex, decode) { std::vector data = {0x61, 0x6e, 0x6f}; EXPECT_EQ(decode("616e6f"), data); - try - { - std::string str = ""; - decode("616", 3, data.data(), data.size()); - } - catch (const std::exception &e) - { - EXPECT_STREQ(e.what(), "hex::decode: isn't hex"); - } + + EXPECT_THROW(decode("FFF", 3, data.data(), data.size()), std::logic_error); + EXPECT_THROW(decode("!@#!", 4, data.data(), data.size()), std::logic_error); + EXPECT_THROW(decode("FF", 2, data.data(), 0), std::length_error); + EXPECT_NO_THROW(decode("", 0, data.data(), 0)); } TEST(hex, decode_1e7) { From 8da5ce49ef1857c8d3231e929c17bfa0fcacf7bf Mon Sep 17 00:00:00 2001 From: SEK1RO Date: Thu, 26 Sep 2024 20:05:38 +0300 Subject: [PATCH 6/6] fix(base64): coverage --- Makefile | 1 + src/base64.cpp | 33 +++++++++++++++------------------ test/test-base64.cpp | 12 ++++++++++++ 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index 525492d..77bf9be 100644 --- a/Makefile +++ b/Makefile @@ -83,6 +83,7 @@ docs: doxygen Doxyfile cover: ${DIRS} ${patsubst %, ${BINDIR}/%${-g}-cov, ${TESTS}} + rm -f **/*.gcda ${patsubst %, ./${BINDIR}/%${-g}-cov;, ${TESTS}} gcovr --html-nested cov/index.html --txt --exclude-throw-branches diff --git a/src/base64.cpp b/src/base64.cpp index 8976085..e00972b 100644 --- a/src/base64.cpp +++ b/src/base64.cpp @@ -74,25 +74,22 @@ namespace base64 str[i * 4 + 3] = digits[data[i * 3 + 2] & 0x3F]; } uint64_t last_idx = data_size / 3 * 4; - if (last_idx + 3 < str_size) + switch (data_size % 3) { - switch (data_size % 3) - { - case 1: - str[last_idx] = digits[data[data_size - 1] >> 2]; - str[last_idx + 1] = digits[data[data_size - 1] << 4 & 0x30]; - str[last_idx + 2] = '='; - str[last_idx + 3] = '='; - break; - case 2: - str[last_idx] = digits[data[data_size - 2] >> 2]; - str[last_idx + 1] = digits[(data[data_size - 2] << 4 | data[data_size - 1] >> 4) & 0x3F]; - str[last_idx + 2] = digits[data[data_size - 1] & 0x0F]; - str[last_idx + 3] = '='; - break; - default: - break; - } + case 1: + str[last_idx] = digits[data[data_size - 1] >> 2]; + str[last_idx + 1] = digits[data[data_size - 1] << 4 & 0x30]; + str[last_idx + 2] = '='; + str[last_idx + 3] = '='; + break; + case 2: + str[last_idx] = digits[data[data_size - 2] >> 2]; + str[last_idx + 1] = digits[(data[data_size - 2] << 4 | data[data_size - 1] >> 4) & 0x3F]; + str[last_idx + 2] = digits[data[data_size - 1] & 0x0F]; + str[last_idx + 3] = '='; + break; + default: + break; } } std::string encode(std::span data) noexcept diff --git a/test/test-base64.cpp b/test/test-base64.cpp index f0877da..92d2316 100644 --- a/test/test-base64.cpp +++ b/test/test-base64.cpp @@ -28,6 +28,12 @@ TEST(base64, encode) { for (auto it : tests) EXPECT_EQ(it.first, encode(hex::decode(it.second))); + + std::vector data = {0x74, 0x65, 0x73, 0x74}; + std::string str = ""; + EXPECT_THROW(encode(data.data(), std::numeric_limits::max(), str.data(), str.size()), std::overflow_error); + EXPECT_THROW(encode(data.data(), data.size(), str.data(), str.size()), std::length_error); + EXPECT_NO_THROW(encode(data.data(), 0, str.data(), str.size())); } TEST(base64, encode_1e7) { @@ -38,6 +44,12 @@ TEST(base64, decode) { for (auto it : tests) EXPECT_EQ(hex::encode(decode(it.first)), it.second); + + std::vector data = {0x61, 0x6e, 0x6f}; + EXPECT_THROW(decode("FFF", 3, data.data(), data.size()), std::logic_error); + EXPECT_THROW(decode("!@#!", 4, data.data(), data.size()), std::logic_error); + EXPECT_THROW(decode("FF==", 2, data.data(), 0), std::length_error); + EXPECT_NO_THROW(decode("" , 0, data.data(), 0)); } TEST(base64, decode_1e7) {