github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/swarm/api/filesystem_test.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 12:09:46</date> 10 //</624342668989698048> 11 12 // 13 // 14 // 15 // 16 // 17 // 18 // 19 // 20 // 21 // 22 // 23 // 24 // 25 // 26 // 27 28 package api 29 30 import ( 31 "bytes" 32 "context" 33 "io/ioutil" 34 "os" 35 "path/filepath" 36 "testing" 37 38 "github.com/ethereum/go-ethereum/common" 39 "github.com/ethereum/go-ethereum/swarm/storage" 40 ) 41 42 var testDownloadDir, _ = ioutil.TempDir(os.TempDir(), "bzz-test") 43 44 func testFileSystem(t *testing.T, f func(*FileSystem, bool)) { 45 testAPI(t, func(api *API, toEncrypt bool) { 46 f(NewFileSystem(api), toEncrypt) 47 }) 48 } 49 50 func readPath(t *testing.T, parts ...string) string { 51 file := filepath.Join(parts...) 52 content, err := ioutil.ReadFile(file) 53 54 if err != nil { 55 t.Fatalf("unexpected error reading '%v': %v", file, err) 56 } 57 return string(content) 58 } 59 60 func TestApiDirUpload0(t *testing.T) { 61 testFileSystem(t, func(fs *FileSystem, toEncrypt bool) { 62 api := fs.api 63 bzzhash, err := fs.Upload(filepath.Join("testdata", "test0"), "", toEncrypt) 64 if err != nil { 65 t.Fatalf("unexpected error: %v", err) 66 } 67 content := readPath(t, "testdata", "test0", "index.html") 68 resp := testGet(t, api, bzzhash, "index.html") 69 exp := expResponse(content, "text/html; charset=utf-8", 0) 70 checkResponse(t, resp, exp) 71 72 content = readPath(t, "testdata", "test0", "index.css") 73 resp = testGet(t, api, bzzhash, "index.css") 74 exp = expResponse(content, "text/css", 0) 75 checkResponse(t, resp, exp) 76 77 addr := storage.Address(common.Hex2Bytes(bzzhash)) 78 _, _, _, _, err = api.Get(context.TODO(), NOOPDecrypt, addr, "") 79 if err == nil { 80 t.Fatalf("expected error: %v", err) 81 } 82 83 downloadDir := filepath.Join(testDownloadDir, "test0") 84 defer os.RemoveAll(downloadDir) 85 err = fs.Download(bzzhash, downloadDir) 86 if err != nil { 87 t.Fatalf("unexpected error: %v", err) 88 } 89 newbzzhash, err := fs.Upload(downloadDir, "", toEncrypt) 90 if err != nil { 91 t.Fatalf("unexpected error: %v", err) 92 } 93 // 94 if !toEncrypt && bzzhash != newbzzhash { 95 t.Fatalf("download %v reuploaded has incorrect hash, expected %v, got %v", downloadDir, bzzhash, newbzzhash) 96 } 97 }) 98 } 99 100 func TestApiDirUploadModify(t *testing.T) { 101 testFileSystem(t, func(fs *FileSystem, toEncrypt bool) { 102 api := fs.api 103 bzzhash, err := fs.Upload(filepath.Join("testdata", "test0"), "", toEncrypt) 104 if err != nil { 105 t.Errorf("unexpected error: %v", err) 106 return 107 } 108 109 addr := storage.Address(common.Hex2Bytes(bzzhash)) 110 addr, err = api.Modify(context.TODO(), addr, "index.html", "", "") 111 if err != nil { 112 t.Errorf("unexpected error: %v", err) 113 return 114 } 115 index, err := ioutil.ReadFile(filepath.Join("testdata", "test0", "index.html")) 116 if err != nil { 117 t.Errorf("unexpected error: %v", err) 118 return 119 } 120 ctx := context.TODO() 121 hash, wait, err := api.Store(ctx, bytes.NewReader(index), int64(len(index)), toEncrypt) 122 if err != nil { 123 t.Errorf("unexpected error: %v", err) 124 return 125 } 126 err = wait(ctx) 127 if err != nil { 128 t.Errorf("unexpected error: %v", err) 129 return 130 } 131 addr, err = api.Modify(context.TODO(), addr, "index2.html", hash.Hex(), "text/html; charset=utf-8") 132 if err != nil { 133 t.Errorf("unexpected error: %v", err) 134 return 135 } 136 addr, err = api.Modify(context.TODO(), addr, "img/logo.png", hash.Hex(), "text/html; charset=utf-8") 137 if err != nil { 138 t.Errorf("unexpected error: %v", err) 139 return 140 } 141 bzzhash = addr.Hex() 142 143 content := readPath(t, "testdata", "test0", "index.html") 144 resp := testGet(t, api, bzzhash, "index2.html") 145 exp := expResponse(content, "text/html; charset=utf-8", 0) 146 checkResponse(t, resp, exp) 147 148 resp = testGet(t, api, bzzhash, "img/logo.png") 149 exp = expResponse(content, "text/html; charset=utf-8", 0) 150 checkResponse(t, resp, exp) 151 152 content = readPath(t, "testdata", "test0", "index.css") 153 resp = testGet(t, api, bzzhash, "index.css") 154 exp = expResponse(content, "text/css", 0) 155 checkResponse(t, resp, exp) 156 157 _, _, _, _, err = api.Get(context.TODO(), nil, addr, "") 158 if err == nil { 159 t.Errorf("expected error: %v", err) 160 } 161 }) 162 } 163 164 func TestApiDirUploadWithRootFile(t *testing.T) { 165 testFileSystem(t, func(fs *FileSystem, toEncrypt bool) { 166 api := fs.api 167 bzzhash, err := fs.Upload(filepath.Join("testdata", "test0"), "index.html", toEncrypt) 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, "") 175 exp := expResponse(content, "text/html; charset=utf-8", 0) 176 checkResponse(t, resp, exp) 177 }) 178 } 179 180 func TestApiFileUpload(t *testing.T) { 181 testFileSystem(t, func(fs *FileSystem, toEncrypt bool) { 182 api := fs.api 183 bzzhash, err := fs.Upload(filepath.Join("testdata", "test0", "index.html"), "", toEncrypt) 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, "index.html") 191 exp := expResponse(content, "text/html; charset=utf-8", 0) 192 checkResponse(t, resp, exp) 193 }) 194 } 195 196 func TestApiFileUploadWithRootFile(t *testing.T) { 197 testFileSystem(t, func(fs *FileSystem, toEncrypt bool) { 198 api := fs.api 199 bzzhash, err := fs.Upload(filepath.Join("testdata", "test0", "index.html"), "index.html", toEncrypt) 200 if err != nil { 201 t.Errorf("unexpected error: %v", err) 202 return 203 } 204 205 content := readPath(t, "testdata", "test0", "index.html") 206 resp := testGet(t, api, bzzhash, "") 207 exp := expResponse(content, "text/html; charset=utf-8", 0) 208 checkResponse(t, resp, exp) 209 }) 210 } 211