resolver_test.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. self := &testBackend{}
  19. self.contracts = make(map[string](map[string]string))
  20. self.contracts[HashRegContractAddress] = make(map[string]string)
  21. key := storageAddress(storageMapping(storageIdx2Addr(1), codehash[:]))
  22. self.contracts[HashRegContractAddress][key] = hash.Hex()
  23. self.contracts[URLHintContractAddress] = make(map[string]string)
  24. mapaddr := storageMapping(storageIdx2Addr(1), hash[:])
  25. key = storageAddress(storageFixedArray(mapaddr, storageIdx2Addr(0)))
  26. self.contracts[URLHintContractAddress][key] = common.ToHex([]byte(url))
  27. key = storageAddress(storageFixedArray(mapaddr, storageIdx2Addr(1)))
  28. self.contracts[URLHintContractAddress][key] = "0x00"
  29. return self
  30. }
  31. func (self *testBackend) StorageAt(ca, sa string) (res string) {
  32. c := self.contracts[ca]
  33. if c == nil {
  34. return
  35. }
  36. res = c[sa]
  37. return
  38. }
  39. func TestKeyToContentHash(t *testing.T) {
  40. b := NewTestBackend()
  41. res := New(b, URLHintContractAddress, HashRegContractAddress)
  42. got, err := res.KeyToContentHash(codehash)
  43. if err != nil {
  44. t.Errorf("expected no error, got %v", err)
  45. } else {
  46. if got != hash {
  47. t.Errorf("incorrect result, expected %x, got %x: ", hash.Hex(), got.Hex())
  48. }
  49. }
  50. }
  51. func TestContentHashToUrl(t *testing.T) {
  52. b := NewTestBackend()
  53. res := New(b, URLHintContractAddress, HashRegContractAddress)
  54. got, err := res.ContentHashToUrl(hash)
  55. if err != nil {
  56. t.Errorf("expected no error, got %v", err)
  57. } else {
  58. if string(got) != url {
  59. t.Errorf("incorrect result, expected %v, got %s: ", url, string(got))
  60. }
  61. }
  62. }
  63. func TestKeyToUrl(t *testing.T) {
  64. b := NewTestBackend()
  65. res := New(b, URLHintContractAddress, HashRegContractAddress)
  66. got, _, err := res.KeyToUrl(codehash)
  67. if err != nil {
  68. t.Errorf("expected no error, got %v", err)
  69. } else {
  70. if string(got) != url {
  71. t.Errorf("incorrect result, expected %v, got %s: ", url, string(got))
  72. }
  73. }
  74. }