From b482abe3bf62ef46816e81ccdab68bc24f498ce6 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Thu, 2 Apr 2020 15:38:39 +0200 Subject: Add test_memory test case This patch adds a test_memory test case that demonstrates new[]/free mismatches with the NK_read_config and NK_get_password_safe_slot_status functions if run with valgrind, see #173. --- unittest/test_memory.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 unittest/test_memory.c (limited to 'unittest') diff --git a/unittest/test_memory.c b/unittest/test_memory.c new file mode 100644 index 0000000..34ea7d5 --- /dev/null +++ b/unittest/test_memory.c @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2020 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 . + * + * SPDX-License-Identifier: LGPL-3.0 + */ + +#include +#include "../NK_C_API.h" + +// This test should be run with valgrind to make sure that there are no +// memory leaks in the tested functions: +// valgrind ./test_memory +int main() { + int result = NK_login_auto(); + if (result != 1) + return 1; + + int retry_count = NK_get_admin_retry_count(); + if (retry_count != 3) + return 1; + retry_count = NK_get_user_retry_count(); + if (retry_count != 3) + return 1; + + enum NK_device_model model = NK_get_device_model(); + if (model != NK_PRO && model != NK_STORAGE) + return 1; + + uint8_t *config = NK_read_config(); + if (config == NULL) + return 1; + free(config); + + result = NK_enable_password_safe("123456"); + if (result != 0) + return 1; + + uint8_t *slot_status = NK_get_password_safe_slot_status(); + if (slot_status == NULL) { + return 1; + } + free(slot_status); + + NK_logout(); + + return 0; +} + -- cgit v1.2.1 From 2a7b3f4e2ae09d665f9783030323dfb1a4c5ee9f Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Thu, 2 Apr 2020 15:51:25 +0200 Subject: Add missing free functions to C API This patch adds two missing free functions, NK_free_config and NK_free_password_safe_slot_status, to enable memory-safe use of the C API. Fixes #173. --- unittest/test_memory.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'unittest') diff --git a/unittest/test_memory.c b/unittest/test_memory.c index 34ea7d5..20b11b2 100644 --- a/unittest/test_memory.c +++ b/unittest/test_memory.c @@ -44,7 +44,7 @@ int main() { uint8_t *config = NK_read_config(); if (config == NULL) return 1; - free(config); + NK_free_config(config); result = NK_enable_password_safe("123456"); if (result != 0) @@ -54,7 +54,7 @@ int main() { if (slot_status == NULL) { return 1; } - free(slot_status); + NK_free_password_safe_slot_status(slot_status); NK_logout(); -- cgit v1.2.1