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