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