api_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // Copyright 2016 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. "errors"
  19. "fmt"
  20. "io"
  21. "io/ioutil"
  22. "os"
  23. "testing"
  24. "github.com/ethereum/go-ethereum/common"
  25. "github.com/ethereum/go-ethereum/log"
  26. "github.com/ethereum/go-ethereum/swarm/storage"
  27. )
  28. func testApi(t *testing.T, f func(*Api)) {
  29. datadir, err := ioutil.TempDir("", "bzz-test")
  30. if err != nil {
  31. t.Fatalf("unable to create temp dir: %v", err)
  32. }
  33. os.RemoveAll(datadir)
  34. defer os.RemoveAll(datadir)
  35. dpa, err := storage.NewLocalDPA(datadir)
  36. if err != nil {
  37. return
  38. }
  39. api := NewApi(dpa, nil)
  40. dpa.Start()
  41. f(api)
  42. dpa.Stop()
  43. }
  44. type testResponse struct {
  45. reader storage.LazySectionReader
  46. *Response
  47. }
  48. func checkResponse(t *testing.T, resp *testResponse, exp *Response) {
  49. if resp.MimeType != exp.MimeType {
  50. t.Errorf("incorrect mimeType. expected '%s', got '%s'", exp.MimeType, resp.MimeType)
  51. }
  52. if resp.Status != exp.Status {
  53. t.Errorf("incorrect status. expected '%d', got '%d'", exp.Status, resp.Status)
  54. }
  55. if resp.Size != exp.Size {
  56. t.Errorf("incorrect size. expected '%d', got '%d'", exp.Size, resp.Size)
  57. }
  58. if resp.reader != nil {
  59. content := make([]byte, resp.Size)
  60. read, _ := resp.reader.Read(content)
  61. if int64(read) != exp.Size {
  62. t.Errorf("incorrect content length. expected '%d...', got '%d...'", read, exp.Size)
  63. }
  64. resp.Content = string(content)
  65. }
  66. if resp.Content != exp.Content {
  67. // if !bytes.Equal(resp.Content, exp.Content)
  68. t.Errorf("incorrect content. expected '%s...', got '%s...'", string(exp.Content), string(resp.Content))
  69. }
  70. }
  71. // func expResponse(content []byte, mimeType string, status int) *Response {
  72. func expResponse(content string, mimeType string, status int) *Response {
  73. log.Trace(fmt.Sprintf("expected content (%v): %v ", len(content), content))
  74. return &Response{mimeType, status, int64(len(content)), content}
  75. }
  76. // func testGet(t *testing.T, api *Api, bzzhash string) *testResponse {
  77. func testGet(t *testing.T, api *Api, bzzhash, path string) *testResponse {
  78. key := storage.Key(common.Hex2Bytes(bzzhash))
  79. reader, mimeType, status, err := api.Get(key, path)
  80. if err != nil {
  81. t.Fatalf("unexpected error: %v", err)
  82. }
  83. quitC := make(chan bool)
  84. size, err := reader.Size(quitC)
  85. if err != nil {
  86. t.Fatalf("unexpected error: %v", err)
  87. }
  88. log.Trace(fmt.Sprintf("reader size: %v ", size))
  89. s := make([]byte, size)
  90. _, err = reader.Read(s)
  91. if err != io.EOF {
  92. t.Fatalf("unexpected error: %v", err)
  93. }
  94. reader.Seek(0, 0)
  95. return &testResponse{reader, &Response{mimeType, status, size, string(s)}}
  96. // return &testResponse{reader, &Response{mimeType, status, reader.Size(), nil}}
  97. }
  98. func TestApiPut(t *testing.T) {
  99. testApi(t, func(api *Api) {
  100. content := "hello"
  101. exp := expResponse(content, "text/plain", 0)
  102. // exp := expResponse([]byte(content), "text/plain", 0)
  103. key, err := api.Put(content, exp.MimeType)
  104. if err != nil {
  105. t.Fatalf("unexpected error: %v", err)
  106. }
  107. resp := testGet(t, api, key.String(), "")
  108. checkResponse(t, resp, exp)
  109. })
  110. }
  111. // testResolver implements the Resolver interface and either returns the given
  112. // hash if it is set, or returns a "name not found" error
  113. type testResolver struct {
  114. hash *common.Hash
  115. }
  116. func newTestResolver(addr string) *testResolver {
  117. r := &testResolver{}
  118. if addr != "" {
  119. hash := common.HexToHash(addr)
  120. r.hash = &hash
  121. }
  122. return r
  123. }
  124. func (t *testResolver) Resolve(addr string) (common.Hash, error) {
  125. if t.hash == nil {
  126. return common.Hash{}, fmt.Errorf("DNS name not found: %q", addr)
  127. }
  128. return *t.hash, nil
  129. }
  130. // TestAPIResolve tests resolving URIs which can either contain content hashes
  131. // or ENS names
  132. func TestAPIResolve(t *testing.T) {
  133. ensAddr := "swarm.eth"
  134. hashAddr := "1111111111111111111111111111111111111111111111111111111111111111"
  135. resolvedAddr := "2222222222222222222222222222222222222222222222222222222222222222"
  136. doesResolve := newTestResolver(resolvedAddr)
  137. doesntResolve := newTestResolver("")
  138. type test struct {
  139. desc string
  140. dns Resolver
  141. addr string
  142. immutable bool
  143. result string
  144. expectErr error
  145. }
  146. tests := []*test{
  147. {
  148. desc: "DNS not configured, hash address, returns hash address",
  149. dns: nil,
  150. addr: hashAddr,
  151. result: hashAddr,
  152. },
  153. {
  154. desc: "DNS not configured, ENS address, returns error",
  155. dns: nil,
  156. addr: ensAddr,
  157. expectErr: errors.New(`no DNS to resolve name: "swarm.eth"`),
  158. },
  159. {
  160. desc: "DNS configured, hash address, hash resolves, returns resolved address",
  161. dns: doesResolve,
  162. addr: hashAddr,
  163. result: resolvedAddr,
  164. },
  165. {
  166. desc: "DNS configured, immutable hash address, hash resolves, returns hash address",
  167. dns: doesResolve,
  168. addr: hashAddr,
  169. immutable: true,
  170. result: hashAddr,
  171. },
  172. {
  173. desc: "DNS configured, hash address, hash doesn't resolve, returns hash address",
  174. dns: doesntResolve,
  175. addr: hashAddr,
  176. result: hashAddr,
  177. },
  178. {
  179. desc: "DNS configured, ENS address, name resolves, returns resolved address",
  180. dns: doesResolve,
  181. addr: ensAddr,
  182. result: resolvedAddr,
  183. },
  184. {
  185. desc: "DNS configured, immutable ENS address, name resolves, returns error",
  186. dns: doesResolve,
  187. addr: ensAddr,
  188. immutable: true,
  189. expectErr: errors.New(`immutable address not a content hash: "swarm.eth"`),
  190. },
  191. {
  192. desc: "DNS configured, ENS address, name doesn't resolve, returns error",
  193. dns: doesntResolve,
  194. addr: ensAddr,
  195. expectErr: errors.New(`DNS name not found: "swarm.eth"`),
  196. },
  197. }
  198. for _, x := range tests {
  199. t.Run(x.desc, func(t *testing.T) {
  200. api := &Api{dns: x.dns}
  201. uri := &URI{Addr: x.addr, Scheme: "bzz"}
  202. if x.immutable {
  203. uri.Scheme = "bzz-immutable"
  204. }
  205. res, err := api.Resolve(uri)
  206. if err == nil {
  207. if x.expectErr != nil {
  208. t.Fatalf("expected error %q, got result %q", x.expectErr, res)
  209. }
  210. if res.String() != x.result {
  211. t.Fatalf("expected result %q, got %q", x.result, res)
  212. }
  213. } else {
  214. if x.expectErr == nil {
  215. t.Fatalf("expected no error, got %q", err)
  216. }
  217. if err.Error() != x.expectErr.Error() {
  218. t.Fatalf("expected error %q, got %q", x.expectErr, err)
  219. }
  220. }
  221. })
  222. }
  223. }