summaryrefslogtreecommitdiffstats
path: root/tests/UUID
diff options
context:
space:
mode:
Diffstat (limited to 'tests/UUID')
-rw-r--r--tests/UUID/CMakeLists.txt35
-rw-r--r--tests/UUID/UUIDTest.cpp132
2 files changed, 167 insertions, 0 deletions
diff --git a/tests/UUID/CMakeLists.txt b/tests/UUID/CMakeLists.txt
new file mode 100644
index 000000000..72746d10d
--- /dev/null
+++ b/tests/UUID/CMakeLists.txt
@@ -0,0 +1,35 @@
+enable_testing()
+
+set (SHARED_SRCS
+ ${CMAKE_SOURCE_DIR}/src/UUID.cpp
+ ${CMAKE_SOURCE_DIR}/src/StringUtils.cpp
+)
+
+set (SHARED_HDRS
+ ${CMAKE_SOURCE_DIR}/src/UUID.h
+ ${CMAKE_SOURCE_DIR}/src/StringUtils.h
+)
+
+set (SRCS
+ UUIDTest.cpp
+)
+
+source_group("Shared" FILES ${SHARED_SRCS} ${SHARED_HDRS})
+source_group("Sources" FILES ${SRCS})
+
+add_executable(UUIDTest ${SRCS} ${SHARED_SRCS} ${SHARED_HDRS})
+target_link_libraries(UUIDTest mbedcrypto)
+target_compile_definitions(UUIDTest PRIVATE TEST_GLOBALS=1)
+target_include_directories(UUIDTest PRIVATE
+ ${CMAKE_SOURCE_DIR}/src/
+ ${CMAKE_SOURCE_DIR}/lib/mbedtls/include
+)
+
+add_test(NAME UUID-test COMMAND UUIDTest)
+
+
+# Put the projects into solution folders (MSVC):
+set_target_properties(
+ UUIDTest
+ PROPERTIES FOLDER Tests
+)
diff --git a/tests/UUID/UUIDTest.cpp b/tests/UUID/UUIDTest.cpp
new file mode 100644
index 000000000..abd9db8df
--- /dev/null
+++ b/tests/UUID/UUIDTest.cpp
@@ -0,0 +1,132 @@
+
+// UUIDTest.cpp
+
+#include "Globals.h"
+#include "UUID.h"
+
+#include <numeric> // for std::iota
+
+
+/** Test that FromString -> ToShortString preserves the original value for short UUIDs. */
+static void UUIDFromStringToShortString()
+{
+ const char TestStrings[][33]{
+ "0123456789abcdef0123456789ABCDEF",
+ "d188b2648cc311e7bb31be2e44b06b34",
+ "e760d270d8b34288b895d9f78a31e083",
+ "7052f2f2594246abb8e3fed602158870",
+ "7f14d4b60cd84ba7885c8301b67ce891",
+ "57be7039250548b590af272291fabcfa"
+ };
+
+ for (auto TestString : TestStrings)
+ {
+ cUUID UUID;
+ assert_test(UUID.FromString(TestString));
+ auto ResultString = UUID.ToShortString();
+ // Result should be equivalent to original
+ assert_test(NoCaseCompare(ResultString, TestString) == 0);
+ // And should be all lower case
+ assert_test(ResultString == StrToLower(ResultString));
+ }
+}
+
+
+
+
+
+/** Test that FromString -> ToLongString preserves the original value for long UUIDs. */
+static void UUIDFromStringToLongString()
+{
+ const char TestStrings[][37]{
+ "01234567-89ab-cdef-0123-456789ABCDEF",
+ "d188b264-8cc3-11e7-bb31-be2e44b06b34",
+ "e760d270-d8b3-4288-b895-d9f78a31e083",
+ "7052f2f2-5942-46ab-b8e3-fed602158870",
+ "7f14d4b6-0cd8-4ba7-885c-8301b67ce891",
+ "57be7039-2505-48b5-90af-272291fabcfa"
+ };
+
+ for (auto TestString : TestStrings)
+ {
+ cUUID UUID;
+ assert_test(UUID.FromString(TestString));
+ auto ResultString = UUID.ToLongString();
+ // Result should be equivalent to original
+ assert_test(NoCaseCompare(ResultString, TestString) == 0);
+ // And should be all lower case
+ assert_test(ResultString == StrToLower(ResultString));
+ }
+}
+
+
+
+
+
+/** Test that FromRaw -> ToRaw preserves the original value. */
+static void UUIDFromRawToRaw()
+{
+ std::array<Byte, 16> TestData[16]{};
+ // Fill test data with all values 0 - 255
+ for (int i = 0; i != 16; ++i)
+ {
+ std::iota(begin(TestData[i]), end(TestData[i]), i * 16);
+ }
+
+ for (const auto & TestRaw : TestData)
+ {
+ cUUID UUID;
+ UUID.FromRaw(TestRaw);
+ auto ResultRaw = UUID.ToRaw();
+ assert_test(ResultRaw == TestRaw);
+ }
+}
+
+
+
+
+
+/** Test that IsNil correctly identifies nil UUIDs. */
+static void UUIDNil()
+{
+ const auto NilString = "00000000-0000-0000-0000-000000000000";
+ const auto NonNilString = "e760d270-d8b3-4288-b895-d9f78a31e083";
+
+ {
+ cUUID UUID;
+ assert_test(UUID.FromString(NilString));
+ assert_test(UUID.IsNil());
+ }
+ {
+ cUUID UUID;
+ assert_test(UUID.FromString(NonNilString));
+ assert_test(!UUID.IsNil());
+ }
+}
+
+
+
+
+
+int main(int argc, char * argv[])
+{
+ LOG("UUID tests started");
+
+ LOG("Testing short string UUIDs");
+ UUIDFromStringToShortString();
+
+ LOG("Testing long strings UUIDs");
+ UUIDFromStringToLongString();
+
+ LOG("Testing raw UUIDs");
+ UUIDFromRawToRaw();
+
+ LOG("Testing nil UUIDs");
+ UUIDNil();
+
+ LOG("UUID tests finished");
+}
+
+
+
+