unittests/crypto: changed to really compare digest vs string of digest
* added additional test with longer input * uncrustified file
This commit is contained in:
parent
80023f07d8
commit
59a69796ab
@ -10,6 +10,7 @@
|
|||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "embUnit/embUnit.h"
|
#include "embUnit/embUnit.h"
|
||||||
|
|
||||||
@ -17,15 +18,33 @@
|
|||||||
|
|
||||||
#include "tests-crypto.h"
|
#include "tests-crypto.h"
|
||||||
|
|
||||||
|
static int compare_str_vs_digest(const char *str,
|
||||||
|
const unsigned char hash[SHA256_DIGEST_LENGTH])
|
||||||
|
{
|
||||||
|
char ch[3] = { 0, 0, 0 };
|
||||||
|
size_t iter_hash = 0;
|
||||||
|
size_t str_length = strlen(str);
|
||||||
|
for (size_t i = 0; i < str_length; i += 2) {
|
||||||
|
ch[0] = str[i];
|
||||||
|
ch[1] = str[i + 1];
|
||||||
|
|
||||||
|
if (hash[iter_hash++] != strtol(ch, NULL, 16)) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
static int calc_and_compare_hash(const char *str, const char *expected)
|
static int calc_and_compare_hash(const char *str, const char *expected)
|
||||||
{
|
{
|
||||||
static unsigned char hash[SHA256_DIGEST_LENGTH];
|
static unsigned char hash[SHA256_DIGEST_LENGTH];
|
||||||
sha256_context_t sha256;
|
sha256_context_t sha256;
|
||||||
|
|
||||||
sha256_init(&sha256);
|
sha256_init(&sha256);
|
||||||
sha256_update(&sha256, str, strlen(str));
|
sha256_update(&sha256, str, strlen(str));
|
||||||
sha256_final(hash, &sha256);
|
sha256_final(hash, &sha256);
|
||||||
|
|
||||||
return strncmp((const char *) hash, expected, SHA256_DIGEST_LENGTH);
|
return compare_str_vs_digest(expected, hash);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test_crypto_sha256_hash_sequence(void)
|
static void test_crypto_sha256_hash_sequence(void)
|
||||||
@ -49,16 +68,27 @@ static void test_crypto_sha256_hash_sequence(void)
|
|||||||
"78206a866dbb2bf017d8e34274aed01a8ce405b69d45db30bafa00f5eeed7d5e"));
|
"78206a866dbb2bf017d8e34274aed01a8ce405b69d45db30bafa00f5eeed7d5e"));
|
||||||
TEST_ASSERT(calc_and_compare_hash("",
|
TEST_ASSERT(calc_and_compare_hash("",
|
||||||
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"));
|
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"));
|
||||||
|
TEST_ASSERT(calc_and_compare_hash(
|
||||||
|
"RIOT is an open-source microkernel-based operating system, designed"
|
||||||
|
" to match the requirements of Internet of Things (IoT) devices and"
|
||||||
|
" other embedded devices. These requirements include a very low memory"
|
||||||
|
" footprint (on the order of a few kilobytes), high energy efficiency"
|
||||||
|
", real-time capabilities, communication stacks for both wireless and"
|
||||||
|
" wired networks, and support for a wide range of low-power hardware.",
|
||||||
|
"06c84971e2831c48b8293144c762e3236a78217354896185b14a3a84f7cd8066"));
|
||||||
|
/* test failing sha256 by switching last byte of expected hash from 3b to 3c */
|
||||||
|
TEST_ASSERT(!calc_and_compare_hash("This test fails!",
|
||||||
|
"c19d3bf8588897076873f1a0a106ba840ca46bd1179d592953acecc4df59593c"));
|
||||||
}
|
}
|
||||||
|
|
||||||
Test *tests_crypto_sha256_tests(void)
|
Test *tests_crypto_sha256_tests(void)
|
||||||
{
|
{
|
||||||
EMB_UNIT_TESTFIXTURES(fixtures) {
|
EMB_UNIT_TESTFIXTURES(fixtures) {
|
||||||
new_TestFixture(test_crypto_sha256_hash_sequence),
|
new_TestFixture(test_crypto_sha256_hash_sequence),
|
||||||
};
|
};
|
||||||
|
|
||||||
EMB_UNIT_TESTCALLER(crypto_sha256_tests, NULL, NULL,
|
EMB_UNIT_TESTCALLER(crypto_sha256_tests, NULL, NULL,
|
||||||
fixtures);
|
fixtures);
|
||||||
|
|
||||||
return (Test *)&crypto_sha256_tests;
|
return (Test *)&crypto_sha256_tests;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user