mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-25 20:37:53 +08:00
test: 82% coverage on blocks
License: MIT Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
This commit is contained in:
parent
685cd28dc8
commit
fcdf52735e
@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user