api_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. "fmt"
  19. "io"
  20. "io/ioutil"
  21. "os"
  22. "testing"
  23. "github.com/ethereum/go-ethereum/common"
  24. "github.com/ethereum/go-ethereum/log"
  25. "github.com/ethereum/go-ethereum/swarm/storage"
  26. )
  27. func testApi(t *testing.T, f func(*Api)) {
  28. datadir, err := ioutil.TempDir("", "bzz-test")
  29. if err != nil {
  30. t.Fatalf("unable to create temp dir: %v", err)
  31. }
  32. os.RemoveAll(datadir)
  33. defer os.RemoveAll(datadir)
  34. dpa, err := storage.NewLocalDPA(datadir)
  35. if err != nil {
  36. return
  37. }
  38. api := NewApi(dpa, nil)
  39. dpa.Start()
  40. f(api)
  41. dpa.Stop()
  42. }
  43. type testResponse struct {
  44. reader storage.LazySectionReader
  45. *Response
  46. }
  47. func checkResponse(t *testing.T, resp *testResponse, exp *Response) {
  48. if resp.MimeType != exp.MimeType {
  49. t.Errorf("incorrect mimeType. expected '%s', got '%s'", exp.MimeType, resp.MimeType)
  50. }
  51. if resp.Status != exp.Status {
  52. t.Errorf("incorrect status. expected '%d', got '%d'", exp.Status, resp.Status)
  53. }
  54. if resp.Size != exp.Size {
  55. t.Errorf("incorrect size. expected '%d', got '%d'", exp.Size, resp.Size)
  56. }
  57. if resp.reader != nil {
  58. content := make([]byte, resp.Size)
  59. read, _ := resp.reader.Read(content)
  60. if int64(read) != exp.Size {
  61. t.Errorf("incorrect content length. expected '%d...', got '%d...'", read, exp.Size)
  62. }
  63. resp.Content = string(content)
  64. }
  65. if resp.Content != exp.Content {
  66. // if !bytes.Equal(resp.Content, exp.Content)
  67. t.Errorf("incorrect content. expected '%s...', got '%s...'", string(exp.Content), string(resp.Content))
  68. }
  69. }
  70. // func expResponse(content []byte, mimeType string, status int) *Response {
  71. func expResponse(content string, mimeType string, status int) *Response {
  72. log.Trace(fmt.Sprintf("expected content (%v): %v ", len(content), content))
  73. return &Response{mimeType, status, int64(len(content)), content}
  74. }
  75. // func testGet(t *testing.T, api *Api, bzzhash string) *testResponse {
  76. func testGet(t *testing.T, api *Api, bzzhash, path string) *testResponse {
  77. key := storage.Key(common.Hex2Bytes(bzzhash))
  78. reader, mimeType, status, err := api.Get(key, path)
  79. if err != nil {
  80. t.Fatalf("unexpected error: %v", err)
  81. }
  82. quitC := make(chan bool)
  83. size, err := reader.Size(quitC)
  84. if err != nil {
  85. t.Fatalf("unexpected error: %v", err)
  86. }
  87. log.Trace(fmt.Sprintf("reader size: %v ", size))
  88. s := make([]byte, size)
  89. _, err = reader.Read(s)
  90. if err != io.EOF {
  91. t.Fatalf("unexpected error: %v", err)
  92. }
  93. reader.Seek(0, 0)
  94. return &testResponse{reader, &Response{mimeType, status, size, string(s)}}
  95. // return &testResponse{reader, &Response{mimeType, status, reader.Size(), nil}}
  96. }
  97. func TestApiPut(t *testing.T) {
  98. testApi(t, func(api *Api) {
  99. content := "hello"
  100. exp := expResponse(content, "text/plain", 0)
  101. // exp := expResponse([]byte(content), "text/plain", 0)
  102. key, err := api.Put(content, exp.MimeType)
  103. if err != nil {
  104. t.Fatalf("unexpected error: %v", err)
  105. }
  106. resp := testGet(t, api, key.String(), "")
  107. checkResponse(t, resp, exp)
  108. })
  109. }