Skip to content

Commit

Permalink
increase test coverage common/address (fractalplatform#508)
Browse files Browse the repository at this point in the history
* add MarshalText unit test and note
  • Loading branch information
liuji85 authored and erickyan86 committed Oct 9, 2019
1 parent 1164629 commit 356fb46
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 11 deletions.
6 changes: 4 additions & 2 deletions common/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ func (a *Address) SetBytes(b []byte) {
copy(a[AddressLength-len(b):], b)
}

// MarshalText returns the hex representation of a.
// MarshalText returns the hex representation of a. Implements encoding.TextMarshaler
// is supported by most codec implementations (e.g. for yaml or toml).
func (a Address) MarshalText() ([]byte, error) {
return hexutil.Bytes(a[:]).MarshalText()
}
Expand All @@ -140,7 +141,8 @@ func (a *UnprefixedAddress) UnmarshalText(input []byte) error {
return hexutil.UnmarshalFixedUnprefixedText("UnprefixedAddress", input, a[:])
}

// MarshalText encodes the address as hex.
// MarshalText encodes the address as hex.Implements encoding.TextMarshaler
// is supported by most codec implementations (e.g. for yaml or toml).
func (a UnprefixedAddress) MarshalText() ([]byte, error) {
return []byte(hex.EncodeToString(a[:])), nil
}
Expand Down
87 changes: 86 additions & 1 deletion common/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
package common

import (
"bytes"
"encoding/json"

"math"
"math/big"
"testing"

"gopkg.in/yaml.v2"
)

func TestIsHexAddress(t *testing.T) {
Expand Down Expand Up @@ -102,6 +105,88 @@ func TestAddressHexChecksum(t *testing.T) {
}
}

func TestAddressConvert(t *testing.T) {
var tests = []struct {
BigInt *big.Int
HexString string
Bytes []byte
}{
{big.NewInt(math.MaxInt64),
"0x0000000000000000000000007FFfFFFffFfFfFff",
[]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 127, 255, 255, 255, 255, 255, 255, 255}},
{big.NewInt(10),
"0x000000000000000000000000000000000000000A",
[]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10}},
{big.NewInt(15),
"0x000000000000000000000000000000000000000F",
[]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15}},
}
for i, test := range tests {
addr := BigToAddress(test.BigInt)
if addr.Hex() != test.HexString {
t.Errorf("test #%d: failed to match when it should (%s != %s)", i, addr.Hex(), test.HexString)
}
if !bytes.Equal(addr.Bytes(), test.Bytes) {
t.Errorf("test #%d: failed to match when it should (%x != %x)", i, addr.Bytes(), test.Bytes)
}
}
}

func TestAddressMarshal(t *testing.T) {
testAddr := HexToAddress("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed")
if marshaltext, err := yaml.Marshal(testAddr); err != nil {
t.Errorf("MarshalText err: %v", err)
} else {
target := []byte{48, 120, 53, 97, 97, 101, 98, 54, 48, 53, 51, 102, 51, 101, 57, 52, 99, 57, 98, 57, 97, 48, 57, 102, 51, 51, 54, 54, 57, 52, 51, 53, 101, 55, 101, 102, 49, 98, 101, 97, 101, 100, 10}
if !bytes.Equal(marshaltext, target) {
t.Errorf("MarshalText mismatch when it should (%x != %x)", marshaltext, target)
}

newAddress := Address{}
if err := yaml.Unmarshal(marshaltext, &newAddress); err != nil {
t.Errorf("UnmarshalText err: %v", err)
}
if 0 != newAddress.Compare(testAddr) {
t.Errorf("UnmarshalText address mismatch")
}
}
}

func TestUnprefixedAddressMarshal(t *testing.T) {
marshaltext := []byte{53, 97, 97, 101, 98, 54, 48, 53, 51, 102, 51, 101, 57, 52, 99, 57, 98, 57, 97, 48, 57, 102, 51, 51, 54, 54, 57, 52, 51, 53, 101, 55, 101, 102, 49, 98, 101, 97, 101, 100, 10}
unprefixedAddr := UnprefixedAddress{}
if err := yaml.Unmarshal(marshaltext, &unprefixedAddr); err != nil {
t.Errorf("UnmarshalText err: %v", err)
}

if fetchedMarshaltext, err := yaml.Marshal(unprefixedAddr); err != nil {
t.Errorf("MarshalText err: %v", err)
} else {
if !bytes.Equal(marshaltext, fetchedMarshaltext) {
t.Errorf("MarshalText mismatch when it should (%s != %s)", marshaltext, fetchedMarshaltext)
}
}
}

func TestMixedcaseAddress(t *testing.T) {
hexString := "0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed"
originalAddr := HexToAddress(hexString)
testAddr := NewMixedcaseAddress(originalAddr)
mixedcaseRes := "0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed [chksum ok]"
if mixedcaseRes != testAddr.String() {
t.Errorf("MixedcaseAddress String mismatched")
}

mixedcaseRes = "0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed"
if mixedcaseRes != testAddr.Original() {
t.Errorf("MixedcaseAddress Original mismatched")
}

if 0 != testAddr.Address().Compare(originalAddr) {
t.Errorf("MixedcaseAddress address mismatch")
}
}

func BenchmarkAddressHex(b *testing.B) {
testAddr := HexToAddress("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed")
for n := 0; n < b.N; n++ {
Expand Down
6 changes: 4 additions & 2 deletions common/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ func (h *Hash) UnmarshalJSON(input []byte) error {
return hexutil.UnmarshalFixedJSON(hashT, input, h[:])
}

// MarshalText returns the hex representation of h.
// MarshalText returns the hex representation of h.Implements encoding.TextMarshaler
// is supported by most codec implementations (e.g. for yaml or toml).
func (h Hash) MarshalText() ([]byte, error) {
return hexutil.Bytes(h[:]).MarshalText()
}
Expand All @@ -109,7 +110,8 @@ func (h *UnprefixedHash) UnmarshalText(input []byte) error {
return hexutil.UnmarshalFixedUnprefixedText("UnprefixedHash", input, h[:])
}

// MarshalText encodes the hash as hex.
// MarshalText encodes the hash as hex. Implements encoding.TextMarshaler
// is supported by most codec implementations (e.g. for yaml or toml).
func (h UnprefixedHash) MarshalText() ([]byte, error) {
return []byte(hex.EncodeToString(h[:])), nil
}
3 changes: 2 additions & 1 deletion common/pubkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ func (p PubKey) String() string {
return p.Hex()
}

// MarshalText returns the hex representation of a.
// MarshalText returns the hex representation of p. Implements encoding.TextMarshaler
// is supported by most codec implementations (e.g. for yaml or toml).
func (p PubKey) MarshalText() ([]byte, error) {
return hexutil.Bytes(p[:]).MarshalText()
}
Expand Down
5 changes: 3 additions & 2 deletions consensus/dpos/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ type IDB interface {
GetCandidateInfoByTime(epoch uint64, name string, timestamp uint64) (*CandidateInfo, error)
}

// CandidateType candiate status
// CandidateType candidate status
type CandidateType uint64

const (
Expand All @@ -75,7 +75,8 @@ const (
Unkown
)

// MarshalText returns the hex representation of a.
// MarshalText returns the hex representation of a. Implements encoding.TextMarshaler
// is supported by most codec implementations (e.g. for yaml or toml).
func (t CandidateType) MarshalText() ([]byte, error) {
return t.MarshalJSON()
}
Expand Down
8 changes: 5 additions & 3 deletions consensus/dpos/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@ import (
"fmt"
"math/big"
"testing"

"gopkg.in/yaml.v2"
)

func TestCandidateType(t *testing.T) {
func TestCandidateMarshalText(t *testing.T) {
typeList := []CandidateType{Normal, Freeze, Black, Jail, Unkown}
for _, t := range typeList {
if text, err := t.MarshalText(); err != nil {
if text, err := yaml.Marshal(t); err != nil {
panic(fmt.Errorf("MarshalText --- %v", err))
} else {
var newCt CandidateType
if err := newCt.UnmarshalText(text); err != nil {
if err := yaml.Unmarshal(text, &newCt); err != nil {
panic(fmt.Errorf("UnmarshalText --- %v", err))
} else {
if newCt != t {
Expand Down

0 comments on commit 356fb46

Please sign in to comment.