github.com/murrekatt/go-ethereum@v1.5.8-0.20170123175102-fc52f2c007fb/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 "sync" 25 "testing" 26 ) 27 28 var testDownloadDir, _ = ioutil.TempDir(os.TempDir(), "bzz-test") 29 30 func testFileSystem(t *testing.T, f func(*FileSystem)) { 31 testApi(t, func(api *Api) { 32 f(NewFileSystem(api)) 33 }) 34 } 35 36 func readPath(t *testing.T, parts ...string) string { 37 file := filepath.Join(parts...) 38 content, err := ioutil.ReadFile(file) 39 40 if err != nil { 41 t.Fatalf("unexpected error reading '%v': %v", file, err) 42 } 43 return string(content) 44 } 45 46 func TestApiDirUpload0(t *testing.T) { 47 testFileSystem(t, func(fs *FileSystem) { 48 api := fs.api 49 bzzhash, err := fs.Upload(filepath.Join("testdata", "test0"), "") 50 if err != nil { 51 t.Fatalf("unexpected error: %v", err) 52 } 53 content := readPath(t, "testdata", "test0", "index.html") 54 resp := testGet(t, api, bzzhash+"/index.html") 55 exp := expResponse(content, "text/html; charset=utf-8", 0) 56 checkResponse(t, resp, exp) 57 58 content = readPath(t, "testdata", "test0", "index.css") 59 resp = testGet(t, api, bzzhash+"/index.css") 60 exp = expResponse(content, "text/css", 0) 61 checkResponse(t, resp, exp) 62 63 _, _, _, err = api.Get(bzzhash, true) 64 if err == nil { 65 t.Fatalf("expected error: %v", err) 66 } 67 68 downloadDir := filepath.Join(testDownloadDir, "test0") 69 defer os.RemoveAll(downloadDir) 70 err = fs.Download(bzzhash, downloadDir) 71 if err != nil { 72 t.Fatalf("unexpected error: %v", err) 73 } 74 newbzzhash, err := fs.Upload(downloadDir, "") 75 if err != nil { 76 t.Fatalf("unexpected error: %v", err) 77 } 78 if bzzhash != newbzzhash { 79 t.Fatalf("download %v reuploaded has incorrect hash, expected %v, got %v", downloadDir, bzzhash, newbzzhash) 80 } 81 }) 82 } 83 84 func TestApiDirUploadModify(t *testing.T) { 85 testFileSystem(t, func(fs *FileSystem) { 86 api := fs.api 87 bzzhash, err := fs.Upload(filepath.Join("testdata", "test0"), "") 88 if err != nil { 89 t.Errorf("unexpected error: %v", err) 90 return 91 } 92 93 bzzhash, err = api.Modify(bzzhash+"/index.html", "", "", true) 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 bzzhash, err = api.Modify(bzzhash+"/index2.html", hash.Hex(), "text/html; charset=utf-8", true) 111 if err != nil { 112 t.Errorf("unexpected error: %v", err) 113 return 114 } 115 bzzhash, err = api.Modify(bzzhash+"/img/logo.png", hash.Hex(), "text/html; charset=utf-8", true) 116 if err != nil { 117 t.Errorf("unexpected error: %v", err) 118 return 119 } 120 121 content := readPath(t, "testdata", "test0", "index.html") 122 resp := testGet(t, api, bzzhash+"/index2.html") 123 exp := expResponse(content, "text/html; charset=utf-8", 0) 124 checkResponse(t, resp, exp) 125 126 resp = testGet(t, api, bzzhash+"/img/logo.png") 127 exp = expResponse(content, "text/html; charset=utf-8", 0) 128 checkResponse(t, resp, exp) 129 130 content = readPath(t, "testdata", "test0", "index.css") 131 resp = testGet(t, api, bzzhash+"/index.css") 132 exp = expResponse(content, "text/css", 0) 133 checkResponse(t, resp, exp) 134 135 _, _, _, err = api.Get(bzzhash, true) 136 if err == nil { 137 t.Errorf("expected error: %v", err) 138 } 139 }) 140 } 141 142 func TestApiDirUploadWithRootFile(t *testing.T) { 143 testFileSystem(t, func(fs *FileSystem) { 144 api := fs.api 145 bzzhash, err := fs.Upload(filepath.Join("testdata", "test0"), "index.html") 146 if err != nil { 147 t.Errorf("unexpected error: %v", err) 148 return 149 } 150 151 content := readPath(t, "testdata", "test0", "index.html") 152 resp := testGet(t, api, bzzhash) 153 exp := expResponse(content, "text/html; charset=utf-8", 0) 154 checkResponse(t, resp, exp) 155 }) 156 } 157 158 func TestApiFileUpload(t *testing.T) { 159 testFileSystem(t, func(fs *FileSystem) { 160 api := fs.api 161 bzzhash, err := fs.Upload(filepath.Join("testdata", "test0", "index.html"), "") 162 if err != nil { 163 t.Errorf("unexpected error: %v", err) 164 return 165 } 166 167 content := readPath(t, "testdata", "test0", "index.html") 168 resp := testGet(t, api, bzzhash+"/index.html") 169 exp := expResponse(content, "text/html; charset=utf-8", 0) 170 checkResponse(t, resp, exp) 171 }) 172 } 173 174 func TestApiFileUploadWithRootFile(t *testing.T) { 175 testFileSystem(t, func(fs *FileSystem) { 176 api := fs.api 177 bzzhash, err := fs.Upload(filepath.Join("testdata", "test0", "index.html"), "index.html") 178 if err != nil { 179 t.Errorf("unexpected error: %v", err) 180 return 181 } 182 183 content := readPath(t, "testdata", "test0", "index.html") 184 resp := testGet(t, api, bzzhash) 185 exp := expResponse(content, "text/html; charset=utf-8", 0) 186 checkResponse(t, resp, exp) 187 }) 188 }