From 5e72941919f19c43c1cd7f79c5cb913caf2705e4 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Fri, 14 Oct 2016 14:01:10 +0200 Subject: Test C API in C++ (Catch) Signed-off-by: Szczepan Zalega --- CMakeLists.txt | 4 +++- unittest/catch_main.cpp | 2 ++ unittest/test_C_API.cpp | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 unittest/catch_main.cpp create mode 100644 unittest/test_C_API.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 803c465..eedfd35 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,6 +28,8 @@ set(SOURCE_FILES log.cc misc.cc NitrokeyManager.cc - NK_C_API.cc include/CommandFailedException.h include/LibraryException.h) + NK_C_API.cc include/CommandFailedException.h include/LibraryException.h + unittest/test_C_API.cpp + unittest/catch_main.cpp) add_executable(libnitrokey ${SOURCE_FILES}) \ No newline at end of file diff --git a/unittest/catch_main.cpp b/unittest/catch_main.cpp new file mode 100644 index 0000000..c8270db --- /dev/null +++ b/unittest/catch_main.cpp @@ -0,0 +1,2 @@ +#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() +#include "catch.hpp" \ No newline at end of file diff --git a/unittest/test_C_API.cpp b/unittest/test_C_API.cpp new file mode 100644 index 0000000..37d3c7f --- /dev/null +++ b/unittest/test_C_API.cpp @@ -0,0 +1,34 @@ +static const int TOO_LONG_STRING = 200; + +#include "catch.hpp" + +#include +#include +#include "log.h" +#include "../NK_C_API.h" + +TEST_CASE("C API connect", "[BASIC]") { + auto login = NK_login_auto(); + REQUIRE(login != 0); + NK_logout(); + login = NK_login_auto(); + REQUIRE(login != 0); + NK_logout(); + login = NK_login_auto(); + REQUIRE(login != 0); +} + +TEST_CASE("Check retry count", "[BASIC]") { + REQUIRE(NK_get_admin_retry_count() == 3); + REQUIRE(NK_get_user_retry_count() == 3); +} + +TEST_CASE("Check long strings", "[STANDARD]") { + char* longPin = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; + char *pin = "123123123"; + auto result = NK_change_user_PIN(longPin, pin); + REQUIRE(result == TOO_LONG_STRING); + result = NK_change_user_PIN(pin, longPin); + REQUIRE(result == TOO_LONG_STRING); + CAPTURE(result); +} \ No newline at end of file -- cgit v1.2.3