1
0
mirror of https://github.com/fluencelabs/tendermint synced 2025-06-26 19:21:44 +00:00
Files
.circleci
.github
DOCKER
abci
benchmarks
blockchain
cmd
config
consensus
crypto
docs
evidence
libs
autofile
bech32
cli
clist
common
LICENSE
async.go
async_test.go
bit_array.go
bit_array_test.go
bytes.go
bytes_test.go
byteslice.go
byteslice_test.go
cmap.go
cmap_test.go
colors.go
date.go
date_test.go
errors.go
errors_test.go
heap.go
int.go
int_test.go
io.go
kvpair.go
math.go
net.go
net_test.go
nil.go
os.go
os_test.go
random.go
random_test.go
repeat_timer.go
repeat_timer_test.go
service.go
service_test.go
string.go
string_test.go
tempfile.go
tempfile_test.go
throttle_timer.go
throttle_timer_test.go
types.pb.go
types.proto
typespb_test.go
word.go
db
events
flowrate
log
pubsub
test
version
.editorconfig
.gitignore
CHANGELOG.md
README.md
circle.yml
test.sh
lite
mempool
networks
node
p2p
privval
proxy
rpc
scripts
state
test
tools
types
version
.editorconfig
.gitignore
CHANGELOG.md
CHANGELOG_PENDING.md
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Gopkg.lock
Gopkg.toml
LICENSE
Makefile
README.md
ROADMAP.md
SECURITY.md
UPGRADING.md
Vagrantfile
appveyor.yml
codecov.yml
docker-compose.yml
tendermint/libs/common/bytes_test.go
2018-07-01 22:36:03 -04:00

66 lines
1.3 KiB
Go

package common
import (
"encoding/json"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
// This is a trivial test for protobuf compatibility.
func TestMarshal(t *testing.T) {
bz := []byte("hello world")
dataB := HexBytes(bz)
bz2, err := dataB.Marshal()
assert.Nil(t, err)
assert.Equal(t, bz, bz2)
var dataB2 HexBytes
err = (&dataB2).Unmarshal(bz)
assert.Nil(t, err)
assert.Equal(t, dataB, dataB2)
}
// Test that the hex encoding works.
func TestJSONMarshal(t *testing.T) {
type TestStruct struct {
B1 []byte
B2 HexBytes
}
cases := []struct {
input []byte
expected string
}{
{[]byte(``), `{"B1":"","B2":""}`},
{[]byte(`a`), `{"B1":"YQ==","B2":"61"}`},
{[]byte(`abc`), `{"B1":"YWJj","B2":"616263"}`},
}
for i, tc := range cases {
t.Run(fmt.Sprintf("Case %d", i), func(t *testing.T) {
ts := TestStruct{B1: tc.input, B2: tc.input}
// Test that it marshals correctly to JSON.
jsonBytes, err := json.Marshal(ts)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, string(jsonBytes), tc.expected)
// TODO do fuzz testing to ensure that unmarshal fails
// Test that unmarshaling works correctly.
ts2 := TestStruct{}
err = json.Unmarshal(jsonBytes, &ts2)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, ts2.B1, tc.input)
assert.Equal(t, ts2.B2, HexBytes(tc.input))
})
}
}