github.com/metacurrency/holochain@v0.1.0-alpha-26.0.20200915073418-5c83169c9b5b/header_test.go (about) 1 package holochain 2 3 import ( 4 "bytes" 5 "crypto/rand" 6 "fmt" 7 . "github.com/holochain/holochain-proto/hash" 8 ic "github.com/libp2p/go-libp2p-crypto" 9 . "github.com/smartystreets/goconvey/convey" 10 "testing" 11 "time" 12 ) 13 14 func TestHeaderNew(t *testing.T) { 15 h, key, now := chainTestSetup() 16 17 Convey("it should make a header and return its hash", t, func() { 18 e := GobEntry{C: "some data"} 19 ph := NullHash() 20 hash, header, err := newHeader(h, now, "evenNumbers", &e, key, ph, ph, NullHash()) 21 22 So(err, ShouldBeNil) 23 // encode the header and create a hash of it 24 b, _ := header.Marshal() 25 var h2 Hash 26 h2, _ = Sum(h, b) 27 So(h2.String(), ShouldEqual, hash.String()) 28 }) 29 30 Convey("it should make a header and return its hash if change header", t, func() { 31 e := GobEntry{C: "some data"} 32 ph := NullHash() 33 modHash, _ := NewHash("QmP1DfoUjiWH2ZBo1PBH6FupdBucbDepx3HpWmEY6JMUpY") 34 hash, header, err := newHeader(h, now, "evenNumbers", &e, key, ph, ph, modHash) 35 36 So(err, ShouldBeNil) 37 // encode the header and create a hash of it 38 b, _ := header.Marshal() 39 var h2 Hash 40 h2, _ = Sum(h, b) 41 So(h2.String(), ShouldEqual, hash.String()) 42 }) 43 } 44 45 func TestHeaderToJSON(t *testing.T) { 46 h, key, now := chainTestSetup() 47 Convey("it should convert a header to JSON", t, func() { 48 e := GobEntry{C: "1234"} 49 hd := testHeader(h, "evenNumbers", &e, key, now) 50 j, err := hd.ToJSON() 51 So(err, ShouldBeNil) 52 So(j, ShouldStartWith, `{"Type":"evenNumbers","Time":"`) 53 So(j, ShouldEndWith, `","EntryLink":"QmNiCwBNA8MWDADTFVq1BonUEJbS2SvjAoNkZZrhEwcuU2","HeaderLink":"QmNiCwBNA8MWDADTFVq1BonUEJbS2SvjAoNkZZrhEwcuUi","TypeLink":"","Signature":"3eDinUfqsX4V2iuwFvFNSwyy4KEugYj6DPpssjrAsabkVvozBrWrLJRuA9AXhiN8R3MzZvyLfW2BV8zKDevSDiVR"}`) 54 }) 55 } 56 57 func TestHeaderMarshal(t *testing.T) { 58 h, key, now := chainTestSetup() 59 60 e := GobEntry{C: "some data"} 61 hd := testHeader(h, "evenNumbers", &e, key, now) 62 hd.Change, _ = NewHash("QmNiCwBNA8MWDADTFVq1BonUEJbS2SvjAoNkZZrhEwcuU2") 63 Convey("it should round-trip", t, func() { 64 b, err := hd.Marshal() 65 So(err, ShouldBeNil) 66 var nh Header 67 err = (&nh).Unmarshal(b, 34) 68 So(err, ShouldBeNil) 69 So(fmt.Sprintf("%v", nh), ShouldEqual, fmt.Sprintf("%v", *hd)) 70 }) 71 } 72 73 func TestSignatureB58(t *testing.T) { 74 h, key, now := chainTestSetup() 75 e := GobEntry{C: "1234"} 76 hd := testHeader(h, "evenNumbers", &e, key, now) 77 Convey("it should round-trip", t, func() { 78 b58sig := hd.Sig.B58String() 79 So(b58sig, ShouldEqual, "3eDinUfqsX4V2iuwFvFNSwyy4KEugYj6DPpssjrAsabkVvozBrWrLJRuA9AXhiN8R3MzZvyLfW2BV8zKDevSDiVR") 80 newSig := SignatureFromB58String(b58sig) 81 So(newSig.Equal(hd.Sig), ShouldBeTrue) 82 }) 83 } 84 85 func TestMarshalSignature(t *testing.T) { 86 var s Signature 87 Convey("it should round-trip an empty signature", t, func() { 88 var b bytes.Buffer 89 90 err := MarshalSignature(&b, &s) 91 So(err, ShouldBeNil) 92 var ns Signature 93 err = UnmarshalSignature(&b, &ns) 94 So(err, ShouldBeNil) 95 So(fmt.Sprintf("%v", ns), ShouldEqual, fmt.Sprintf("%v", s)) 96 }) 97 98 Convey("it should round-trip a random signature", t, func() { 99 var b bytes.Buffer 100 101 r := make([]byte, 64) 102 _, err := rand.Read(r) 103 s.S = r 104 err = MarshalSignature(&b, &s) 105 So(err, ShouldBeNil) 106 var ns Signature 107 err = UnmarshalSignature(&b, &ns) 108 So(err, ShouldBeNil) 109 So(fmt.Sprintf("%v", ns), ShouldEqual, fmt.Sprintf("%v", s)) 110 }) 111 } 112 113 //----- test util functions 114 115 func testHeader(h HashSpec, t string, entry Entry, key ic.PrivKey, now time.Time) *Header { 116 hd := mkTestHeader(t) 117 sig, err := key.Sign([]byte(hd.EntryLink)) 118 if err != nil { 119 panic(err) 120 } 121 hd.Sig = Signature{S: sig} 122 return &hd 123 } 124 125 func mkTestHeader(t string) Header { 126 hl, _ := NewHash("QmNiCwBNA8MWDADTFVq1BonUEJbS2SvjAoNkZZrhEwcuUi") 127 el, _ := NewHash("QmNiCwBNA8MWDADTFVq1BonUEJbS2SvjAoNkZZrhEwcuU2") 128 now := time.Unix(1, 1) // pick a constant time so the test will always work 129 h1 := Header{Time: now, Type: t, // Meta: "dog", 130 HeaderLink: hl, 131 EntryLink: el, 132 TypeLink: NullHash(), 133 } 134 h1.Change = NullHash() 135 136 //h1.Sig.S.321) 137 return h1 138 }