github.com/ontio/ontology@v1.14.4/vm/crossvm_codec/codec_test.go (about)

     1  /*
     2   * Copyright (C) 2018 The ontology Authors
     3   * This file is part of The ontology library.
     4   *
     5   * The ontology is free software: you can redistribute it and/or modify
     6   * it under the terms of the GNU Lesser General Public License as published by
     7   * the Free Software Foundation, either version 3 of the License, or
     8   * (at your option) any later version.
     9   *
    10   * The ontology is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU Lesser General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU Lesser General Public License
    16   * along with The ontology.  If not, see <http://www.gnu.org/licenses/>.
    17   */
    18  package crossvm_codec
    19  
    20  import (
    21  	"encoding/hex"
    22  	"math/big"
    23  	"testing"
    24  
    25  	"github.com/ontio/ontology/common"
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  func TestDe1(t *testing.T) {
    30  	h, _ := hex.DecodeString("657674001001000000010500000068656c6c6f")
    31  
    32  	_, err := parseNotify(h)
    33  	assert.Nil(t, err)
    34  }
    35  
    36  func EncodeNotify(t *testing.T, value interface{}) []byte {
    37  	val, err := EncodeValue(value)
    38  	assert.Nil(t, err)
    39  
    40  	return append([]byte("evt\x00"), val...)
    41  }
    42  
    43  func TestDeserializeNotify(t *testing.T) {
    44  	addr := common.AddressFromVmCode([]byte("123"))
    45  	value := []interface{}{"helloworld", []byte("1234"), 123, -1, -128, -260, true, big.NewInt(100), addr, common.UINT256_EMPTY}
    46  	expected := []interface{}{"helloworld", hex.EncodeToString([]byte("1234")), "123", "-1", "-128", "-260", true, "100", addr.ToBase58(), common.UINT256_EMPTY.ToHexString()}
    47  	for i, val := range value {
    48  		assert.Equal(t, DeserializeNotify(EncodeNotify(t, val)), interface{}(expected[i]))
    49  	}
    50  
    51  	assert.Equal(t, DeserializeNotify(EncodeNotify(t, value)), interface{}(expected))
    52  }