github.com/FUSIONFoundation/efsn@v3.6.2-0.20200916075423-dbb5dd5d2cc7+incompatible/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  
    28  const testDataSize = 0x1000000
    29  
    30  func TestFileStorerandom(t *testing.T) {
    31  	testFileStoreRandom(false, t)
    32  	testFileStoreRandom(true, t)
    33  }
    34  
    35  func testFileStoreRandom(toEncrypt bool, t *testing.T) {
    36  	tdb, cleanup, err := newTestDbStore(false, false)
    37  	defer cleanup()
    38  	if err != nil {
    39  		t.Fatalf("init dbStore failed: %v", err)
    40  	}
    41  	db := tdb.LDBStore
    42  	db.setCapacity(50000)
    43  	memStore := NewMemStore(NewDefaultStoreParams(), db)
    44  	localStore := &LocalStore{
    45  		memStore: memStore,
    46  		DbStore:  db,
    47  	}
    48  
    49  	fileStore := NewFileStore(localStore, NewFileStoreParams())
    50  	defer os.RemoveAll("/tmp/bzz")
    51  
    52  	reader, slice := GenerateRandomData(testDataSize)
    53  	ctx := context.TODO()
    54  	key, wait, err := fileStore.Store(ctx, reader, testDataSize, toEncrypt)
    55  	if err != nil {
    56  		t.Fatalf("Store error: %v", err)
    57  	}
    58  	err = wait(ctx)
    59  	if err != nil {
    60  		t.Fatalf("Store waitt error: %v", err.Error())
    61  	}
    62  	resultReader, isEncrypted := fileStore.Retrieve(context.TODO(), key)
    63  	if isEncrypted != toEncrypt {
    64  		t.Fatalf("isEncrypted expected %v got %v", toEncrypt, isEncrypted)
    65  	}
    66  	resultSlice := make([]byte, len(slice))
    67  	n, err := resultReader.ReadAt(resultSlice, 0)
    68  	if err != io.EOF {
    69  		t.Fatalf("Retrieve error: %v", err)
    70  	}
    71  	if n != len(slice) {
    72  		t.Fatalf("Slice size error got %d, expected %d.", n, len(slice))
    73  	}
    74  	if !bytes.Equal(slice, resultSlice) {
    75  		t.Fatalf("Comparison error.")
    76  	}
    77  	ioutil.WriteFile("/tmp/slice.bzz.16M", slice, 0666)
    78  	ioutil.WriteFile("/tmp/result.bzz.16M", resultSlice, 0666)
    79  	localStore.memStore = NewMemStore(NewDefaultStoreParams(), db)
    80  	resultReader, isEncrypted = fileStore.Retrieve(context.TODO(), key)
    81  	if isEncrypted != toEncrypt {
    82  		t.Fatalf("isEncrypted expected %v got %v", toEncrypt, isEncrypted)
    83  	}
    84  	for i := range resultSlice {
    85  		resultSlice[i] = 0
    86  	}
    87  	n, err = resultReader.ReadAt(resultSlice, 0)
    88  	if err != io.EOF {
    89  		t.Fatalf("Retrieve error after removing memStore: %v", err)
    90  	}
    91  	if n != len(slice) {
    92  		t.Fatalf("Slice size error after removing memStore got %d, expected %d.", n, len(slice))
    93  	}
    94  	if !bytes.Equal(slice, resultSlice) {
    95  		t.Fatalf("Comparison error after removing memStore.")
    96  	}
    97  }
    98  
    99  func TestFileStoreCapacity(t *testing.T) {
   100  	testFileStoreCapacity(false, t)
   101  	testFileStoreCapacity(true, t)
   102  }
   103  
   104  func testFileStoreCapacity(toEncrypt bool, t *testing.T) {
   105  	tdb, cleanup, err := newTestDbStore(false, false)
   106  	defer cleanup()
   107  	if err != nil {
   108  		t.Fatalf("init dbStore failed: %v", err)
   109  	}
   110  	db := tdb.LDBStore
   111  	memStore := NewMemStore(NewDefaultStoreParams(), db)
   112  	localStore := &LocalStore{
   113  		memStore: memStore,
   114  		DbStore:  db,
   115  	}
   116  	fileStore := NewFileStore(localStore, NewFileStoreParams())
   117  	reader, slice := GenerateRandomData(testDataSize)
   118  	ctx := context.TODO()
   119  	key, wait, err := fileStore.Store(ctx, reader, testDataSize, toEncrypt)
   120  	if err != nil {
   121  		t.Errorf("Store error: %v", err)
   122  	}
   123  	err = wait(ctx)
   124  	if err != nil {
   125  		t.Fatalf("Store error: %v", err)
   126  	}
   127  	resultReader, isEncrypted := fileStore.Retrieve(context.TODO(), key)
   128  	if isEncrypted != toEncrypt {
   129  		t.Fatalf("isEncrypted expected %v got %v", toEncrypt, isEncrypted)
   130  	}
   131  	resultSlice := make([]byte, len(slice))
   132  	n, err := resultReader.ReadAt(resultSlice, 0)
   133  	if err != io.EOF {
   134  		t.Fatalf("Retrieve error: %v", err)
   135  	}
   136  	if n != len(slice) {
   137  		t.Fatalf("Slice size error got %d, expected %d.", n, len(slice))
   138  	}
   139  	if !bytes.Equal(slice, resultSlice) {
   140  		t.Fatalf("Comparison error.")
   141  	}
   142  	// Clear memStore
   143  	memStore.setCapacity(0)
   144  	// check whether it is, indeed, empty
   145  	fileStore.ChunkStore = memStore
   146  	resultReader, isEncrypted = fileStore.Retrieve(context.TODO(), key)
   147  	if isEncrypted != toEncrypt {
   148  		t.Fatalf("isEncrypted expected %v got %v", toEncrypt, isEncrypted)
   149  	}
   150  	if _, err = resultReader.ReadAt(resultSlice, 0); err == nil {
   151  		t.Fatalf("Was able to read %d bytes from an empty memStore.", len(slice))
   152  	}
   153  	// check how it works with localStore
   154  	fileStore.ChunkStore = localStore
   155  	//	localStore.dbStore.setCapacity(0)
   156  	resultReader, isEncrypted = fileStore.Retrieve(context.TODO(), key)
   157  	if isEncrypted != toEncrypt {
   158  		t.Fatalf("isEncrypted expected %v got %v", toEncrypt, isEncrypted)
   159  	}
   160  	for i := range resultSlice {
   161  		resultSlice[i] = 0
   162  	}
   163  	n, err = resultReader.ReadAt(resultSlice, 0)
   164  	if err != io.EOF {
   165  		t.Fatalf("Retrieve error after clearing memStore: %v", err)
   166  	}
   167  	if n != len(slice) {
   168  		t.Fatalf("Slice size error after clearing memStore got %d, expected %d.", n, len(slice))
   169  	}
   170  	if !bytes.Equal(slice, resultSlice) {
   171  		t.Fatalf("Comparison error after clearing memStore.")
   172  	}
   173  }