github.com/gobitfly/go-ethereum@v1.8.12/swarm/api/filesystem_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 "bytes" 21 "io/ioutil" 22 "os" 23 "path/filepath" 24 "testing" 25 26 "github.com/ethereum/go-ethereum/common" 27 "github.com/ethereum/go-ethereum/swarm/storage" 28 ) 29 30 var testDownloadDir, _ = ioutil.TempDir(os.TempDir(), "bzz-test") 31 32 func testFileSystem(t *testing.T, f func(*FileSystem, bool)) { 33 testAPI(t, func(api *API, toEncrypt bool) { 34 f(NewFileSystem(api), toEncrypt) 35 }) 36 } 37 38 func readPath(t *testing.T, parts ...string) string { 39 file := filepath.Join(parts...) 40 content, err := ioutil.ReadFile(file) 41 42 if err != nil { 43 t.Fatalf("unexpected error reading '%v': %v", file, err) 44 } 45 return string(content) 46 } 47 48 func TestApiDirUpload0(t *testing.T) { 49 testFileSystem(t, func(fs *FileSystem, toEncrypt bool) { 50 api := fs.api 51 bzzhash, err := fs.Upload(filepath.Join("testdata", "test0"), "", toEncrypt) 52 if err != nil { 53 t.Fatalf("unexpected error: %v", err) 54 } 55 content := readPath(t, "testdata", "test0", "index.html") 56 resp := testGet(t, api, bzzhash, "index.html") 57 exp := expResponse(content, "text/html; charset=utf-8", 0) 58 checkResponse(t, resp, exp) 59 60 content = readPath(t, "testdata", "test0", "index.css") 61 resp = testGet(t, api, bzzhash, "index.css") 62 exp = expResponse(content, "text/css", 0) 63 checkResponse(t, resp, exp) 64 65 addr := storage.Address(common.Hex2Bytes(bzzhash)) 66 _, _, _, _, err = api.Get(addr, "") 67 if err == nil { 68 t.Fatalf("expected error: %v", err) 69 } 70 71 downloadDir := filepath.Join(testDownloadDir, "test0") 72 defer os.RemoveAll(downloadDir) 73 err = fs.Download(bzzhash, downloadDir) 74 if err != nil { 75 t.Fatalf("unexpected error: %v", err) 76 } 77 newbzzhash, err := fs.Upload(downloadDir, "", toEncrypt) 78 if err != nil { 79 t.Fatalf("unexpected error: %v", err) 80 } 81 // TODO: currently the hash is not deterministic in the encrypted case 82 if !toEncrypt && bzzhash != newbzzhash { 83 t.Fatalf("download %v reuploaded has incorrect hash, expected %v, got %v", downloadDir, bzzhash, newbzzhash) 84 } 85 }) 86 } 87 88 func TestApiDirUploadModify(t *testing.T) { 89 testFileSystem(t, func(fs *FileSystem, toEncrypt bool) { 90 api := fs.api 91 bzzhash, err := fs.Upload(filepath.Join("testdata", "test0"), "", toEncrypt) 92 if err != nil { 93 t.Errorf("unexpected error: %v", err) 94 return 95 } 96 97 addr := storage.Address(common.Hex2Bytes(bzzhash)) 98 addr, err = api.Modify(addr, "index.html", "", "") 99 if err != nil { 100 t.Errorf("unexpected error: %v", err) 101 return 102 } 103 index, err := ioutil.ReadFile(filepath.Join("testdata", "test0", "index.html")) 104 if err != nil { 105 t.Errorf("unexpected error: %v", err) 106 return 107 } 108 hash, wait, err := api.Store(bytes.NewReader(index), int64(len(index)), toEncrypt) 109 wait() 110 if err != nil { 111 t.Errorf("unexpected error: %v", err) 112 return 113 } 114 addr, err = api.Modify(addr, "index2.html", hash.Hex(), "text/html; charset=utf-8") 115 if err != nil { 116 t.Errorf("unexpected error: %v", err) 117 return 118 } 119 addr, err = api.Modify(addr, "img/logo.png", hash.Hex(), "text/html; charset=utf-8") 120 if err != nil { 121 t.Errorf("unexpected error: %v", err) 122 return 123 } 124 bzzhash = addr.Hex() 125 126 content := readPath(t, "testdata", "test0", "index.html") 127 resp := testGet(t, api, bzzhash, "index2.html") 128 exp := expResponse(content, "text/html; charset=utf-8", 0) 129 checkResponse(t, resp, exp) 130 131 resp = testGet(t, api, bzzhash, "img/logo.png") 132 exp = expResponse(content, "text/html; charset=utf-8", 0) 133 checkResponse(t, resp, exp) 134 135 content = readPath(t, "testdata", "test0", "index.css") 136 resp = testGet(t, api, bzzhash, "index.css") 137 exp = expResponse(content, "text/css", 0) 138 checkResponse(t, resp, exp) 139 140 _, _, _, _, err = api.Get(addr, "") 141 if err == nil { 142 t.Errorf("expected error: %v", err) 143 } 144 }) 145 } 146 147 func TestApiDirUploadWithRootFile(t *testing.T) { 148 testFileSystem(t, func(fs *FileSystem, toEncrypt bool) { 149 api := fs.api 150 bzzhash, err := fs.Upload(filepath.Join("testdata", "test0"), "index.html", toEncrypt) 151 if err != nil { 152 t.Errorf("unexpected error: %v", err) 153 return 154 } 155 156 content := readPath(t, "testdata", "test0", "index.html") 157 resp := testGet(t, api, bzzhash, "") 158 exp := expResponse(content, "text/html; charset=utf-8", 0) 159 checkResponse(t, resp, exp) 160 }) 161 } 162 163 func TestApiFileUpload(t *testing.T) { 164 testFileSystem(t, func(fs *FileSystem, toEncrypt bool) { 165 api := fs.api 166 bzzhash, err := fs.Upload(filepath.Join("testdata", "test0", "index.html"), "", toEncrypt) 167 if err != nil { 168 t.Errorf("unexpected error: %v", err) 169 return 170 } 171 172 content := readPath(t, "testdata", "test0", "index.html") 173 resp := testGet(t, api, bzzhash, "index.html") 174 exp := expResponse(content, "text/html; charset=utf-8", 0) 175 checkResponse(t, resp, exp) 176 }) 177 } 178 179 func TestApiFileUploadWithRootFile(t *testing.T) { 180 testFileSystem(t, func(fs *FileSystem, toEncrypt bool) { 181 api := fs.api 182 bzzhash, err := fs.Upload(filepath.Join("testdata", "test0", "index.html"), "index.html", toEncrypt) 183 if err != nil { 184 t.Errorf("unexpected error: %v", err) 185 return 186 } 187 188 content := readPath(t, "testdata", "test0", "index.html") 189 resp := testGet(t, api, bzzhash, "") 190 exp := expResponse(content, "text/html; charset=utf-8", 0) 191 checkResponse(t, resp, exp) 192 }) 193 }