github.com/shyftnetwork/go-empyrean@v1.8.3-0.20191127201940-fbfca9338f04/swarm/storage/filestore_test.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser 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  // The go-ethereum library 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 Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package storage
    18  
    19  import (
    20  	"bytes"
    21  	"context"
    22  	"io"
    23  	"io/ioutil"
    24  	"os"
    25  	"testing"
    26  
    27  	"github.com/ShyftNetwork/go-empyrean/swarm/testutil"
    28  )
    29  
    30  const testDataSize = 0x0001000
    31  
    32  func TestFileStorerandom(t *testing.T) {
    33  	testFileStoreRandom(false, t)
    34  	testFileStoreRandom(true, t)
    35  }
    36  
    37  func testFileStoreRandom(toEncrypt bool, t *testing.T) {
    38  	tdb, cleanup, err := newTestDbStore(false, false)
    39  	defer cleanup()
    40  	if err != nil {
    41  		t.Fatalf("init dbStore failed: %v", err)
    42  	}
    43  	db := tdb.LDBStore
    44  	db.setCapacity(50000)
    45  	memStore := NewMemStore(NewDefaultStoreParams(), db)
    46  	localStore := &LocalStore{
    47  		memStore: memStore,
    48  		DbStore:  db,
    49  	}
    50  
    51  	fileStore := NewFileStore(localStore, NewFileStoreParams())
    52  	defer os.RemoveAll("/tmp/bzz")
    53  
    54  	slice := testutil.RandomBytes(1, testDataSize)
    55  	ctx := context.TODO()
    56  	key, wait, err := fileStore.Store(ctx, bytes.NewReader(slice), testDataSize, toEncrypt)
    57  	if err != nil {
    58  		t.Fatalf("Store error: %v", err)
    59  	}
    60  	err = wait(ctx)
    61  	if err != nil {
    62  		t.Fatalf("Store waitt error: %v", err.Error())
    63  	}
    64  	resultReader, isEncrypted := fileStore.Retrieve(context.TODO(), key)
    65  	if isEncrypted != toEncrypt {
    66  		t.Fatalf("isEncrypted expected %v got %v", toEncrypt, isEncrypted)
    67  	}
    68  	resultSlice := make([]byte, testDataSize)
    69  	n, err := resultReader.ReadAt(resultSlice, 0)
    70  	if err != io.EOF {
    71  		t.Fatalf("Retrieve error: %v", err)
    72  	}
    73  	if n != testDataSize {
    74  		t.Fatalf("Slice size error got %d, expected %d.", n, testDataSize)
    75  	}
    76  	if !bytes.Equal(slice, resultSlice) {
    77  		t.Fatalf("Comparison error.")
    78  	}
    79  	ioutil.WriteFile("/tmp/slice.bzz.16M", slice, 0666)
    80  	ioutil.WriteFile("/tmp/result.bzz.16M", resultSlice, 0666)
    81  	localStore.memStore = NewMemStore(NewDefaultStoreParams(), db)
    82  	resultReader, isEncrypted = fileStore.Retrieve(context.TODO(), key)
    83  	if isEncrypted != toEncrypt {
    84  		t.Fatalf("isEncrypted expected %v got %v", toEncrypt, isEncrypted)
    85  	}
    86  	for i := range resultSlice {
    87  		resultSlice[i] = 0
    88  	}
    89  	n, err = resultReader.ReadAt(resultSlice, 0)
    90  	if err != io.EOF {
    91  		t.Fatalf("Retrieve error after removing memStore: %v", err)
    92  	}
    93  	if n != len(slice) {
    94  		t.Fatalf("Slice size error after removing memStore got %d, expected %d.", n, len(slice))
    95  	}
    96  	if !bytes.Equal(slice, resultSlice) {
    97  		t.Fatalf("Comparison error after removing memStore.")
    98  	}
    99  }
   100  
   101  func TestFileStoreCapacity(t *testing.T) {
   102  	testFileStoreCapacity(false, t)
   103  	testFileStoreCapacity(true, t)
   104  }
   105  
   106  func testFileStoreCapacity(toEncrypt bool, t *testing.T) {
   107  	tdb, cleanup, err := newTestDbStore(false, false)
   108  	defer cleanup()
   109  	if err != nil {
   110  		t.Fatalf("init dbStore failed: %v", err)
   111  	}
   112  	db := tdb.LDBStore
   113  	memStore := NewMemStore(NewDefaultStoreParams(), db)
   114  	localStore := &LocalStore{
   115  		memStore: memStore,
   116  		DbStore:  db,
   117  	}
   118  	fileStore := NewFileStore(localStore, NewFileStoreParams())
   119  	slice := testutil.RandomBytes(1, testDataSize)
   120  	ctx := context.TODO()
   121  	key, wait, err := fileStore.Store(ctx, bytes.NewReader(slice), testDataSize, toEncrypt)
   122  	if err != nil {
   123  		t.Errorf("Store error: %v", err)
   124  	}
   125  	err = wait(ctx)
   126  	if err != nil {
   127  		t.Fatalf("Store error: %v", err)
   128  	}
   129  	resultReader, isEncrypted := fileStore.Retrieve(context.TODO(), key)
   130  	if isEncrypted != toEncrypt {
   131  		t.Fatalf("isEncrypted expected %v got %v", toEncrypt, isEncrypted)
   132  	}
   133  	resultSlice := make([]byte, len(slice))
   134  	n, err := resultReader.ReadAt(resultSlice, 0)
   135  	if err != io.EOF {
   136  		t.Fatalf("Retrieve error: %v", err)
   137  	}
   138  	if n != len(slice) {
   139  		t.Fatalf("Slice size error got %d, expected %d.", n, len(slice))
   140  	}
   141  	if !bytes.Equal(slice, resultSlice) {
   142  		t.Fatalf("Comparison error.")
   143  	}
   144  	// Clear memStore
   145  	memStore.setCapacity(0)
   146  	// check whether it is, indeed, empty
   147  	fileStore.ChunkStore = memStore
   148  	resultReader, isEncrypted = fileStore.Retrieve(context.TODO(), key)
   149  	if isEncrypted != toEncrypt {
   150  		t.Fatalf("isEncrypted expected %v got %v", toEncrypt, isEncrypted)
   151  	}
   152  	if _, err = resultReader.ReadAt(resultSlice, 0); err == nil {
   153  		t.Fatalf("Was able to read %d bytes from an empty memStore.", len(slice))
   154  	}
   155  	// check how it works with localStore
   156  	fileStore.ChunkStore = localStore
   157  	//	localStore.dbStore.setCapacity(0)
   158  	resultReader, isEncrypted = fileStore.Retrieve(context.TODO(), key)
   159  	if isEncrypted != toEncrypt {
   160  		t.Fatalf("isEncrypted expected %v got %v", toEncrypt, isEncrypted)
   161  	}
   162  	for i := range resultSlice {
   163  		resultSlice[i] = 0
   164  	}
   165  	n, err = resultReader.ReadAt(resultSlice, 0)
   166  	if err != io.EOF {
   167  		t.Fatalf("Retrieve error after clearing memStore: %v", err)
   168  	}
   169  	if n != len(slice) {
   170  		t.Fatalf("Slice size error after clearing memStore got %d, expected %d.", n, len(slice))
   171  	}
   172  	if !bytes.Equal(slice, resultSlice) {
   173  		t.Fatalf("Comparison error after clearing memStore.")
   174  	}
   175  }