github.com/alexdevranger/node-1.8.27@v0.0.0-20221128213301-aa5841e41d2d/cmd/swarm/export_test.go (about)

     1  // Copyright 2018 The go-ethereum Authors
     2  // This file is part of go-dubxcoin.
     3  //
     4  // go-dubxcoin 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  	"io"
    23  	"net/http"
    24  	"os"
    25  	"runtime"
    26  	"strings"
    27  	"testing"
    28  
    29  	"github.com/alexdevranger/node-1.8.27/swarm"
    30  	"github.com/alexdevranger/node-1.8.27/swarm/testutil"
    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  	if runtime.GOOS == "windows" {
    42  		t.Skip()
    43  	}
    44  	cluster := newTestCluster(t, 1)
    45  
    46  	// generate random 1mb file
    47  	content := testutil.RandomBytes(1, 1000000)
    48  	fileName := testutil.TempFileWithContent(t, string(content))
    49  	defer os.Remove(fileName)
    50  
    51  	// upload the file with 'swarm up' and expect a hash
    52  	up := runSwarm(t, "--bzzapi", cluster.Nodes[0].URL, "up", fileName)
    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, bytes.NewReader(content), 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  }