swarm/storage/localstore: add TestExportImport and fix Export function

This commit is contained in:
Janos Guljas 2019-03-05 14:16:02 +01:00
parent 44633ce2f7
commit 3e6e03a7b3
3 changed files with 83 additions and 5 deletions

View file

@ -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 {

View file

@ -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 <http://www.gnu.org/licenses/>.
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)
}
}
}

View file

@ -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)