github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/swarm/multihash/multihash_test.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 12:09:47</date>
    10  //</624342671875379200>
    11  
    12  //
    13  //
    14  //
    15  //
    16  //
    17  //
    18  //
    19  //
    20  //
    21  //
    22  //
    23  //
    24  //
    25  //
    26  //
    27  
    28  package multihash
    29  
    30  import (
    31  	"bytes"
    32  	"math/rand"
    33  	"testing"
    34  )
    35  
    36  //
    37  func TestCheckMultihash(t *testing.T) {
    38  	hashbytes := make([]byte, 32)
    39  	c, err := rand.Read(hashbytes)
    40  	if err != nil {
    41  		t.Fatal(err)
    42  	} else if c < 32 {
    43  		t.Fatal("short read")
    44  	}
    45  
    46  	expected := ToMultihash(hashbytes)
    47  
    48  	l, hl, _ := GetMultihashLength(expected)
    49  	if l != 32 {
    50  		t.Fatalf("expected length %d, got %d", 32, l)
    51  	} else if hl != 2 {
    52  		t.Fatalf("expected header length %d, got %d", 2, hl)
    53  	}
    54  	if _, _, err := GetMultihashLength(expected[1:]); err == nil {
    55  		t.Fatal("expected failure on corrupt header")
    56  	}
    57  	if _, _, err := GetMultihashLength(expected[:len(expected)-2]); err == nil {
    58  		t.Fatal("expected failure on short content")
    59  	}
    60  	dh, _ := FromMultihash(expected)
    61  	if !bytes.Equal(dh, hashbytes) {
    62  		t.Fatalf("expected content hash %x, got %x", hashbytes, dh)
    63  	}
    64  }
    65