github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/protos/utils/commonutils_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 utils
    18  
    19  import (
    20  	"bytes"
    21  	"testing"
    22  
    23  	"github.com/hyperledger/fabric/core/crypto/primitives"
    24  
    25  	"github.com/golang/protobuf/proto"
    26  	cb "github.com/hyperledger/fabric/protos/common"
    27  )
    28  
    29  func TestNonceRandomness(t *testing.T) {
    30  	n1, err := CreateNonce()
    31  	if err != nil {
    32  		t.Fatal(err)
    33  	}
    34  	n2, err := CreateNonce()
    35  	if err != nil {
    36  		t.Fatal(err)
    37  	}
    38  	if bytes.Equal(n1, n2) {
    39  		t.Fatalf("Expected nonces to be different, got %x and %x", n1, n2)
    40  	}
    41  }
    42  
    43  func TestNonceLength(t *testing.T) {
    44  	n, err := CreateNonce()
    45  	if err != nil {
    46  		t.Fatal(err)
    47  	}
    48  	actual := len(n)
    49  	expected := primitives.NonceSize
    50  	if actual != expected {
    51  		t.Fatalf("Expected nonce to be of size %d, got %d instead", expected, actual)
    52  	}
    53  
    54  }
    55  
    56  func TestExtractEnvelopeWrongIndex(t *testing.T) {
    57  	block := testBlock()
    58  	if _, err := ExtractEnvelope(block, len(block.GetData().Data)); err == nil {
    59  		t.Fatal("Expected envelope extraction to fail (wrong index)")
    60  	}
    61  }
    62  
    63  func TestExtractEnvelopeWrongIndexOrPanic(t *testing.T) {
    64  	defer func() {
    65  		if r := recover(); r == nil {
    66  			t.Fatal("Expected envelope extraction to panic (wrong index)")
    67  		}
    68  	}()
    69  
    70  	block := testBlock()
    71  	ExtractEnvelopeOrPanic(block, len(block.GetData().Data))
    72  }
    73  
    74  func TestExtractEnvelope(t *testing.T) {
    75  	if envelope, err := ExtractEnvelope(testBlock(), 0); err != nil {
    76  		t.Fatalf("Expected envelop extraction to succeed: %s", err)
    77  	} else if !proto.Equal(envelope, testEnvelope()) {
    78  		t.Fatal("Expected extracted envelope to match test envelope")
    79  	}
    80  }
    81  
    82  func TestExtractEnvelopeOrPanic(t *testing.T) {
    83  	defer func() {
    84  		if r := recover(); r != nil {
    85  			t.Fatal("Expected envelope extraction to succeed")
    86  		}
    87  	}()
    88  
    89  	if !proto.Equal(ExtractEnvelopeOrPanic(testBlock(), 0), testEnvelope()) {
    90  		t.Fatal("Expected extracted envelope to match test envelope")
    91  	}
    92  }
    93  
    94  func TestExtractPayload(t *testing.T) {
    95  	if payload, err := ExtractPayload(testEnvelope()); err != nil {
    96  		t.Fatalf("Expected payload extraction to succeed: %s", err)
    97  	} else if !proto.Equal(payload, testPayload()) {
    98  		t.Fatal("Expected extracted payload to match test payload")
    99  	}
   100  }
   101  
   102  func TestExtractPayloadOrPanic(t *testing.T) {
   103  	defer func() {
   104  		if r := recover(); r != nil {
   105  			t.Fatal("Expected payload extraction to succeed")
   106  		}
   107  	}()
   108  
   109  	if !proto.Equal(ExtractPayloadOrPanic(testEnvelope()), testPayload()) {
   110  		t.Fatal("Expected extracted payload to match test payload")
   111  	}
   112  }
   113  
   114  // Helper functions
   115  
   116  func testPayload() *cb.Payload {
   117  	return &cb.Payload{
   118  		Header: MakePayloadHeader(
   119  			MakeChannelHeader(cb.HeaderType_MESSAGE, int32(1), "test", 0),
   120  			MakeSignatureHeader([]byte("creator"), []byte("nonce"))),
   121  		Data: []byte("test"),
   122  	}
   123  }
   124  
   125  func testEnvelope() *cb.Envelope {
   126  	// No need to set the signature
   127  	return &cb.Envelope{Payload: MarshalOrPanic(testPayload())}
   128  }
   129  
   130  func testBlock() *cb.Block {
   131  	// No need to set the block's Header, or Metadata
   132  	return &cb.Block{
   133  		Data: &cb.BlockData{
   134  			Data: [][]byte{MarshalOrPanic(testEnvelope())},
   135  		},
   136  	}
   137  }