Skip to content

Commit

Permalink
Migrate sha1 unit test to new framework (#470)
Browse files Browse the repository at this point in the history
This migrates unit tests related to sha1 to new framework, ref: #428.

---------

Signed-off-by: Shivshankar-Reddy <shiva.sheri.github@gmail.com>
Signed-off-by: Madelyn Olson <madelyneolson@gmail.com>
Co-authored-by: Madelyn Olson <madelyneolson@gmail.com>
  • Loading branch information
Shivshankar-Reddy and madolson committed May 10, 2024
1 parent 138a7d9 commit e242799
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 34 deletions.
1 change: 0 additions & 1 deletion src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -6932,7 +6932,6 @@ struct serverTest {
{"ziplist", ziplistTest},
{"quicklist", quicklistTest},
{"zipmap", zipmapTest},
{"sha1test", sha1Test},
{"endianconv", endianconvTest},
{"zmalloc", zmalloc_test},
{"dict", dictTest},
Expand Down
30 changes: 0 additions & 30 deletions src/sha1.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,33 +207,3 @@ void SHA1Final(unsigned char digest[20], SHA1_CTX* context)
memset(&finalcount, '\0', sizeof(finalcount));
}
/* ================ end of sha1.c ================ */

#ifdef SERVER_TEST
#define BUFSIZE 4096

#define UNUSED(x) (void)(x)
int sha1Test(int argc, char **argv, int flags)
{
SHA1_CTX ctx;
unsigned char hash[20], buf[BUFSIZE];
int i;

UNUSED(argc);
UNUSED(argv);
UNUSED(flags);

for(i=0;i<BUFSIZE;i++)
buf[i] = i;

SHA1Init(&ctx);
for(i=0;i<1000;i++)
SHA1Update(&ctx, buf, BUFSIZE);
SHA1Final(hash, &ctx);

printf("SHA1=");
for(i=0;i<20;i++)
printf("%02x", hash[i]);
printf("\n");
return 0;
}
#endif
3 changes: 0 additions & 3 deletions src/sha1.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,4 @@ void SHA1Init(SHA1_CTX* context);
__attribute__((noinline)) void SHA1Update(SHA1_CTX* context, const unsigned char* data, uint32_t len);
void SHA1Final(unsigned char digest[20], SHA1_CTX* context);

#ifdef SERVER_TEST
int sha1Test(int argc, char **argv, int flags);
#endif
#endif
3 changes: 3 additions & 0 deletions src/unit/test_files.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ int test_kvstoreIteratorRemoveAllKeysDeleteEmptyDict(int argc, char **argv, int
int test_kvstoreDictIteratorRemoveAllKeysNoDeleteEmptyDict(int argc, char **argv, int flags);
int test_kvstoreDictIteratorRemoveAllKeysDeleteEmptyDict(int argc, char **argv, int flags);
int test_sds(int argc, char **argv, int flags);
int test_sha1(int argc, char **argv, int flags);
int test_string2ll(int argc, char **argv, int flags);
int test_string2l(int argc, char **argv, int flags);
int test_ll2string(int argc, char **argv, int flags);
Expand All @@ -34,6 +35,7 @@ unitTest __test_crc64combine_c[] = {{"test_crc64combine", test_crc64combine}, {N
unitTest __test_intset_c[] = {{"test_intsetValueEncodings", test_intsetValueEncodings}, {"test_intsetBasicAdding", test_intsetBasicAdding}, {"test_intsetLargeNumberRandomAdd", test_intsetLargeNumberRandomAdd}, {"test_intsetUpgradeFromint16Toint32", test_intsetUpgradeFromint16Toint32}, {"test_intsetUpgradeFromint16Toint64", test_intsetUpgradeFromint16Toint64}, {"test_intsetUpgradeFromint32Toint64", test_intsetUpgradeFromint32Toint64}, {"test_intsetStressLookups", test_intsetStressLookups}, {"test_intsetStressAddDelete", test_intsetStressAddDelete}, {NULL, NULL}};
unitTest __test_kvstore_c[] = {{"test_kvstoreAdd16Keys", test_kvstoreAdd16Keys}, {"test_kvstoreIteratorRemoveAllKeysNoDeleteEmptyDict", test_kvstoreIteratorRemoveAllKeysNoDeleteEmptyDict}, {"test_kvstoreIteratorRemoveAllKeysDeleteEmptyDict", test_kvstoreIteratorRemoveAllKeysDeleteEmptyDict}, {"test_kvstoreDictIteratorRemoveAllKeysNoDeleteEmptyDict", test_kvstoreDictIteratorRemoveAllKeysNoDeleteEmptyDict}, {"test_kvstoreDictIteratorRemoveAllKeysDeleteEmptyDict", test_kvstoreDictIteratorRemoveAllKeysDeleteEmptyDict}, {NULL, NULL}};
unitTest __test_sds_c[] = {{"test_sds", test_sds}, {NULL, NULL}};
unitTest __test_sha1_c[] = {{"test_sha1", test_sha1}, {NULL, NULL}};
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_reclaimFilePageCache", test_reclaimFilePageCache}, {NULL, NULL}};

struct unitTestSuite {
Expand All @@ -45,5 +47,6 @@ struct unitTestSuite {
{"test_intset.c", __test_intset_c},
{"test_kvstore.c", __test_kvstore_c},
{"test_sds.c", __test_sds_c},
{"test_sha1.c", __test_sha1_c},
{"test_util.c", __test_util_c},
};
27 changes: 27 additions & 0 deletions src/unit/test_sha1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "../sha1.c"
#include "test_help.h"

#define BUFSIZE 4096

int test_sha1(int argc, char **argv, int flags) {
SHA1_CTX ctx;
unsigned char hash[20], buf[BUFSIZE];
unsigned char expected[20] = {0x15, 0xdd, 0x99, 0xa1, 0x99, 0x1e, 0x0b, 0x38,
0x26, 0xfe, 0xde, 0x3d, 0xef, 0xfc, 0x1f, 0xeb, 0xa4, 0x22, 0x78, 0xe6};
int i;

UNUSED(argc);
UNUSED(argv);
UNUSED(flags);

for(i=0;i<BUFSIZE;i++)
buf[i] = i;

SHA1Init(&ctx);
for(i=0;i<1000;i++)
SHA1Update(&ctx, buf, BUFSIZE);
SHA1Final(hash, &ctx);

TEST_ASSERT(memcmp(hash, expected, 20) == 0);
return 0;
}

0 comments on commit e242799

Please sign in to comment.