resolver_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package resolver
  2. import (
  3. "testing"
  4. "github.com/ethereum/go-ethereum/common"
  5. "github.com/ethereum/go-ethereum/crypto"
  6. )
  7. type testBackend struct {
  8. // contracts mock
  9. contracts map[string](map[string]string)
  10. }
  11. var (
  12. text = "test"
  13. codehash = common.StringToHash("1234")
  14. hash = common.BytesToHash(crypto.Sha3([]byte(text)))
  15. url = "bzz://bzzhash/my/path/contr.act"
  16. )
  17. func NewTestBackend() *testBackend {
  18. HashRegContractAddress = common.BigToAddress(common.Big0).Hex()[2:]
  19. UrlHintContractAddress = common.BigToAddress(common.Big1).Hex()[2:]
  20. self := &testBackend{}
  21. self.contracts = make(map[string](map[string]string))
  22. self.contracts[HashRegContractAddress] = make(map[string]string)
  23. key := storageAddress(storageMapping(storageIdx2Addr(1), codehash[:]))
  24. self.contracts[HashRegContractAddress][key] = hash.Hex()
  25. self.contracts[UrlHintContractAddress] = make(map[string]string)
  26. mapaddr := storageMapping(storageIdx2Addr(1), hash[:])
  27. key = storageAddress(storageFixedArray(mapaddr, storageIdx2Addr(0)))
  28. self.contracts[UrlHintContractAddress][key] = common.ToHex([]byte(url))
  29. key = storageAddress(storageFixedArray(mapaddr, storageIdx2Addr(1)))
  30. self.contracts[UrlHintContractAddress][key] = "0x00"
  31. return self
  32. }
  33. func (self *testBackend) StorageAt(ca, sa string) (res string) {
  34. c := self.contracts[ca]
  35. if c == nil {
  36. return
  37. }
  38. res = c[sa]
  39. return
  40. }
  41. func (self *testBackend) Transact(fromStr, toStr, nonceStr, valueStr, gasStr, gasPriceStr, codeStr string) (string, error) {
  42. return "", nil
  43. }
  44. func TestKeyToContentHash(t *testing.T) {
  45. b := NewTestBackend()
  46. res := New(b)
  47. got, err := res.KeyToContentHash(codehash)
  48. if err != nil {
  49. t.Errorf("expected no error, got %v", err)
  50. } else {
  51. if got != hash {
  52. t.Errorf("incorrect result, expected '%v', got '%v'", hash.Hex(), got.Hex())
  53. }
  54. }
  55. }
  56. func TestContentHashToUrl(t *testing.T) {
  57. b := NewTestBackend()
  58. res := New(b)
  59. got, err := res.ContentHashToUrl(hash)
  60. if err != nil {
  61. t.Errorf("expected no error, got %v", err)
  62. } else {
  63. if got != url {
  64. t.Errorf("incorrect result, expected '%v', got '%s'", url, got)
  65. }
  66. }
  67. }
  68. func TestKeyToUrl(t *testing.T) {
  69. b := NewTestBackend()
  70. res := New(b)
  71. got, _, err := res.KeyToUrl(codehash)
  72. if err != nil {
  73. t.Errorf("expected no error, got %v", err)
  74. } else {
  75. if got != url {
  76. t.Errorf("incorrect result, expected \n'%s', got \n'%s'", url, got)
  77. }
  78. }
  79. }