mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-26 06:36:43 +00:00
swarm/network: fix filesystem API - upload/Modify tests
This commit is contained in:
parent
2371ac7265
commit
58397953bc
4 changed files with 27 additions and 12 deletions
|
|
@ -1,7 +1,7 @@
|
||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// "bytes"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
@ -51,10 +51,8 @@ func checkResponse(t *testing.T, resp *testResponse, exp *Response) {
|
||||||
resp.Content = string(content)
|
resp.Content = string(content)
|
||||||
}
|
}
|
||||||
if resp.Content != exp.Content {
|
if resp.Content != exp.Content {
|
||||||
// if !bytes.Equal(resp.Content, exp.Content) {
|
// if !bytes.Equal(resp.Content, exp.Content)
|
||||||
t.Errorf("incorrect content. expected '%s...', got '%s...'", string(exp.Content), string(resp.Content))
|
t.Errorf("incorrect content. expected '%s...', got '%s...'", string(exp.Content), string(resp.Con}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// func expResponse(content []byte, mimeType string, status int) *Response {
|
// func expResponse(content []byte, mimeType string, status int) *Response {
|
||||||
func expResponse(content string, mimeType string, status int) *Response {
|
func expResponse(content string, mimeType string, status int) *Response {
|
||||||
|
|
@ -67,7 +65,13 @@ func testGet(t *testing.T, api *Api, bzzhash string) *testResponse {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unexpected error: %v", err)
|
t.Fatalf("unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
return &testResponse{reader, &Response{mimeType, status, reader.Size(), ""}}
|
|
||||||
|
s := make([]byte, reader.Size())
|
||||||
|
_, err = reader.Read(s)
|
||||||
|
if err != io.EOF {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
return &testResponse{reader, &Response{mimeType, status, reader.Size(), string(s)}}
|
||||||
// return &testResponse{reader, &Response{mimeType, status, reader.Size(), nil}}
|
// return &testResponse{reader, &Response{mimeType, status, reader.Size(), nil}}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@ var (
|
||||||
"CacheCapacity": 5000,
|
"CacheCapacity": 5000,
|
||||||
"Radius": 0,
|
"Radius": 0,
|
||||||
"Branches": 128,
|
"Branches": 128,
|
||||||
"Hash": "SHA256",
|
"Hash": "SHA3",
|
||||||
"CallInterval": 3000000000,
|
"CallInterval": 3000000000,
|
||||||
"KadDbPath": "` + filepath.Join("TMPDIR", "0d2f62485607cf38d9d795d93682a517661e513e", "bzz-peers.json") + `",
|
"KadDbPath": "` + filepath.Join("TMPDIR", "0d2f62485607cf38d9d795d93682a517661e513e", "bzz-peers.json") + `",
|
||||||
"MaxProx": 8,
|
"MaxProx": 8,
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,15 @@
|
||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"runtime"
|
"runtime"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/logger"
|
||||||
|
"github.com/ethereum/go-ethereum/logger/glog"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
@ -17,8 +21,8 @@ func init() {
|
||||||
_, filename, _, _ := runtime.Caller(1)
|
_, filename, _, _ := runtime.Caller(1)
|
||||||
testDir = path.Join(path.Dir(filename), "../test")
|
testDir = path.Join(path.Dir(filename), "../test")
|
||||||
testDownloadDir, _ = ioutil.TempDir(os.TempDir(), "bzz-test")
|
testDownloadDir, _ = ioutil.TempDir(os.TempDir(), "bzz-test")
|
||||||
}
|
glog.SetV(logger.Detail)
|
||||||
|
glog.SetV = nil
|
||||||
func testFileSystem(t *testing.T, f func(*FileSystem)) {
|
func testFileSystem(t *testing.T, f func(*FileSystem)) {
|
||||||
testApi(t, func(api *Api) {
|
testApi(t, func(api *Api) {
|
||||||
f(NewFileSystem(api))
|
f(NewFileSystem(api))
|
||||||
|
|
@ -29,6 +33,7 @@ func readPath(t *testing.T, parts ...string) string {
|
||||||
// func readPath(t *testing.T, parts ...string) []byte {
|
// func readPath(t *testing.T, parts ...string) []byte {
|
||||||
file := path.Join(parts...)
|
file := path.Join(parts...)
|
||||||
content, err := ioutil.ReadFile(file)
|
content, err := ioutil.ReadFile(file)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unexpected error reading '%v': %v", file, err)
|
t.Fatalf("unexpected error reading '%v': %v", file, err)
|
||||||
}
|
}
|
||||||
|
|
@ -45,13 +50,17 @@ func TestApiDirUpload0(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
content := readPath(t, testDir, "test0", "index.html")
|
content := readPath(t, testDir, "test0", "index.html")
|
||||||
|
t.Logf("content (%v): %v ", len(content), content)
|
||||||
resp := testGet(t, api, bzzhash+"/index.html")
|
resp := testGet(t, api, bzzhash+"/index.html")
|
||||||
exp := expRes ponse(content, "text/html; charset=utf-8", 0)
|
exp := expRes ponse(content, "text/html; charset=utf-8", 0)
|
||||||
|
t.Logf("index.html (size%v=?=%v)", resp.Size, exp.Size)
|
||||||
checkResponse(t, resp, exp)
|
checkResponse(t, resp, exp)
|
||||||
|
t.FailNow()
|
||||||
|
|
||||||
content = readPath(t, testDir, "test0", "index.css")
|
content = readPath(t, testDir, "test0", "index.css")
|
||||||
resp = testGet(t, api, bzzhash+"/index.css")
|
resp = testGet(t, api, bzzhash+"/index.css")
|
||||||
exp = expResponse(content, "text/css", 0)
|
exp = expResponse(content, "text/css", 0)
|
||||||
|
t.Logf("index.css (size%v=?=%v)", resp.Size, exp.Size)
|
||||||
checkResponse(t, resp, exp)
|
checkResponse(t, resp, exp)
|
||||||
|
|
||||||
content = readPath(t, testDir, "test0", "img", "logo.png")
|
content = readPath(t, testDir, "test0", "img", "logo.png")
|
||||||
|
|
@ -163,7 +172,7 @@ func TestApiFileUploadWithRootFile(t *testing.T) {
|
||||||
testFileSystem(t, func(fs *FileSystem) {
|
testFileSystem(t, func(fs *FileSystem) {
|
||||||
api := fs.api
|
api := fs.api
|
||||||
bzzhash, err := fs.Upload(path.Join(testDir, "test0", "index.html"), "index.html")
|
bzzhash, err := fs.Upload(path.Join(testDir, "test0", "index.html"), "index.html")
|
||||||
if err != nil {
|
if err != io.EOF {
|
||||||
t.Errorf("unexpected error: %v", err)
|
t.Errorf("unexpected error: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -43,6 +43,8 @@ func (self *Storage) Get(bzzpath string) (*Response, error) {
|
||||||
return &Response{mimeType, status, expsize, string(body[:size])}, err
|
return &Response{mimeType, status, expsize, string(body[:size])}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Modify(rootHash, path, contentHash, contentType) takes th e manifest trie rooted in rootHash,
|
||||||
|
// and merge on to it. creating an entry w conentType (mime)
|
||||||
func (self *Storage) Modify(rootHash, path, contentHash, contentType string) (newRootHash string, err error) {
|
func (self *Storage) Modify(rootHash, path, contentHash, contentType string) (newRootHash string, err error) {
|
||||||
return self.api.Modify(rootHash+"/"+path, contentHash, contentType, true)
|
return self.api.Modify(rootHash+"/"+path, contentHash, contentType, true)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue