github.com/braveheart12/insolar-09-08-19@v0.8.7/platformpolicy/keys_test.go (about)

     1  /*
     2   *    Copyright 2019 Insolar Technologies
     3   *
     4   *    Licensed under the Apache License, Version 2.0 (the "License");
     5   *    you may not use this file except in compliance with the License.
     6   *    You may obtain a copy of the License at
     7   *
     8   *        http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   *    Unless required by applicable law or agreed to in writing, software
    11   *    distributed under the License is distributed on an "AS IS" BASIS,
    12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   *    See the License for the specific language governing permissions and
    14   *    limitations under the License.
    15   */
    16  
    17  package platformpolicy
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  	"github.com/stretchr/testify/require"
    24  )
    25  
    26  func TestExportImportPrivateKey(t *testing.T) {
    27  	ks := NewKeyProcessor()
    28  
    29  	privateKey, _ := ks.GeneratePrivateKey()
    30  
    31  	encoded, err := ks.ExportPrivateKeyPEM(privateKey)
    32  	require.NoError(t, err)
    33  	decoded, err := ks.ImportPrivateKeyPEM(encoded)
    34  	require.NoError(t, err)
    35  
    36  	assert.ObjectsAreEqual(decoded, privateKey)
    37  }
    38  
    39  func TestExportImportPublicKey(t *testing.T) {
    40  	ks := NewKeyProcessor()
    41  
    42  	privateKey, _ := ks.GeneratePrivateKey()
    43  	publicKey := ks.ExtractPublicKey(privateKey)
    44  
    45  	encoded, err := ks.ExportPublicKeyPEM(publicKey)
    46  	require.NoError(t, err)
    47  	decoded, err := ks.ImportPublicKeyPEM(encoded)
    48  	require.NoError(t, err)
    49  
    50  	assert.ObjectsAreEqual(decoded, privateKey)
    51  }
    52  
    53  func TestExportImportPublicKeyBinary(t *testing.T) {
    54  	ks := NewKeyProcessor()
    55  
    56  	privateKey, _ := ks.GeneratePrivateKey()
    57  	publicKey := ks.ExtractPublicKey(privateKey)
    58  
    59  	encoded, err := ks.ExportPublicKeyPEM(publicKey)
    60  	require.NoError(t, err)
    61  
    62  	bin, err := ks.ExportPublicKeyBinary(publicKey)
    63  	require.NoError(t, err)
    64  	assert.Len(t, bin, 66)
    65  
    66  	binPK, err := ks.ImportPublicKeyBinary(bin)
    67  	require.NoError(t, err)
    68  
    69  	encodedBinPK, err := ks.ExportPublicKeyPEM(binPK)
    70  	require.NoError(t, err)
    71  
    72  	assert.Equal(t, encoded, encodedBinPK)
    73  }