Rename conflictive macros, refactor crypto tests

This commit is contained in:
Sergio R. Caprile committed 2025-03-04 11:55:57 -03:00
1 parent 562e82f5ac
commit 61fc480850
7 files changed
+104 -41

No files matched your search

+68 -3
View File
@@ -3587,6 +3587,63 @@ static void test_sha1(void) {
test_sha1_str(")_)+_)!&^*%$#>>>{}}}{{{][[[[]]]", expected_hash_3);
}
static void test_sha256_str(const char *string,
const unsigned char *expected_hash) {
unsigned char digest[32];
mg_sha256(digest, (unsigned char *) string, strlen(string));
ASSERT((memcmp(digest, expected_hash, 32) == 0));
}
static void test_sha256(void) {
const unsigned char expected_hash_1[] = {
0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4,
0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b,
0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55};
const unsigned char expected_hash_2[] = {
0xbc, 0x07, 0x32, 0x21, 0x17, 0x8e, 0x81, 0xbd, 0x2f, 0x67, 0x13,
0x3a, 0xca, 0xb4, 0x07, 0xad, 0x5b, 0x61, 0x8b, 0x33, 0xd2, 0x95,
0x9e, 0x94, 0x45, 0x45, 0xdc, 0x24, 0x99, 0x0a, 0xff, 0x92};
const unsigned char expected_hash_3[] = {
0x1b, 0x65, 0x3e, 0xda, 0x9a, 0x2a, 0x24, 0x55, 0xa3, 0x56, 0x38,
0x08, 0xf4, 0xf7, 0xc5, 0xa6, 0xc5, 0x2d, 0x2c, 0xb1, 0x71, 0xe5,
0x90, 0x4c, 0x83, 0x9c, 0x77, 0x92, 0x51, 0xa2, 0x84, 0x4a};
test_sha256_str("", expected_hash_1);
test_sha256_str(
"#&*%$DHFH(0x12345)^&*(^!@$%^^&&*1298**&^%DHKSHFLS)(*)&^^%$#!!!!",
expected_hash_2);
test_sha256_str(")_)+_)!&^*%$#>>>{}}}{{{][[[[]]]", expected_hash_3);
}
static void test_sha384_str(const char *string,
const unsigned char *expected_hash) {
unsigned char digest[48];
mg_sha384(digest, (unsigned char *) string, strlen(string));
ASSERT((memcmp(digest, expected_hash, 48) == 0));
}
static void test_sha384(void) {
const unsigned char expected_hash_1[] = {
0x38, 0xb0, 0x60, 0xa7, 0x51, 0xac, 0x96, 0x38, 0x4c, 0xd9, 0x32, 0x7e,
0xb1, 0xb1, 0xe3, 0x6a, 0x21, 0xfd, 0xb7, 0x11, 0x14, 0xbe, 0x07, 0x43,
0x4c, 0x0c, 0xc7, 0xbf, 0x63, 0xf6, 0xe1, 0xda, 0x27, 0x4e, 0xde, 0xbf,
0xe7, 0x6f, 0x65, 0xfb, 0xd5, 0x1a, 0xd2, 0xf1, 0x48, 0x98, 0xb9, 0x5b};
const unsigned char expected_hash_2[] = {
0x77, 0xe7, 0x0a, 0x31, 0xe5, 0xcd, 0x68, 0xa4, 0xc5, 0xb3, 0x70, 0x55,
0x38, 0xd0, 0x90, 0xb0, 0xcd, 0xb6, 0xf4, 0x1c, 0x2e, 0xe6, 0xf4, 0xdd,
0xf6, 0xb4, 0xfc, 0x97, 0x01, 0x79, 0x3c, 0x89, 0x82, 0x3b, 0x13, 0xa2,
0x48, 0xa7, 0xfe, 0xd2, 0xd0, 0xc4, 0xbf, 0xed, 0x85, 0xb6, 0x20, 0xc7};
const unsigned char expected_hash_3[] = {
0x45, 0xa1, 0xc6, 0x4d, 0x99, 0x29, 0x42, 0x87, 0x49, 0x46, 0x73, 0x3c,
0x3b, 0xc8, 0xbc, 0x9c, 0x43, 0x10, 0x75, 0x23, 0x89, 0x22, 0x04, 0x41,
0xcd, 0xa3, 0x34, 0xeb, 0x97, 0x9f, 0x2a, 0xbf, 0x17, 0x94, 0x38, 0x72,
0x6b, 0xd8, 0x8e, 0xcc, 0xb5, 0x50, 0xc6, 0x5b, 0x35, 0x1f, 0x91, 0x90};
test_sha384_str("", expected_hash_1);
test_sha384_str(
"#&*%$DHFH(0x12345)^&*(^!@$%^^&&*1298**&^%DHKSHFLS)(*)&^^%$#!!!!",
expected_hash_2);
test_sha384_str(")_)+_)!&^*%$#>>>{}}}{{{][[[[]]]", expected_hash_3);
}
static void test_split(void) {
struct mg_str a, b, s;
@@ -3638,7 +3695,8 @@ static void test_split(void) {
ASSERT(mg_strcmp(b, mg_str("")) == 0);
}
static void test_crypto(void) {
static void test_x25519(void) {
#if MG_TLS == MG_TLS_BUILTIN
uint8_t key[X25519_BYTES];
uint8_t buf[X25519_BYTES];
char tmp[100];
@@ -3649,6 +3707,15 @@ static void test_crypto(void) {
mg_snprintf(tmp, sizeof(tmp), "%M", mg_print_hex, sizeof(buf), buf);
MG_INFO(("%s", tmp));
ASSERT(mg_strcmp(mg_str("8f40c5adb6"), mg_str_n(tmp, 10)) == 0);
#endif
}
static void test_crypto(void) {
test_md5();
test_sha1();
test_sha256();
test_sha384();
test_x25519();
}
int main(void) {
@@ -3698,8 +3765,6 @@ int main(void) {
(void) test_sntp, (void) test_mqtt, (void) test_http_client;
#endif
test_poll();
test_md5();
test_sha1();
printf("SUCCESS. Total tests: %d\n", s_num_tests);
return EXIT_SUCCESS;