aboutsummaryrefslogtreecommitdiff
path: root/unittest/test_strdup.cpp
diff options
context:
space:
mode:
authorSzczepan Zalega <szczepan@nitrokey.com>2018-04-19 15:12:28 +0200
committerSzczepan Zalega <szczepan@nitrokey.com>2018-04-19 15:12:28 +0200
commit391a276ba35216337b777c65fda62561a6e9383f (patch)
tree201f025951868128fc64c0253589546ca0dea53f /unittest/test_strdup.cpp
parent7f5f471d8f3a67fe109e4c8c241de2e1098d275e (diff)
parent9ab13fd0ae3d85dcb9a3fcef0594aacb1946086b (diff)
downloadlibnitrokey-391a276ba35216337b777c65fda62561a6e9383f.tar.gz
libnitrokey-391a276ba35216337b777c65fda62561a6e9383f.tar.bz2
Merge branch '110-mixed_strings'
Make sure all C API strings are deallocable. Tested with Python's 3.6.5 'pytest -sv test_{pro,storage}.py' on Storage v0.50, Ubuntu 18.04 Closes #110
Diffstat (limited to 'unittest/test_strdup.cpp')
-rw-r--r--unittest/test_strdup.cpp57
1 files changed, 57 insertions, 0 deletions
diff --git a/unittest/test_strdup.cpp b/unittest/test_strdup.cpp
new file mode 100644
index 0000000..f980eb9
--- /dev/null
+++ b/unittest/test_strdup.cpp
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2015-2018 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
+ */
+
+// issue: https://github.com/Nitrokey/libnitrokey/issues/110
+// tests according to the issue's author, Robin Krahl (robinkrahl)
+// suggested run command: valgrind --tool=memcheck --leak-check=full ./test_strdup
+
+#include <cstdio>
+#include <memory.h>
+#include "NK_C_API.h"
+#include "catch.hpp"
+
+
+static const int SHORT_STRING_LENGTH = 10;
+
+TEST_CASE("Test strdup memory free error", "[BASIC]")
+{
+ NK_set_debug(false);
+ char *c = NK_status(); /* error --> string literal */
+ REQUIRE(c != nullptr);
+ REQUIRE(strnlen(c, SHORT_STRING_LENGTH) == 0);
+ puts(c);
+ free(c);
+}
+
+TEST_CASE("Test strdup memory leak", "[BASIC]")
+{
+ NK_set_debug(false);
+ bool connected = NK_login_auto() == 1;
+ if (!connected) return;
+
+ REQUIRE(connected);
+ char *c = NK_status(); /* no error --> dynamically allocated */
+ REQUIRE(c != nullptr);
+ REQUIRE(strnlen(c, SHORT_STRING_LENGTH) > 0);
+ puts(c);
+ free(c);
+}
+