github.com/aergoio/aergo@v1.3.1/p2p/signature_test.go (about)

     1  /**
     2   *  @file
     3   *  @copyright defined in aergo/LICENSE.txt
     4   */
     5  
     6  package p2p
     7  
     8  import (
     9  	"github.com/aergoio/aergo/p2p/p2pcommon"
    10  	"testing"
    11  
    12  	"github.com/aergoio/aergo/types"
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func Test_defaultMsgSigner_signMsg(t *testing.T) {
    17  	t.Run("TSameKey", func(t *testing.T) {
    18  		// msg and msg2 is same at first
    19  		sampleMsg1 := &types.P2PMessage{Header: &types.MsgHeader{Subprotocol: p2pcommon.PingResponse.Uint32(), Length: 5}, Data: []byte{0, 1, 2, 3, 4}}
    20  		sampleMsg2 := &types.P2PMessage{Header: &types.MsgHeader{Subprotocol: p2pcommon.PingResponse.Uint32(), Length: 5}, Data: []byte{0, 1, 2, 3, 4}}
    21  		pm := newDefaultMsgSigner(sampleKey1Priv, sampleKey1Pub, sampleKey1ID)
    22  		if err := pm.SignMsg(sampleMsg1); (err != nil) != false {
    23  			t.Errorf("defaultMsgSigner.signMsg() error = %v, wantErr %v", err, false)
    24  		}
    25  		assert.NotNil(t, sampleMsg1.Header.Sign)
    26  		assert.True(t, len(sampleMsg1.Header.Sign) > 0)
    27  
    28  		// different memory but same value is same signature
    29  		if err := pm.SignMsg(sampleMsg2); (err != nil) != false {
    30  			t.Errorf("defaultMsgSigner.signMsg() error = %v, wantErr %v", err, false)
    31  		}
    32  		assert.Equal(t, sampleMsg1.Header.Sign, sampleMsg2.Header.Sign)
    33  	})
    34  
    35  	t.Run("TDiffKey", func(t *testing.T) {
    36  		// msg and msg2 is same at first
    37  		sampleMsg1 := &types.P2PMessage{Header: &types.MsgHeader{Subprotocol: p2pcommon.PingResponse.Uint32(), Length: 5}, Data: []byte{0, 1, 2, 3, 4}}
    38  		sampleMsg2 := &types.P2PMessage{Header: &types.MsgHeader{Subprotocol: p2pcommon.PingResponse.Uint32(), Length: 5}, Data: []byte{0, 1, 2, 3, 4}}
    39  		pm := newDefaultMsgSigner(sampleKey1Priv, sampleKey1Pub, sampleKey1ID)
    40  		if err := pm.SignMsg(sampleMsg1); (err != nil) != false {
    41  			t.Errorf("defaultMsgSigner.signMsg() error = %v, wantErr %v", err, false)
    42  		}
    43  		assert.NotNil(t, sampleMsg1.Header.Sign)
    44  		assert.True(t, len(sampleMsg1.Header.Sign) > 0)
    45  
    46  		// same value but different pk is different signature
    47  		pm2 := newDefaultMsgSigner(sampleKey2Priv, sampleKey2Pub, sampleKey2ID)
    48  		if err := pm2.SignMsg(sampleMsg2); (err != nil) != false {
    49  			t.Errorf("defaultMsgSigner.signMsg() error = %v, wantErr %v", err, false)
    50  		}
    51  		assert.NotEqual(t, sampleMsg1.Header.Sign, sampleMsg2.Header.Sign)
    52  	})
    53  
    54  }
    55  
    56  func Test_defaultMsgSigner_verifyMsg(t *testing.T) {
    57  	pubkey1bytes, _ := sampleKey1Pub.Bytes()
    58  	pubkey2bytes, _ := sampleKey2Pub.Bytes()
    59  	t.Run("TSucc", func(t *testing.T) {
    60  		// msg and msg2 is same at first
    61  		sampleMsg1 := &types.P2PMessage{Header: &types.MsgHeader{Subprotocol: p2pcommon.PingResponse.Uint32(), Length: 5, NodePubKey: pubkey1bytes}, Data: []byte{0, 1, 2, 3, 4}}
    62  
    63  		pm := newDefaultMsgSigner(sampleKey1Priv, sampleKey1Pub, sampleKey1ID)
    64  		assert.Nil(t, pm.SignMsg(sampleMsg1))
    65  		expectedSig := append(make([]byte, 0), sampleMsg1.GetHeader().GetSign()...)
    66  		assert.Equal(t, expectedSig, sampleMsg1.GetHeader().GetSign())
    67  
    68  		pm2 := newDefaultMsgSigner(sampleKey2Priv, sampleKey2Pub, sampleKey2ID)
    69  		// different memory but same value is same signature
    70  		if err := pm.VerifyMsg(sampleMsg1, sampleKey1ID); (err != nil) != false {
    71  			t.Errorf("defaultMsgSigner.verifyMsg() error = %v, wantErr %v", err, false)
    72  		}
    73  		if err := pm2.VerifyMsg(sampleMsg1, sampleKey1ID); (err != nil) != false {
    74  			t.Errorf("defaultMsgSigner.verifyMsg() error = %v, wantErr %v", err, false)
    75  		}
    76  	})
    77  
    78  	t.Run("TDiffKey", func(t *testing.T) {
    79  		// msg and msg2 is same at first
    80  		sampleMsg1 := &types.P2PMessage{Header: &types.MsgHeader{Subprotocol: p2pcommon.PingResponse.Uint32(), Length: 5}, Data: []byte{0, 1, 2, 3, 4}}
    81  		pm := newDefaultMsgSigner(sampleKey1Priv, sampleKey1Pub, sampleKey1ID)
    82  		assert.Nil(t, pm.SignMsg(sampleMsg1))
    83  		expectedSig := append(make([]byte, 0), sampleMsg1.GetHeader().GetSign()...)
    84  		assert.Equal(t, expectedSig, sampleMsg1.GetHeader().GetSign())
    85  
    86  		sampleMsg1.Header.NodePubKey = pubkey2bytes
    87  		pm2 := newDefaultMsgSigner(sampleKey2Priv, sampleKey2Pub, sampleKey2ID)
    88  		// different memory but same value is same signature
    89  		if err := pm.VerifyMsg(sampleMsg1, sampleKey2ID); (err != nil) != true {
    90  			t.Errorf("defaultMsgSigner.verifyMsg() error = %v, wantErr %v", err, false)
    91  		}
    92  		if err := pm2.VerifyMsg(sampleMsg1, sampleKey2ID); (err != nil) != true {
    93  			t.Errorf("defaultMsgSigner.verifyMsg() error = %v, wantErr %v", err, false)
    94  		}
    95  	})
    96  
    97  	t.Run("TDiffFields", func(t *testing.T) {
    98  		// msg and msg2 is same at first
    99  		sampleMsg1 := &types.P2PMessage{Header: &types.MsgHeader{Subprotocol: p2pcommon.PingResponse.Uint32(), Length: 5}, Data: []byte{0, 1, 2, 3, 4}}
   100  
   101  		pm := newDefaultMsgSigner(sampleKey1Priv, sampleKey1Pub, sampleKey1ID)
   102  		assert.Nil(t, pm.SignMsg(sampleMsg1))
   103  		expectedSig := append(make([]byte, 0), sampleMsg1.GetHeader().GetSign()...)
   104  		assert.Equal(t, expectedSig, sampleMsg1.GetHeader().GetSign())
   105  		sampleMsg1.Data = append(sampleMsg1.Data, 5, 6)
   106  		pm2 := newDefaultMsgSigner(sampleKey2Priv, sampleKey2Pub, sampleKey2ID)
   107  		// different memory but same value is same signature
   108  		if err := pm2.VerifyMsg(sampleMsg1, sampleKey1ID); (err != nil) != true {
   109  			t.Errorf("defaultMsgSigner.verifyMsg() error = %v, wantErr %v", err, false)
   110  		}
   111  	})
   112  
   113  }