From 2ebdc3fd8466948a5f2083727eea145e3fb61e19 Mon Sep 17 00:00:00 2001 From: francisco souza Date: Thu, 3 Jun 2021 10:04:50 -0400 Subject: [PATCH] internal/checksum: add some tests --- internal/checksum/checksum_test.go | 45 ++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/internal/checksum/checksum_test.go b/internal/checksum/checksum_test.go index 16cbbbf77a..55410971ad 100644 --- a/internal/checksum/checksum_test.go +++ b/internal/checksum/checksum_test.go @@ -3,3 +3,48 @@ // license that can be found in the LICENSE file. package checksum + +import ( + "bytes" + "crypto/rand" + "encoding/base64" + "testing" +) + +func TestEncodedCrc32cChecksum(t *testing.T) { + var data [32]byte + _, err := rand.Read(data[:]) + if err != nil { + t.Fatal(err) + } + + encoded := EncodedCrc32cChecksum(data[:]) + decoded, err := base64.StdEncoding.DecodeString(encoded) + if err != nil { + t.Fatal(err) + } + + expected := crc32cChecksum(data[:]) + if !bytes.Equal(decoded, expected) { + t.Errorf("incorrect value after decoding\nwant %x, got %x", expected, decoded) + } +} + +func TestEncodedMd5Hash(t *testing.T) { + var data [32]byte + _, err := rand.Read(data[:]) + if err != nil { + t.Fatal(err) + } + + encoded := EncodedMd5Hash(data[:]) + decoded, err := base64.StdEncoding.DecodeString(encoded) + if err != nil { + t.Fatal(err) + } + + expected := MD5Hash(data[:]) + if !bytes.Equal(decoded, expected) { + t.Errorf("incorrect value after decoding\nwant %x, got %x", expected, decoded) + } +}