github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/swarm/api/filesystem_test.go (about) 1 // This file is part of the go-sberex library. The go-sberex library is 2 // free software: you can redistribute it and/or modify it under the terms 3 // of the GNU Lesser General Public License as published by the Free 4 // Software Foundation, either version 3 of the License, or (at your option) 5 // any later version. 6 // 7 // The go-sberex library is distributed in the hope that it will be useful, 8 // but WITHOUT ANY WARRANTY; without even the implied warranty of 9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 10 // General Public License <http://www.gnu.org/licenses/> for more details. 11 12 package api 13 14 import ( 15 "bytes" 16 "io/ioutil" 17 "os" 18 "path/filepath" 19 "sync" 20 "testing" 21 22 "github.com/Sberex/go-sberex/common" 23 "github.com/Sberex/go-sberex/swarm/storage" 24 ) 25 26 var testDownloadDir, _ = ioutil.TempDir(os.TempDir(), "bzz-test") 27 28 func testFileSystem(t *testing.T, f func(*FileSystem)) { 29 testApi(t, func(api *Api) { 30 f(NewFileSystem(api)) 31 }) 32 } 33 34 func readPath(t *testing.T, parts ...string) string { 35 file := filepath.Join(parts...) 36 content, err := ioutil.ReadFile(file) 37 38 if err != nil { 39 t.Fatalf("unexpected error reading '%v': %v", file, err) 40 } 41 return string(content) 42 } 43 44 func TestApiDirUpload0(t *testing.T) { 45 testFileSystem(t, func(fs *FileSystem) { 46 api := fs.api 47 bzzhash, err := fs.Upload(filepath.Join("testdata", "test0"), "") 48 if err != nil { 49 t.Fatalf("unexpected error: %v", err) 50 } 51 content := readPath(t, "testdata", "test0", "index.html") 52 resp := testGet(t, api, bzzhash, "index.html") 53 exp := expResponse(content, "text/html; charset=utf-8", 0) 54 checkResponse(t, resp, exp) 55 56 content = readPath(t, "testdata", "test0", "index.css") 57 resp = testGet(t, api, bzzhash, "index.css") 58 exp = expResponse(content, "text/css", 0) 59 checkResponse(t, resp, exp) 60 61 key := storage.Key(common.Hex2Bytes(bzzhash)) 62 _, _, _, err = api.Get(key, "") 63 if err == nil { 64 t.Fatalf("expected error: %v", err) 65 } 66 67 downloadDir := filepath.Join(testDownloadDir, "test0") 68 defer os.RemoveAll(downloadDir) 69 err = fs.Download(bzzhash, downloadDir) 70 if err != nil { 71 t.Fatalf("unexpected error: %v", err) 72 } 73 newbzzhash, err := fs.Upload(downloadDir, "") 74 if err != nil { 75 t.Fatalf("unexpected error: %v", err) 76 } 77 if bzzhash != newbzzhash { 78 t.Fatalf("download %v reuploaded has incorrect hash, expected %v, got %v", downloadDir, bzzhash, newbzzhash) 79 } 80 }) 81 } 82 83 func TestApiDirUploadModify(t *testing.T) { 84 testFileSystem(t, func(fs *FileSystem) { 85 api := fs.api 86 bzzhash, err := fs.Upload(filepath.Join("testdata", "test0"), "") 87 if err != nil { 88 t.Errorf("unexpected error: %v", err) 89 return 90 } 91 92 key := storage.Key(common.Hex2Bytes(bzzhash)) 93 key, err = api.Modify(key, "index.html", "", "") 94 if err != nil { 95 t.Errorf("unexpected error: %v", err) 96 return 97 } 98 index, err := ioutil.ReadFile(filepath.Join("testdata", "test0", "index.html")) 99 if err != nil { 100 t.Errorf("unexpected error: %v", err) 101 return 102 } 103 wg := &sync.WaitGroup{} 104 hash, err := api.Store(bytes.NewReader(index), int64(len(index)), wg) 105 wg.Wait() 106 if err != nil { 107 t.Errorf("unexpected error: %v", err) 108 return 109 } 110 key, err = api.Modify(key, "index2.html", hash.Hex(), "text/html; charset=utf-8") 111 if err != nil { 112 t.Errorf("unexpected error: %v", err) 113 return 114 } 115 key, err = api.Modify(key, "img/logo.png", hash.Hex(), "text/html; charset=utf-8") 116 if err != nil { 117 t.Errorf("unexpected error: %v", err) 118 return 119 } 120 bzzhash = key.String() 121 122 content := readPath(t, "testdata", "test0", "index.html") 123 resp := testGet(t, api, bzzhash, "index2.html") 124 exp := expResponse(content, "text/html; charset=utf-8", 0) 125 checkResponse(t, resp, exp) 126 127 resp = testGet(t, api, bzzhash, "img/logo.png") 128 exp = expResponse(content, "text/html; charset=utf-8", 0) 129 checkResponse(t, resp, exp) 130 131 content = readPath(t, "testdata", "test0", "index.css") 132 resp = testGet(t, api, bzzhash, "index.css") 133 exp = expResponse(content, "text/css", 0) 134 checkResponse(t, resp, exp) 135 136 _, _, _, err = api.Get(key, "") 137 if err == nil { 138 t.Errorf("expected error: %v", err) 139 } 140 }) 141 } 142 143 func TestApiDirUploadWithRootFile(t *testing.T) { 144 testFileSystem(t, func(fs *FileSystem) { 145 api := fs.api 146 bzzhash, err := fs.Upload(filepath.Join("testdata", "test0"), "index.html") 147 if err != nil { 148 t.Errorf("unexpected error: %v", err) 149 return 150 } 151 152 content := readPath(t, "testdata", "test0", "index.html") 153 resp := testGet(t, api, bzzhash, "") 154 exp := expResponse(content, "text/html; charset=utf-8", 0) 155 checkResponse(t, resp, exp) 156 }) 157 } 158 159 func TestApiFileUpload(t *testing.T) { 160 testFileSystem(t, func(fs *FileSystem) { 161 api := fs.api 162 bzzhash, err := fs.Upload(filepath.Join("testdata", "test0", "index.html"), "") 163 if err != nil { 164 t.Errorf("unexpected error: %v", err) 165 return 166 } 167 168 content := readPath(t, "testdata", "test0", "index.html") 169 resp := testGet(t, api, bzzhash, "index.html") 170 exp := expResponse(content, "text/html; charset=utf-8", 0) 171 checkResponse(t, resp, exp) 172 }) 173 } 174 175 func TestApiFileUploadWithRootFile(t *testing.T) { 176 testFileSystem(t, func(fs *FileSystem) { 177 api := fs.api 178 bzzhash, err := fs.Upload(filepath.Join("testdata", "test0", "index.html"), "index.html") 179 if err != nil { 180 t.Errorf("unexpected error: %v", err) 181 return 182 } 183 184 content := readPath(t, "testdata", "test0", "index.html") 185 resp := testGet(t, api, bzzhash, "") 186 exp := expResponse(content, "text/html; charset=utf-8", 0) 187 checkResponse(t, resp, exp) 188 }) 189 }