forked from FactomProject/FactomCode
/
commit_test.go
101 lines (84 loc) · 2.56 KB
/
commit_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
package common_test
import (
"crypto/rand"
"encoding/hex"
"fmt"
"testing"
"github.com/FactomProject/FactomCode/common"
ed "github.com/FactomProject/ed25519"
)
var (
_ = fmt.Sprint("testing")
)
func TestCommitEntryMarshal(t *testing.T) {
fmt.Printf("---\nTestCommitEntryMarshal\n---\n")
ce := common.NewCommitEntry()
// test MarshalBinary on a zeroed CommitEntry
if p, err := ce.MarshalBinary(); err != nil {
t.Error(err)
} else if z := make([]byte, common.CommitEntrySize); string(p) != string(z) {
t.Errorf("Marshal failed on zeroed CommitEntry")
}
// build a CommitEntry for testing
ce.Version = 0
ce.MilliTime = &[6]byte{1, 1, 1, 1, 1, 1}
p, _ := hex.DecodeString("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
ce.EntryHash.SetBytes(p)
ce.Credits = 1
// make a key and sign the msg
if pub, privkey, err := ed.GenerateKey(rand.Reader); err != nil {
t.Error(err)
} else {
ce.ECPubKey = pub
ce.Sig = ed.Sign(privkey, ce.CommitMsg())
}
// marshal and unmarshal the commit and see if it matches
ce2 := common.NewCommitEntry()
if p, err := ce.MarshalBinary(); err != nil {
t.Error(err)
} else {
t.Logf("%x\n", p)
ce2.UnmarshalBinary(p)
}
if !ce2.IsValid() {
t.Errorf("signature did not match after unmarshalbinary")
}
}
func TestCommitChainMarshal(t *testing.T) {
fmt.Printf("---\nTestCommitChainMarshal\n---\n")
cc := common.NewCommitChain()
// test MarshalBinary on a zeroed CommitChain
if p, err := cc.MarshalBinary(); err != nil {
t.Error(err)
} else if z := make([]byte, common.CommitChainSize); string(p) != string(z) {
t.Errorf("Marshal failed on zeroed CommitChain")
}
// build a CommitChain for testing
cc.Version = 0
cc.MilliTime = &[6]byte{1, 1, 1, 1, 1, 1}
p, _ := hex.DecodeString("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
cc.ChainIDHash.SetBytes(p)
p, _ = hex.DecodeString("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
cc.Weld.SetBytes(p)
p, _ = hex.DecodeString("cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc")
cc.EntryHash.SetBytes(p)
cc.Credits = 11
// make a key and sign the msg
if pub, privkey, err := ed.GenerateKey(rand.Reader); err != nil {
t.Error(err)
} else {
cc.ECPubKey = pub
cc.Sig = ed.Sign(privkey, cc.CommitMsg())
}
// marshal and unmarshal the commit and see if it matches
cc2 := common.NewCommitChain()
if p, err := cc.MarshalBinary(); err != nil {
t.Error(err)
} else {
t.Logf("%x\n", p)
cc2.UnmarshalBinary(p)
}
if !cc2.IsValid() {
t.Errorf("signature did not match after unmarshalbinary")
}
}