github.com/koko1123/flow-go-1@v0.29.6/storage/badger/init_test.go (about)

     1  package badger_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/dgraph-io/badger/v3"
     7  	"github.com/stretchr/testify/require"
     8  
     9  	bstorage "github.com/koko1123/flow-go-1/storage/badger"
    10  	"github.com/koko1123/flow-go-1/storage/badger/operation"
    11  	"github.com/koko1123/flow-go-1/utils/unittest"
    12  )
    13  
    14  func TestInitPublic(t *testing.T) {
    15  	unittest.RunWithTypedBadgerDB(t, bstorage.InitPublic, func(db *badger.DB) {
    16  		err := operation.EnsurePublicDB(db)
    17  		require.NoError(t, err)
    18  		err = operation.EnsureSecretDB(db)
    19  		require.Error(t, err)
    20  	})
    21  }
    22  
    23  func TestInitSecret(t *testing.T) {
    24  	unittest.RunWithTypedBadgerDB(t, bstorage.InitSecret, func(db *badger.DB) {
    25  		err := operation.EnsureSecretDB(db)
    26  		require.NoError(t, err)
    27  		err = operation.EnsurePublicDB(db)
    28  		require.Error(t, err)
    29  	})
    30  }
    31  
    32  // opening a database which has previously been opened with encryption enabled,
    33  // using a different encryption key, should fail
    34  func TestEncryptionKeyMismatch(t *testing.T) {
    35  	unittest.RunWithTempDir(t, func(dir string) {
    36  
    37  		// open a database with encryption enabled
    38  		key1 := unittest.SeedFixture(32)
    39  		db := unittest.TypedBadgerDB(t, dir, func(options badger.Options) (*badger.DB, error) {
    40  			options = options.WithEncryptionKey(key1)
    41  			return badger.Open(options)
    42  		})
    43  		db.Close()
    44  
    45  		// open the same database with a different key
    46  		key2 := unittest.SeedFixture(32)
    47  		opts := badger.
    48  			DefaultOptions(dir).
    49  			WithKeepL0InMemory(true).
    50  			WithEncryptionKey(key2).
    51  			WithLogger(nil)
    52  		_, err := badger.Open(opts)
    53  		// opening the database should return an error
    54  		require.Error(t, err)
    55  	})
    56  }