github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/identity/extractor_test.go (about)

     1  /*
     2   * Copyright (C) 2017 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package identity
    19  
    20  import (
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  var (
    27  	originalSignerID = FromAddress("0x53a835143c0ef3bbcbfa796d7eb738ca7dd28f68")
    28  	hijackedSignerID = FromAddress("0xded9913d38bfe94845b9e21fd32f43d0240e2f34")
    29  )
    30  
    31  func TestAuthenticate_WhenSignatureIsCorrect(t *testing.T) {
    32  	message := []byte("Boop!")
    33  	signature := SignatureHex("1f89542f406b2d638fe09cd9912d0b8c0b5ebb4aef67d52ab046973e34fb430a1953576cd19d140eddb099aea34b2985fbd99e716d3b2f96a964141fdb84b32000")
    34  
    35  	extractor := &extractor{}
    36  	signerID, err := extractor.Extract(message, signature)
    37  	assert.NoError(t, err)
    38  	assert.Exactly(t, originalSignerID, signerID, "Original signer should be extracted")
    39  }
    40  
    41  func TestAuthenticate_WhenBase64MessageSignatureIsCorrect(t *testing.T) {
    42  	message := []byte("MystVpnSessionId:Boop!")
    43  	signature := SignatureBase64("V6ifmvLuAT+hbtLBX/0xm3C0afywxTIdw1HqLmA4onpwmibHbxVhl50Gr3aRUZMqw1WxkfSIVdhpbCluHGBKsgE=")
    44  
    45  	extractor := &extractor{}
    46  	signerID, err := extractor.Extract(message, signature)
    47  	assert.NoError(t, err)
    48  	assert.Exactly(t, originalSignerID, signerID, "Extracted signer should match original signer")
    49  }
    50  
    51  func TestAuthenticate_WhenSignatureIsEmpty(t *testing.T) {
    52  	message := []byte("Boop!")
    53  	signature := SignatureHex("")
    54  
    55  	extractor := &extractor{}
    56  	signerID, err := extractor.Extract(message, signature)
    57  	assert.EqualError(t, err, "empty signature")
    58  	assert.Exactly(t, Identity{}, signerID)
    59  }
    60  
    61  func TestAuthenticate_WhenSignatureIsMalformed(t *testing.T) {
    62  	message := []byte("Boop!")
    63  	signature := SignatureHex("7369676e6564")
    64  
    65  	extractor := &extractor{}
    66  	signerID, err := extractor.Extract(message, signature)
    67  	assert.EqualError(t, err, "invalid signature length")
    68  	assert.Exactly(t, Identity{}, signerID)
    69  }
    70  
    71  func TestAuthenticate_WhenMessageIsChanged(t *testing.T) {
    72  	message := []byte("Boop changed!")
    73  	signature := SignatureHex("1f89542f406b2d638fe09cd9912d0b8c0b5ebb4aef67d52ab046973e34fb430a1953576cd19d140eddb099aea34b2985fbd99e716d3b2f96a964141fdb84b32000")
    74  
    75  	extractor := &extractor{}
    76  	signerID, err := extractor.Extract(message, signature)
    77  	assert.NoError(t, err)
    78  	assert.NotEqual(t, originalSignerID, signerID, "Original signer should not be extracted")
    79  	assert.Exactly(t, hijackedSignerID, signerID, "Another signer extracted")
    80  }