github.com/jeffallen/go-ethereum@v1.1.4-0.20150910155051-571d3236c49c/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/ethereum/go-ethereum/common"
    23  	"github.com/ethereum/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.StringToHash("1234")
    34  	hash     = common.BytesToHash(crypto.Sha3([]byte(text)))
    35  	url      = "bzz://bzzhash/my/path/contr.act"
    36  )
    37  
    38  func NewTestBackend() *testBackend {
    39  	HashRegAddr = common.BigToAddress(common.Big0).Hex() //[2:]
    40  	UrlHintAddr = common.BigToAddress(common.Big1).Hex() //[2:]
    41  	self := &testBackend{}
    42  	self.contracts = make(map[string](map[string]string))
    43  
    44  	self.contracts[HashRegAddr[2:]] = make(map[string]string)
    45  	key := storageAddress(storageMapping(storageIdx2Addr(1), codehash[:]))
    46  	self.contracts[HashRegAddr[2:]][key] = hash.Hex()
    47  
    48  	self.contracts[UrlHintAddr[2:]] = make(map[string]string)
    49  	mapaddr := storageMapping(storageIdx2Addr(1), hash[:])
    50  
    51  	key = storageAddress(storageFixedArray(mapaddr, storageIdx2Addr(0)))
    52  	self.contracts[UrlHintAddr[2:]][key] = common.ToHex([]byte(url))
    53  	key = storageAddress(storageFixedArray(mapaddr, storageIdx2Addr(1)))
    54  	self.contracts[UrlHintAddr[2:]][key] = "0x0"
    55  	return self
    56  }
    57  
    58  func (self *testBackend) StorageAt(ca, sa string) (res string) {
    59  	c := self.contracts[ca]
    60  	if c == nil {
    61  		return
    62  	}
    63  	res = c[sa]
    64  	return
    65  }
    66  
    67  func (self *testBackend) Transact(fromStr, toStr, nonceStr, valueStr, gasStr, gasPriceStr, codeStr string) (string, error) {
    68  	return "", nil
    69  }
    70  
    71  func (self *testBackend) Call(fromStr, toStr, valueStr, gasStr, gasPriceStr, codeStr string) (string, string, error) {
    72  	return "", "", nil
    73  }
    74  
    75  func TestSetGlobalRegistrar(t *testing.T) {
    76  	b := NewTestBackend()
    77  	res := New(b)
    78  	_, err := res.SetGlobalRegistrar("addresshex", common.BigToAddress(common.Big1))
    79  	if err != nil {
    80  		t.Errorf("unexpected error: %v'", err)
    81  	}
    82  }
    83  
    84  func TestHashToHash(t *testing.T) {
    85  	b := NewTestBackend()
    86  	res := New(b)
    87  	// res.SetHashReg()
    88  
    89  	got, err := res.HashToHash(codehash)
    90  	if err != nil {
    91  		t.Errorf("expected no error, got %v", err)
    92  	} else {
    93  		if got != hash {
    94  			t.Errorf("incorrect result, expected '%v', got '%v'", hash.Hex(), got.Hex())
    95  		}
    96  	}
    97  }
    98  
    99  func TestHashToUrl(t *testing.T) {
   100  	b := NewTestBackend()
   101  	res := New(b)
   102  	// res.SetUrlHint()
   103  
   104  	got, err := res.HashToUrl(hash)
   105  	if err != nil {
   106  		t.Errorf("expected 	 error, got %v", err)
   107  	} else {
   108  		if got != url {
   109  			t.Errorf("incorrect result, expected '%v', got '%s'", url, got)
   110  		}
   111  	}
   112  }