github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/swarm/api/filesystem_test.go (about) 1 // Copyright 2016 The Spectrum Authors 2 // This file is part of the Spectrum library. 3 // 4 // The Spectrum 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 Spectrum 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 Spectrum 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 "sync" 25 "testing" 26 27 "github.com/SmartMeshFoundation/Spectrum/common" 28 "github.com/SmartMeshFoundation/Spectrum/swarm/storage" 29 ) 30 31 var testDownloadDir, _ = ioutil.TempDir(os.TempDir(), "bzz-test") 32 33 func testFileSystem(t *testing.T, f func(*FileSystem)) { 34 testApi(t, func(api *Api) { 35 f(NewFileSystem(api)) 36 }) 37 } 38 39 func readPath(t *testing.T, parts ...string) string { 40 file := filepath.Join(parts...) 41 content, err := ioutil.ReadFile(file) 42 43 if err != nil { 44 t.Fatalf("unexpected error reading '%v': %v", file, err) 45 } 46 return string(content) 47 } 48 49 func TestApiDirUpload0(t *testing.T) { 50 testFileSystem(t, func(fs *FileSystem) { 51 api := fs.api 52 bzzhash, err := fs.Upload(filepath.Join("testdata", "test0"), "") 53 if err != nil { 54 t.Fatalf("unexpected error: %v", err) 55 } 56 content := readPath(t, "testdata", "test0", "index.html") 57 resp := testGet(t, api, bzzhash, "index.html") 58 exp := expResponse(content, "text/html; charset=utf-8", 0) 59 checkResponse(t, resp, exp) 60 61 content = readPath(t, "testdata", "test0", "index.css") 62 resp = testGet(t, api, bzzhash, "index.css") 63 exp = expResponse(content, "text/css", 0) 64 checkResponse(t, resp, exp) 65 66 key := storage.Key(common.Hex2Bytes(bzzhash)) 67 _, _, _, err = api.Get(key, "") 68 if err == nil { 69 t.Fatalf("expected error: %v", err) 70 } 71 72 downloadDir := filepath.Join(testDownloadDir, "test0") 73 defer os.RemoveAll(downloadDir) 74 err = fs.Download(bzzhash, downloadDir) 75 if err != nil { 76 t.Fatalf("unexpected error: %v", err) 77 } 78 newbzzhash, err := fs.Upload(downloadDir, "") 79 if err != nil { 80 t.Fatalf("unexpected error: %v", err) 81 } 82 if 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) { 90 api := fs.api 91 bzzhash, err := fs.Upload(filepath.Join("testdata", "test0"), "") 92 if err != nil { 93 t.Errorf("unexpected error: %v", err) 94 return 95 } 96 97 key := storage.Key(common.Hex2Bytes(bzzhash)) 98 key, err = api.Modify(key, "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 wg := &sync.WaitGroup{} 109 hash, err := api.Store(bytes.NewReader(index), int64(len(index)), wg) 110 wg.Wait() 111 if err != nil { 112 t.Errorf("unexpected error: %v", err) 113 return 114 } 115 key, err = api.Modify(key, "index2.html", hash.Hex(), "text/html; charset=utf-8") 116 if err != nil { 117 t.Errorf("unexpected error: %v", err) 118 return 119 } 120 key, err = api.Modify(key, "img/logo.png", hash.Hex(), "text/html; charset=utf-8") 121 if err != nil { 122 t.Errorf("unexpected error: %v", err) 123 return 124 } 125 bzzhash = key.String() 126 127 content := readPath(t, "testdata", "test0", "index.html") 128 resp := testGet(t, api, bzzhash, "index2.html") 129 exp := expResponse(content, "text/html; charset=utf-8", 0) 130 checkResponse(t, resp, exp) 131 132 resp = testGet(t, api, bzzhash, "img/logo.png") 133 exp = expResponse(content, "text/html; charset=utf-8", 0) 134 checkResponse(t, resp, exp) 135 136 content = readPath(t, "testdata", "test0", "index.css") 137 resp = testGet(t, api, bzzhash, "index.css") 138 exp = expResponse(content, "text/css", 0) 139 checkResponse(t, resp, exp) 140 141 _, _, _, err = api.Get(key, "") 142 if err == nil { 143 t.Errorf("expected error: %v", err) 144 } 145 }) 146 } 147 148 func TestApiDirUploadWithRootFile(t *testing.T) { 149 testFileSystem(t, func(fs *FileSystem) { 150 api := fs.api 151 bzzhash, err := fs.Upload(filepath.Join("testdata", "test0"), "index.html") 152 if err != nil { 153 t.Errorf("unexpected error: %v", err) 154 return 155 } 156 157 content := readPath(t, "testdata", "test0", "index.html") 158 resp := testGet(t, api, bzzhash, "") 159 exp := expResponse(content, "text/html; charset=utf-8", 0) 160 checkResponse(t, resp, exp) 161 }) 162 } 163 164 func TestApiFileUpload(t *testing.T) { 165 testFileSystem(t, func(fs *FileSystem) { 166 api := fs.api 167 bzzhash, err := fs.Upload(filepath.Join("testdata", "test0", "index.html"), "") 168 if err != nil { 169 t.Errorf("unexpected error: %v", err) 170 return 171 } 172 173 content := readPath(t, "testdata", "test0", "index.html") 174 resp := testGet(t, api, bzzhash, "index.html") 175 exp := expResponse(content, "text/html; charset=utf-8", 0) 176 checkResponse(t, resp, exp) 177 }) 178 } 179 180 func TestApiFileUploadWithRootFile(t *testing.T) { 181 testFileSystem(t, func(fs *FileSystem) { 182 api := fs.api 183 bzzhash, err := fs.Upload(filepath.Join("testdata", "test0", "index.html"), "index.html") 184 if err != nil { 185 t.Errorf("unexpected error: %v", err) 186 return 187 } 188 189 content := readPath(t, "testdata", "test0", "index.html") 190 resp := testGet(t, api, bzzhash, "") 191 exp := expResponse(content, "text/html; charset=utf-8", 0) 192 checkResponse(t, resp, exp) 193 }) 194 }