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