diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..3ac5633 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,65 @@ +# Copyright 2020 Johan Lindquist +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +project(craggy-test) +set(CMAKE_CXX_STANDARD 11) + +# Download and unpack googletest at configure time +configure_file(gtest/CMakeLists.txt.in googletest-download/CMakeLists.txt) +execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . + RESULT_VARIABLE result + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download ) +if(result) + message(FATAL_ERROR "CMake step for googletest failed: ${result}") +endif() +execute_process(COMMAND ${CMAKE_COMMAND} --build . + RESULT_VARIABLE result + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download ) +if(result) + message(FATAL_ERROR "Build step for googletest failed: ${result}") +endif() + +# Prevent overriding the parent project's compiler/linker +# settings on Windows +set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) + +# Add googletest directly to our build. This defines +# the gtest and gtest_main targets. +add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src + ${CMAKE_CURRENT_BINARY_DIR}/googletest-build + EXCLUDE_FROM_ALL) + +# The gtest/gtest_main targets carry header search path +# dependencies automatically when using CMake 2.8.11 or +# later. Otherwise we have to add them here ourselves. +if (CMAKE_VERSION VERSION_LESS 2.8.11) + include_directories("${gtest_SOURCE_DIR}/include") +endif() + +# Now simply link against gtest or gtest_main as needed. Eg +add_executable(craggy-test TestCrypto.cpp) + +include(GoogleTest) + +gtest_discover_tests(craggy-test) + +target_link_libraries(craggy-test craggy) +target_link_libraries(craggy-test gtest_main) + +# add_test(NAME example_test COMMAND craggy-test) +include (CTest) + +# find_program( MEMORYCHECK_COMMAND valgrind ) +# set( MEMORYCHECK_COMMAND_OPTIONS "--trace-children=yes --leak-check=full --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=20" ) \ No newline at end of file diff --git a/tests/TestCrypto.cpp b/tests/TestCrypto.cpp new file mode 100644 index 0000000..8f6d49f --- /dev/null +++ b/tests/TestCrypto.cpp @@ -0,0 +1,42 @@ +/* Copyright 2020 Craggy Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "gtest/gtest.h" + +#include "CraggyCrypto.h" + +namespace { + +TEST(CraggyCryptoTests, FillRandomBytes) { + + auto *originalBytes = new uint8_t[32]; + memset(originalBytes,0,32); + + auto *bytes = new uint8_t[32]; + memset(bytes,0,32); + + CraggyResult craggyResult = CraggyResultSuccess; + + bool result = craggy_fillRandomBytes(bytes, (size_t)32, &craggyResult); + + EXPECT_EQ(true, result); + EXPECT_NE(0, std::memcmp(originalBytes, bytes, 32)); + + delete[] bytes; + +} + +} // namespace \ No newline at end of file diff --git a/tests/gtest/CMakeLists.txt.in b/tests/gtest/CMakeLists.txt.in new file mode 100644 index 0000000..2953051 --- /dev/null +++ b/tests/gtest/CMakeLists.txt.in @@ -0,0 +1,15 @@ +cmake_minimum_required(VERSION 3.12) + +project(googletest-download NONE) + +include(ExternalProject) +ExternalProject_Add(googletest + GIT_REPOSITORY https://github.com/google/googletest.git + GIT_TAG main + SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src" + BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build" + CONFIGURE_COMMAND "" + BUILD_COMMAND "" + INSTALL_COMMAND "" + TEST_COMMAND "" + ) \ No newline at end of file