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