mirror of https://github.com/ethereum/go-ethereum
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
97 lines
2.4 KiB
97 lines
2.4 KiB
10 years ago
|
package registrar
|
||
10 years ago
|
|
||
|
import (
|
||
|
"testing"
|
||
10 years ago
|
|
||
|
"github.com/ethereum/go-ethereum/common"
|
||
|
"github.com/ethereum/go-ethereum/crypto"
|
||
10 years ago
|
)
|
||
|
|
||
10 years ago
|
type testBackend struct {
|
||
|
// contracts mock
|
||
|
contracts map[string](map[string]string)
|
||
|
}
|
||
|
|
||
|
var (
|
||
|
text = "test"
|
||
10 years ago
|
codehash = common.StringToHash("1234")
|
||
|
hash = common.BytesToHash(crypto.Sha3([]byte(text)))
|
||
10 years ago
|
url = "bzz://bzzhash/my/path/contr.act"
|
||
|
)
|
||
|
|
||
|
func NewTestBackend() *testBackend {
|
||
10 years ago
|
HashRegAddr = common.BigToAddress(common.Big0).Hex() //[2:]
|
||
|
UrlHintAddr = common.BigToAddress(common.Big1).Hex() //[2:]
|
||
10 years ago
|
self := &testBackend{}
|
||
|
self.contracts = make(map[string](map[string]string))
|
||
|
|
||
10 years ago
|
self.contracts[HashRegAddr[2:]] = make(map[string]string)
|
||
10 years ago
|
key := storageAddress(storageMapping(storageIdx2Addr(1), codehash[:]))
|
||
10 years ago
|
self.contracts[HashRegAddr[2:]][key] = hash.Hex()
|
||
10 years ago
|
|
||
10 years ago
|
self.contracts[UrlHintAddr[2:]] = make(map[string]string)
|
||
10 years ago
|
mapaddr := storageMapping(storageIdx2Addr(1), hash[:])
|
||
10 years ago
|
|
||
|
key = storageAddress(storageFixedArray(mapaddr, storageIdx2Addr(0)))
|
||
10 years ago
|
self.contracts[UrlHintAddr[2:]][key] = common.ToHex([]byte(url))
|
||
10 years ago
|
key = storageAddress(storageFixedArray(mapaddr, storageIdx2Addr(1)))
|
||
10 years ago
|
self.contracts[UrlHintAddr[2:]][key] = "0x0"
|
||
10 years ago
|
return self
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
func (self *testBackend) StorageAt(ca, sa string) (res string) {
|
||
|
c := self.contracts[ca]
|
||
|
if c == nil {
|
||
|
return
|
||
|
}
|
||
|
res = c[sa]
|
||
|
return
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
func (self *testBackend) Transact(fromStr, toStr, nonceStr, valueStr, gasStr, gasPriceStr, codeStr string) (string, error) {
|
||
|
return "", nil
|
||
|
}
|
||
|
|
||
10 years ago
|
func (self *testBackend) Call(fromStr, toStr, valueStr, gasStr, gasPriceStr, codeStr string) (string, string, error) {
|
||
|
return "", "", nil
|
||
|
}
|
||
|
|
||
|
func TestSetGlobalRegistrar(t *testing.T) {
|
||
10 years ago
|
b := NewTestBackend()
|
||
10 years ago
|
res := New(b)
|
||
10 years ago
|
err := res.SetGlobalRegistrar("addresshex", common.BigToAddress(common.Big1))
|
||
10 years ago
|
if err != nil {
|
||
10 years ago
|
t.Errorf("unexpected error: %v'", err)
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
func TestHashToHash(t *testing.T) {
|
||
10 years ago
|
b := NewTestBackend()
|
||
10 years ago
|
res := New(b)
|
||
10 years ago
|
// res.SetHashReg()
|
||
|
|
||
|
got, err := res.HashToHash(codehash)
|
||
10 years ago
|
if err != nil {
|
||
|
t.Errorf("expected no error, got %v", err)
|
||
|
} else {
|
||
10 years ago
|
if got != hash {
|
||
|
t.Errorf("incorrect result, expected '%v', got '%v'", hash.Hex(), got.Hex())
|
||
10 years ago
|
}
|
||
|
}
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
func TestHashToUrl(t *testing.T) {
|
||
10 years ago
|
b := NewTestBackend()
|
||
10 years ago
|
res := New(b)
|
||
10 years ago
|
// res.SetUrlHint()
|
||
|
|
||
|
got, err := res.HashToUrl(hash)
|
||
10 years ago
|
if err != nil {
|
||
10 years ago
|
t.Errorf("expected error, got %v", err)
|
||
10 years ago
|
} else {
|
||
10 years ago
|
if got != url {
|
||
10 years ago
|
t.Errorf("incorrect result, expected '%v', got '%s'", url, got)
|
||
10 years ago
|
}
|
||
|
}
|
||
10 years ago
|
}
|