github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/swarm/multihash/multihash_test.go (about) 1 2 //此源码被清华学神尹成大魔王专业翻译分析并修改 3 //尹成QQ77025077 4 //尹成微信18510341407 5 //尹成所在QQ群721929980 6 //尹成邮箱 yinc13@mails.tsinghua.edu.cn 7 //尹成毕业于清华大学,微软区块链领域全球最有价值专家 8 //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620 9 // 10 // 11 // 12 // 13 // 14 // 15 // 16 // 17 // 18 // 19 // 20 // 21 // 22 // 23 // 24 25 package multihash 26 27 import ( 28 "bytes" 29 "math/rand" 30 "testing" 31 ) 32 33 // 34 func TestCheckMultihash(t *testing.T) { 35 hashbytes := make([]byte, 32) 36 c, err := rand.Read(hashbytes) 37 if err != nil { 38 t.Fatal(err) 39 } else if c < 32 { 40 t.Fatal("short read") 41 } 42 43 expected := ToMultihash(hashbytes) 44 45 l, hl, _ := GetMultihashLength(expected) 46 if l != 32 { 47 t.Fatalf("expected length %d, got %d", 32, l) 48 } else if hl != 2 { 49 t.Fatalf("expected header length %d, got %d", 2, hl) 50 } 51 if _, _, err := GetMultihashLength(expected[1:]); err == nil { 52 t.Fatal("expected failure on corrupt header") 53 } 54 if _, _, err := GetMultihashLength(expected[:len(expected)-2]); err == nil { 55 t.Fatal("expected failure on short content") 56 } 57 dh, _ := FromMultihash(expected) 58 if !bytes.Equal(dh, hashbytes) { 59 t.Fatalf("expected content hash %x, got %x", hashbytes, dh) 60 } 61 }