github.com/iotexproject/iotex-core@v1.14.1-rc1/blockchain/filedao/filedao_header_test.go (about)

     1  // Copyright (c) 2020 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package filedao
     7  
     8  import (
     9  	"context"
    10  	"testing"
    11  
    12  	"github.com/pkg/errors"
    13  	"github.com/stretchr/testify/require"
    14  
    15  	"github.com/iotexproject/go-pkgs/hash"
    16  
    17  	"github.com/iotexproject/iotex-core/db"
    18  	"github.com/iotexproject/iotex-core/testutil"
    19  )
    20  
    21  func TestFileProto(t *testing.T) {
    22  	r := require.New(t)
    23  
    24  	h := &FileHeader{
    25  		Version:        FileV2,
    26  		Compressor:     "test",
    27  		BlockStoreSize: 32,
    28  		Start:          3,
    29  	}
    30  	ser, err := h.Serialize()
    31  	r.NoError(err)
    32  	h1, err := DeserializeFileHeader(ser)
    33  	r.NoError(err)
    34  	r.Equal(h, h1)
    35  
    36  	c := &FileTip{
    37  		Height: 1003,
    38  		Hash:   hash.Hash256{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
    39  	}
    40  	ser, err = c.Serialize()
    41  	r.NoError(err)
    42  	c1, err := DeserializeFileTip(ser)
    43  	r.NoError(err)
    44  	r.Equal(c, c1)
    45  }
    46  
    47  func TestFileReadWrite(t *testing.T) {
    48  	testHeader := func(kv db.KVStore, t *testing.T) {
    49  		r := require.New(t)
    50  		r.NotNil(kv)
    51  
    52  		ctx := context.Background()
    53  		r.NoError(kv.Start(ctx))
    54  		defer kv.Stop(ctx)
    55  
    56  		h, err := ReadHeaderV2(kv)
    57  		r.Equal(db.ErrNotExist, errors.Cause(err))
    58  		r.Nil(h)
    59  
    60  		h = &FileHeader{
    61  			Version:        FileV2,
    62  			Compressor:     "test",
    63  			BlockStoreSize: 32,
    64  			Start:          3,
    65  		}
    66  		r.NoError(WriteHeaderV2(kv, h))
    67  
    68  		h1, err := ReadHeaderV2(kv)
    69  		r.NoError(err)
    70  		r.Equal(h, h1)
    71  
    72  		c, err := ReadTip(kv, _headerDataNs, _topHeightKey)
    73  		r.Equal(db.ErrNotExist, errors.Cause(err))
    74  		r.Nil(c)
    75  
    76  		c = &FileTip{
    77  			Height: 1003,
    78  			Hash:   hash.Hash256{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
    79  		}
    80  		r.NoError(WriteTip(kv, _headerDataNs, _topHeightKey, c))
    81  
    82  		c1, err := ReadTip(kv, _headerDataNs, _topHeightKey)
    83  		r.NoError(err)
    84  		r.Equal(c, c1)
    85  	}
    86  
    87  	r := require.New(t)
    88  	testPath, err := testutil.PathOfTempFile("test-header")
    89  	r.NoError(err)
    90  	defer func() {
    91  		testutil.CleanupPath(testPath)
    92  	}()
    93  
    94  	cfg := db.DefaultConfig
    95  	cfg.DbPath = testPath
    96  	t.Run("test file header", func(t *testing.T) {
    97  		testHeader(db.NewBoltDB(cfg), t)
    98  	})
    99  }