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