From 3e6e03a7b3b294eab471056a0dc54e6c2a8be838 Mon Sep 17 00:00:00 2001 From: Janos Guljas Date: Tue, 5 Mar 2019 14:16:02 +0100 Subject: [PATCH] swarm/storage/localstore: add TestExportImport and fix Export function --- swarm/storage/localstore/export.go | 8 +- swarm/storage/localstore/export_test.go | 78 +++++++++++++++++++ .../localstore/subscription_pull_test.go | 2 +- 3 files changed, 83 insertions(+), 5 deletions(-) create mode 100644 swarm/storage/localstore/export_test.go diff --git a/swarm/storage/localstore/export.go b/swarm/storage/localstore/export.go index 1cf78edb2b..f097294910 100644 --- a/swarm/storage/localstore/export.go +++ b/swarm/storage/localstore/export.go @@ -1,4 +1,4 @@ -// Copyright 2018 The go-ethereum Authors +// Copyright 2019 The go-ethereum Authors // This file is part of the go-ethereum library. // // The go-ethereum library is free software: you can redistribute it and/or modify @@ -45,7 +45,7 @@ func (db *DB) Export(w io.Writer) (count int64, err error) { return false, err } count++ - return true, nil + return false, nil }, nil) return count, err @@ -71,7 +71,7 @@ func (db *DB) Import(r io.Reader) (count int64, err error) { } } - if len(hdr.Name) != 64 || len(hdr.Name) != 128 { + if len(hdr.Name) != 64 && len(hdr.Name) != 128 { log.Warn("ignoring non-chunk file", "name", hdr.Name) continue } @@ -90,7 +90,7 @@ func (db *DB) Import(r io.Reader) (count int64, err error) { } } key := chunk.Address(keybytes) - ch := chunk.NewChunk(key, data[32:]) + ch := chunk.NewChunk(key, data) go func() { select { diff --git a/swarm/storage/localstore/export_test.go b/swarm/storage/localstore/export_test.go new file mode 100644 index 0000000000..d2c653b723 --- /dev/null +++ b/swarm/storage/localstore/export_test.go @@ -0,0 +1,78 @@ +// Copyright 2019 The go-ethereum Authors +// 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 . + +package localstore + +import ( + "bytes" + "testing" + + "github.com/ethereum/go-ethereum/swarm/chunk" +) + +func TestExportImport(t *testing.T) { + db1, cleanup1 := newTestDB(t, nil) + defer cleanup1() + + var chunkCount = 100 + + chunks := make(map[string][]byte, chunkCount) + for i := 0; i < chunkCount; i++ { + ch := generateTestRandomChunk() + + err := db1.NewPutter(chunk.ModePutUpload).Put(ch) + if err != nil { + t.Fatal(err) + } + chunks[string(ch.Address())] = ch.Data() + } + + var buf bytes.Buffer + + c, err := db1.Export(&buf) + if err != nil { + t.Fatal(err) + } + wantChunksCount := int64(len(chunks)) + if c != wantChunksCount { + t.Errorf("got export count %v, want %v", c, wantChunksCount) + } + + db2, cleanup2 := newTestDB(t, nil) + defer cleanup2() + + c, err = db2.Import(&buf) + if err != nil { + t.Fatal(err) + } + if c != wantChunksCount { + t.Errorf("got import count %v, want %v", c, wantChunksCount) + } + + getter := db2.NewGetter(chunk.ModeGetRequest) + + for a, want := range chunks { + addr := chunk.Address([]byte(a)) + ch, err := getter.Get(addr) + if err != nil { + t.Fatal(err) + } + got := ch.Data() + if !bytes.Equal(got, want) { + t.Fatalf("chunk %s: got data %x, want %x", addr.Hex(), got, want) + } + } +} diff --git a/swarm/storage/localstore/subscription_pull_test.go b/swarm/storage/localstore/subscription_pull_test.go index 1ef652ec4b..4c68f41296 100644 --- a/swarm/storage/localstore/subscription_pull_test.go +++ b/swarm/storage/localstore/subscription_pull_test.go @@ -493,7 +493,7 @@ func TestDB_LastPullSubscriptionChunk(t *testing.T) { db, cleanupFunc := newTestDB(t, nil) defer cleanupFunc() - uploader := db.NewPutter(ModePutUpload) + uploader := db.NewPutter(chunk.ModePutUpload) addrs := make(map[uint8][]chunk.Address)