github.com/c2s/go-ethereum@v1.9.7/p2p/enode/idscheme_test.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser 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 // The go-ethereum library 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 Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package enode 18 19 import ( 20 "bytes" 21 "crypto/ecdsa" 22 "encoding/hex" 23 "math/big" 24 "testing" 25 26 "github.com/ethereum/go-ethereum/crypto" 27 "github.com/ethereum/go-ethereum/p2p/enr" 28 "github.com/ethereum/go-ethereum/rlp" 29 "github.com/stretchr/testify/assert" 30 "github.com/stretchr/testify/require" 31 ) 32 33 var ( 34 privkey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") 35 pubkey = &privkey.PublicKey 36 ) 37 38 func TestEmptyNodeID(t *testing.T) { 39 var r enr.Record 40 if addr := ValidSchemes.NodeAddr(&r); addr != nil { 41 t.Errorf("wrong address on empty record: got %v, want %v", addr, nil) 42 } 43 44 require.NoError(t, SignV4(&r, privkey)) 45 expected := "a448f24c6d18e575453db13171562b71999873db5b286df957af199ec94617f7" 46 assert.Equal(t, expected, hex.EncodeToString(ValidSchemes.NodeAddr(&r))) 47 } 48 49 // Checks that failure to sign leaves the record unmodified. 50 func TestSignError(t *testing.T) { 51 invalidKey := &ecdsa.PrivateKey{D: new(big.Int), PublicKey: *pubkey} 52 53 var r enr.Record 54 emptyEnc, _ := rlp.EncodeToBytes(&r) 55 if err := SignV4(&r, invalidKey); err == nil { 56 t.Fatal("expected error from SignV4") 57 } 58 newEnc, _ := rlp.EncodeToBytes(&r) 59 if !bytes.Equal(newEnc, emptyEnc) { 60 t.Fatal("record modified even though signing failed") 61 } 62 } 63 64 // TestGetSetSecp256k1 tests encoding/decoding and setting/getting of the Secp256k1 key. 65 func TestGetSetSecp256k1(t *testing.T) { 66 var r enr.Record 67 if err := SignV4(&r, privkey); err != nil { 68 t.Fatal(err) 69 } 70 71 var pk Secp256k1 72 require.NoError(t, r.Load(&pk)) 73 assert.EqualValues(t, pubkey, &pk) 74 }