github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/swarm/storage/filestore_test.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 12:09:49</date>
    10  //</624342681077682176>
    11  
    12  //
    13  //
    14  //
    15  //
    16  //
    17  //
    18  //
    19  //
    20  //
    21  //
    22  //
    23  //
    24  //
    25  //
    26  //
    27  
    28  package storage
    29  
    30  import (
    31  	"bytes"
    32  	"context"
    33  	"io"
    34  	"io/ioutil"
    35  	"os"
    36  	"testing"
    37  )
    38  
    39  const testDataSize = 0x1000000
    40  
    41  func TestFileStorerandom(t *testing.T) {
    42  	testFileStoreRandom(false, t)
    43  	testFileStoreRandom(true, t)
    44  }
    45  
    46  func testFileStoreRandom(toEncrypt bool, t *testing.T) {
    47  	tdb, cleanup, err := newTestDbStore(false, false)
    48  	defer cleanup()
    49  	if err != nil {
    50  		t.Fatalf("init dbStore failed: %v", err)
    51  	}
    52  	db := tdb.LDBStore
    53  	db.setCapacity(50000)
    54  	memStore := NewMemStore(NewDefaultStoreParams(), db)
    55  	localStore := &LocalStore{
    56  		memStore: memStore,
    57  		DbStore:  db,
    58  	}
    59  
    60  	fileStore := NewFileStore(localStore, NewFileStoreParams())
    61  	defer os.RemoveAll("/tmp/bzz")
    62  
    63  	reader, slice := generateRandomData(testDataSize)
    64  	ctx := context.TODO()
    65  	key, wait, err := fileStore.Store(ctx, reader, testDataSize, toEncrypt)
    66  	if err != nil {
    67  		t.Errorf("Store error: %v", err)
    68  	}
    69  	err = wait(ctx)
    70  	if err != nil {
    71  		t.Fatalf("Store waitt error: %v", err.Error())
    72  	}
    73  	resultReader, isEncrypted := fileStore.Retrieve(context.TODO(), key)
    74  	if isEncrypted != toEncrypt {
    75  		t.Fatalf("isEncrypted expected %v got %v", toEncrypt, isEncrypted)
    76  	}
    77  	resultSlice := make([]byte, len(slice))
    78  	n, err := resultReader.ReadAt(resultSlice, 0)
    79  	if err != io.EOF {
    80  		t.Errorf("Retrieve error: %v", err)
    81  	}
    82  	if n != len(slice) {
    83  		t.Errorf("Slice size error got %d, expected %d.", n, len(slice))
    84  	}
    85  	if !bytes.Equal(slice, resultSlice) {
    86  		t.Errorf("Comparison error.")
    87  	}
    88  	ioutil.WriteFile("/tmp/slice.bzz.16M", slice, 0666)
    89  	ioutil.WriteFile("/tmp/result.bzz.16M", resultSlice, 0666)
    90  	localStore.memStore = NewMemStore(NewDefaultStoreParams(), db)
    91  	resultReader, isEncrypted = fileStore.Retrieve(context.TODO(), key)
    92  	if isEncrypted != toEncrypt {
    93  		t.Fatalf("isEncrypted expected %v got %v", toEncrypt, isEncrypted)
    94  	}
    95  	for i := range resultSlice {
    96  		resultSlice[i] = 0
    97  	}
    98  	n, err = resultReader.ReadAt(resultSlice, 0)
    99  	if err != io.EOF {
   100  		t.Errorf("Retrieve error after removing memStore: %v", err)
   101  	}
   102  	if n != len(slice) {
   103  		t.Errorf("Slice size error after removing memStore got %d, expected %d.", n, len(slice))
   104  	}
   105  	if !bytes.Equal(slice, resultSlice) {
   106  		t.Errorf("Comparison error after removing memStore.")
   107  	}
   108  }
   109  
   110  func TestFileStoreCapacity(t *testing.T) {
   111  	testFileStoreCapacity(false, t)
   112  	testFileStoreCapacity(true, t)
   113  }
   114  
   115  func testFileStoreCapacity(toEncrypt bool, t *testing.T) {
   116  	tdb, cleanup, err := newTestDbStore(false, false)
   117  	defer cleanup()
   118  	if err != nil {
   119  		t.Fatalf("init dbStore failed: %v", err)
   120  	}
   121  	db := tdb.LDBStore
   122  	memStore := NewMemStore(NewDefaultStoreParams(), db)
   123  	localStore := &LocalStore{
   124  		memStore: memStore,
   125  		DbStore:  db,
   126  	}
   127  	fileStore := NewFileStore(localStore, NewFileStoreParams())
   128  	reader, slice := generateRandomData(testDataSize)
   129  	ctx := context.TODO()
   130  	key, wait, err := fileStore.Store(ctx, reader, testDataSize, toEncrypt)
   131  	if err != nil {
   132  		t.Errorf("Store error: %v", err)
   133  	}
   134  	err = wait(ctx)
   135  	if err != nil {
   136  		t.Errorf("Store error: %v", err)
   137  	}
   138  	resultReader, isEncrypted := fileStore.Retrieve(context.TODO(), key)
   139  	if isEncrypted != toEncrypt {
   140  		t.Fatalf("isEncrypted expected %v got %v", toEncrypt, isEncrypted)
   141  	}
   142  	resultSlice := make([]byte, len(slice))
   143  	n, err := resultReader.ReadAt(resultSlice, 0)
   144  	if err != io.EOF {
   145  		t.Errorf("Retrieve error: %v", err)
   146  	}
   147  	if n != len(slice) {
   148  		t.Errorf("Slice size error got %d, expected %d.", n, len(slice))
   149  	}
   150  	if !bytes.Equal(slice, resultSlice) {
   151  		t.Errorf("Comparison error.")
   152  	}
   153  //
   154  	memStore.setCapacity(0)
   155  //
   156  	fileStore.ChunkStore = memStore
   157  	resultReader, isEncrypted = fileStore.Retrieve(context.TODO(), key)
   158  	if isEncrypted != toEncrypt {
   159  		t.Fatalf("isEncrypted expected %v got %v", toEncrypt, isEncrypted)
   160  	}
   161  	if _, err = resultReader.ReadAt(resultSlice, 0); err == nil {
   162  		t.Errorf("Was able to read %d bytes from an empty memStore.", len(slice))
   163  	}
   164  //
   165  	fileStore.ChunkStore = localStore
   166  //
   167  	resultReader, isEncrypted = fileStore.Retrieve(context.TODO(), key)
   168  	if isEncrypted != toEncrypt {
   169  		t.Fatalf("isEncrypted expected %v got %v", toEncrypt, isEncrypted)
   170  	}
   171  	for i := range resultSlice {
   172  		resultSlice[i] = 0
   173  	}
   174  	n, err = resultReader.ReadAt(resultSlice, 0)
   175  	if err != io.EOF {
   176  		t.Errorf("Retrieve error after clearing memStore: %v", err)
   177  	}
   178  	if n != len(slice) {
   179  		t.Errorf("Slice size error after clearing memStore got %d, expected %d.", n, len(slice))
   180  	}
   181  	if !bytes.Equal(slice, resultSlice) {
   182  		t.Errorf("Comparison error after clearing memStore.")
   183  	}
   184  }
   185