forked from holochain/holochain-proto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheader_test.go
97 lines (83 loc) · 2.29 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
package holochain
import (
"bytes"
"crypto/rand"
"fmt"
ic "github.com/libp2p/go-libp2p-crypto"
. "github.com/smartystreets/goconvey/convey"
"testing"
"time"
)
func TestNewHeader(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, "myData", &e, key, ph, ph)
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 TestMarshalHeader(t *testing.T) {
h, key, now := chainTestSetup()
e := GobEntry{C: "some data"}
hd := testHeader(h, "myData", &e, key, now)
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 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(hd.EntryLink.H)
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.Sig.S.321)
return h1
}