@cryptotaxi247 / kubo / commits / fcdf52735

test: 82% coverage on blocks

License: MIT Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>

Jakub Sztandera committed Aug 15, 2016 at 12:46 UTC fcdf52735ec0c6d205c51409adbc75b2ef7f82e7
1 file changed +81 -1
blocks/blocks_test.go
+81 -1
@@ -1,6 +1,12 @@
1 package blocks
2
3 -import "testing"
3 +import (
4 + "bytes"
5 + "testing"
6 +
7 + mh "gx/ipfs/QmYf7ng2hG5XBtJA3tN34DQ2GUN5HNksEw1rLDkmr6vGku/go-multihash"
8 + u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
9 +)
10
11 func TestBlocksBasic(t *testing.T) {
12
@@ -14,3 +20,77 @@ func TestBlocksBasic(t *testing.T) {
20 // Test some data
21 NewBlock([]byte("Hello world!"))
22 }
23 +
24 +func TestData(t *testing.T) {
25 + data := []byte("some data")
26 + block := NewBlock(data)
27 +
28 + if !bytes.Equal(block.Data(), data) {
29 + t.Error("data is wrong")
30 + }
31 +}
32 +
33 +func TestHash(t *testing.T) {
34 + data := []byte("some other data")
35 + block := NewBlock(data)
36 +
37 + hash, err := mh.Sum(data, mh.SHA2_256, -1)
38 + if err != nil {
39 + t.Fatal(err)
40 + }
41 +
42 + if !bytes.Equal(block.Multihash(), hash) {
43 + t.Error("wrong multihash")
44 + }
45 +}
46 +
47 +func TestKey(t *testing.T) {
48 + data := []byte("yet another data")
49 + block := NewBlock(data)
50 + key := block.Key()
51 +
52 + if !bytes.Equal(block.Multihash(), key.ToMultihash()) {
53 + t.Error("key contains wrong data")
54 + }
55 +}
56 +
57 +func TestManualHash(t *testing.T) {
58 + oldDebugState := u.Debug
59 + defer (func() {
60 + u.Debug = oldDebugState
61 + })()
62 +
63 + data := []byte("I can't figure out more names .. data")
64 + hash, err := mh.Sum(data, mh.SHA2_256, -1)
65 + if err != nil {
66 + t.Fatal(err)
67 + }
68 +
69 + u.Debug = false
70 + block, err := NewBlockWithHash(data, hash)
71 + if err != nil {
72 + t.Fatal(err)
73 + }
74 +
75 + if !bytes.Equal(block.Multihash(), hash) {
76 + t.Error("wrong multihash")
77 + }
78 +
79 + data[5] = byte((uint32(data[5]) + 5) % 256) // Transfrom hash to be different
80 + block, err = NewBlockWithHash(data, hash)
81 + if err != nil {
82 + t.Fatal(err)
83 + }
84 +
85 + if !bytes.Equal(block.Multihash(), hash) {
86 + t.Error("wrong multihash")
87 + }
88 +
89 + u.Debug = true
90 +
91 + block, err = NewBlockWithHash(data, hash)
92 + if err == nil {
93 + t.Fatal(err)
94 + }
95 +
96 +}