From 4aa1b6bdc168756c5af0d7ec0d4f412d038a34ca Mon Sep 17 00:00:00 2001 From: siddharth Date: Sun, 10 Oct 2021 10:17:10 -0400 Subject: lib: add test for sha1 --- Makefile | 4 ++++ lib/sha1_test.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 lib/sha1_test.go diff --git a/Makefile b/Makefile index cd6490a..1de7ab7 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,10 @@ fmt: go fmt ${MOD} ${MOD}/challenge ${MOD}/lib .PHONY: fmt +test: + go test ${MOD}/lib +.PHONY: test + clean: go clean rm -f *~ ./*/*~ diff --git a/lib/sha1_test.go b/lib/sha1_test.go new file mode 100644 index 0000000..93fdb6f --- /dev/null +++ b/lib/sha1_test.go @@ -0,0 +1,46 @@ +// Copyright © 2021 rsiddharth +// SPDX-License-Identifier: ISC + +package lib + +import ( + "testing" +) + +func TestSha1Hash(t *testing.T) { + sha1 := Sha1{} + sha1.Init([]uint32{}) + + // Test 1 + m := "abc" + sha1.Message(StrToBytes(m)) + h := sha1.Hash() + e := "a9993e364706816aba3e25717850c26c9cd0d89d" // Expected hash. + if BytesToHexStr(h) != e { + t.Errorf("sha1 test 1 failed: %x != %s\n", h, e) + } + + // Test 2 + m = "abcdbcdecdefdefgefghfghighijhi" + m += "jkijkljklmklmnlmnomnopnopq" + sha1.Message(StrToBytes(m)) + h = sha1.Hash() + e = "84983e441c3bd26ebaae4aa1f95129e5e54670f1" // Expected hash. + if BytesToHexStr(h) != e { + t.Errorf("sha1 test 2 failed: %x != %s\n", h, e) + } + + // Test 3 + m = "" + m1 := "01234567012345670123456701234567" + m1 += "01234567012345670123456701234567" + for i := 0; i < 10; i++ { + m += m1 + } + sha1.Message(StrToBytes(m)) + h = sha1.Hash() + e = "dea356a2cddd90c7a7ecedc5ebb563934f460452" // Expected hash. + if BytesToHexStr(h) != e { + t.Errorf("sha1 test 3 failed: %x != %s\n", h, e) + } +} -- cgit v1.2.3