go-pulse/swarm/storage/hasherstore_test.go
Anton Evangelatov b3711af051 swarm: ctx propagation; bmt fixes; pss generic notification framework (#17150)
* cmd/swarm: minor cli flag text adjustments

* swarm/api/http: sticky footer for swarm landing page using flex

* swarm/api/http: sticky footer for error pages and fix for multiple choices

* cmd/swarm, swarm/storage, swarm: fix  mingw on windows test issues

* cmd/swarm: update description of swarm cmd

* swarm: added network ID test

* cmd/swarm: support for smoke tests on the production swarm cluster

* cmd/swarm/swarm-smoke: simplify cluster logic as per suggestion

* swarm: propagate ctx to internal apis (#754)

* swarm/metrics: collect disk measurements

* swarm/bmt: fix io.Writer interface

  * Write now tolerates arbitrary variable buffers
  * added variable buffer tests
  * Write loop and finalise optimisation
  * refactor / rename
  * add tests for empty input

* swarm/pss: (UPDATE) Generic notifications package (#744)

swarm/pss: Generic package for creating pss notification svcs

* swarm: Adding context to more functions

* swarm/api: change colour of landing page in templates

* swarm/api: change landing page to react to enter keypress
2018-07-09 14:11:49 +02:00

123 lines
3.8 KiB
Go

// Copyright 2018 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 storage
import (
"bytes"
"context"
"testing"
"github.com/ethereum/go-ethereum/swarm/storage/encryption"
"github.com/ethereum/go-ethereum/common"
)
func TestHasherStore(t *testing.T) {
var tests = []struct {
chunkLength int
toEncrypt bool
}{
{10, false},
{100, false},
{1000, false},
{4096, false},
{10, true},
{100, true},
{1000, true},
{4096, true},
}
for _, tt := range tests {
chunkStore := NewMapChunkStore()
hasherStore := NewHasherStore(chunkStore, MakeHashFunc(DefaultHash), tt.toEncrypt)
// Put two random chunks into the hasherStore
chunkData1 := GenerateRandomChunk(int64(tt.chunkLength)).SData
key1, err := hasherStore.Put(chunkData1)
if err != nil {
t.Fatalf("Expected no error got \"%v\"", err)
}
chunkData2 := GenerateRandomChunk(int64(tt.chunkLength)).SData
key2, err := hasherStore.Put(chunkData2)
if err != nil {
t.Fatalf("Expected no error got \"%v\"", err)
}
hasherStore.Close()
// Wait until chunks are really stored
err = hasherStore.Wait(context.TODO())
if err != nil {
t.Fatalf("Expected no error got \"%v\"", err)
}
// Get the first chunk
retrievedChunkData1, err := hasherStore.Get(key1)
if err != nil {
t.Fatalf("Expected no error, got \"%v\"", err)
}
// Retrieved data should be same as the original
if !bytes.Equal(chunkData1, retrievedChunkData1) {
t.Fatalf("Expected retrieved chunk data %v, got %v", common.Bytes2Hex(chunkData1), common.Bytes2Hex(retrievedChunkData1))
}
// Get the second chunk
retrievedChunkData2, err := hasherStore.Get(key2)
if err != nil {
t.Fatalf("Expected no error, got \"%v\"", err)
}
// Retrieved data should be same as the original
if !bytes.Equal(chunkData2, retrievedChunkData2) {
t.Fatalf("Expected retrieved chunk data %v, got %v", common.Bytes2Hex(chunkData2), common.Bytes2Hex(retrievedChunkData2))
}
hash1, encryptionKey1, err := parseReference(key1, hasherStore.hashSize)
if err != nil {
t.Fatalf("Expected no error, got \"%v\"", err)
}
if tt.toEncrypt {
if encryptionKey1 == nil {
t.Fatal("Expected non-nil encryption key, got nil")
} else if len(encryptionKey1) != encryption.KeyLength {
t.Fatalf("Expected encryption key length %v, got %v", encryption.KeyLength, len(encryptionKey1))
}
}
if !tt.toEncrypt && encryptionKey1 != nil {
t.Fatalf("Expected nil encryption key, got key with length %v", len(encryptionKey1))
}
// Check if chunk data in store is encrypted or not
chunkInStore, err := chunkStore.Get(hash1)
if err != nil {
t.Fatalf("Expected no error got \"%v\"", err)
}
chunkDataInStore := chunkInStore.SData
if tt.toEncrypt && bytes.Equal(chunkData1, chunkDataInStore) {
t.Fatalf("Chunk expected to be encrypted but it is stored without encryption")
}
if !tt.toEncrypt && !bytes.Equal(chunkData1, chunkDataInStore) {
t.Fatalf("Chunk expected to be not encrypted but stored content is different. Expected %v got %v", common.Bytes2Hex(chunkData1), common.Bytes2Hex(chunkDataInStore))
}
}
}