github.com/trustbloc/kms-go@v1.1.2/kms/localkms/localkms_reader_test.go (about)

     1  /*
     2   Copyright SecureKey Technologies Inc. All Rights Reserved.
     3  
     4   SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package localkms
     8  
     9  import (
    10  	"fmt"
    11  	"io"
    12  	"testing"
    13  
    14  	"github.com/stretchr/testify/require"
    15  
    16  	"github.com/trustbloc/kms-go/kms"
    17  )
    18  
    19  func TestLocalKMSReader(t *testing.T) {
    20  	someKey := []byte("someKeyData")
    21  	someKeyID := "newKeyID"
    22  	storeData := map[string][]byte{
    23  		someKeyID: someKey,
    24  	}
    25  
    26  	t.Run("success case - create a valid storeReader with a non empty and stored keysetID", func(t *testing.T) {
    27  		localStore := &inMemoryKMSStore{keys: storeData}
    28  
    29  		l := newReader(localStore, someKeyID)
    30  		require.NotEmpty(t, l)
    31  		data := make([]byte, 512)
    32  		n, err := l.Read(data)
    33  		require.NoError(t, err)
    34  		require.Equal(t, len(someKey), n)
    35  
    36  		// try to read again - should return io.EOF
    37  		n, err = l.Read(data)
    38  		require.EqualError(t, err, io.EOF.Error())
    39  		require.Equal(t, n, 0)
    40  	})
    41  
    42  	t.Run("error case - create an invalid read with empty keysetID", func(t *testing.T) {
    43  		localStore := &inMemoryKMSStore{keys: storeData}
    44  
    45  		l := newReader(localStore, "")
    46  		require.NotEmpty(t, l)
    47  		data := make([]byte, 512)
    48  		n, err := l.Read(data)
    49  		require.EqualError(t, err, "keysetID is not set")
    50  		require.Equal(t, 0, n)
    51  	})
    52  
    53  	t.Run("error case - create an invalid read with non stored keyset", func(t *testing.T) {
    54  		mockStore := newInMemoryKMSStore()
    55  
    56  		l := newReader(mockStore, someKeyID)
    57  		require.NotEmpty(t, l)
    58  		data := make([]byte, 512)
    59  		n, err := l.Read(data)
    60  		require.EqualError(t, err,
    61  			fmt.Errorf("cannot read data for keysetID %s: %w", someKeyID, kms.ErrKeyNotFound).Error())
    62  		require.Equal(t, 0, n)
    63  	})
    64  
    65  	t.Run("success case - create a valid storeReader with very large keyset data", func(t *testing.T) {
    66  		var veryLargeData []byte
    67  		dataLen := 1000 * 1000
    68  		blockSize := 512
    69  		for i := 0; i < dataLen; i++ {
    70  			veryLargeData = append(veryLargeData, 'a')
    71  		}
    72  
    73  		mockStore := newInMemoryKMSStore()
    74  
    75  		mockStore.keys[someKeyID] = veryLargeData
    76  
    77  		l := newReader(mockStore, someKeyID)
    78  		require.NotEmpty(t, l)
    79  		data := make([]byte, blockSize)
    80  		bytesRead := 0
    81  		var readData []byte
    82  		for bytesRead < dataLen-blockSize {
    83  			n, err := l.Read(data)
    84  			require.NoError(t, err)
    85  			// data has capacity of 512 bytes (ie require multiple Read() calls to consume the whole keyset data)
    86  			require.Equal(t, blockSize, n)
    87  			bytesRead += n
    88  			readData = append(readData, data...)
    89  		}
    90  
    91  		// last read..
    92  		n, err := l.Read(data)
    93  		require.NoError(t, err)
    94  		// append remainder data read
    95  		readData = append(readData, data[:n]...)
    96  		// data has capacity of blockSize bytes, last read would be the remainder chunk of dataLen which is smaller
    97  		// than blockSize bytes
    98  		require.Equal(t, dataLen%blockSize, n)
    99  		require.Equal(t, len(readData), dataLen)
   100  
   101  		// try to read one more time, should return an error
   102  		n, err = l.Read(data)
   103  		require.EqualError(t, err, io.EOF.Error())
   104  		require.Equal(t, n, 0)
   105  	})
   106  }