uri_test.go 4.3 KB

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