-
Notifications
You must be signed in to change notification settings - Fork 0
/
blockchain_test.go
70 lines (60 loc) · 1.7 KB
/
blockchain_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
package main
import (
"bytes"
"encoding/hex"
"testing"
)
// This test just verifies that life is ok.
func TestTruth(t *testing.T) {
if true != true {
t.Error("everything I know is wrong")
}
}
func TestBlock(t *testing.T) {
InitBlockchain()
length := len(Blockchain)
if length != 1 {
t.Error("New Blockchain should have length 1, but has length: ", length)
}
}
func TestNewBlock(t *testing.T) {
testOldBlock := createTestBlock()
testStr := "testing new block!"
b := NewBlock(testOldBlock, testStr)
if b.Data != "testing new block!" {
t.Error("New Block should have data: ", testStr, "but has data: ", b.Data)
}
if bytes.Compare(b.PrevHash, testOldBlock.calculateHash()) != 0 {
t.Errorf("New Block should have PrevHash: %x, but has PrevHash: %x",
testOldBlock.calculateHash(), b.PrevHash)
}
if bytes.Compare(b.Hash, b.calculateHash()) != 0 {
t.Errorf("New Block should have Hash: %x, but has Hash: %x",
b.calculateHash(), b.Hash)
}
}
func TestAddInvalidBlock(t *testing.T) {
InitBlockchain()
testOldBlock := createTestBlock()
testStr := "testing new block!"
b := NewBlock(testOldBlock, testStr)
err := AddBlock(b)
if err == nil {
t.Error("Adding invalid block should have thrown an error.")
}
}
func TestAddValidBlock(t *testing.T) {
InitBlockchain()
lastBlock := Blockchain[len(Blockchain)-1]
testStr := "testing new block!"
b := NewBlock(lastBlock, testStr)
err := AddBlock(b)
if err != nil {
t.Error("Adding block should have not returned error, but returned", err)
}
}
func createTestBlock() Block {
const s = "590c9f8430c7435807df8ba9a476e3f1295d46ef210f6efae2043a4c085a569e"
decodedTestHash, _ := hex.DecodeString(s)
return Block{"test", 0, []byte{}, decodedTestHash}
}