github.com/defanghe/fabric@v2.1.1+incompatible/protoutil/signeddata_test.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package protoutil_test
     8  
     9  import (
    10  	"bytes"
    11  	"testing"
    12  
    13  	"github.com/golang/protobuf/proto"
    14  	"github.com/hyperledger/fabric-protos-go/common"
    15  	"github.com/hyperledger/fabric/protoutil"
    16  )
    17  
    18  // More duplicate utility which should go away, but the utils are a bit of a mess right now with import cycles
    19  func marshalOrPanic(msg proto.Message) []byte {
    20  	data, err := proto.Marshal(msg)
    21  	if err != nil {
    22  		panic("Error marshaling")
    23  	}
    24  	return data
    25  }
    26  
    27  func TestNilConfigEnvelopeAsSignedData(t *testing.T) {
    28  	var ce *common.ConfigUpdateEnvelope
    29  	_, err := protoutil.ConfigUpdateEnvelopeAsSignedData(ce)
    30  	if err == nil {
    31  		t.Fatalf("Should have errored trying to convert a nil signed config item to signed data")
    32  	}
    33  }
    34  
    35  func TestConfigEnvelopeAsSignedData(t *testing.T) {
    36  	configBytes := []byte("Foo")
    37  	signatures := [][]byte{[]byte("Signature1"), []byte("Signature2")}
    38  	identities := [][]byte{[]byte("Identity1"), []byte("Identity2")}
    39  
    40  	configSignatures := make([]*common.ConfigSignature, len(signatures))
    41  	for i := range configSignatures {
    42  		configSignatures[i] = &common.ConfigSignature{
    43  			SignatureHeader: marshalOrPanic(&common.SignatureHeader{
    44  				Creator: identities[i],
    45  			}),
    46  			Signature: signatures[i],
    47  		}
    48  	}
    49  
    50  	ce := &common.ConfigUpdateEnvelope{
    51  		ConfigUpdate: configBytes,
    52  		Signatures:   configSignatures,
    53  	}
    54  
    55  	signedData, err := protoutil.ConfigUpdateEnvelopeAsSignedData(ce)
    56  	if err != nil {
    57  		t.Fatalf("Unexpected error: %s", err)
    58  	}
    59  
    60  	for i, sigData := range signedData {
    61  		if !bytes.Equal(sigData.Identity, identities[i]) {
    62  			t.Errorf("Expected identity to match at index %d", i)
    63  		}
    64  		if !bytes.Equal(sigData.Data, append(configSignatures[i].SignatureHeader, configBytes...)) {
    65  			t.Errorf("Expected signature over concatenation of config item bytes and signature header")
    66  		}
    67  		if !bytes.Equal(sigData.Signature, signatures[i]) {
    68  			t.Errorf("Expected signature to match at index %d", i)
    69  		}
    70  	}
    71  }
    72  
    73  func TestNilEnvelopeAsSignedData(t *testing.T) {
    74  	var env *common.Envelope
    75  	_, err := protoutil.EnvelopeAsSignedData(env)
    76  	if err == nil {
    77  		t.Fatalf("Should have errored trying to convert a nil envelope")
    78  	}
    79  }
    80  
    81  func TestEnvelopeAsSignedData(t *testing.T) {
    82  	identity := []byte("Foo")
    83  	sig := []byte("Bar")
    84  
    85  	shdrbytes, err := proto.Marshal(&common.SignatureHeader{Creator: identity})
    86  	if err != nil {
    87  		t.Fatalf("%s", err)
    88  	}
    89  
    90  	env := &common.Envelope{
    91  		Payload: marshalOrPanic(&common.Payload{
    92  			Header: &common.Header{
    93  				SignatureHeader: shdrbytes,
    94  			},
    95  		}),
    96  		Signature: sig,
    97  	}
    98  
    99  	signedData, err := protoutil.EnvelopeAsSignedData(env)
   100  	if err != nil {
   101  		t.Fatalf("Unexpected error converting envelope to SignedData: %s", err)
   102  	}
   103  
   104  	if len(signedData) != 1 {
   105  		t.Fatalf("Expected 1 entry of signed data, but got %d", len(signedData))
   106  	}
   107  
   108  	if !bytes.Equal(signedData[0].Identity, identity) {
   109  		t.Errorf("Wrong identity bytes")
   110  	}
   111  	if !bytes.Equal(signedData[0].Data, env.Payload) {
   112  		t.Errorf("Wrong data bytes")
   113  	}
   114  	if !bytes.Equal(signedData[0].Signature, sig) {
   115  		t.Errorf("Wrong data bytes")
   116  	}
   117  }