uri_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Copyright 2017 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. package api
  17. import (
  18. "bytes"
  19. "reflect"
  20. "testing"
  21. "github.com/ethereum/go-ethereum/swarm/storage"
  22. )
  23. func TestParseURI(t *testing.T) {
  24. type test struct {
  25. uri string
  26. expectURI *URI
  27. expectErr bool
  28. expectRaw bool
  29. expectImmutable bool
  30. expectList bool
  31. expectHash bool
  32. expectDeprecatedRaw bool
  33. expectDeprecatedImmutable bool
  34. expectValidKey bool
  35. expectAddr storage.Address
  36. }
  37. tests := []test{
  38. {
  39. uri: "",
  40. expectErr: true,
  41. },
  42. {
  43. uri: "foo",
  44. expectErr: true,
  45. },
  46. {
  47. uri: "bzz",
  48. expectErr: true,
  49. },
  50. {
  51. uri: "bzz:",
  52. expectURI: &URI{Scheme: "bzz"},
  53. },
  54. {
  55. uri: "bzz-immutable:",
  56. expectURI: &URI{Scheme: "bzz-immutable"},
  57. expectImmutable: true,
  58. },
  59. {
  60. uri: "bzz-raw:",
  61. expectURI: &URI{Scheme: "bzz-raw"},
  62. expectRaw: true,
  63. },
  64. {
  65. uri: "bzz:/",
  66. expectURI: &URI{Scheme: "bzz"},
  67. },
  68. {
  69. uri: "bzz:/abc123",
  70. expectURI: &URI{Scheme: "bzz", Addr: "abc123"},
  71. },
  72. {
  73. uri: "bzz:/abc123/path/to/entry",
  74. expectURI: &URI{Scheme: "bzz", Addr: "abc123", Path: "path/to/entry"},
  75. },
  76. {
  77. uri: "bzz-raw:/",
  78. expectURI: &URI{Scheme: "bzz-raw"},
  79. expectRaw: true,
  80. },
  81. {
  82. uri: "bzz-raw:/abc123",
  83. expectURI: &URI{Scheme: "bzz-raw", Addr: "abc123"},
  84. expectRaw: true,
  85. },
  86. {
  87. uri: "bzz-raw:/abc123/path/to/entry",
  88. expectURI: &URI{Scheme: "bzz-raw", Addr: "abc123", Path: "path/to/entry"},
  89. expectRaw: true,
  90. },
  91. {
  92. uri: "bzz://",
  93. expectURI: &URI{Scheme: "bzz"},
  94. },
  95. {
  96. uri: "bzz://abc123",
  97. expectURI: &URI{Scheme: "bzz", Addr: "abc123"},
  98. },
  99. {
  100. uri: "bzz://abc123/path/to/entry",
  101. expectURI: &URI{Scheme: "bzz", Addr: "abc123", Path: "path/to/entry"},
  102. },
  103. {
  104. uri: "bzz-hash:",
  105. expectURI: &URI{Scheme: "bzz-hash"},
  106. expectHash: true,
  107. },
  108. {
  109. uri: "bzz-hash:/",
  110. expectURI: &URI{Scheme: "bzz-hash"},
  111. expectHash: true,
  112. },
  113. {
  114. uri: "bzz-list:",
  115. expectURI: &URI{Scheme: "bzz-list"},
  116. expectList: true,
  117. },
  118. {
  119. uri: "bzz-list:/",
  120. expectURI: &URI{Scheme: "bzz-list"},
  121. expectList: true,
  122. },
  123. {
  124. uri: "bzz-raw://4378d19c26590f1a818ed7d6a62c3809e149b0999cab5ce5f26233b3b423bf8c",
  125. expectURI: &URI{Scheme: "bzz-raw",
  126. Addr: "4378d19c26590f1a818ed7d6a62c3809e149b0999cab5ce5f26233b3b423bf8c",
  127. },
  128. expectValidKey: true,
  129. expectRaw: true,
  130. expectAddr: storage.Address{67, 120, 209, 156, 38, 89, 15, 26,
  131. 129, 142, 215, 214, 166, 44, 56, 9,
  132. 225, 73, 176, 153, 156, 171, 92, 229,
  133. 242, 98, 51, 179, 180, 35, 191, 140,
  134. },
  135. },
  136. }
  137. for _, x := range tests {
  138. actual, err := Parse(x.uri)
  139. if x.expectErr {
  140. if err == nil {
  141. t.Fatalf("expected %s to error", x.uri)
  142. }
  143. continue
  144. }
  145. if err != nil {
  146. t.Fatalf("error parsing %s: %s", x.uri, err)
  147. }
  148. if !reflect.DeepEqual(actual, x.expectURI) {
  149. t.Fatalf("expected %s to return %#v, got %#v", x.uri, x.expectURI, actual)
  150. }
  151. if actual.Raw() != x.expectRaw {
  152. t.Fatalf("expected %s raw to be %t, got %t", x.uri, x.expectRaw, actual.Raw())
  153. }
  154. if actual.Immutable() != x.expectImmutable {
  155. t.Fatalf("expected %s immutable to be %t, got %t", x.uri, x.expectImmutable, actual.Immutable())
  156. }
  157. if actual.List() != x.expectList {
  158. t.Fatalf("expected %s list to be %t, got %t", x.uri, x.expectList, actual.List())
  159. }
  160. if actual.Hash() != x.expectHash {
  161. t.Fatalf("expected %s hash to be %t, got %t", x.uri, x.expectHash, actual.Hash())
  162. }
  163. if x.expectValidKey {
  164. if actual.Address() == nil {
  165. t.Fatalf("expected %s to return a valid key, got nil", x.uri)
  166. } else {
  167. if !bytes.Equal(x.expectAddr, actual.Address()) {
  168. t.Fatalf("expected %s to be decoded to %v", x.expectURI.Addr, x.expectAddr)
  169. }
  170. }
  171. }
  172. }
  173. }