2017-04-14 11:29:00 +03:00
|
|
|
// Copyright 2017 The go-ethereum Authors
|
2017-04-05 01:20:07 +03:00
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2017-04-07 01:22:22 +03:00
|
|
|
"bytes"
|
2017-04-05 01:20:07 +03:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
|
|
|
"sort"
|
|
|
|
"testing"
|
|
|
|
|
2017-04-07 01:22:22 +03:00
|
|
|
"github.com/ethereum/go-ethereum/swarm/api"
|
2018-06-20 15:06:27 +03:00
|
|
|
swarmhttp "github.com/ethereum/go-ethereum/swarm/api/http"
|
2017-04-05 01:20:07 +03:00
|
|
|
"github.com/ethereum/go-ethereum/swarm/testutil"
|
|
|
|
)
|
|
|
|
|
2018-06-20 15:06:27 +03:00
|
|
|
func serverFunc(api *api.API) testutil.TestServer {
|
|
|
|
return swarmhttp.NewServer(api)
|
|
|
|
}
|
|
|
|
|
2017-04-07 01:22:22 +03:00
|
|
|
// TestClientUploadDownloadRaw test uploading and downloading raw data to swarm
|
|
|
|
func TestClientUploadDownloadRaw(t *testing.T) {
|
2018-06-20 15:06:27 +03:00
|
|
|
testClientUploadDownloadRaw(false, t)
|
|
|
|
}
|
|
|
|
func TestClientUploadDownloadRawEncrypted(t *testing.T) {
|
|
|
|
testClientUploadDownloadRaw(true, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testClientUploadDownloadRaw(toEncrypt bool, t *testing.T) {
|
|
|
|
srv := testutil.NewTestSwarmServer(t, serverFunc)
|
2017-04-05 01:20:07 +03:00
|
|
|
defer srv.Close()
|
|
|
|
|
2017-04-07 01:22:22 +03:00
|
|
|
client := NewClient(srv.URL)
|
|
|
|
|
|
|
|
// upload some raw data
|
|
|
|
data := []byte("foo123")
|
2018-06-20 15:06:27 +03:00
|
|
|
hash, err := client.UploadRaw(bytes.NewReader(data), int64(len(data)), toEncrypt)
|
2017-04-07 01:22:22 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check we can download the same data
|
2018-06-20 15:06:27 +03:00
|
|
|
res, isEncrypted, err := client.DownloadRaw(hash)
|
2017-04-07 01:22:22 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-06-20 15:06:27 +03:00
|
|
|
if isEncrypted != toEncrypt {
|
|
|
|
t.Fatalf("Expected encyption status %v got %v", toEncrypt, isEncrypted)
|
|
|
|
}
|
2017-04-07 01:22:22 +03:00
|
|
|
defer res.Close()
|
|
|
|
gotData, err := ioutil.ReadAll(res)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(gotData, data) {
|
|
|
|
t.Fatalf("expected downloaded data to be %q, got %q", data, gotData)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestClientUploadDownloadFiles test uploading and downloading files to swarm
|
|
|
|
// manifests
|
|
|
|
func TestClientUploadDownloadFiles(t *testing.T) {
|
2018-06-20 15:06:27 +03:00
|
|
|
testClientUploadDownloadFiles(false, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientUploadDownloadFilesEncrypted(t *testing.T) {
|
|
|
|
testClientUploadDownloadFiles(true, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testClientUploadDownloadFiles(toEncrypt bool, t *testing.T) {
|
|
|
|
srv := testutil.NewTestSwarmServer(t, serverFunc)
|
2017-04-07 01:22:22 +03:00
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
client := NewClient(srv.URL)
|
|
|
|
upload := func(manifest, path string, data []byte) string {
|
|
|
|
file := &File{
|
|
|
|
ReadCloser: ioutil.NopCloser(bytes.NewReader(data)),
|
|
|
|
ManifestEntry: api.ManifestEntry{
|
|
|
|
Path: path,
|
|
|
|
ContentType: "text/plain",
|
|
|
|
Size: int64(len(data)),
|
|
|
|
},
|
|
|
|
}
|
2018-06-20 15:06:27 +03:00
|
|
|
hash, err := client.Upload(file, manifest, toEncrypt)
|
2017-04-07 01:22:22 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return hash
|
|
|
|
}
|
|
|
|
checkDownload := func(manifest, path string, expected []byte) {
|
|
|
|
file, err := client.Download(manifest, path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
if file.Size != int64(len(expected)) {
|
|
|
|
t.Fatalf("expected downloaded file to be %d bytes, got %d", len(expected), file.Size)
|
|
|
|
}
|
2017-08-08 20:34:35 +03:00
|
|
|
if file.ContentType != "text/plain" {
|
|
|
|
t.Fatalf("expected downloaded file to have type %q, got %q", "text/plain", file.ContentType)
|
2017-04-07 01:22:22 +03:00
|
|
|
}
|
|
|
|
data, err := ioutil.ReadAll(file)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(data, expected) {
|
|
|
|
t.Fatalf("expected downloaded data to be %q, got %q", expected, data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// upload a file to the root of a manifest
|
|
|
|
rootData := []byte("some-data")
|
|
|
|
rootHash := upload("", "", rootData)
|
|
|
|
|
|
|
|
// check we can download the root file
|
|
|
|
checkDownload(rootHash, "", rootData)
|
|
|
|
|
|
|
|
// upload another file to the same manifest
|
|
|
|
otherData := []byte("some-other-data")
|
|
|
|
newHash := upload(rootHash, "some/other/path", otherData)
|
|
|
|
|
|
|
|
// check we can download both files from the new manifest
|
|
|
|
checkDownload(newHash, "", rootData)
|
|
|
|
checkDownload(newHash, "some/other/path", otherData)
|
|
|
|
|
|
|
|
// replace the root file with different data
|
|
|
|
newHash = upload(newHash, "", otherData)
|
|
|
|
|
|
|
|
// check both files have the other data
|
|
|
|
checkDownload(newHash, "", otherData)
|
|
|
|
checkDownload(newHash, "some/other/path", otherData)
|
|
|
|
}
|
|
|
|
|
|
|
|
var testDirFiles = []string{
|
|
|
|
"file1.txt",
|
|
|
|
"file2.txt",
|
|
|
|
"dir1/file3.txt",
|
|
|
|
"dir1/file4.txt",
|
|
|
|
"dir2/file5.txt",
|
|
|
|
"dir2/dir3/file6.txt",
|
|
|
|
"dir2/dir4/file7.txt",
|
|
|
|
"dir2/dir4/file8.txt",
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTestDirectory(t *testing.T) string {
|
2017-04-05 01:20:07 +03:00
|
|
|
dir, err := ioutil.TempDir("", "swarm-client-test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-04-07 01:22:22 +03:00
|
|
|
|
|
|
|
for _, file := range testDirFiles {
|
2017-04-05 01:20:07 +03:00
|
|
|
path := filepath.Join(dir, file)
|
|
|
|
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
|
2017-04-07 01:22:22 +03:00
|
|
|
os.RemoveAll(dir)
|
2017-04-05 01:20:07 +03:00
|
|
|
t.Fatalf("error creating dir for %s: %s", path, err)
|
|
|
|
}
|
2017-04-07 01:22:22 +03:00
|
|
|
if err := ioutil.WriteFile(path, []byte(file), 0644); err != nil {
|
|
|
|
os.RemoveAll(dir)
|
2017-04-05 01:20:07 +03:00
|
|
|
t.Fatalf("error writing file %s: %s", path, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-07 01:22:22 +03:00
|
|
|
return dir
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestClientUploadDownloadDirectory tests uploading and downloading a
|
|
|
|
// directory of files to a swarm manifest
|
|
|
|
func TestClientUploadDownloadDirectory(t *testing.T) {
|
2018-06-20 15:06:27 +03:00
|
|
|
srv := testutil.NewTestSwarmServer(t, serverFunc)
|
2017-04-07 01:22:22 +03:00
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
dir := newTestDirectory(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
// upload the directory
|
2017-04-05 01:20:07 +03:00
|
|
|
client := NewClient(srv.URL)
|
2017-04-07 01:22:22 +03:00
|
|
|
defaultPath := filepath.Join(dir, testDirFiles[0])
|
2018-06-20 15:06:27 +03:00
|
|
|
hash, err := client.UploadDirectory(dir, defaultPath, "", false)
|
2017-04-07 01:22:22 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error uploading directory: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check we can download the individual files
|
|
|
|
checkDownloadFile := func(path string, expected []byte) {
|
|
|
|
file, err := client.Download(hash, path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
data, err := ioutil.ReadAll(file)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(data, expected) {
|
|
|
|
t.Fatalf("expected data to be %q, got %q", expected, data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, file := range testDirFiles {
|
|
|
|
checkDownloadFile(file, []byte(file))
|
|
|
|
}
|
|
|
|
|
|
|
|
// check we can download the default path
|
|
|
|
checkDownloadFile("", []byte(testDirFiles[0]))
|
|
|
|
|
|
|
|
// check we can download the directory
|
|
|
|
tmp, err := ioutil.TempDir("", "swarm-client-test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
if err := client.DownloadDirectory(hash, "", tmp); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
for _, file := range testDirFiles {
|
|
|
|
data, err := ioutil.ReadFile(filepath.Join(tmp, file))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(data, []byte(file)) {
|
|
|
|
t.Fatalf("expected data to be %q, got %q", file, data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestClientFileList tests listing files in a swarm manifest
|
|
|
|
func TestClientFileList(t *testing.T) {
|
2018-06-20 15:06:27 +03:00
|
|
|
testClientFileList(false, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientFileListEncrypted(t *testing.T) {
|
|
|
|
testClientFileList(true, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testClientFileList(toEncrypt bool, t *testing.T) {
|
|
|
|
srv := testutil.NewTestSwarmServer(t, serverFunc)
|
2017-04-07 01:22:22 +03:00
|
|
|
defer srv.Close()
|
2017-04-05 01:20:07 +03:00
|
|
|
|
2017-04-07 01:22:22 +03:00
|
|
|
dir := newTestDirectory(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
client := NewClient(srv.URL)
|
2018-06-20 15:06:27 +03:00
|
|
|
hash, err := client.UploadDirectory(dir, "", "", toEncrypt)
|
2017-04-05 01:20:07 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error uploading directory: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ls := func(prefix string) []string {
|
2017-04-07 01:22:22 +03:00
|
|
|
list, err := client.List(hash, prefix)
|
2017-04-05 01:20:07 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-04-07 01:22:22 +03:00
|
|
|
paths := make([]string, 0, len(list.CommonPrefixes)+len(list.Entries))
|
2017-08-08 20:34:35 +03:00
|
|
|
paths = append(paths, list.CommonPrefixes...)
|
2017-04-07 01:22:22 +03:00
|
|
|
for _, entry := range list.Entries {
|
|
|
|
paths = append(paths, entry.Path)
|
2017-04-05 01:20:07 +03:00
|
|
|
}
|
|
|
|
sort.Strings(paths)
|
|
|
|
return paths
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := map[string][]string{
|
2017-11-08 13:45:52 +03:00
|
|
|
"": {"dir1/", "dir2/", "file1.txt", "file2.txt"},
|
|
|
|
"file": {"file1.txt", "file2.txt"},
|
|
|
|
"file1": {"file1.txt"},
|
|
|
|
"file2.txt": {"file2.txt"},
|
|
|
|
"file12": {},
|
|
|
|
"dir": {"dir1/", "dir2/"},
|
|
|
|
"dir1": {"dir1/"},
|
|
|
|
"dir1/": {"dir1/file3.txt", "dir1/file4.txt"},
|
|
|
|
"dir1/file": {"dir1/file3.txt", "dir1/file4.txt"},
|
|
|
|
"dir1/file3.txt": {"dir1/file3.txt"},
|
|
|
|
"dir1/file34": {},
|
|
|
|
"dir2/": {"dir2/dir3/", "dir2/dir4/", "dir2/file5.txt"},
|
|
|
|
"dir2/file": {"dir2/file5.txt"},
|
|
|
|
"dir2/dir": {"dir2/dir3/", "dir2/dir4/"},
|
|
|
|
"dir2/dir3/": {"dir2/dir3/file6.txt"},
|
|
|
|
"dir2/dir4/": {"dir2/dir4/file7.txt", "dir2/dir4/file8.txt"},
|
|
|
|
"dir2/dir4/file": {"dir2/dir4/file7.txt", "dir2/dir4/file8.txt"},
|
|
|
|
"dir2/dir4/file7.txt": {"dir2/dir4/file7.txt"},
|
|
|
|
"dir2/dir4/file78": {},
|
2017-04-05 01:20:07 +03:00
|
|
|
}
|
|
|
|
for prefix, expected := range tests {
|
|
|
|
actual := ls(prefix)
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
2017-04-07 01:22:22 +03:00
|
|
|
t.Fatalf("expected prefix %q to return %v, got %v", prefix, expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestClientMultipartUpload tests uploading files to swarm using a multipart
|
|
|
|
// upload
|
|
|
|
func TestClientMultipartUpload(t *testing.T) {
|
2018-06-20 15:06:27 +03:00
|
|
|
srv := testutil.NewTestSwarmServer(t, serverFunc)
|
2017-04-07 01:22:22 +03:00
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
// define an uploader which uploads testDirFiles with some data
|
|
|
|
data := []byte("some-data")
|
|
|
|
uploader := UploaderFunc(func(upload UploadFn) error {
|
|
|
|
for _, name := range testDirFiles {
|
|
|
|
file := &File{
|
|
|
|
ReadCloser: ioutil.NopCloser(bytes.NewReader(data)),
|
|
|
|
ManifestEntry: api.ManifestEntry{
|
|
|
|
Path: name,
|
|
|
|
ContentType: "text/plain",
|
|
|
|
Size: int64(len(data)),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := upload(file); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
// upload the files as a multipart upload
|
|
|
|
client := NewClient(srv.URL)
|
|
|
|
hash, err := client.MultipartUpload("", uploader)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check we can download the individual files
|
|
|
|
checkDownloadFile := func(path string) {
|
|
|
|
file, err := client.Download(hash, path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
gotData, err := ioutil.ReadAll(file)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(gotData, data) {
|
|
|
|
t.Fatalf("expected data to be %q, got %q", data, gotData)
|
2017-04-05 01:20:07 +03:00
|
|
|
}
|
|
|
|
}
|
2017-04-07 01:22:22 +03:00
|
|
|
for _, file := range testDirFiles {
|
|
|
|
checkDownloadFile(file)
|
|
|
|
}
|
2017-04-05 01:20:07 +03:00
|
|
|
}
|