http.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 testutil
  17. import (
  18. "io/ioutil"
  19. "net/http"
  20. "net/http/httptest"
  21. "os"
  22. "testing"
  23. "github.com/ethereum/go-ethereum/swarm/api"
  24. "github.com/ethereum/go-ethereum/swarm/storage"
  25. "github.com/ethereum/go-ethereum/swarm/storage/mru"
  26. )
  27. type TestServer interface {
  28. ServeHTTP(http.ResponseWriter, *http.Request)
  29. }
  30. // simulated timeProvider
  31. type fakeTimeProvider struct {
  32. currentTime uint64
  33. }
  34. func (f *fakeTimeProvider) Tick() {
  35. f.currentTime++
  36. }
  37. func (f *fakeTimeProvider) Now() mru.Timestamp {
  38. return mru.Timestamp{Time: f.currentTime}
  39. }
  40. func NewTestSwarmServer(t *testing.T, serverFunc func(*api.API) TestServer) *TestSwarmServer {
  41. dir, err := ioutil.TempDir("", "swarm-storage-test")
  42. if err != nil {
  43. t.Fatal(err)
  44. }
  45. storeparams := storage.NewDefaultLocalStoreParams()
  46. storeparams.DbCapacity = 5000000
  47. storeparams.CacheCapacity = 5000
  48. storeparams.Init(dir)
  49. localStore, err := storage.NewLocalStore(storeparams, nil)
  50. if err != nil {
  51. os.RemoveAll(dir)
  52. t.Fatal(err)
  53. }
  54. fileStore := storage.NewFileStore(localStore, storage.NewFileStoreParams())
  55. // mutable resources test setup
  56. resourceDir, err := ioutil.TempDir("", "swarm-resource-test")
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. fakeTimeProvider := &fakeTimeProvider{
  61. currentTime: 42,
  62. }
  63. mru.TimestampProvider = fakeTimeProvider
  64. rhparams := &mru.HandlerParams{}
  65. rh, err := mru.NewTestHandler(resourceDir, rhparams)
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. a := api.NewAPI(fileStore, nil, rh.Handler, nil)
  70. srv := httptest.NewServer(serverFunc(a))
  71. return &TestSwarmServer{
  72. Server: srv,
  73. FileStore: fileStore,
  74. dir: dir,
  75. Hasher: storage.MakeHashFunc(storage.DefaultHash)(),
  76. timestampProvider: fakeTimeProvider,
  77. cleanup: func() {
  78. srv.Close()
  79. rh.Close()
  80. os.RemoveAll(dir)
  81. os.RemoveAll(resourceDir)
  82. },
  83. }
  84. }
  85. type TestSwarmServer struct {
  86. *httptest.Server
  87. Hasher storage.SwarmHash
  88. FileStore *storage.FileStore
  89. dir string
  90. cleanup func()
  91. timestampProvider *fakeTimeProvider
  92. }
  93. func (t *TestSwarmServer) Close() {
  94. t.cleanup()
  95. }
  96. func (t *TestSwarmServer) GetCurrentTime() mru.Timestamp {
  97. return t.timestampProvider.Now()
  98. }