aboutsummaryrefslogtreecommitdiff
path: root/src/microhttpd/test_sha256.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/microhttpd/test_sha256.c')
-rw-r--r--src/microhttpd/test_sha256.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/microhttpd/test_sha256.c b/src/microhttpd/test_sha256.c
index 863859fa..91b28af2 100644
--- a/src/microhttpd/test_sha256.c
+++ b/src/microhttpd/test_sha256.c
@@ -27,6 +27,7 @@
27#include "sha256.h" 27#include "sha256.h"
28#include "test_helpers.h" 28#include "test_helpers.h"
29#include <stdio.h> 29#include <stdio.h>
30#include <stdlib.h>
30 31
31static int verbose = 0; /* verbose level (0-1)*/ 32static int verbose = 0; /* verbose level (0-1)*/
32 33
@@ -416,6 +417,48 @@ test2_bin (void)
416} 417}
417 418
418 419
420/* Use data set number 7 as it has the longest sequence */
421#define DATA_POS 6
422#define MAX_OFFSET 31
423
424static int
425test_unaligned (void)
426{
427 int num_failed = 0;
428 unsigned int offset;
429 uint8_t *buf;
430 uint8_t *digest_buf;
431
432 const struct data_unit2 *const tdata = data_units2 + DATA_POS;
433
434 buf = malloc (tdata->bin_l.len + MAX_OFFSET);
435 digest_buf = malloc (SHA256_DIGEST_SIZE + MAX_OFFSET);
436 if ((NULL == buf) || (NULL == digest_buf))
437 exit (99);
438
439 for (offset = MAX_OFFSET; offset >= 1; --offset)
440 {
441 struct sha256_ctx ctx;
442 uint8_t *unaligned_digest;
443 uint8_t *unaligned_buf;
444
445 unaligned_buf = buf + offset;
446 memcpy (unaligned_buf, tdata->bin_l.bin, tdata->bin_l.len);
447 unaligned_digest = digest_buf + MAX_OFFSET - offset;
448 memset (unaligned_digest, 0, SHA256_DIGEST_SIZE);
449
450 MHD_SHA256_init (&ctx);
451 MHD_SHA256_update (&ctx, unaligned_buf, tdata->bin_l.len);
452 MHD_SHA256_finish (&ctx, unaligned_digest);
453 num_failed += check_result (__FUNCTION__, MAX_OFFSET - offset,
454 unaligned_digest, tdata->digest);
455 }
456 free (digest_buf);
457 free (buf);
458 return num_failed;
459}
460
461
419int 462int
420main (int argc, char *argv[]) 463main (int argc, char *argv[])
421{ 464{
@@ -430,5 +473,7 @@ main (int argc, char *argv[])
430 num_failed += test2_str (); 473 num_failed += test2_str ();
431 num_failed += test2_bin (); 474 num_failed += test2_bin ();
432 475
476 num_failed += test_unaligned ();
477
433 return num_failed ? 1 : 0; 478 return num_failed ? 1 : 0;
434} 479}