github.com/lzy4123/fabric@v2.1.1+incompatible/gossip/api/crypto_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package api
     8  
     9  import (
    10  	"encoding/pem"
    11  	"io/ioutil"
    12  	"path/filepath"
    13  	"testing"
    14  
    15  	"github.com/hyperledger/fabric-protos-go/msp"
    16  	"github.com/hyperledger/fabric/protoutil"
    17  	"github.com/stretchr/testify/assert"
    18  )
    19  
    20  func TestPeerIdentityTypeString(t *testing.T) {
    21  	certBytes, err := ioutil.ReadFile(filepath.Join("testdata", "peer.pem"))
    22  	assert.NoError(t, err)
    23  
    24  	for _, testCase := range []struct {
    25  		description string
    26  		identity    PeerIdentityType
    27  		expectedOut string
    28  	}{
    29  		{
    30  			description: "non serialized identity",
    31  			identity:    PeerIdentityType("some garbage"),
    32  			expectedOut: "non SerializedIdentity: c29tZSBnYXJiYWdl",
    33  		},
    34  		{
    35  			description: "non PEM identity",
    36  			identity: PeerIdentityType(protoutil.MarshalOrPanic(&msp.SerializedIdentity{
    37  				Mspid:   "SampleOrg",
    38  				IdBytes: []byte{1, 2, 3},
    39  			})),
    40  			expectedOut: "non PEM encoded identity: CglTYW1wbGVPcmcSAwECAw==",
    41  		},
    42  		{
    43  			description: "non x509 identity",
    44  			identity: PeerIdentityType(protoutil.MarshalOrPanic(&msp.SerializedIdentity{
    45  				Mspid: "SampleOrg",
    46  				IdBytes: pem.EncodeToMemory(&pem.Block{
    47  					Type:  "CERTIFICATE",
    48  					Bytes: []byte{1, 2, 3},
    49  				}),
    50  			})),
    51  			expectedOut: `non x509 identity: CglTYW1wbGVPcmcSOy0tLS0tQkVHSU4gQ0VSVElGSUNBVEUtLS0tLQpBUUlECi0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0K`,
    52  		},
    53  		{
    54  			description: "x509 identity",
    55  			identity: PeerIdentityType(protoutil.MarshalOrPanic(&msp.SerializedIdentity{
    56  				Mspid:   "SampleOrg",
    57  				IdBytes: certBytes,
    58  			})),
    59  			expectedOut: `{"CN":"peer0.org1.example.com","Issuer-CN":"ca.org1.example.com","Issuer-L-ST-C":"[San Francisco]-[]-[US]","Issuer-OU":["COP"],"L-ST-C":"[San Francisco]-[]-[US]","MSP":"SampleOrg","OU":["COP"]}`,
    60  		},
    61  	} {
    62  		t.Run(testCase.description, func(t *testing.T) {
    63  			assert.Equal(t, testCase.identity.String(), testCase.expectedOut)
    64  		})
    65  	}
    66  
    67  }