Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: 82% coverage on blocks #3086

Merged
merged 4 commits into from
Aug 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion blocks/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
)

var ErrWrongHash = errors.New("data did not match given hash!")

type Block interface {
Multihash() mh.Multihash
Data() []byte
Expand All @@ -37,7 +39,7 @@ func NewBlockWithHash(data []byte, h mh.Multihash) (*BasicBlock, error) {
if u.Debug {
chk := u.Hash(data)
if string(chk) != string(h) {
return nil, errors.New("Data did not match given hash!")
return nil, ErrWrongHash
}
}
return &BasicBlock{data: data, multihash: h}, nil
Expand Down
82 changes: 81 additions & 1 deletion blocks/blocks_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package blocks

import "testing"
import (
"bytes"
"testing"

mh "gx/ipfs/QmYf7ng2hG5XBtJA3tN34DQ2GUN5HNksEw1rLDkmr6vGku/go-multihash"
u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
)

func TestBlocksBasic(t *testing.T) {

Expand All @@ -14,3 +20,77 @@ func TestBlocksBasic(t *testing.T) {
// Test some data
NewBlock([]byte("Hello world!"))
}

func TestData(t *testing.T) {
data := []byte("some data")
block := NewBlock(data)

if !bytes.Equal(block.Data(), data) {
t.Error("data is wrong")
}
}

func TestHash(t *testing.T) {
data := []byte("some other data")
block := NewBlock(data)

hash, err := mh.Sum(data, mh.SHA2_256, -1)
if err != nil {
t.Fatal(err)
}

if !bytes.Equal(block.Multihash(), hash) {
t.Error("wrong multihash")
}
}

func TestKey(t *testing.T) {
data := []byte("yet another data")
block := NewBlock(data)
key := block.Key()

if !bytes.Equal(block.Multihash(), key.ToMultihash()) {
t.Error("key contains wrong data")
}
}

func TestManualHash(t *testing.T) {
oldDebugState := u.Debug
defer (func() {
u.Debug = oldDebugState
})()

data := []byte("I can't figure out more names .. data")
hash, err := mh.Sum(data, mh.SHA2_256, -1)
if err != nil {
t.Fatal(err)
}

u.Debug = false
block, err := NewBlockWithHash(data, hash)
if err != nil {
t.Fatal(err)
}

if !bytes.Equal(block.Multihash(), hash) {
t.Error("wrong multihash")
}

data[5] = byte((uint32(data[5]) + 5) % 256) // Transfrom hash to be different
block, err = NewBlockWithHash(data, hash)
if err != nil {
t.Fatal(err)
}

if !bytes.Equal(block.Multihash(), hash) {
t.Error("wrong multihash")
}

u.Debug = true

block, err = NewBlockWithHash(data, hash)
if err != ErrWrongHash {
t.Fatal(err)
}

}