github.com/ethereumproject/go-ethereum@v5.5.2+incompatible/common/registrar/registrar_test.go (about)

     1  // Copyright 2015 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum 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 go-ethereum 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 go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package registrar
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/ethereumproject/go-ethereum/common"
    23  	"github.com/ethereumproject/go-ethereum/crypto"
    24  )
    25  
    26  type testBackend struct {
    27  	// contracts mock
    28  	contracts map[string](map[string]string)
    29  }
    30  
    31  var (
    32  	text     = "test"
    33  	codehash = common.Hash{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '1', '2', '3', '4'}
    34  	hash     = crypto.Sha3Hash([]byte(text))
    35  	url      = "bzz://bzzhash/my/path/contr.act"
    36  )
    37  
    38  func NewTestBackend() *testBackend {
    39  	self := &testBackend{}
    40  	self.contracts = make(map[string](map[string]string))
    41  	return self
    42  }
    43  
    44  func (self *testBackend) initHashReg() {
    45  	self.contracts[HashRegAddr[2:]] = make(map[string]string)
    46  	key := storageAddress(storageMapping(storageIdx2Addr(1), codehash[:]))
    47  	self.contracts[HashRegAddr[2:]][key] = hash.Hex()
    48  }
    49  
    50  func (self *testBackend) initUrlHint() {
    51  	self.contracts[UrlHintAddr[2:]] = make(map[string]string)
    52  	mapaddr := storageMapping(storageIdx2Addr(1), hash[:])
    53  
    54  	key := storageAddress(storageFixedArray(mapaddr, storageIdx2Addr(0)))
    55  	self.contracts[UrlHintAddr[2:]][key] = common.ToHex([]byte(url))
    56  	key = storageAddress(storageFixedArray(mapaddr, storageIdx2Addr(1)))
    57  	self.contracts[UrlHintAddr[2:]][key] = "0x0"
    58  }
    59  
    60  func (self *testBackend) StorageAt(ca, sa string) (res string) {
    61  	c := self.contracts[ca]
    62  	if c == nil {
    63  		return "0x0"
    64  	}
    65  	res = c[sa]
    66  	return
    67  }
    68  
    69  func (self *testBackend) Transact(fromStr, toStr, nonceStr, valueStr, gasStr, gasPriceStr, codeStr string) (string, error) {
    70  	return "", nil
    71  }
    72  
    73  func (self *testBackend) Call(fromStr, toStr, valueStr, gasStr, gasPriceStr, codeStr string) (string, string, error) {
    74  	return "", "", nil
    75  }
    76  
    77  func TestSetGlobalRegistrar(t *testing.T) {
    78  	b := NewTestBackend()
    79  	res := New(b)
    80  	_, err := res.SetGlobalRegistrar("addresshex", common.BigToAddress(common.Big1))
    81  	if err != nil {
    82  		t.Errorf("unexpected error: %v'", err)
    83  	}
    84  }
    85  
    86  func TestHashToHash(t *testing.T) {
    87  	b := NewTestBackend()
    88  	res := New(b)
    89  
    90  	HashRegAddr = "0x0"
    91  	got, err := res.HashToHash(codehash)
    92  	if err == nil {
    93  		t.Errorf("expected error")
    94  	} else {
    95  		exp := "HashReg address is not set"
    96  		if err.Error() != exp {
    97  			t.Errorf("incorrect error, expected '%v', got '%v'", exp, err.Error())
    98  		}
    99  	}
   100  
   101  	HashRegAddr = common.BigToAddress(common.Big1).Hex() //[2:]
   102  	got, err = res.HashToHash(codehash)
   103  	if err == nil {
   104  		t.Errorf("expected error")
   105  	} else {
   106  		exp := "HashToHash: content hash not found for '" + codehash.Hex() + "'"
   107  		if err.Error() != exp {
   108  			t.Errorf("incorrect error, expected '%v', got '%v'", exp, err.Error())
   109  		}
   110  	}
   111  
   112  	b.initHashReg()
   113  	got, err = res.HashToHash(codehash)
   114  	if err != nil {
   115  		t.Errorf("expected no error, got %v", err)
   116  	} else {
   117  		if got != hash {
   118  			t.Errorf("incorrect result, expected '%v', got '%v'", hash.Hex(), got.Hex())
   119  		}
   120  	}
   121  }
   122  
   123  func storageFixedArray(addr, idx []byte) []byte {
   124  	var carry byte
   125  	for i := 31; i >= 0; i-- {
   126  		var b byte = addr[i] + idx[i] + carry
   127  		if b < addr[i] {
   128  			carry = 1
   129  		} else {
   130  			carry = 0
   131  		}
   132  		addr[i] = b
   133  	}
   134  	return addr
   135  }