Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-block-format
Commits
3dc57fc9
Commit
3dc57fc9
authored
Aug 18, 2016
by
Jeromy Johnson
Committed by
GitHub
Aug 18, 2016
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #3086 from ipfs/feat/test-cover-blocks
test: 82% coverage on blocks
parents
1e01b02f
4588bc62
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
84 additions
and
2 deletions
+84
-2
blocks.go
blocks.go
+3
-1
blocks_test.go
blocks_test.go
+81
-1
No files found.
blocks.go
View file @
3dc57fc9
...
@@ -11,6 +11,8 @@ import (
...
@@ -11,6 +11,8 @@ import (
u
"gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
u
"gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
)
)
var
ErrWrongHash
=
errors
.
New
(
"data did not match given hash!"
)
type
Block
interface
{
type
Block
interface
{
Multihash
()
mh
.
Multihash
Multihash
()
mh
.
Multihash
Data
()
[]
byte
Data
()
[]
byte
...
@@ -37,7 +39,7 @@ func NewBlockWithHash(data []byte, h mh.Multihash) (*BasicBlock, error) {
...
@@ -37,7 +39,7 @@ func NewBlockWithHash(data []byte, h mh.Multihash) (*BasicBlock, error) {
if
u
.
Debug
{
if
u
.
Debug
{
chk
:=
u
.
Hash
(
data
)
chk
:=
u
.
Hash
(
data
)
if
string
(
chk
)
!=
string
(
h
)
{
if
string
(
chk
)
!=
string
(
h
)
{
return
nil
,
e
rr
ors
.
New
(
"Data did not match given hash!"
)
return
nil
,
E
rr
WrongHash
}
}
}
}
return
&
BasicBlock
{
data
:
data
,
multihash
:
h
},
nil
return
&
BasicBlock
{
data
:
data
,
multihash
:
h
},
nil
...
...
blocks_test.go
View file @
3dc57fc9
package
blocks
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
)
{
func
TestBlocksBasic
(
t
*
testing
.
T
)
{
...
@@ -14,3 +20,77 @@ func TestBlocksBasic(t *testing.T) {
...
@@ -14,3 +20,77 @@ func TestBlocksBasic(t *testing.T) {
// Test some data
// Test some data
NewBlock
([]
byte
(
"Hello world!"
))
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
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment