Skip to content

Commit 298f7da

Browse files
authored
Adding unit tests for sha256 (valkey-io#2632)
Adding comprehensive unit tests for SHA-256 implementation. These tests verify: 1. Basic functionality with known test vectors (e.g., "abc") 2. Handling of large input data (4KB repeated 1000 times) 3. Edge case with repeated single-byte input (1 million 'a' characters) The tests ensure compatibility with standard SHA-256 implementations and will help detect regressions during future code changes.
1 parent 8d562d2 commit 298f7da

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

src/unit/test_files.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ int test_typesAndAllocSize(int argc, char **argv, int flags);
192192
int test_sdsHeaderSizes(int argc, char **argv, int flags);
193193
int test_sdssplitargs(int argc, char **argv, int flags);
194194
int test_sha1(int argc, char **argv, int flags);
195+
int test_sha256_abc(int argc, char **argv, int flags);
196+
int test_sha256_large(int argc, char **argv, int flags);
197+
int test_sha256_million_a(int argc, char **argv, int flags);
195198
int test_string2ll(int argc, char **argv, int flags);
196199
int test_string2l(int argc, char **argv, int flags);
197200
int test_ll2string(int argc, char **argv, int flags);
@@ -268,6 +271,7 @@ unitTest __test_quicklist_c[] = {{"test_quicklistCreateList", test_quicklistCrea
268271
unitTest __test_rax_c[] = {{"test_raxRandomWalk", test_raxRandomWalk}, {"test_raxIteratorUnitTests", test_raxIteratorUnitTests}, {"test_raxTryInsertUnitTests", test_raxTryInsertUnitTests}, {"test_raxRegressionTest1", test_raxRegressionTest1}, {"test_raxRegressionTest2", test_raxRegressionTest2}, {"test_raxRegressionTest3", test_raxRegressionTest3}, {"test_raxRegressionTest4", test_raxRegressionTest4}, {"test_raxRegressionTest5", test_raxRegressionTest5}, {"test_raxRegressionTest6", test_raxRegressionTest6}, {"test_raxBenchmark", test_raxBenchmark}, {"test_raxHugeKey", test_raxHugeKey}, {"test_raxFuzz", test_raxFuzz}, {"test_raxRecompressHugeKey", test_raxRecompressHugeKey}, {NULL, NULL}};
269272
unitTest __test_sds_c[] = {{"test_sds", test_sds}, {"test_typesAndAllocSize", test_typesAndAllocSize}, {"test_sdsHeaderSizes", test_sdsHeaderSizes}, {"test_sdssplitargs", test_sdssplitargs}, {NULL, NULL}};
270273
unitTest __test_sha1_c[] = {{"test_sha1", test_sha1}, {NULL, NULL}};
274+
unitTest __test_sha256_c[] = {{"test_sha256_abc", test_sha256_abc}, {"test_sha256_large", test_sha256_large}, {"test_sha256_million_a", test_sha256_million_a}, {NULL, NULL}};
271275
unitTest __test_util_c[] = {{"test_string2ll", test_string2ll}, {"test_string2l", test_string2l}, {"test_ll2string", test_ll2string}, {"test_ld2string", test_ld2string}, {"test_fixedpoint_d2string", test_fixedpoint_d2string}, {"test_version2num", test_version2num}, {"test_reclaimFilePageCache", test_reclaimFilePageCache}, {"test_writePointerWithPadding", test_writePointerWithPadding}, {NULL, NULL}};
272276
unitTest __test_valkey_strtod_c[] = {{"test_valkey_strtod", test_valkey_strtod}, {NULL, NULL}};
273277
unitTest __test_vector_c[] = {{"test_vector", test_vector}, {NULL, NULL}};
@@ -296,6 +300,7 @@ struct unitTestSuite {
296300
{"test_rax.c", __test_rax_c},
297301
{"test_sds.c", __test_sds_c},
298302
{"test_sha1.c", __test_sha1_c},
303+
{"test_sha256.c", __test_sha256_c},
299304
{"test_util.c", __test_util_c},
300305
{"test_valkey_strtod.c", __test_valkey_strtod_c},
301306
{"test_vector.c", __test_vector_c},

src/unit/test_sha256.c

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Comprehensive unit tests for SHA-256 implementation.
3+
*
4+
* These tests verify:
5+
* 1. Basic functionality with known test vectors (e.g., "abc")
6+
* 2. Handling of large input data (4KB repeated 1000 times)
7+
* 3. Edge case with repeated single-byte input (1 million 'a' characters)
8+
*
9+
* The tests ensure compatibility with standard SHA-256 implementations
10+
* and will help detect regressions during future code changes.
11+
*/
12+
13+
#include "../sha256.h"
14+
#include "test_help.h"
15+
#include <string.h>
16+
17+
#define BUFSIZE 4096
18+
19+
int test_sha256_abc(int argc, char **argv, int flags) {
20+
SHA256_CTX ctx;
21+
BYTE hash[32];
22+
BYTE expected[32] = {
23+
0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
24+
0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
25+
0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
26+
0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad};
27+
const char *test_str = "abc";
28+
29+
UNUSED(argc);
30+
UNUSED(argv);
31+
UNUSED(flags);
32+
33+
sha256_init(&ctx);
34+
sha256_update(&ctx, (BYTE *)test_str, 3);
35+
sha256_final(&ctx, hash);
36+
37+
TEST_ASSERT(memcmp(hash, expected, 32) == 0);
38+
return 0;
39+
}
40+
41+
int test_sha256_large(int argc, char **argv, int flags) {
42+
SHA256_CTX ctx;
43+
BYTE hash[32], buf[BUFSIZE];
44+
BYTE expected[32] = {
45+
0x8e, 0x44, 0xff, 0x94, 0xb6, 0x8f, 0xcb, 0x09,
46+
0x6a, 0x8d, 0x5c, 0xdb, 0x8f, 0x1c, 0xc7, 0x8a,
47+
0x9c, 0x47, 0x58, 0x45, 0xf1, 0x1a, 0x8d, 0x67,
48+
0x6f, 0x39, 0xc9, 0x53, 0x7e, 0xd2, 0x31, 0xe0};
49+
int i;
50+
51+
UNUSED(argc);
52+
UNUSED(argv);
53+
UNUSED(flags);
54+
55+
for (i = 0; i < BUFSIZE; i++)
56+
buf[i] = i % 256;
57+
58+
sha256_init(&ctx);
59+
for (i = 0; i < 1000; i++)
60+
sha256_update(&ctx, buf, BUFSIZE);
61+
sha256_final(&ctx, hash);
62+
63+
TEST_ASSERT(memcmp(hash, expected, 32) == 0);
64+
return 0;
65+
}
66+
67+
int test_sha256_million_a(int argc, char **argv, int flags) {
68+
SHA256_CTX ctx;
69+
BYTE hash[32];
70+
BYTE expected[32] = {
71+
0xcd, 0xc7, 0x6e, 0x5c, 0x99, 0x14, 0xfb, 0x92,
72+
0x81, 0xa1, 0xc7, 0xe2, 0x84, 0xd7, 0x3e, 0x67,
73+
0xf1, 0x80, 0x9a, 0x48, 0xa4, 0x97, 0x20, 0x0e,
74+
0x04, 0x6d, 0x39, 0xcc, 0xc7, 0x11, 0x2c, 0xd0};
75+
int i;
76+
BYTE a = 'a';
77+
78+
UNUSED(argc);
79+
UNUSED(argv);
80+
UNUSED(flags);
81+
82+
sha256_init(&ctx);
83+
for (i = 0; i < 1000000; i++)
84+
sha256_update(&ctx, &a, 1);
85+
sha256_final(&ctx, hash);
86+
87+
TEST_ASSERT(memcmp(hash, expected, 32) == 0);
88+
return 0;
89+
}

0 commit comments

Comments
 (0)