server_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 http_test
  17. import (
  18. "bytes"
  19. "errors"
  20. "fmt"
  21. "io/ioutil"
  22. "net/http"
  23. "strings"
  24. "sync"
  25. "testing"
  26. "github.com/ethereum/go-ethereum/common"
  27. "github.com/ethereum/go-ethereum/swarm/api"
  28. swarm "github.com/ethereum/go-ethereum/swarm/api/client"
  29. "github.com/ethereum/go-ethereum/swarm/storage"
  30. "github.com/ethereum/go-ethereum/swarm/testutil"
  31. )
  32. func TestBzzGetPath(t *testing.T) {
  33. var err error
  34. testmanifest := []string{
  35. `{"entries":[{"path":"a/","hash":"674af7073604ebfc0282a4ab21e5ef1a3c22913866879ebc0816f8a89896b2ed","contentType":"application/bzz-manifest+json","status":0}]}`,
  36. `{"entries":[{"path":"a","hash":"011b4d03dd8c01f1049143cf9c4c817e4b167f1d1b83e5c6f0f10d89ba1e7bce","contentType":"","status":0},{"path":"b/","hash":"0a87b1c3e4bf013686cdf107ec58590f2004610ee58cc2240f26939f691215f5","contentType":"application/bzz-manifest+json","status":0}]}`,
  37. `{"entries":[{"path":"b","hash":"011b4d03dd8c01f1049143cf9c4c817e4b167f1d1b83e5c6f0f10d89ba1e7bce","contentType":"","status":0},{"path":"c","hash":"011b4d03dd8c01f1049143cf9c4c817e4b167f1d1b83e5c6f0f10d89ba1e7bce","contentType":"","status":0}]}`,
  38. }
  39. testrequests := make(map[string]int)
  40. testrequests["/"] = 0
  41. testrequests["/a/"] = 1
  42. testrequests["/a/b/"] = 2
  43. testrequests["/x"] = 0
  44. testrequests[""] = 0
  45. expectedfailrequests := []string{"", "/x"}
  46. reader := [3]*bytes.Reader{}
  47. key := [3]storage.Key{}
  48. srv := testutil.NewTestSwarmServer(t)
  49. defer srv.Close()
  50. wg := &sync.WaitGroup{}
  51. for i, mf := range testmanifest {
  52. reader[i] = bytes.NewReader([]byte(mf))
  53. key[i], err = srv.Dpa.Store(reader[i], int64(len(mf)), wg, nil)
  54. if err != nil {
  55. t.Fatal(err)
  56. }
  57. wg.Wait()
  58. }
  59. _, err = http.Get(srv.URL + "/bzz-raw:/" + common.ToHex(key[0])[2:] + "/a")
  60. if err != nil {
  61. t.Fatalf("Failed to connect to proxy: %v", err)
  62. }
  63. for k, v := range testrequests {
  64. var resp *http.Response
  65. var respbody []byte
  66. url := srv.URL + "/bzz-raw:/"
  67. if k[:] != "" {
  68. url += common.ToHex(key[0])[2:] + "/" + k[1:] + "?content_type=text/plain"
  69. }
  70. resp, err = http.Get(url)
  71. if err != nil {
  72. t.Fatalf("Request failed: %v", err)
  73. }
  74. defer resp.Body.Close()
  75. respbody, err = ioutil.ReadAll(resp.Body)
  76. if string(respbody) != testmanifest[v] {
  77. isexpectedfailrequest := false
  78. for _, r := range expectedfailrequests {
  79. if k[:] == r {
  80. isexpectedfailrequest = true
  81. }
  82. }
  83. if !isexpectedfailrequest {
  84. t.Fatalf("Response body does not match, expected: %v, got %v", testmanifest[v], string(respbody))
  85. }
  86. }
  87. }
  88. for k, v := range testrequests {
  89. var resp *http.Response
  90. var respbody []byte
  91. url := srv.URL + "/bzz-hash:/"
  92. if k[:] != "" {
  93. url += common.ToHex(key[0])[2:] + "/" + k[1:]
  94. }
  95. resp, err = http.Get(url)
  96. if err != nil {
  97. t.Fatalf("Request failed: %v", err)
  98. }
  99. defer resp.Body.Close()
  100. respbody, err = ioutil.ReadAll(resp.Body)
  101. if err != nil {
  102. t.Fatalf("Read request body: %v", err)
  103. }
  104. if string(respbody) != key[v].String() {
  105. isexpectedfailrequest := false
  106. for _, r := range expectedfailrequests {
  107. if k[:] == r {
  108. isexpectedfailrequest = true
  109. }
  110. }
  111. if !isexpectedfailrequest {
  112. t.Fatalf("Response body does not match, expected: %v, got %v", key[v], string(respbody))
  113. }
  114. }
  115. }
  116. for _, c := range []struct {
  117. path string
  118. json string
  119. html string
  120. }{
  121. {
  122. path: "/",
  123. json: `{"common_prefixes":["a/"]}`,
  124. html: "<!DOCTYPE html>\n<html>\n<head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n <title>Swarm index of bzz:/262e5c08c03c2789b6daef487dfa14b4d132f5340d781a3ecb1d5122ab65640c/</title>\n</head>\n\n<body>\n <h1>Swarm index of bzz:/262e5c08c03c2789b6daef487dfa14b4d132f5340d781a3ecb1d5122ab65640c/</h1>\n <hr>\n <table>\n <thead>\n <tr>\n\t<th>Path</th>\n\t<th>Type</th>\n\t<th>Size</th>\n </tr>\n </thead>\n\n <tbody>\n \n\t<tr>\n\t <td><a href=\"a/\">a/</a></td>\n\t <td>DIR</td>\n\t <td>-</td>\n\t</tr>\n \n\n \n </table>\n <hr>\n</body>\n",
  125. },
  126. {
  127. path: "/a/",
  128. json: `{"common_prefixes":["a/b/"],"entries":[{"hash":"011b4d03dd8c01f1049143cf9c4c817e4b167f1d1b83e5c6f0f10d89ba1e7bce","path":"a/a","mod_time":"0001-01-01T00:00:00Z"}]}`,
  129. html: "<!DOCTYPE html>\n<html>\n<head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n <title>Swarm index of bzz:/262e5c08c03c2789b6daef487dfa14b4d132f5340d781a3ecb1d5122ab65640c/a/</title>\n</head>\n\n<body>\n <h1>Swarm index of bzz:/262e5c08c03c2789b6daef487dfa14b4d132f5340d781a3ecb1d5122ab65640c/a/</h1>\n <hr>\n <table>\n <thead>\n <tr>\n\t<th>Path</th>\n\t<th>Type</th>\n\t<th>Size</th>\n </tr>\n </thead>\n\n <tbody>\n \n\t<tr>\n\t <td><a href=\"b/\">b/</a></td>\n\t <td>DIR</td>\n\t <td>-</td>\n\t</tr>\n \n\n \n\t<tr>\n\t <td><a href=\"a\">a</a></td>\n\t <td></td>\n\t <td>0</td>\n\t</tr>\n \n </table>\n <hr>\n</body>\n",
  130. },
  131. {
  132. path: "/a/b/",
  133. json: `{"entries":[{"hash":"011b4d03dd8c01f1049143cf9c4c817e4b167f1d1b83e5c6f0f10d89ba1e7bce","path":"a/b/b","mod_time":"0001-01-01T00:00:00Z"},{"hash":"011b4d03dd8c01f1049143cf9c4c817e4b167f1d1b83e5c6f0f10d89ba1e7bce","path":"a/b/c","mod_time":"0001-01-01T00:00:00Z"}]}`,
  134. html: "<!DOCTYPE html>\n<html>\n<head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n <title>Swarm index of bzz:/262e5c08c03c2789b6daef487dfa14b4d132f5340d781a3ecb1d5122ab65640c/a/b/</title>\n</head>\n\n<body>\n <h1>Swarm index of bzz:/262e5c08c03c2789b6daef487dfa14b4d132f5340d781a3ecb1d5122ab65640c/a/b/</h1>\n <hr>\n <table>\n <thead>\n <tr>\n\t<th>Path</th>\n\t<th>Type</th>\n\t<th>Size</th>\n </tr>\n </thead>\n\n <tbody>\n \n\n \n\t<tr>\n\t <td><a href=\"b\">b</a></td>\n\t <td></td>\n\t <td>0</td>\n\t</tr>\n \n\t<tr>\n\t <td><a href=\"c\">c</a></td>\n\t <td></td>\n\t <td>0</td>\n\t</tr>\n \n </table>\n <hr>\n</body>\n",
  135. },
  136. {
  137. path: "/x",
  138. },
  139. {
  140. path: "",
  141. },
  142. } {
  143. k := c.path
  144. url := srv.URL + "/bzz-list:/"
  145. if k[:] != "" {
  146. url += common.ToHex(key[0])[2:] + "/" + k[1:]
  147. }
  148. t.Run("json list "+c.path, func(t *testing.T) {
  149. resp, err := http.Get(url)
  150. if err != nil {
  151. t.Fatalf("HTTP request: %v", err)
  152. }
  153. defer resp.Body.Close()
  154. respbody, err := ioutil.ReadAll(resp.Body)
  155. if err != nil {
  156. t.Fatalf("Read response body: %v", err)
  157. }
  158. body := strings.TrimSpace(string(respbody))
  159. if body != c.json {
  160. isexpectedfailrequest := false
  161. for _, r := range expectedfailrequests {
  162. if k[:] == r {
  163. isexpectedfailrequest = true
  164. }
  165. }
  166. if !isexpectedfailrequest {
  167. t.Errorf("Response list body %q does not match, expected: %v, got %v", k, c.json, body)
  168. }
  169. }
  170. })
  171. t.Run("html list "+c.path, func(t *testing.T) {
  172. req, err := http.NewRequest(http.MethodGet, url, nil)
  173. if err != nil {
  174. t.Fatalf("New request: %v", err)
  175. }
  176. req.Header.Set("Accept", "text/html")
  177. resp, err := http.DefaultClient.Do(req)
  178. if err != nil {
  179. t.Fatalf("HTTP request: %v", err)
  180. }
  181. defer resp.Body.Close()
  182. respbody, err := ioutil.ReadAll(resp.Body)
  183. if err != nil {
  184. t.Fatalf("Read response body: %v", err)
  185. }
  186. if string(respbody) != c.html {
  187. isexpectedfailrequest := false
  188. for _, r := range expectedfailrequests {
  189. if k[:] == r {
  190. isexpectedfailrequest = true
  191. }
  192. }
  193. if !isexpectedfailrequest {
  194. t.Errorf("Response list body %q does not match, expected: %q, got %q", k, c.html, string(respbody))
  195. }
  196. }
  197. })
  198. }
  199. nonhashtests := []string{
  200. srv.URL + "/bzz:/name",
  201. srv.URL + "/bzz-immutable:/nonhash",
  202. srv.URL + "/bzz-raw:/nonhash",
  203. srv.URL + "/bzz-list:/nonhash",
  204. srv.URL + "/bzz-hash:/nonhash",
  205. }
  206. nonhashresponses := []string{
  207. "error resolving name: no DNS to resolve name: &#34;name&#34;",
  208. "error resolving nonhash: immutable address not a content hash: &#34;nonhash&#34;",
  209. "error resolving nonhash: no DNS to resolve name: &#34;nonhash&#34;",
  210. "error resolving nonhash: no DNS to resolve name: &#34;nonhash&#34;",
  211. "error resolving nonhash: no DNS to resolve name: &#34;nonhash&#34;",
  212. }
  213. for i, url := range nonhashtests {
  214. var resp *http.Response
  215. var respbody []byte
  216. resp, err = http.Get(url)
  217. if err != nil {
  218. t.Fatalf("Request failed: %v", err)
  219. }
  220. defer resp.Body.Close()
  221. respbody, err = ioutil.ReadAll(resp.Body)
  222. if err != nil {
  223. t.Fatalf("ReadAll failed: %v", err)
  224. }
  225. if !strings.Contains(string(respbody), nonhashresponses[i]) {
  226. t.Fatalf("Non-Hash response body does not match, expected: %v, got: %v", nonhashresponses[i], string(respbody))
  227. }
  228. }
  229. }
  230. // TestBzzRootRedirect tests that getting the root path of a manifest without
  231. // a trailing slash gets redirected to include the trailing slash so that
  232. // relative URLs work as expected.
  233. func TestBzzRootRedirect(t *testing.T) {
  234. srv := testutil.NewTestSwarmServer(t)
  235. defer srv.Close()
  236. // create a manifest with some data at the root path
  237. client := swarm.NewClient(srv.URL)
  238. data := []byte("data")
  239. file := &swarm.File{
  240. ReadCloser: ioutil.NopCloser(bytes.NewReader(data)),
  241. ManifestEntry: api.ManifestEntry{
  242. Path: "",
  243. ContentType: "text/plain",
  244. Size: int64(len(data)),
  245. },
  246. }
  247. hash, err := client.Upload(file, "")
  248. if err != nil {
  249. t.Fatal(err)
  250. }
  251. // define a CheckRedirect hook which ensures there is only a single
  252. // redirect to the correct URL
  253. redirected := false
  254. httpClient := http.Client{
  255. CheckRedirect: func(req *http.Request, via []*http.Request) error {
  256. if redirected {
  257. return errors.New("too many redirects")
  258. }
  259. redirected = true
  260. expectedPath := "/bzz:/" + hash + "/"
  261. if req.URL.Path != expectedPath {
  262. return fmt.Errorf("expected redirect to %q, got %q", expectedPath, req.URL.Path)
  263. }
  264. return nil
  265. },
  266. }
  267. // perform the GET request and assert the response
  268. res, err := httpClient.Get(srv.URL + "/bzz:/" + hash)
  269. if err != nil {
  270. t.Fatal(err)
  271. }
  272. defer res.Body.Close()
  273. if !redirected {
  274. t.Fatal("expected GET /bzz:/<hash> to redirect to /bzz:/<hash>/ but it didn't")
  275. }
  276. gotData, err := ioutil.ReadAll(res.Body)
  277. if err != nil {
  278. t.Fatal(err)
  279. }
  280. if !bytes.Equal(gotData, data) {
  281. t.Fatalf("expected response to equal %q, got %q", data, gotData)
  282. }
  283. }