...

Source file src/github.com/ThalesIgnite/crypto11/close_test.go

Documentation: github.com/ThalesIgnite/crypto11

     1  // Copyright 2018 Thales e-Security, Inc
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining
     4  // a copy of this software and associated documentation files (the
     5  // "Software"), to deal in the Software without restriction, including
     6  // without limitation the rights to use, copy, modify, merge, publish,
     7  // distribute, sublicense, and/or sell copies of the Software, and to
     8  // permit persons to whom the Software is furnished to do so, subject to
     9  // the following conditions:
    10  //
    11  // The above copyright notice and this permission notice shall be
    12  // included in all copies or substantial portions of the Software.
    13  //
    14  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    15  // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    16  // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    17  // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
    18  // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
    19  // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
    20  // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    21  
    22  package crypto11
    23  
    24  import (
    25  	"crypto/dsa"
    26  	"crypto/elliptic"
    27  	"testing"
    28  
    29  	"github.com/stretchr/testify/assert"
    30  
    31  	"github.com/stretchr/testify/require"
    32  )
    33  
    34  func TestErrorAfterClosed(t *testing.T) {
    35  	ctx, err := ConfigureFromFile("config")
    36  	require.NoError(t, err)
    37  
    38  	err = ctx.Close()
    39  	require.NoError(t, err)
    40  
    41  	bytes := randomBytes()
    42  
    43  	_, err = ctx.FindKey(bytes, nil)
    44  	assert.Equal(t, errClosed, err)
    45  
    46  	_, err = ctx.FindKeys(bytes, nil)
    47  	assert.Equal(t, errClosed, err)
    48  
    49  	_, err = ctx.FindKeysWithAttributes(NewAttributeSet())
    50  	assert.Equal(t, errClosed, err)
    51  
    52  	_, err = ctx.FindKeyPair(bytes, nil)
    53  	assert.Equal(t, errClosed, err)
    54  
    55  	_, err = ctx.FindKeyPairs(bytes, nil)
    56  	assert.Equal(t, errClosed, err)
    57  
    58  	_, err = ctx.FindKeyPairsWithAttributes(NewAttributeSet())
    59  	assert.Equal(t, errClosed, err)
    60  
    61  	_, err = ctx.GenerateSecretKey(bytes, 256, CipherAES)
    62  	assert.Equal(t, errClosed, err)
    63  
    64  	_, err = ctx.GenerateSecretKeyWithLabel(bytes, bytes, 256, CipherAES)
    65  	assert.Equal(t, errClosed, err)
    66  
    67  	_, err = ctx.GenerateRSAKeyPair(bytes, 2048)
    68  	assert.Equal(t, errClosed, err)
    69  
    70  	_, err = ctx.GenerateRSAKeyPairWithLabel(bytes, bytes, 2048)
    71  	assert.Equal(t, errClosed, err)
    72  
    73  	_, err = ctx.GenerateDSAKeyPair(bytes, dsaSizes[dsa.L1024N160])
    74  	assert.Equal(t, errClosed, err)
    75  
    76  	_, err = ctx.GenerateDSAKeyPairWithLabel(bytes, bytes, dsaSizes[dsa.L1024N160])
    77  	assert.Equal(t, errClosed, err)
    78  
    79  	_, err = ctx.GenerateECDSAKeyPair(bytes, elliptic.P224())
    80  	assert.Equal(t, errClosed, err)
    81  
    82  	_, err = ctx.GenerateECDSAKeyPairWithLabel(bytes, bytes, elliptic.P224())
    83  	assert.Equal(t, errClosed, err)
    84  
    85  	_, err = ctx.NewRandomReader()
    86  	assert.Equal(t, errClosed, err)
    87  
    88  	cert := generateRandomCert(t)
    89  
    90  	err = ctx.ImportCertificate(bytes, cert)
    91  	assert.Equal(t, errClosed, err)
    92  
    93  	err = ctx.ImportCertificateWithLabel(bytes, bytes, cert)
    94  	assert.Equal(t, errClosed, err)
    95  
    96  	err = ctx.ImportCertificateWithAttributes(NewAttributeSet(), cert)
    97  	assert.Equal(t, errClosed, err)
    98  
    99  	_, err = ctx.GetAttribute(nil, CkaLabel)
   100  	assert.Equal(t, errClosed, err)
   101  
   102  	_, err = ctx.GetAttributes(nil, []AttributeType{CkaLabel})
   103  	assert.Equal(t, errClosed, err)
   104  
   105  	_, err = ctx.GetPubAttribute(nil, CkaLabel)
   106  	assert.Equal(t, errClosed, err)
   107  
   108  	_, err = ctx.GetPubAttributes(nil, []AttributeType{CkaLabel})
   109  	assert.Equal(t, errClosed, err)
   110  }
   111  

View as plain text