aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSzczepan Zalega <szczepan@nitrokey.com>2019-01-02 13:32:43 +0100
committerSzczepan Zalega <szczepan@nitrokey.com>2019-01-02 13:32:43 +0100
commit577f6278318774ace9eec86c7fdb7db260e7a4c0 (patch)
treeb00f6771c967c848347a157e79df5c99af9d7ff8
parent57773ad5f7b6eb5e6a2020dd1bd4264bdd823808 (diff)
downloadlibnitrokey-577f6278318774ace9eec86c7fdb7db260e7a4c0.tar.gz
libnitrokey-577f6278318774ace9eec86c7fdb7db260e7a4c0.tar.bz2
Add safe to user data C++ tests - test_safe.cpp
Signed-off-by: Szczepan Zalega <szczepan@nitrokey.com>
-rw-r--r--unittest/test_safe.cpp93
1 files changed, 93 insertions, 0 deletions
diff --git a/unittest/test_safe.cpp b/unittest/test_safe.cpp
new file mode 100644
index 0000000..d6f8b63
--- /dev/null
+++ b/unittest/test_safe.cpp
@@ -0,0 +1,93 @@
+/*
+ * Copyright (c) 2019 Nitrokey UG
+ *
+ * This file is part of libnitrokey.
+ *
+ * libnitrokey is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * any later version.
+ *
+ * libnitrokey is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with libnitrokey. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: LGPL-3.0
+ */
+
+#include "catch2/catch.hpp"
+
+#include <iostream>
+#include <cstring>
+#include "log.h"
+#include "../NK_C_API.h"
+
+int login;
+
+TEST_CASE("C API connect", "[BASIC]") {
+ INFO("This test set assumes either Pro or Storage device is connected.");
+ INFO("Here should be implemented only tests not changing the device's state, "
+ "and safe to user data.");
+
+ login = NK_login_auto();
+ REQUIRE(login != 0);
+ NK_set_debug_level(3);
+}
+
+TEST_CASE("Check retry count", "[BASIC]") {
+ INFO("This test assumes your PINs' attempt counters are set to 3");
+ REQUIRE(login != 0);
+ REQUIRE(NK_get_admin_retry_count() == 3);
+ REQUIRE(NK_get_user_retry_count() == 3);
+}
+
+void validate_cstring(char *s){
+ constexpr uint16_t max_length = 8*1024;
+ REQUIRE(s != nullptr);
+ REQUIRE(strnlen(s, max_length) > 0);
+ REQUIRE(strnlen(s, max_length) < max_length);
+ std::cout << s << std::endl;
+}
+
+TEST_CASE("Status command for Pro or Storage", "[BASIC]") {
+ REQUIRE(login != 0);
+ char* s = nullptr;
+
+ auto const m = NK_get_device_model();
+ REQUIRE(m != NK_DISCONNECTED);
+ if (m == NK_PRO)
+ s = NK_status();
+ else if (m == NK_STORAGE){
+ s = NK_get_status_storage_as_string();
+ }
+
+ validate_cstring(s);
+ free(s);
+ s = nullptr;
+}
+
+TEST_CASE("Device serial", "[BASIC]") {
+ REQUIRE(login != 0);
+ char* s = nullptr;
+ s = NK_device_serial_number();
+ validate_cstring(s);
+ free(s);
+ s = nullptr;
+}
+
+TEST_CASE("Firmware version", "[BASIC]") {
+ REQUIRE(login != 0);
+ // Currently all devices has major version '0'
+ // No firmware ever had a minor equal to '0'
+ REQUIRE(NK_get_major_firmware_version() == 0);
+ REQUIRE(NK_get_minor_firmware_version() != 0);
+}
+
+TEST_CASE("Library version", "[BASIC]") {
+ REQUIRE(NK_get_major_library_version() == 3);
+ REQUIRE(NK_get_minor_library_version() >= 4);
+}