github.com/yimialmonte/fabric@v2.1.1+incompatible/discovery/client/signer_test.go (about)

     1  /*
     2  Copyright IBM Corp All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package discovery
     8  
     9  import (
    10  	"crypto/rand"
    11  	"io"
    12  	"sync"
    13  	"sync/atomic"
    14  	"testing"
    15  
    16  	"github.com/hyperledger/fabric/bccsp/factory"
    17  	"github.com/pkg/errors"
    18  	"github.com/stretchr/testify/assert"
    19  )
    20  
    21  func init() {
    22  	factory.InitFactories(nil)
    23  }
    24  
    25  func TestSameMessage(t *testing.T) {
    26  	var signedInvokedCount int
    27  	sign := func(msg []byte) ([]byte, error) {
    28  		signedInvokedCount++
    29  		return msg, nil
    30  	}
    31  
    32  	ms := NewMemoizeSigner(sign, 10)
    33  	for i := 0; i < 5; i++ {
    34  		sig, err := ms.Sign([]byte{1, 2, 3})
    35  		assert.NoError(t, err)
    36  		assert.Equal(t, []byte{1, 2, 3}, sig)
    37  		assert.Equal(t, 1, signedInvokedCount)
    38  	}
    39  }
    40  
    41  func TestDifferentMessages(t *testing.T) {
    42  	var n uint = 50
    43  	var signedInvokedCount uint32
    44  	sign := func(msg []byte) ([]byte, error) {
    45  		atomic.AddUint32(&signedInvokedCount, 1)
    46  		return msg, nil
    47  	}
    48  
    49  	ms := NewMemoizeSigner(sign, n)
    50  	parallelSignRange := func(start, end uint) {
    51  		var wg sync.WaitGroup
    52  		wg.Add((int)(end - start))
    53  		for i := start; i < end; i++ {
    54  			i := i
    55  			go func() {
    56  				defer wg.Done()
    57  				sig, err := ms.Sign([]byte{byte(i)})
    58  				assert.NoError(t, err)
    59  				assert.Equal(t, []byte{byte(i)}, sig)
    60  			}()
    61  		}
    62  		wg.Wait()
    63  	}
    64  
    65  	// Query once
    66  	parallelSignRange(0, n)
    67  	assert.Equal(t, uint32(n), atomic.LoadUint32(&signedInvokedCount))
    68  
    69  	// Query twice
    70  	parallelSignRange(0, n)
    71  	assert.Equal(t, uint32(n), atomic.LoadUint32(&signedInvokedCount))
    72  
    73  	// Query thrice on a disjoint range
    74  	for i := n + 1; i < 2*n; i++ {
    75  		parallelSignRange(i, i+1)
    76  	}
    77  	oldSignedInvokedCount := atomic.LoadUint32(&signedInvokedCount)
    78  
    79  	// Ensure that some of the early messages 0-n were purged from memory
    80  	parallelSignRange(0, n)
    81  	assert.True(t, oldSignedInvokedCount < atomic.LoadUint32(&signedInvokedCount))
    82  }
    83  
    84  func TestFailure(t *testing.T) {
    85  	sign := func(_ []byte) ([]byte, error) {
    86  		return nil, errors.New("something went wrong")
    87  	}
    88  
    89  	ms := NewMemoizeSigner(sign, 1)
    90  	_, err := ms.Sign([]byte{1, 2, 3})
    91  	assert.Equal(t, "something went wrong", err.Error())
    92  }
    93  
    94  func TestNotSavingInMem(t *testing.T) {
    95  	sign := func(_ []byte) ([]byte, error) {
    96  		b := make([]byte, 30)
    97  		_, err := io.ReadFull(rand.Reader, b)
    98  		assert.NoError(t, err)
    99  		return b, nil
   100  	}
   101  	ms := NewMemoizeSigner(sign, 0)
   102  	sig1, _ := ms.sign(([]byte)("aa"))
   103  	sig2, _ := ms.sign(([]byte)("aa"))
   104  	assert.NotEqual(t, sig1, sig2)
   105  
   106  }