diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..cbffb5c --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "tests/lib"] + path = tests/lib + url = https://github.com/google/googletest diff --git a/CMakeLists.txt b/CMakeLists.txt index 3ba64ad..c375f8e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,4 +8,6 @@ add_library(aaruformat SHARED include/aaruformat/consts.h include/aaruformat/enu include/aaruformat/decls.h include/aaruformat/structs.h src/identify.c src/open.c include/aaruformat/context.h src/close.c include/aaruformat/errors.h src/read.c src/crc64.c src/cst.c src/ecc_cd.c src/helpers.c) -include_directories(include include/aaruformat) \ No newline at end of file +include_directories(include include/aaruformat) + +add_subdirectory(tests) \ No newline at end of file diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..8ab387d --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,14 @@ +# 'Google_test' is the subproject name +project(tests) + +# 'lib' is the folder with Google Test sources +add_subdirectory(lib) +include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR}) + +file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/data/random + DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/data/) + +# 'Google_Tests_run' is the target name +# 'test1.cpp tests2.cpp' are source files with tests +add_executable(tests_run crc64.cpp) +target_link_libraries(tests_run gtest gtest_main "aaruformat") diff --git a/tests/crc64.cpp b/tests/crc64.cpp new file mode 100644 index 0000000..6a6fddb --- /dev/null +++ b/tests/crc64.cpp @@ -0,0 +1,138 @@ +// +// Created by claunia on 5/10/21. +// + +#include "../include/aaruformat.h" + +#include "gtest/gtest.h" +#include +#include +#include + +#define EXPECTED_CRC64 0xbf09992cc5ede38e +#define EXPECTED_CRC64_15BYTES 0x797F3766FD93975B +#define EXPECTED_CRC64_31BYTES 0xCD9201905A7937FD +#define EXPECTED_CRC64_63BYTES 0x29F331FC90702BF4 +#define EXPECTED_CRC64_2352BYTES 0x126435DB43477623 + +static const uint8_t* buffer; +static const uint8_t* buffer_misaligned; + +class crc64Fixture : public ::testing::Test +{ + public: + crc64Fixture() + { + // initialization; + // can also be done in SetUp() + } + + protected: + void SetUp() + { + char path[PATH_MAX]; + char filename[PATH_MAX]; + + getcwd(path, PATH_MAX); + snprintf(filename, PATH_MAX, "%s/data/random", path); + + FILE* file = fopen(filename, "rb"); + buffer = (const uint8_t*)malloc(1048576); + fread((void*)buffer, 1, 1048576, file); + fclose(file); + + buffer_misaligned = (const uint8_t*)malloc(1048577); + memcpy((void*)(buffer_misaligned + 1), buffer, 1048576); + } + + void TearDown() + { + free((void*)buffer); + free((void*)buffer_misaligned); + } + + ~crc64Fixture() + { + // resources cleanup, no exceptions allowed + } + + // shared user data +}; + +TEST_F(crc64Fixture, crc64_auto) +{ + void* ctx = crc64_init_ecma(); + uint64_t crc; + + EXPECT_NE(ctx, nullptr); + + crc64_update(ctx, buffer, 1048576); + crc = crc64_final(ctx); + + EXPECT_EQ(crc, EXPECTED_CRC64); +} + +TEST_F(crc64Fixture, crc64_auto_misaligned) +{ + void* ctx = crc64_init_ecma(); + uint64_t crc; + + EXPECT_NE(ctx, nullptr); + + crc64_update(ctx, buffer_misaligned + 1, 1048576); + crc = crc64_final(ctx); + + EXPECT_EQ(crc, EXPECTED_CRC64); +} + +TEST_F(crc64Fixture, crc64_auto_15bytes) +{ + void* ctx = crc64_init_ecma(); + uint64_t crc; + + EXPECT_NE(ctx, nullptr); + + crc64_update(ctx, buffer, 15); + crc = crc64_final(ctx); + + EXPECT_EQ(crc, EXPECTED_CRC64_15BYTES); +} + +TEST_F(crc64Fixture, crc64_auto_31bytes) +{ + void* ctx = crc64_init_ecma(); + uint64_t crc; + + EXPECT_NE(ctx, nullptr); + + crc64_update(ctx, buffer, 31); + crc = crc64_final(ctx); + + EXPECT_EQ(crc, EXPECTED_CRC64_31BYTES); +} + +TEST_F(crc64Fixture, crc64_auto_63bytes) +{ + void* ctx = crc64_init_ecma(); + uint64_t crc; + + EXPECT_NE(ctx, nullptr); + + crc64_update(ctx, buffer, 63); + crc = crc64_final(ctx); + + EXPECT_EQ(crc, EXPECTED_CRC64_63BYTES); +} + +TEST_F(crc64Fixture, crc64_auto_2352bytes) +{ + void* ctx = crc64_init_ecma(); + uint64_t crc; + + EXPECT_NE(ctx, nullptr); + + crc64_update(ctx, buffer, 2352); + crc = crc64_final(ctx); + + EXPECT_EQ(crc, EXPECTED_CRC64_2352BYTES); +} \ No newline at end of file diff --git a/tests/data/random b/tests/data/random new file mode 100644 index 0000000..466b7fa Binary files /dev/null and b/tests/data/random differ diff --git a/tests/lib b/tests/lib new file mode 160000 index 0000000..9d21db9 --- /dev/null +++ b/tests/lib @@ -0,0 +1 @@ +Subproject commit 9d21db9e0a60a1ea61ec19331c9bc0dd33e907b1