uri_test.go 4.5 KB

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