github.com/oskarth/go-ethereum@v1.6.8-0.20191013093314-dac24a9d3494/cmd/swarm/export_test.go (about)

     1  // Copyright 2018 The go-ethereum Authors
     2  // This file is part of go-ethereum.
     3  //
     4  // go-ethereum is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU 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  // go-ethereum 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 General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU General Public License
    15  // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package main
    18  
    19  import (
    20  	"bytes"
    21  	"crypto/md5"
    22  	"crypto/rand"
    23  	"io"
    24  	"io/ioutil"
    25  	"net/http"
    26  	"os"
    27  	"runtime"
    28  	"strings"
    29  	"testing"
    30  
    31  	"github.com/ethereum/go-ethereum/swarm"
    32  )
    33  
    34  // TestCLISwarmExportImport perform the following test:
    35  // 1. runs swarm node
    36  // 2. uploads a random file
    37  // 3. runs an export of the local datastore
    38  // 4. runs a second swarm node
    39  // 5. imports the exported datastore
    40  // 6. fetches the uploaded random file from the second node
    41  func TestCLISwarmExportImport(t *testing.T) {
    42  	if runtime.GOOS == "windows" {
    43  		t.Skip()
    44  	}
    45  	cluster := newTestCluster(t, 1)
    46  
    47  	// generate random 10mb file
    48  	f, cleanup := generateRandomFile(t, 10000000)
    49  	defer cleanup()
    50  
    51  	// upload the file with 'swarm up' and expect a hash
    52  	up := runSwarm(t, "--bzzapi", cluster.Nodes[0].URL, "up", f.Name())
    53  	_, matches := up.ExpectRegexp(`[a-f\d]{64}`)
    54  	up.ExpectExit()
    55  	hash := matches[0]
    56  
    57  	var info swarm.Info
    58  	if err := cluster.Nodes[0].Client.Call(&info, "bzz_info"); err != nil {
    59  		t.Fatal(err)
    60  	}
    61  
    62  	cluster.Stop()
    63  	defer cluster.Cleanup()
    64  
    65  	// generate an export.tar
    66  	exportCmd := runSwarm(t, "db", "export", info.Path+"/chunks", info.Path+"/export.tar", strings.TrimPrefix(info.BzzKey, "0x"))
    67  	exportCmd.ExpectExit()
    68  
    69  	// start second cluster
    70  	cluster2 := newTestCluster(t, 1)
    71  
    72  	var info2 swarm.Info
    73  	if err := cluster2.Nodes[0].Client.Call(&info2, "bzz_info"); err != nil {
    74  		t.Fatal(err)
    75  	}
    76  
    77  	// stop second cluster, so that we close LevelDB
    78  	cluster2.Stop()
    79  	defer cluster2.Cleanup()
    80  
    81  	// import the export.tar
    82  	importCmd := runSwarm(t, "db", "import", info2.Path+"/chunks", info.Path+"/export.tar", strings.TrimPrefix(info2.BzzKey, "0x"))
    83  	importCmd.ExpectExit()
    84  
    85  	// spin second cluster back up
    86  	cluster2.StartExistingNodes(t, 1, strings.TrimPrefix(info2.BzzAccount, "0x"))
    87  
    88  	// try to fetch imported file
    89  	res, err := http.Get(cluster2.Nodes[0].URL + "/bzz:/" + hash)
    90  	if err != nil {
    91  		t.Fatal(err)
    92  	}
    93  
    94  	if res.StatusCode != 200 {
    95  		t.Fatalf("expected HTTP status %d, got %s", 200, res.Status)
    96  	}
    97  
    98  	// compare downloaded file with the generated random file
    99  	mustEqualFiles(t, f, res.Body)
   100  }
   101  
   102  func mustEqualFiles(t *testing.T, up io.Reader, down io.Reader) {
   103  	h := md5.New()
   104  	upLen, err := io.Copy(h, up)
   105  	if err != nil {
   106  		t.Fatal(err)
   107  	}
   108  	upHash := h.Sum(nil)
   109  	h.Reset()
   110  	downLen, err := io.Copy(h, down)
   111  	if err != nil {
   112  		t.Fatal(err)
   113  	}
   114  	downHash := h.Sum(nil)
   115  
   116  	if !bytes.Equal(upHash, downHash) || upLen != downLen {
   117  		t.Fatalf("downloaded imported file md5=%x (length %v) is not the same as the generated one mp5=%x (length %v)", downHash, downLen, upHash, upLen)
   118  	}
   119  }
   120  
   121  func generateRandomFile(t *testing.T, size int) (f *os.File, teardown func()) {
   122  	// create a tmp file
   123  	tmp, err := ioutil.TempFile("", "swarm-test")
   124  	if err != nil {
   125  		t.Fatal(err)
   126  	}
   127  
   128  	// callback for tmp file cleanup
   129  	teardown = func() {
   130  		tmp.Close()
   131  		os.Remove(tmp.Name())
   132  	}
   133  
   134  	// write 10mb random data to file
   135  	buf := make([]byte, 10000000)
   136  	_, err = rand.Read(buf)
   137  	if err != nil {
   138  		t.Fatal(err)
   139  	}
   140  	ioutil.WriteFile(tmp.Name(), buf, 0755)
   141  
   142  	return tmp, teardown
   143  }