registrar_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum 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. // go-ethereum 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 go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package registrar
  17. import (
  18. "testing"
  19. "github.com/ethereum/go-ethereum/common"
  20. "github.com/ethereum/go-ethereum/crypto"
  21. )
  22. type testBackend struct {
  23. // contracts mock
  24. contracts map[string](map[string]string)
  25. }
  26. var (
  27. text = "test"
  28. codehash = common.StringToHash("1234")
  29. hash = common.BytesToHash(crypto.Sha3([]byte(text)))
  30. url = "bzz://bzzhash/my/path/contr.act"
  31. )
  32. func NewTestBackend() *testBackend {
  33. HashRegAddr = common.BigToAddress(common.Big0).Hex() //[2:]
  34. UrlHintAddr = common.BigToAddress(common.Big1).Hex() //[2:]
  35. self := &testBackend{}
  36. self.contracts = make(map[string](map[string]string))
  37. self.contracts[HashRegAddr[2:]] = make(map[string]string)
  38. key := storageAddress(storageMapping(storageIdx2Addr(1), codehash[:]))
  39. self.contracts[HashRegAddr[2:]][key] = hash.Hex()
  40. self.contracts[UrlHintAddr[2:]] = make(map[string]string)
  41. mapaddr := storageMapping(storageIdx2Addr(1), hash[:])
  42. key = storageAddress(storageFixedArray(mapaddr, storageIdx2Addr(0)))
  43. self.contracts[UrlHintAddr[2:]][key] = common.ToHex([]byte(url))
  44. key = storageAddress(storageFixedArray(mapaddr, storageIdx2Addr(1)))
  45. self.contracts[UrlHintAddr[2:]][key] = "0x0"
  46. return self
  47. }
  48. func (self *testBackend) StorageAt(ca, sa string) (res string) {
  49. c := self.contracts[ca]
  50. if c == nil {
  51. return
  52. }
  53. res = c[sa]
  54. return
  55. }
  56. func (self *testBackend) Transact(fromStr, toStr, nonceStr, valueStr, gasStr, gasPriceStr, codeStr string) (string, error) {
  57. return "", nil
  58. }
  59. func (self *testBackend) Call(fromStr, toStr, valueStr, gasStr, gasPriceStr, codeStr string) (string, string, error) {
  60. return "", "", nil
  61. }
  62. func TestSetGlobalRegistrar(t *testing.T) {
  63. b := NewTestBackend()
  64. res := New(b)
  65. _, err := res.SetGlobalRegistrar("addresshex", common.BigToAddress(common.Big1))
  66. if err != nil {
  67. t.Errorf("unexpected error: %v'", err)
  68. }
  69. }
  70. func TestHashToHash(t *testing.T) {
  71. b := NewTestBackend()
  72. res := New(b)
  73. // res.SetHashReg()
  74. got, err := res.HashToHash(codehash)
  75. if err != nil {
  76. t.Errorf("expected no error, got %v", err)
  77. } else {
  78. if got != hash {
  79. t.Errorf("incorrect result, expected '%v', got '%v'", hash.Hex(), got.Hex())
  80. }
  81. }
  82. }
  83. func TestHashToUrl(t *testing.T) {
  84. b := NewTestBackend()
  85. res := New(b)
  86. // res.SetUrlHint()
  87. got, err := res.HashToUrl(hash)
  88. if err != nil {
  89. t.Errorf("expected error, got %v", err)
  90. } else {
  91. if got != url {
  92. t.Errorf("incorrect result, expected '%v', got '%s'", url, got)
  93. }
  94. }
  95. }