github.com/blend/go-sdk@v1.20220411.3/crypto/stream_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package crypto
     9  
    10  import (
    11  	"bytes"
    12  	"io"
    13  	"testing"
    14  
    15  	"github.com/blend/go-sdk/assert"
    16  )
    17  
    18  func Test_Stream_EncrypterDecrypter(t *testing.T) {
    19  	t.Parallel()
    20  	its := assert.New(t)
    21  
    22  	encKey, err := CreateKey(32)
    23  	its.Nil(err)
    24  	macKey, err := CreateKey(32)
    25  	its.Nil(err)
    26  	plaintext := "Eleven is the best person in all of Hawkins Indiana. Some more text"
    27  	pt := []byte(plaintext)
    28  
    29  	src := bytes.NewReader(pt)
    30  
    31  	se, err := NewStreamEncrypter(encKey, macKey, src)
    32  	its.Nil(err)
    33  	its.NotNil(se)
    34  
    35  	encrypted, err := io.ReadAll(se)
    36  	its.Nil(err)
    37  	its.NotNil(encrypted)
    38  
    39  	sd, err := NewStreamDecrypter(encKey, macKey, se.Meta(), bytes.NewReader(encrypted))
    40  	its.Nil(err)
    41  	its.NotNil(sd)
    42  
    43  	decrypted, err := io.ReadAll(sd)
    44  	its.Nil(err)
    45  	its.Equal(plaintext, string(decrypted))
    46  
    47  	its.Nil(sd.Authenticate())
    48  }
    49  
    50  func Test_checkedWrite(t *testing.T) {
    51  	t.Parallel()
    52  	its := assert.New(t)
    53  
    54  	writer := bytes.NewBuffer(nil)
    55  	data := []byte{1, 1, 1}
    56  	v, err := checkedWrite(writer, data)
    57  	its.Nil(err)
    58  	its.Equal(len(data), v)
    59  }