github.com/oskarth/go-ethereum@v1.6.8-0.20191013093314-dac24a9d3494/swarm/multihash/multihash_test.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package multihash 18 19 import ( 20 "bytes" 21 "math/rand" 22 "testing" 23 ) 24 25 // parse multihash, and check that invalid multihashes fail 26 func TestCheckMultihash(t *testing.T) { 27 hashbytes := make([]byte, 32) 28 c, err := rand.Read(hashbytes) 29 if err != nil { 30 t.Fatal(err) 31 } else if c < 32 { 32 t.Fatal("short read") 33 } 34 35 expected := ToMultihash(hashbytes) 36 37 l, hl, _ := GetMultihashLength(expected) 38 if l != 32 { 39 t.Fatalf("expected length %d, got %d", 32, l) 40 } else if hl != 2 { 41 t.Fatalf("expected header length %d, got %d", 2, hl) 42 } 43 if _, _, err := GetMultihashLength(expected[1:]); err == nil { 44 t.Fatal("expected failure on corrupt header") 45 } 46 if _, _, err := GetMultihashLength(expected[:len(expected)-2]); err == nil { 47 t.Fatal("expected failure on short content") 48 } 49 dh, _ := FromMultihash(expected) 50 if !bytes.Equal(dh, hashbytes) { 51 t.Fatalf("expected content hash %x, got %x", hashbytes, dh) 52 } 53 }