/
heartbeat_test.go
172 lines (143 loc) · 3.74 KB
/
heartbeat_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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright 2017 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package messages_test
import (
"testing"
"github.com/FactomProject/factomd/common/constants"
"github.com/FactomProject/factomd/common/identity"
. "github.com/FactomProject/factomd/common/messages"
. "github.com/FactomProject/factomd/common/messages/msgsupport"
"github.com/FactomProject/factomd/common/primitives"
"github.com/FactomProject/factomd/testHelper"
)
func TestUnmarshalNilHeartbeat(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Errorf("Panic caught during the test - %v", r)
}
}()
a := new(Heartbeat)
err := a.UnmarshalBinary(nil)
if err == nil {
t.Errorf("Error is nil when it shouldn't be")
}
err = a.UnmarshalBinary([]byte{})
if err == nil {
t.Errorf("Error is nil when it shouldn't be")
}
}
func TestMarshalUnmarshalHeartbeat(t *testing.T) {
msg := newHeartbeat()
hex, err := msg.MarshalBinary()
if err != nil {
t.Error(err)
}
t.Logf("Marshalled - %x", hex)
msg2, err := UnmarshalMessage(hex)
if err != nil {
t.Error(err)
}
str := msg2.String()
t.Logf("str - %v", str)
if msg2.Type() != constants.HEARTBEAT_MSG {
t.Error("Invalid message type unmarshalled")
}
hex2, err := msg2.(*Heartbeat).MarshalBinary()
if err != nil {
t.Error(err)
}
if len(hex) != len(hex2) {
t.Error("Hexes aren't of identical length")
}
for i := range hex {
if hex[i] != hex2[i] {
t.Error("Hexes do not match")
}
}
if msg.IsSameAs(msg2.(*Heartbeat)) != true {
t.Errorf("Heartbeat messages are not identical")
}
}
func TestSignAndVerifyHeartbeat(t *testing.T) {
msg := newSignedHeartbeat()
hex, err := msg.MarshalBinary()
if err != nil {
t.Error(err)
}
t.Logf("Marshalled - %x", hex)
t.Logf("Sig - %x", *msg.Signature.GetSignature())
if len(*msg.Signature.GetSignature()) == 0 {
t.Error("Signature not present")
}
valid, err := msg.VerifySignature()
if err != nil {
t.Error(err)
}
if valid == false {
t.Error("Signature is not valid")
}
msg2, err := UnmarshalMessage(hex)
if err != nil {
t.Error(err)
}
if msg2.Type() != constants.HEARTBEAT_MSG {
t.Error("Invalid message type unmarshalled")
}
valid, err = msg2.(*Heartbeat).VerifySignature()
if err != nil {
t.Error(err)
}
if valid == false {
t.Error("Signature 2 is not valid")
}
}
func newHeartbeat() *Heartbeat {
eom := new(Heartbeat)
eom.Timestamp = primitives.NewTimestampNow()
h, err := primitives.NewShaHashFromStr("deadbeef00000000000000000000000000000000000000000000000000000000")
if err != nil {
panic(err)
}
eom.DBlockHash = h
h, err = primitives.NewShaHashFromStr("deadbeef00000000000000000000000000000000000000000000000000000000")
if err != nil {
panic(err)
}
eom.IdentityChainID = h
return eom
}
func newSignedHeartbeat() *Heartbeat {
ack := newHeartbeat()
key, err := primitives.NewPrivateKeyFromHex("07c0d52cb74f4ca3106d80c4a70488426886bccc6ebc10c6bafb37bf8a65f4c38cee85c62a9e48039d4ac294da97943c2001be1539809ea5f54721f0c5477a0a")
if err != nil {
panic(err)
}
err = ack.Sign(key)
if err != nil {
panic(err)
}
return ack
}
func TestValidHeatbeat(t *testing.T) {
s := testHelper.CreateAndPopulateTestStateAndStartValidator()
a := identity.RandomAuthority()
pkey := primitives.RandomPrivateKey()
a.SigningKey = *pkey.Pub
s.IdentityControl.Authorities[a.AuthorityChainID.Fixed()] = a
h := newSignedHeartbeat()
// To pass validate, we need
// The timestamp to be near state timestamp
// The height to be high
h.IdentityChainID = a.AuthorityChainID
h.DBHeight = 100
h.Timestamp = s.GetTimestamp()
h.Signature = nil
err := h.Sign(pkey)
if err != nil {
t.Error(err)
}
if h.Validate(s) != 1 {
t.Errorf("Exp %d found %d", 1, h.Validate(s))
}
}