github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/protos/common/signed_data_test.go (about)

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