github.com/klaytn/klaytn@v1.10.2/datasync/chaindatafetcher/kas/repository_token_transfers_test.go (about)

     1  // Copyright 2020 The klaytn Authors
     2  // This file is part of the klaytn library.
     3  //
     4  // The klaytn library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The klaytn library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the klaytn library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package kas
    18  
    19  import (
    20  	"strings"
    21  	"testing"
    22  
    23  	"github.com/klaytn/klaytn/common"
    24  	"github.com/klaytn/klaytn/common/hexutil"
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  func TestSplitToWords_Success(t *testing.T) {
    29  	data := "0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000850f0263a87af6dd51acb8baab96219041e28fda00000000000000000000000000000000000000000000d3c21bcecceda1000000"
    30  	bytes, err := hexutil.Decode(data)
    31  	assert.NoError(t, err)
    32  
    33  	hashes, err := splitToWords(bytes)
    34  	assert.NoError(t, err)
    35  	assert.Equal(t, common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), hashes[0])
    36  	assert.Equal(t, common.HexToHash("0x000000000000000000000000850f0263a87af6dd51acb8baab96219041e28fda"), hashes[1])
    37  	assert.Equal(t, common.HexToHash("0x00000000000000000000000000000000000000000000d3c21bcecceda1000000"), hashes[2])
    38  }
    39  
    40  func TestSplitToWords_Fail_DataLengthError(t *testing.T) {
    41  	data := "0x0000000000000000000000000000000000850f0263a87af6dd51acb8baab96219041e28fda00000000000000000000000000000000000000000000d3c21bcecceda1000000"
    42  	bytes, err := hexutil.Decode(data)
    43  	assert.NoError(t, err)
    44  
    45  	_, err = splitToWords(bytes)
    46  	assert.Error(t, err)
    47  	assert.True(t, strings.Contains(err.Error(), "data length is not valid"))
    48  }
    49  
    50  func TestWordsToAddress_Success(t *testing.T) {
    51  	hash1 := common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000")
    52  	hash2 := common.HexToHash("0x000000000000000000000000850f0263a87af6dd51acb8baab96219041e28fda")
    53  	hash3 := common.HexToHash("0x00000000000000000000000000000000000000000000d3c21bcecceda1000000")
    54  
    55  	expected1 := common.HexToAddress("0x0000000000000000000000000000000000000000")
    56  	expected2 := common.HexToAddress("0x850f0263a87af6dd51acb8baab96219041e28fda")
    57  	expected3 := common.HexToAddress("0x00000000000000000000d3c21bcecceda1000000")
    58  
    59  	addr1 := wordToAddress(hash1)
    60  	addr2 := wordToAddress(hash2)
    61  	addr3 := wordToAddress(hash3)
    62  
    63  	assert.Equal(t, expected1, addr1)
    64  	assert.Equal(t, expected2, addr2)
    65  	assert.Equal(t, expected3, addr3)
    66  }