libutil

C Utility Library
git clone http://git.omkov.net/libutil
Log | Tree | Refs | README | LICENCE | Download

AuthorJamozed <[email protected]>
Date2021-02-19 05:06:05
Commit3e0d5cbec77132353e36774017fb7f993208a31e
Parentf0b8e1b5c4493237d4fb525a67193017aae89610

base32: Add base32 unit test

Diffstat

M CMakeLists.txt | 2 ++
A src/test/test_base32.c | 157 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

2 files changed, 159 insertions, 0 deletions

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 3d83187..88397a7 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -11,6 +11,7 @@ ADD_LIBRARY(lib STATIC ${SOURCES})
 
 LINK_LIBRARIES(lib)
 
+ADD_EXECUTABLE(test_base32 ${PROJECT_SOURCE_DIR}/src/test/test_base32.c)
 ADD_EXECUTABLE(test_base64 ${PROJECT_SOURCE_DIR}/src/test/test_base64.c)
 ADD_EXECUTABLE(test_crypt  ${PROJECT_SOURCE_DIR}/src/test/test_crypt.c)
 ADD_EXECUTABLE(test_endian ${PROJECT_SOURCE_DIR}/src/test/test_endian.c)
@@ -18,6 +19,7 @@ ADD_EXECUTABLE(test_error  ${PROJECT_SOURCE_DIR}/src/test/test_error.c)
 ADD_EXECUTABLE(test_optget ${PROJECT_SOURCE_DIR}/src/test/test_optget.c)
 
 ENABLE_TESTING()
+ADD_TEST(NAME test_base32 COMMAND test_base32)
 ADD_TEST(NAME test_base64 COMMAND test_base64)
 ADD_TEST(NAME test_crypt  COMMAND test_crypt)
 ADD_TEST(NAME test_endian COMMAND test_endian)
diff --git a/src/test/test_base32.c b/src/test/test_base32.c
new file mode 100644
index 0000000..b49fd00
--- /dev/null
+++ b/src/test/test_base32.c
@@ -0,0 +1,157 @@
+// test_base32.c
+// Base32 unit test for OMKOV lib
+// Copyright (C) 2021, Jakob Wakeling
+// All rights reserved.
+
+/*
+OMKOV Public Domain Licence, version 1.0
+
+Permission is hereby granted to deal with this software and its associated
+documentation files without restriction.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT
+HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE.
+*/
+
+#include "../base32.h"
+#include "unit.h"
+
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+
+int main(void) {
+	{
+		uint8_t i[10] = "1234567890";
+		uint8_t o[16] = { 0 };
+		
+		b32encode(o, i, 0);
+		ASSERT("T000 ENCODE STRING LENGTH 0",
+				!memcmp(o, "", 1));
+		
+		b32encode(o, i, 1);
+		ASSERT("T001 ENCODE STRING LENGTH 1",
+				!memcmp(o, "GE======", 8));
+		
+		b32encode(o, i, 2);
+		ASSERT("T002 ENCODE STRING LENGTH 2",
+				!memcmp(o, "GEZA====", 8));
+		
+		b32encode(o, i, 3);
+		ASSERT("T003 ENCODE STRING LENGTH 3",
+				!memcmp(o, "GEZDG===", 8));
+		
+		b32encode(o, i, 4);
+		ASSERT("T004 ENCODE STRING LENGTH 4",
+				!memcmp(o, "GEZDGNA=", 8));
+		
+		b32encode(o, i, 5);
+		ASSERT("T005 ENCODE STRING LENGTH 5",
+				!memcmp(o, "GEZDGNBV", 8));
+		
+		b32encode(o, i, 6);
+		ASSERT("T006 ENCODE STRING LENGTH 6",
+				!memcmp(o, "GEZDGNBVGY======", 16));
+		
+		b32encode(o, i, 7);
+		ASSERT("T007 ENCODE STRING LENGTH 7",
+				!memcmp(o, "GEZDGNBVGY3Q====", 16));
+		
+		b32encode(o, i, 8);
+		ASSERT("T008 ENCODE STRING LENGTH 8",
+				!memcmp(o, "GEZDGNBVGY3TQ===", 16));
+		
+		b32encode(o, i, 9);
+		ASSERT("T009 ENCODE STRING LENGTH 9",
+				!memcmp(o, "GEZDGNBVGY3TQOI=", 16));
+		
+		b32encode(o, i, 10);
+		ASSERT("T010 ENCODE STRING LENGTH 10",
+				!memcmp(o, "GEZDGNBVGY3TQOJQ", 16));
+	}
+	
+	{
+		uint8_t i[10] =
+			{ 0xCA, 0x2C, 0x10, 0x9B, 0x54, 0xD1, 0x20, 0xBE, 0xCD, 0x1B };
+		uint8_t o[16] = { 0 };
+		
+		b32encode(o, i, 1);
+		ASSERT("T011 ENCODE RANDOM DATA LENGTH 1",
+				!memcmp(o, "ZI======", 8));
+		
+		b32encode(o, i, 2);
+		ASSERT("T012 ENCODE RANDOM DATA LENGTH 2",
+				!memcmp(o, "ZIWA====", 8));
+		
+		b32encode(o, i, 3);
+		ASSERT("T013 ENCODE RANDOM DATA LENGTH 3",
+				!memcmp(o, "ZIWBA===", 8));
+		
+		b32encode(o, i, 4);
+		ASSERT("T014 ENCODE RANDOM DATA LENGTH 4",
+				!memcmp(o, "ZIWBBGY=", 8));
+		
+		b32encode(o, i, 5);
+		ASSERT("T015 ENCODE RANDOM DATA LENGTH 5",
+				!memcmp(o, "ZIWBBG2U", 8));
+		
+		b32encode(o, i, 6);
+		ASSERT("T016 ENCODE RANDOM DATA LENGTH 6",
+				!memcmp(o, "ZIWBBG2U2E======", 16));
+		
+		b32encode(o, i, 7);
+		ASSERT("T017 ENCODE RANDOM DATA LENGTH 7",
+				!memcmp(o, "ZIWBBG2U2EQA====", 16));
+		
+		b32encode(o, i, 8);
+		ASSERT("T018 ENCODE RANDOM DATA LENGTH 8",
+				!memcmp(o, "ZIWBBG2U2EQL4===", 16));
+		
+		b32encode(o, i, 9);
+		ASSERT("T019 ENCODE RANDOM DATA LENGTH 9",
+				!memcmp(o, "ZIWBBG2U2EQL5TI=", 16));
+		
+		b32encode(o, i, 10);
+		ASSERT("T020 ENCODE RANDOM DATA LENGTH 10",
+				!memcmp(o, "ZIWBBG2U2EQL5TI3", 16));
+	}
+	
+	{
+		uint8_t i[16] = "ZIWBBG2U2E======"; uint8_t o[10] = { 0 };
+		
+		b32decode(o, i, 10);
+		ASSERT("T021 DECODE STRING LENGTH 10",
+				!memcmp(o, "123456", 6));
+		
+		b32decode(o, i, 11);
+		ASSERT("T022 DECODE STRING LENGTH 10, 1 PADDING",
+				!memcmp(o, "123456", 6));
+		
+		b32decode(o, i, 12);
+		ASSERT("T023 DECODE STRING LENGTH 10, 2 PADDING",
+				!memcmp(o, "123456", 6));
+		
+		b32decode(o, i, 13);
+		ASSERT("T024 DECODE STRING LENGTH 10, 3 PADDING",
+				!memcmp(o, "123456", 6));
+		
+		b32decode(o, i, 14);
+		ASSERT("T025 DECODE STRING LENGTH 10, 4 PADDING",
+				!memcmp(o, "123456", 6));
+		
+		b32decode(o, i, 15);
+		ASSERT("T026 DECODE STRING LENGTH 10, 5 PADDING",
+				!memcmp(o, "123456", 6));
+		
+		b32decode(o, i, 16);
+		ASSERT("T027 DECODE STRING LENGTH 10, 6 PADDING",
+				!memcmp(o, "123456", 6));
+	}
+	
+	printf("%d of %d tests passed\n", testspassed, testsrun);
+	return testsfailed;
+}