From fcdf52735ec0c6d205c51409adbc75b2ef7f82e7 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Mon, 15 Aug 2016 12:46:49 +0200 Subject: [PATCH 1/4] test: 82% coverage on blocks License: MIT Signed-off-by: Jakub Sztandera --- blocks/blocks_test.go | 82 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 81 insertions(+), 1 deletion(-) diff --git a/blocks/blocks_test.go b/blocks/blocks_test.go index 53a85227575..1d7aa2e7842 100644 --- a/blocks/blocks_test.go +++ b/blocks/blocks_test.go @@ -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) { @@ -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 == nil { + t.Fatal(err) + } + +} From 68d5197265ad6587b0a512e06d955fe8f170c8d7 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Mon, 15 Aug 2016 17:23:44 +0200 Subject: [PATCH 2/4] test: do explicit error check License: MIT Signed-off-by: Jakub Sztandera --- blocks/blocks.go | 4 +++- blocks/blocks_test.go | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/blocks/blocks.go b/blocks/blocks.go index d5f4df700a1..ea4bf70cd13 100644 --- a/blocks/blocks.go +++ b/blocks/blocks.go @@ -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 @@ -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 diff --git a/blocks/blocks_test.go b/blocks/blocks_test.go index 1d7aa2e7842..6c218beec26 100644 --- a/blocks/blocks_test.go +++ b/blocks/blocks_test.go @@ -89,7 +89,7 @@ func TestManualHash(t *testing.T) { u.Debug = true block, err = NewBlockWithHash(data, hash) - if err == nil { + if err != errWrongHash { t.Fatal(err) } From 2408f866c5f91c578b8a2ef16fd429a3deaa6544 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Tue, 16 Aug 2016 12:42:18 +0200 Subject: [PATCH 3/4] blocks: rename errWrongHash to ErrWrongHash License: MIT Signed-off-by: Jakub Sztandera --- blocks/blocks.go | 4 ++-- blocks/blocks_test.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/blocks/blocks.go b/blocks/blocks.go index ea4bf70cd13..202e471cefc 100644 --- a/blocks/blocks.go +++ b/blocks/blocks.go @@ -11,7 +11,7 @@ import ( u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util" ) -var errWrongHash = errors.New("Data did not match given hash!") +var ErrWrongHash = errors.New("Data did not match given hash!") type Block interface { Multihash() mh.Multihash @@ -39,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, errWrongHash + return nil, ErrWrongHash } } return &BasicBlock{data: data, multihash: h}, nil diff --git a/blocks/blocks_test.go b/blocks/blocks_test.go index 6c218beec26..1fecff84457 100644 --- a/blocks/blocks_test.go +++ b/blocks/blocks_test.go @@ -89,7 +89,7 @@ func TestManualHash(t *testing.T) { u.Debug = true block, err = NewBlockWithHash(data, hash) - if err != errWrongHash { + if err != ErrWrongHash { t.Fatal(err) } From 594c946606890a2f3dede58713f454404cb13ccc Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Tue, 16 Aug 2016 17:36:18 +0200 Subject: [PATCH 4/4] docs: decapitalize error message in blocks.go License: MIT Signed-off-by: Jakub Sztandera --- blocks/blocks.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blocks/blocks.go b/blocks/blocks.go index 202e471cefc..c41e1323a11 100644 --- a/blocks/blocks.go +++ b/blocks/blocks.go @@ -11,7 +11,7 @@ import ( u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util" ) -var ErrWrongHash = errors.New("Data did not match given hash!") +var ErrWrongHash = errors.New("data did not match given hash!") type Block interface { Multihash() mh.Multihash