From 356fb46e731dfd988f760a70297e41ee38e59649 Mon Sep 17 00:00:00 2001 From: liuji85 <42334624+liuji85@users.noreply.github.com> Date: Wed, 9 Oct 2019 15:05:42 +0800 Subject: [PATCH] increase test coverage common/address (#508) * add MarshalText unit test and note --- common/address.go | 6 ++- common/address_test.go | 87 ++++++++++++++++++++++++++++++++- common/hash.go | 6 ++- common/pubkey.go | 3 +- consensus/dpos/database.go | 5 +- consensus/dpos/database_test.go | 8 +-- 6 files changed, 104 insertions(+), 11 deletions(-) diff --git a/common/address.go b/common/address.go index 834623dc..9ec53a01 100644 --- a/common/address.go +++ b/common/address.go @@ -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() } @@ -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 } diff --git a/common/address_test.go b/common/address_test.go index e3ba715c..40e9b4b9 100644 --- a/common/address_test.go +++ b/common/address_test.go @@ -17,10 +17,13 @@ package common import ( + "bytes" "encoding/json" - + "math" "math/big" "testing" + + "gopkg.in/yaml.v2" ) func TestIsHexAddress(t *testing.T) { @@ -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++ { diff --git a/common/hash.go b/common/hash.go index 1cf978ee..68fd2a0f 100644 --- a/common/hash.go +++ b/common/hash.go @@ -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() } @@ -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 } diff --git a/common/pubkey.go b/common/pubkey.go index f7eded3c..0df72476 100644 --- a/common/pubkey.go +++ b/common/pubkey.go @@ -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() } diff --git a/consensus/dpos/database.go b/consensus/dpos/database.go index 528cc30f..1be172ba 100644 --- a/consensus/dpos/database.go +++ b/consensus/dpos/database.go @@ -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 ( @@ -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() } diff --git a/consensus/dpos/database_test.go b/consensus/dpos/database_test.go index 4703ec78..11f7b6d4 100644 --- a/consensus/dpos/database_test.go +++ b/consensus/dpos/database_test.go @@ -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 {