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