github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/identity/verifier_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  func TestVerifierIdentity_Verify(t *testing.T) {
    27  	message := []byte("Boop!")
    28  	signature := SignatureHex("1f89542f406b2d638fe09cd9912d0b8c0b5ebb4aef67d52ab046973e34fb430a1953576cd19d140eddb099aea34b2985fbd99e716d3b2f96a964141fdb84b32000")
    29  
    30  	verifier := NewVerifierIdentity(FromAddress("0x53a835143c0ef3bbcbfa796d7eb738ca7dd28f68"))
    31  	res, _ := verifier.Verify(message, signature)
    32  	assert.True(t, res)
    33  }
    34  
    35  func TestVerifierIdentity_VerifyWithUppercaseIdentity(t *testing.T) {
    36  	message := []byte("Boop!")
    37  	signature := SignatureHex("1f89542f406b2d638fe09cd9912d0b8c0b5ebb4aef67d52ab046973e34fb430a1953576cd19d140eddb099aea34b2985fbd99e716d3b2f96a964141fdb84b32000")
    38  
    39  	verifier := NewVerifierIdentity(FromAddress("0x53A835143C0EF3BBCBFA796d7eb738CA7DD28F68"))
    40  	res, _ := verifier.Verify(message, signature)
    41  	assert.True(t, res)
    42  }
    43  
    44  func TestVerifierIdentity_VerifyWhenWrongSender(t *testing.T) {
    45  	message := []byte("boop!")
    46  	signature := SignatureHex("1f89542f406b2d638fe09cd9912d0b8c0b5ebb4aef67d52ab046973e34fb430a1953576cd19d140eddb099aea34b2985fbd99e716d3b2f96a964141fdb84b32000")
    47  
    48  	verifier := NewVerifierIdentity(FromAddress("0x53a835143c0ef3bbcbfa796d7eb738ca7dd28f68"))
    49  	res, _ := verifier.Verify(message, signature)
    50  	assert.False(t, res)
    51  }
    52  
    53  func TestVerifierSigned_Verify(t *testing.T) {
    54  	message := []byte("Boop!")
    55  	signature := SignatureHex("1f89542f406b2d638fe09cd9912d0b8c0b5ebb4aef67d52ab046973e34fb430a1953576cd19d140eddb099aea34b2985fbd99e716d3b2f96a964141fdb84b32000")
    56  
    57  	verifier := NewVerifierSigned()
    58  	res, _ := verifier.Verify(message, signature)
    59  	assert.True(t, res)
    60  }
    61  
    62  func TestVerifierSigned_VerifyWhenMalformedSignature(t *testing.T) {
    63  	message := []byte("Boop!")
    64  	signature := SignatureHex("7369676e6564")
    65  
    66  	verifier := NewVerifierSigned()
    67  	res, _ := verifier.Verify(message, signature)
    68  	assert.False(t, res)
    69  }
    70  
    71  func TestVerifierSigned_VerifyWhenMessageIsChanged(t *testing.T) {
    72  	message := []byte("Boop changed!")
    73  	signature := SignatureHex("1f89542f406b2d638fe09cd9912d0b8c0b5ebb4aef67d52ab046973e34fb430a1953576cd19d140eddb099aea34b2985fbd99e716d3b2f96a964141fdb84b32000")
    74  
    75  	verifier := NewVerifierSigned()
    76  	res, _ := verifier.Verify(message, signature)
    77  	assert.True(t, res)
    78  }