github.com/ethersphere/bee/v2@v2.2.0/pkg/swarm/hasher_test.go (about) 1 // Copyright 2023 The Swarm Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package swarm_test 6 7 import ( 8 "bytes" 9 "testing" 10 11 "github.com/ethersphere/bee/v2/pkg/swarm" 12 ) 13 14 func TestNewHasher(t *testing.T) { 15 t.Parallel() 16 17 tests := []struct { 18 plaintext []byte 19 hash []byte 20 }{ 21 { 22 plaintext: []byte("Digital Freedom Now."), 23 hash: []byte{43, 108, 13, 242, 182, 16, 111, 176, 64, 234, 1, 180, 231, 199, 55, 85, 89, 149, 188, 70, 54, 111, 149, 1, 187, 76, 74, 232, 251, 194, 192, 190}, 24 }, 25 { 26 plaintext: []byte("If Ethereum is the world's CPU, Swarm is the world's Hard Drive"), 27 hash: []byte{189, 23, 172, 191, 253, 137, 130, 94, 251, 161, 91, 101, 97, 229, 100, 172, 122, 47, 152, 84, 63, 116, 108, 216, 0, 66, 111, 10, 247, 85, 13, 210}, 28 }, 29 } 30 31 for _, tc := range tests { 32 h := swarm.NewHasher() 33 34 _, err := h.Write(tc.plaintext) 35 if err != nil { 36 t.Fatal(err) 37 } 38 39 hash := h.Sum(nil) 40 41 if !bytes.Equal(hash, tc.hash) { 42 t.Fatalf("unexpected hash value") 43 } 44 } 45 } 46 47 func TestNewTrHasher(t *testing.T) { 48 t.Parallel() 49 50 tests := []struct { 51 plaintext []byte 52 prefix []byte 53 hash []byte 54 }{ 55 { 56 plaintext: []byte("Digital Freedom Now."), 57 prefix: []byte("swarm"), 58 hash: []byte{143, 34, 52, 99, 113, 222, 205, 101, 157, 242, 66, 64, 18, 236, 79, 127, 187, 2, 169, 109, 173, 96, 35, 38, 43, 254, 218, 153, 189, 177, 119, 115}, 59 }, 60 { 61 plaintext: []byte("If Ethereum is the world's CPU, Swarm is the world's Hard Drive"), 62 prefix: []byte("swarm"), 63 hash: []byte{27, 253, 128, 158, 74, 63, 27, 178, 245, 169, 125, 189, 181, 37, 94, 163, 150, 155, 16, 175, 5, 215, 62, 90, 249, 72, 38, 80, 78, 119, 44, 206}, 64 }, 65 } 66 67 // Run tests cases against TrHasher 68 for _, tc := range tests { 69 h := swarm.NewPrefixHasher(tc.prefix) 70 71 _, err := h.Write(tc.plaintext) 72 if err != nil { 73 t.Fatal(err) 74 } 75 76 hash := h.Sum(nil) 77 78 if !bytes.Equal(hash, tc.hash) { 79 t.Fatalf("unexpected hash value") 80 } 81 } 82 83 // Run tests against NewHasher 84 // This time prefix needs to be manually included in hash 85 for _, tc := range tests { 86 h := swarm.NewHasher() 87 88 _, err := h.Write(tc.prefix) 89 if err != nil { 90 t.Fatal(err) 91 } 92 _, err = h.Write(tc.plaintext) 93 if err != nil { 94 t.Fatal(err) 95 } 96 97 hash := h.Sum(nil) 98 99 if !bytes.Equal(hash, tc.hash) { 100 t.Fatalf("unexpected hash value") 101 } 102 } 103 }