github.com/klaytn/klaytn@v1.10.2/datasync/chaindatafetcher/kas/contract_caller_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 "testing" 21 22 "github.com/golang/mock/gomock" 23 "github.com/klaytn/klaytn/api" 24 "github.com/klaytn/klaytn/common" 25 "github.com/klaytn/klaytn/common/hexutil" 26 "github.com/klaytn/klaytn/datasync/chaindatafetcher/kas/mocks" 27 "github.com/klaytn/klaytn/networks/rpc" 28 "github.com/stretchr/testify/suite" 29 ) 30 31 var ( 32 decodedTrue = hexutil.MustDecode("0x0000000000000000000000000000000000000000000000000000000000000001") 33 decodedFalse = hexutil.MustDecode("0x0000000000000000000000000000000000000000000000000000000000000000") 34 ikip13Input = hexutil.MustDecode("0x01ffc9a701ffc9a700000000000000000000000000000000000000000000000000000000") 35 ikip13InvalidInput = hexutil.MustDecode("0x01ffc9a7ffffffff00000000000000000000000000000000000000000000000000000000") 36 ikip7Input = hexutil.MustDecode("0x01ffc9a76578737100000000000000000000000000000000000000000000000000000000") 37 ikip7MetadataInput = hexutil.MustDecode("0x01ffc9a7a219a02500000000000000000000000000000000000000000000000000000000") 38 ikip17Input = hexutil.MustDecode("0x01ffc9a780ac58cd00000000000000000000000000000000000000000000000000000000") 39 ikip17MetadataInput = hexutil.MustDecode("0x01ffc9a75b5e139f00000000000000000000000000000000000000000000000000000000") 40 ) 41 42 type SuiteContractCaller struct { 43 suite.Suite 44 ctrl *gomock.Controller 45 api *mocks.MockBlockchainAPI 46 caller *contractCaller 47 } 48 49 func (s *SuiteContractCaller) SetupTest() { 50 s.ctrl = gomock.NewController(s.T()) 51 s.api = mocks.NewMockBlockchainAPI(s.ctrl) 52 s.caller = newContractCaller(s.api) 53 } 54 55 func (s *SuiteContractCaller) TearDownTest() { 56 s.ctrl.Finish() 57 } 58 59 func setExpectation(m *mocks.MockBlockchainAPI, contract *common.Address, data, result []byte) { 60 arg := api.CallArgs{ 61 From: common.Address{}, 62 To: contract, 63 Data: data, 64 } 65 66 m.EXPECT().Call(gomock.Any(), gomock.Eq(arg), gomock.Eq(rpc.NewBlockNumberOrHashWithNumber(rpc.LatestBlockNumber))).Return(result, nil).Times(1) 67 } 68 69 func (s *SuiteContractCaller) TestContractCaller_IsKIP13_Success() { 70 addr := common.HexToAddress("1") 71 s.True(addr != (common.Address{})) // make sure that the address is not empty address 72 73 // KIP13 validation 74 setExpectation(s.api, &addr, ikip13Input, decodedTrue) 75 setExpectation(s.api, &addr, ikip13InvalidInput, decodedFalse) 76 77 isKIP13, err := s.caller.isKIP13(addr, nil) 78 s.NoError(err) 79 s.True(isKIP13) 80 } 81 82 func (s *SuiteContractCaller) TestContractCaller_IsKIP7_Success() { 83 addr := common.HexToAddress("2") 84 s.True(addr != (common.Address{})) // make sure that the address is not empty address 85 86 // IKIP7 and IKIP7Metadata validation 87 setExpectation(s.api, &addr, ikip7Input, decodedTrue) 88 setExpectation(s.api, &addr, ikip7MetadataInput, decodedTrue) 89 90 isKIP7, err := s.caller.isKIP7(addr, nil) 91 s.NoError(err) 92 s.True(isKIP7) 93 } 94 95 func (s *SuiteContractCaller) TestContractCaller_IsKIP17_Success() { 96 addr := common.HexToAddress("3") 97 s.True(addr != (common.Address{})) // make sure that the address is not empty address 98 99 // IKIP17 and IKIP17Metadata validation 100 setExpectation(s.api, &addr, ikip17Input, decodedTrue) 101 setExpectation(s.api, &addr, ikip17MetadataInput, decodedTrue) 102 103 isKIP17, err := s.caller.isKIP17(addr, nil) 104 s.NoError(err) 105 s.True(isKIP17) 106 } 107 108 func TestSuiteContractCaller(t *testing.T) { 109 suite.Run(t, new(SuiteContractCaller)) 110 }