diff --git a/tests/unittests/tests-credman/Makefile b/tests/unittests/tests-credman/Makefile new file mode 100644 index 0000000000..48422e909a --- /dev/null +++ b/tests/unittests/tests-credman/Makefile @@ -0,0 +1 @@ +include $(RIOTBASE)/Makefile.base diff --git a/tests/unittests/tests-credman/Makefile.include b/tests/unittests/tests-credman/Makefile.include new file mode 100644 index 0000000000..58aba54c52 --- /dev/null +++ b/tests/unittests/tests-credman/Makefile.include @@ -0,0 +1 @@ +USEMODULE += credman diff --git a/tests/unittests/tests-credman/credentials.h b/tests/unittests/tests-credman/credentials.h new file mode 100644 index 0000000000..6ea87090ea --- /dev/null +++ b/tests/unittests/tests-credman/credentials.h @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2018 Inria + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup tests + * @{ + * + * @file + * @brief Test credentials for credman + * + * @author Raul Fuentes + * @author Aiman Ismail + * + * @} + */ + +#ifndef CREDENTIALS_H +#define CREDENTIALS_H + +#include "net/credman.h" + +#ifdef __cplusplus +extern "C" { +#endif + +static const unsigned char ecdsa_priv_key[] = { + 0x41, 0xC1, 0xCB, 0x6B, 0x51, 0x24, 0x7A, 0x14, + 0x43, 0x21, 0x43, 0x5B, 0x7A, 0x80, 0xE7, 0x14, + 0x89, 0x6A, 0x33, 0xBB, 0xAD, 0x72, 0x94, 0xCA, + 0x40, 0x14, 0x55, 0xA1, 0x94, 0xA9, 0x49, 0xFA +}; + +static const unsigned char ecdsa_pub_key_x[] = { + 0x36, 0xDF, 0xE2, 0xC6, 0xF9, 0xF2, 0xED, 0x29, + 0xDA, 0x0A, 0x9A, 0x8F, 0x62, 0x68, 0x4E, 0x91, + 0x63, 0x75, 0xBA, 0x10, 0x30, 0x0C, 0x28, 0xC5, + 0xE4, 0x7C, 0xFB, 0xF2, 0x5F, 0xA5, 0x8F, 0x52 +}; + +static const unsigned char ecdsa_pub_key_y[] = { + 0x71, 0xA0, 0xD4, 0xFC, 0xDE, 0x1A, 0xB8, 0x78, + 0x5A, 0x3C, 0x78, 0x69, 0x35, 0xA7, 0xCF, 0xAB, + 0xE9, 0x3F, 0x98, 0x72, 0x09, 0xDA, 0xED, 0x0B, + 0x4F, 0xAB, 0xC3, 0x6F, 0xC7, 0x72, 0xF8, 0x29 +}; + +#ifdef __cplusplus +} +#endif + +#endif /* CREDENTIALS_H */ diff --git a/tests/unittests/tests-credman/tests-credman.c b/tests/unittests/tests-credman/tests-credman.c new file mode 100644 index 0000000000..c722f04f10 --- /dev/null +++ b/tests/unittests/tests-credman/tests-credman.c @@ -0,0 +1,251 @@ +/* + * Copyright (C) 2019 HAW Hamburg + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +#include +#include "embUnit.h" +#include "tests-credman.h" +#include "credentials.h" + +#include "net/credman.h" + +#define CREDMAN_TEST_TAG (1) + +static int _compare_credentials(const credman_credential_t *a, + const credman_credential_t *b) +{ + if ((a->tag == b->tag) && (a->type == b->type)) { + return 0; + } + return -1; +} + +static void set_up(void) +{ + /* reset credential pool before every test */ + credman_reset(); +} + +static void test_credman_add(void) +{ + int ret; + unsigned exp_count = 0; + + psk_params_t exp_psk_params = { + .id = { + .s = (void *)"RIOTer", + .len = sizeof("RIOTer") - 1, + }, + .key = { + .s = (void *)"LGPLisyourfriend", + .len = sizeof("LGPLisyourfriend") - 1, + }, + }; + + credman_credential_t credential = { + .tag = CREDMAN_TEST_TAG, + .type = CREDMAN_TYPE_PSK, + .params = { + .psk = exp_psk_params, + }, + }; + + TEST_ASSERT_EQUAL_INT(exp_count, credman_get_used_count()); + + /* add one credential */ + TEST_ASSERT_EQUAL_INT(CREDMAN_OK, credman_add(&credential)); + TEST_ASSERT_EQUAL_INT(++exp_count, credman_get_used_count()); + + /* add duplicate credential */ + ret = credman_add(&credential); + TEST_ASSERT_EQUAL_INT(CREDMAN_EXIST, ret); + TEST_ASSERT_EQUAL_INT(exp_count, credman_get_used_count()); + + /* add invalid credential params */ + memset(&credential.params.psk, 0, sizeof(psk_params_t)); + ret = credman_add(&credential); + TEST_ASSERT_EQUAL_INT(CREDMAN_INVALID, ret); + TEST_ASSERT_EQUAL_INT(exp_count, credman_get_used_count()); + + /* fill the pool */ + memcpy(&credential.params.psk, &exp_psk_params, sizeof(psk_params_t)); + while (credman_get_used_count() < CREDMAN_MAX_CREDENTIALS) { + /* increase tag number so that it is not recognized as duplicate */ + credential.tag++; + TEST_ASSERT_EQUAL_INT(CREDMAN_OK, credman_add(&credential)); + TEST_ASSERT_EQUAL_INT(++exp_count, credman_get_used_count()); + } + + /* add to full pool */ + credential.tag++; + ret = credman_add(&credential); + TEST_ASSERT_EQUAL_INT(CREDMAN_NO_SPACE, ret); + TEST_ASSERT_EQUAL_INT(exp_count, credman_get_used_count()); +} + +static void test_credman_get(void) +{ + int ret; + credman_credential_t out_credential; + credman_credential_t in_credential = { + .tag = CREDMAN_TEST_TAG, + .type = CREDMAN_TYPE_ECDSA, + .params = { + .ecdsa = { + .private_key = ecdsa_priv_key, + .public_key = { .x = ecdsa_pub_key_x, .y = ecdsa_pub_key_y }, + .client_keys = NULL, + .client_keys_size = 0, + }, + }, + }; + + /* get non-existing credential */ + ret = credman_get(&out_credential, in_credential.tag, in_credential.type); + TEST_ASSERT_EQUAL_INT(CREDMAN_NOT_FOUND, ret); + + ret = credman_add(&in_credential); + TEST_ASSERT_EQUAL_INT(CREDMAN_OK, ret); + + ret = credman_get(&out_credential, in_credential.tag, in_credential.type); + TEST_ASSERT_EQUAL_INT(CREDMAN_OK, ret); + TEST_ASSERT(!_compare_credentials(&in_credential, &out_credential)); +} + +static void test_credman_delete(void) +{ + int ret; + unsigned exp_count = 0; + credman_credential_t out_credential; + credman_credential_t in_credential = { + .tag = CREDMAN_TEST_TAG, + .type = CREDMAN_TYPE_ECDSA, + .params = { + .ecdsa = { + .private_key = ecdsa_priv_key, + .public_key = { .x = ecdsa_pub_key_x, .y = ecdsa_pub_key_y }, + .client_keys = NULL, + .client_keys_size = 0, + }, + }, + }; + + /* delete non-existing credential */ + credman_delete(in_credential.tag, in_credential.type); + TEST_ASSERT_EQUAL_INT(exp_count, credman_get_used_count()); + + /* add a credential */ + ret = credman_add(&in_credential); + TEST_ASSERT_EQUAL_INT(CREDMAN_OK, ret); + TEST_ASSERT_EQUAL_INT(++exp_count, credman_get_used_count()); + + /* delete a credential from credential pool */ + credman_delete(in_credential.tag, in_credential.type); + TEST_ASSERT_EQUAL_INT(--exp_count, credman_get_used_count()); + + /* get the deleted credential */ + ret = credman_get(&out_credential, in_credential.tag, in_credential.type); + TEST_ASSERT_EQUAL_INT(CREDMAN_NOT_FOUND, ret); + + /* delete a deleted credential */ + credman_delete(in_credential.tag, in_credential.type); + TEST_ASSERT_EQUAL_INT(exp_count, credman_get_used_count()); +} + +static void test_credman_delete_random_order(void) +{ + credman_tag_t tag1 = CREDMAN_TEST_TAG; + credman_tag_t tag2 = CREDMAN_TEST_TAG + 1; + + credman_credential_t out_credential; + credman_credential_t in_credential = { + .tag = tag1, + .type = CREDMAN_TYPE_ECDSA, + .params = { + .ecdsa = { + .private_key = ecdsa_priv_key, + .public_key = { .x = ecdsa_pub_key_x, .y = ecdsa_pub_key_y }, + .client_keys = NULL, + .client_keys_size = 0, + }, + }, + }; + TEST_ASSERT_EQUAL_INT(0, credman_get_used_count()); + + /* fill the credential pool, assume CREDMAN_MAX_CREDENTIALS is 2 */ + TEST_ASSERT_EQUAL_INT(CREDMAN_OK, credman_add(&in_credential)); + in_credential.tag = tag2; + TEST_ASSERT_EQUAL_INT(CREDMAN_OK, credman_add(&in_credential)); + TEST_ASSERT_EQUAL_INT(2, credman_get_used_count()); + + /* delete the first credential */ + credman_delete(tag1, in_credential.type); + TEST_ASSERT_EQUAL_INT(1, credman_get_used_count()); + + /* get the second credential */ + TEST_ASSERT_EQUAL_INT(CREDMAN_OK, credman_get(&out_credential, tag2, in_credential.type)); + TEST_ASSERT(!_compare_credentials(&in_credential, &out_credential)); +} + +static void test_credman_add_delete_all(void) +{ + credman_tag_t tag1 = CREDMAN_TEST_TAG; + credman_tag_t tag2 = CREDMAN_TEST_TAG + 1; + + credman_credential_t in_credential = { + .tag = tag1, + .type = CREDMAN_TYPE_ECDSA, + .params = { + .ecdsa = { + .private_key = ecdsa_priv_key, + .public_key = { .x = ecdsa_pub_key_x, .y = ecdsa_pub_key_y }, + .client_keys = NULL, + .client_keys_size = 0, + }, + }, + }; + + /* add credentials */ + TEST_ASSERT_EQUAL_INT(CREDMAN_OK, credman_add(&in_credential)); + in_credential.tag = tag2; + TEST_ASSERT_EQUAL_INT(CREDMAN_OK, credman_add(&in_credential)); + TEST_ASSERT_EQUAL_INT(2, credman_get_used_count()); + + /* delete starting from first added credential */ + credman_delete(tag1, in_credential.type); + credman_delete(tag2, in_credential.type); + TEST_ASSERT_EQUAL_INT(0, credman_get_used_count()); + + /* re-add the credentials after deletion */ + in_credential.tag = tag1; + TEST_ASSERT_EQUAL_INT(CREDMAN_OK, credman_add(&in_credential)); + in_credential.tag = tag2; + TEST_ASSERT_EQUAL_INT(CREDMAN_OK, credman_add(&in_credential)); + TEST_ASSERT_EQUAL_INT(2, credman_get_used_count()); +} + +Test *tests_credman_tests(void) +{ + EMB_UNIT_TESTFIXTURES(fixtures) { + new_TestFixture(test_credman_add), + new_TestFixture(test_credman_get), + new_TestFixture(test_credman_delete), + new_TestFixture(test_credman_delete_random_order), + new_TestFixture(test_credman_add_delete_all), + }; + + EMB_UNIT_TESTCALLER(credman_tests, + set_up, + NULL, fixtures); + + return (Test *)&credman_tests; +} + +void tests_credman(void) +{ + TESTS_RUN(tests_credman_tests()); +} diff --git a/tests/unittests/tests-credman/tests-credman.h b/tests/unittests/tests-credman/tests-credman.h new file mode 100644 index 0000000000..b47755a43f --- /dev/null +++ b/tests/unittests/tests-credman/tests-credman.h @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2019 HAW Hamburg + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @addtogroup unittests + * @{ + * + * @file + * @brief Unittests for the ``credman`` module + * + * @author Aiman Ismail + */ + +#ifndef TESTS_CREDMAN_H +#define TESTS_CREDMAN_H +#include "embUnit/embUnit.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief The entry point of this test suite. + */ +void tests_credman(void); + +/** + * @brief Generates tests for credman + * + * @return embUnit tests if successful, NULL if not. + */ +Test *tests_credman_tests(void); + +#ifdef __cplusplus +} +#endif + +#endif /* TESTS_CREDMAN_H */ +/** @} */