diff --git a/tests/sha256/Makefile b/tests/sha256/Makefile deleted file mode 100644 index d5a245e04a..0000000000 --- a/tests/sha256/Makefile +++ /dev/null @@ -1,8 +0,0 @@ -APPLICATION = sha256 -include ../Makefile.tests_common - -USEMODULE += crypto - -DISABLE_MODULE += auto_init - -include $(RIOTBASE)/Makefile.include diff --git a/tests/sha256/main.c b/tests/sha256/main.c deleted file mode 100644 index 337a35316c..0000000000 --- a/tests/sha256/main.c +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright (C) 2013 Christian Mehlis - * - * 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 SHA256 test application - * - * @author Christian Mehlis - * - * @} - */ - -#include -#include -#include - -#include "crypto/sha256.h" - -unsigned char hash[SHA256_DIGEST_LENGTH]; - -void sha256_calc(const char *str, const char *expected) -{ - sha256_context_t sha256; - sha256_init(&sha256); - sha256_update(&sha256, str, strlen(str)); - sha256_final(hash, &sha256); - - printf("Input: %s\n" - "Expected: %s\n" - "Calculated: ", str, expected); - - for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) { - printf("%02x", hash[i]); - } - - printf("\n\n"); -} - -int main(void) -{ - puts("Start."); - - sha256_calc("1234567890_1", - "3eda9ffe5537a588f54d0b2a453e5fa932986d0bc0f9556924f5c2379b2c91b0"); - sha256_calc("1234567890_2", - "a144d0b4d285260ebbbab6840baceaa09eab3e157443c9458de764b7262c8ace"); - sha256_calc("1234567890_3", - "9f839169d293276d1b799707d2171ac1fd5b78d0f3bc7693dbed831524dd2d77"); - sha256_calc("1234567890_4", - "6c5fe2a8e3de58a5e5ac061031a8e802ae1fb9e7197862ec1aedf236f0e23475"); - sha256_calc("0123456789abcde-0123456789abcde-0123456789abcde-0123456789abcde-", - "945ab9d52b069923680c2c067fa6092cbbd9234cf7a38628f3033b2d54d3d3bf"); - sha256_calc("Franz jagt im komplett verwahrlosten Taxi quer durch Bayern", - "d32b568cd1b96d459e7291ebf4b25d007f275c9f13149beeb782fac0716613f8"); - sha256_calc("Frank jagt im komplett verwahrlosten Taxi quer durch Bayern", - "78206a866dbb2bf017d8e34274aed01a8ce405b69d45db30bafa00f5eeed7d5e"); - sha256_calc("", - "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"); - - puts("Done."); - return 0; -} diff --git a/tests/sha256/tests/01-test b/tests/sha256/tests/01-test deleted file mode 100755 index c0047aaf48..0000000000 --- a/tests/sha256/tests/01-test +++ /dev/null @@ -1,76 +0,0 @@ -#!/usr/bin/env expect - -set timeout 5 - -set pid [spawn make term] -puts "-*- Spawened $pid -*-\n" - -set result 0 -expect { - "Start." {} - timeout { - set result 1 - } -} - -while { $result == 0 } { - set result 1 - expect { - -regex {Input: +} { - expect { - "\r\n" {} - "\r" {} - "\n" {} - timeout { break } - } - set val_input $expect_out(buffer) - - expect { - -regex {Expected: +} { - expect { - "\r\n" {} - "\r" {} - "\n" {} - timeout { break } - } - } - timeout { break } - } - set val_expected $expect_out(buffer) - - expect { - -regex {Calculated: +} { - expect { - "\r\n" {} - "\r" {} - "\n" {} - timeout { break } - } - } - timeout { break } - } - set val_calculated $expect_out(buffer) - - if { $val_expected == $val_calculated } { - set result 0 - } else { - puts "-*- ERROR FOR INPUT <$val_input> -*-" - } - } - "Done." { - set result 0 - break - } - timeout { break } - } -} - -if { $result == 0 } { - puts "\n-*- Test successful! -*-\n" -} else { - puts "\n-*- TEST HAD ERRORS! -*-\n" -} -spawn kill -9 $pid -wait -close -exit $result diff --git a/tests/unittests/tests-crypto/Makefile b/tests/unittests/tests-crypto/Makefile new file mode 100644 index 0000000000..48422e909a --- /dev/null +++ b/tests/unittests/tests-crypto/Makefile @@ -0,0 +1 @@ +include $(RIOTBASE)/Makefile.base diff --git a/tests/unittests/tests-crypto/Makefile.include b/tests/unittests/tests-crypto/Makefile.include new file mode 100644 index 0000000000..135ea90d85 --- /dev/null +++ b/tests/unittests/tests-crypto/Makefile.include @@ -0,0 +1,2 @@ +USEMODULE += crypto + diff --git a/tests/unittests/tests-crypto/tests-crypto-sha256.c b/tests/unittests/tests-crypto/tests-crypto-sha256.c new file mode 100644 index 0000000000..8f772e87af --- /dev/null +++ b/tests/unittests/tests-crypto/tests-crypto-sha256.c @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2014 Philipp Rosenkranz + * Copyright (C) 2013 Christian Mehlis + * + * 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 +#include + +#include "embUnit/embUnit.h" + +#include "crypto/sha256.h" + +#include "tests-crypto.h" + +static int calc_and_compare_hash(const char *str, const char *expected) +{ + static unsigned char hash[SHA256_DIGEST_LENGTH]; + sha256_context_t sha256; + sha256_init(&sha256); + sha256_update(&sha256, str, strlen(str)); + sha256_final(hash, &sha256); + + return strncmp((const char *) hash, expected, SHA256_DIGEST_LENGTH); +} + +static void test_crypto_sha256_hash_sequence(void) +{ + TEST_ASSERT(calc_and_compare_hash("1234567890_1", + "3eda9ffe5537a588f54d0b2a453e5fa932986d0bc0f9556924f5c2379b2c91b0")); + TEST_ASSERT(calc_and_compare_hash("1234567890_2", + "a144d0b4d285260ebbbab6840baceaa09eab3e157443c9458de764b7262c8ace")); + TEST_ASSERT(calc_and_compare_hash("1234567890_3", + "9f839169d293276d1b799707d2171ac1fd5b78d0f3bc7693dbed831524dd2d77")); + TEST_ASSERT(calc_and_compare_hash("1234567890_4", + "6c5fe2a8e3de58a5e5ac061031a8e802ae1fb9e7197862ec1aedf236f0e23475")); + TEST_ASSERT(calc_and_compare_hash( + "0123456789abcde-0123456789abcde-0123456789abcde-0123456789abcde-", + "945ab9d52b069923680c2c067fa6092cbbd9234cf7a38628f3033b2d54d3d3bf")); + TEST_ASSERT(calc_and_compare_hash( + "Franz jagt im komplett verwahrlosten Taxi quer durch Bayern", + "d32b568cd1b96d459e7291ebf4b25d007f275c9f13149beeb782fac0716613f8")); + TEST_ASSERT(calc_and_compare_hash( + "Frank jagt im komplett verwahrlosten Taxi quer durch Bayern", + "78206a866dbb2bf017d8e34274aed01a8ce405b69d45db30bafa00f5eeed7d5e")); + TEST_ASSERT(calc_and_compare_hash("", + "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")); +} + +Test *tests_crypto_sha256_tests(void) +{ + EMB_UNIT_TESTFIXTURES(fixtures) { + new_TestFixture(test_crypto_sha256_hash_sequence), +}; + +EMB_UNIT_TESTCALLER(crypto_sha256_tests, NULL, NULL, + fixtures); + +return (Test *)&crypto_sha256_tests; +} diff --git a/tests/unittests/tests-crypto/tests-crypto.c b/tests/unittests/tests-crypto/tests-crypto.c new file mode 100644 index 0000000000..0e4caafc53 --- /dev/null +++ b/tests/unittests/tests-crypto/tests-crypto.c @@ -0,0 +1,14 @@ +/* + * Copyright (C) 2014 Philipp Rosenkranz + * + * 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 "tests-crypto.h" + +void tests_crypto(void) +{ + TESTS_RUN(tests_crypto_sha256_tests()); +} diff --git a/tests/unittests/tests-crypto/tests-crypto.h b/tests/unittests/tests-crypto/tests-crypto.h new file mode 100644 index 0000000000..e63362e364 --- /dev/null +++ b/tests/unittests/tests-crypto/tests-crypto.h @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2014 Philipp Rosenkranz + * + * 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 tests-crypto.h + * @brief Unittests for the ``crypto`` module + * + * @author Philipp Rosenkranz + */ +#ifndef __TESTS_CRYPTO_H_ +#define __TESTS_CRYPTO_H_ + +#include "../unittests.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief The entry point of this test suite. + */ +void tests_crypto(void); + +/** + * @brief Generates tests for crypto/sha256.h + * + * @return embUnit tests if successful, NULL if not. + */ +Test *tests_crypto_sha256_tests(void); + +#ifdef __cplusplus +} +#endif + +#endif /* __TESTS_CRYPTO_H_ */ +/** @} */