github.com/codingfuture/orig-energi3@v0.8.4/swarm/storage/filestore_test.go (about)

     1  // Copyright 2018 The Energi Core Authors
     2  // Copyright 2018 The go-ethereum Authors
     3  // This file is part of the Energi Core library.
     4  //
     5  // The Energi Core library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The Energi Core library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the Energi Core library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package storage
    19  
    20  import (
    21  	"bytes"
    22  	"context"
    23  	"io"
    24  	"io/ioutil"
    25  	"os"
    26  	"testing"
    27  
    28  	"github.com/ethereum/go-ethereum/swarm/testutil"
    29  )
    30  
    31  const testDataSize = 0x0001000
    32  
    33  func TestFileStorerandom(t *testing.T) {
    34  	testFileStoreRandom(false, t)
    35  	testFileStoreRandom(true, t)
    36  }
    37  
    38  func testFileStoreRandom(toEncrypt bool, t *testing.T) {
    39  	tdb, cleanup, err := newTestDbStore(false, false)
    40  	defer cleanup()
    41  	if err != nil {
    42  		t.Fatalf("init dbStore failed: %v", err)
    43  	}
    44  	db := tdb.LDBStore
    45  	db.setCapacity(50000)
    46  	memStore := NewMemStore(NewDefaultStoreParams(), db)
    47  	localStore := &LocalStore{
    48  		memStore: memStore,
    49  		DbStore:  db,
    50  	}
    51  
    52  	fileStore := NewFileStore(localStore, NewFileStoreParams())
    53  	defer os.RemoveAll("/tmp/bzz")
    54  
    55  	slice := testutil.RandomBytes(1, testDataSize)
    56  	ctx := context.TODO()
    57  	key, wait, err := fileStore.Store(ctx, bytes.NewReader(slice), testDataSize, toEncrypt)
    58  	if err != nil {
    59  		t.Fatalf("Store error: %v", err)
    60  	}
    61  	err = wait(ctx)
    62  	if err != nil {
    63  		t.Fatalf("Store waitt error: %v", err.Error())
    64  	}
    65  	resultReader, isEncrypted := fileStore.Retrieve(context.TODO(), key)
    66  	if isEncrypted != toEncrypt {
    67  		t.Fatalf("isEncrypted expected %v got %v", toEncrypt, isEncrypted)
    68  	}
    69  	resultSlice := make([]byte, testDataSize)
    70  	n, err := resultReader.ReadAt(resultSlice, 0)
    71  	if err != io.EOF {
    72  		t.Fatalf("Retrieve error: %v", err)
    73  	}
    74  	if n != testDataSize {
    75  		t.Fatalf("Slice size error got %d, expected %d.", n, testDataSize)
    76  	}
    77  	if !bytes.Equal(slice, resultSlice) {
    78  		t.Fatalf("Comparison error.")
    79  	}
    80  	ioutil.WriteFile("/tmp/slice.bzz.16M", slice, 0666)
    81  	ioutil.WriteFile("/tmp/result.bzz.16M", resultSlice, 0666)
    82  	localStore.memStore = NewMemStore(NewDefaultStoreParams(), db)
    83  	resultReader, isEncrypted = fileStore.Retrieve(context.TODO(), key)
    84  	if isEncrypted != toEncrypt {
    85  		t.Fatalf("isEncrypted expected %v got %v", toEncrypt, isEncrypted)
    86  	}
    87  	for i := range resultSlice {
    88  		resultSlice[i] = 0
    89  	}
    90  	n, err = resultReader.ReadAt(resultSlice, 0)
    91  	if err != io.EOF {
    92  		t.Fatalf("Retrieve error after removing memStore: %v", err)
    93  	}
    94  	if n != len(slice) {
    95  		t.Fatalf("Slice size error after removing memStore got %d, expected %d.", n, len(slice))
    96  	}
    97  	if !bytes.Equal(slice, resultSlice) {
    98  		t.Fatalf("Comparison error after removing memStore.")
    99  	}
   100  }
   101  
   102  func TestFileStoreCapacity(t *testing.T) {
   103  	testFileStoreCapacity(false, t)
   104  	testFileStoreCapacity(true, t)
   105  }
   106  
   107  func testFileStoreCapacity(toEncrypt bool, t *testing.T) {
   108  	tdb, cleanup, err := newTestDbStore(false, false)
   109  	defer cleanup()
   110  	if err != nil {
   111  		t.Fatalf("init dbStore failed: %v", err)
   112  	}
   113  	db := tdb.LDBStore
   114  	memStore := NewMemStore(NewDefaultStoreParams(), db)
   115  	localStore := &LocalStore{
   116  		memStore: memStore,
   117  		DbStore:  db,
   118  	}
   119  	fileStore := NewFileStore(localStore, NewFileStoreParams())
   120  	slice := testutil.RandomBytes(1, testDataSize)
   121  	ctx := context.TODO()
   122  	key, wait, err := fileStore.Store(ctx, bytes.NewReader(slice), testDataSize, toEncrypt)
   123  	if err != nil {
   124  		t.Errorf("Store error: %v", err)
   125  	}
   126  	err = wait(ctx)
   127  	if err != nil {
   128  		t.Fatalf("Store error: %v", err)
   129  	}
   130  	resultReader, isEncrypted := fileStore.Retrieve(context.TODO(), key)
   131  	if isEncrypted != toEncrypt {
   132  		t.Fatalf("isEncrypted expected %v got %v", toEncrypt, isEncrypted)
   133  	}
   134  	resultSlice := make([]byte, len(slice))
   135  	n, err := resultReader.ReadAt(resultSlice, 0)
   136  	if err != io.EOF {
   137  		t.Fatalf("Retrieve error: %v", err)
   138  	}
   139  	if n != len(slice) {
   140  		t.Fatalf("Slice size error got %d, expected %d.", n, len(slice))
   141  	}
   142  	if !bytes.Equal(slice, resultSlice) {
   143  		t.Fatalf("Comparison error.")
   144  	}
   145  	// Clear memStore
   146  	memStore.setCapacity(0)
   147  	// check whether it is, indeed, empty
   148  	fileStore.ChunkStore = memStore
   149  	resultReader, isEncrypted = fileStore.Retrieve(context.TODO(), key)
   150  	if isEncrypted != toEncrypt {
   151  		t.Fatalf("isEncrypted expected %v got %v", toEncrypt, isEncrypted)
   152  	}
   153  	if _, err = resultReader.ReadAt(resultSlice, 0); err == nil {
   154  		t.Fatalf("Was able to read %d bytes from an empty memStore.", len(slice))
   155  	}
   156  	// check how it works with localStore
   157  	fileStore.ChunkStore = localStore
   158  	//	localStore.dbStore.setCapacity(0)
   159  	resultReader, isEncrypted = fileStore.Retrieve(context.TODO(), key)
   160  	if isEncrypted != toEncrypt {
   161  		t.Fatalf("isEncrypted expected %v got %v", toEncrypt, isEncrypted)
   162  	}
   163  	for i := range resultSlice {
   164  		resultSlice[i] = 0
   165  	}
   166  	n, err = resultReader.ReadAt(resultSlice, 0)
   167  	if err != io.EOF {
   168  		t.Fatalf("Retrieve error after clearing memStore: %v", err)
   169  	}
   170  	if n != len(slice) {
   171  		t.Fatalf("Slice size error after clearing memStore got %d, expected %d.", n, len(slice))
   172  	}
   173  	if !bytes.Equal(slice, resultSlice) {
   174  		t.Fatalf("Comparison error after clearing memStore.")
   175  	}
   176  }
   177  
   178  // TestGetAllReferences only tests that GetAllReferences returns an expected
   179  // number of references for a given file
   180  func TestGetAllReferences(t *testing.T) {
   181  	tdb, cleanup, err := newTestDbStore(false, false)
   182  	defer cleanup()
   183  	if err != nil {
   184  		t.Fatalf("init dbStore failed: %v", err)
   185  	}
   186  	db := tdb.LDBStore
   187  	memStore := NewMemStore(NewDefaultStoreParams(), db)
   188  	localStore := &LocalStore{
   189  		memStore: memStore,
   190  		DbStore:  db,
   191  	}
   192  	fileStore := NewFileStore(localStore, NewFileStoreParams())
   193  
   194  	// testRuns[i] and expectedLen[i] are dataSize and expected length respectively
   195  	testRuns := []int{1024, 8192, 16000, 30000, 1000000}
   196  	expectedLens := []int{1, 3, 5, 9, 248}
   197  	for i, r := range testRuns {
   198  		slice := testutil.RandomBytes(1, r)
   199  
   200  		addrs, err := fileStore.GetAllReferences(context.Background(), bytes.NewReader(slice), false)
   201  		if err != nil {
   202  			t.Fatal(err)
   203  		}
   204  		if len(addrs) != expectedLens[i] {
   205  			t.Fatalf("Expected reference array length to be %d, but is %d", expectedLens[i], len(addrs))
   206  		}
   207  	}
   208  }