github.com/core-coin/go-core/v2@v2.1.9/p2p/enode/idscheme_test.go (about) 1 // Copyright 2018 by the Authors 2 // This file is part of the go-core library. 3 // 4 // The go-core 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-core 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-core library. If not, see <http://www.gnu.org/licenses/>. 16 17 package enode 18 19 import ( 20 "bytes" 21 "encoding/hex" 22 "testing" 23 24 "github.com/stretchr/testify/assert" 25 "github.com/stretchr/testify/require" 26 27 "github.com/core-coin/go-core/v2/crypto" 28 "github.com/core-coin/go-core/v2/p2p/enr" 29 "github.com/core-coin/go-core/v2/rlp" 30 ) 31 32 var ( 33 privkey, _ = crypto.UnmarshalPrivateKeyHex("856a9af6b0b651dd2f43b5e12193652ec1701c4da6f1c0d2a366ac4b9dabc9433ef09e41ca129552bd2c029086d9b03604de872a3b3432041f") 34 ) 35 36 func TestEmptyNodeID(t *testing.T) { 37 var r enr.Record 38 if addr := ValidSchemes.NodeAddr(&r); addr != nil { 39 t.Errorf("wrong address on empty record: got %v, want %v", addr, nil) 40 } 41 42 require.NoError(t, SignV4(&r, privkey)) 43 expected := "22384f8cdc73aa2005df6581c0b7afce8c1aa50eb259d87fdf72f6220f173ab6" 44 assert.Equal(t, expected, hex.EncodeToString(ValidSchemes.NodeAddr(&r))) 45 } 46 47 // Checks that failure to sign leaves the record unmodified. 48 func TestSignError(t *testing.T) { 49 invalidKey := &crypto.PrivateKey{} 50 51 var r enr.Record 52 emptyEnc, _ := rlp.EncodeToBytes(&r) 53 if err := SignV4(&r, invalidKey); err == nil { 54 t.Fatal("expected error from SignV4") 55 } 56 newEnc, _ := rlp.EncodeToBytes(&r) 57 if !bytes.Equal(newEnc, emptyEnc) { 58 t.Fatal("record modified even though signing failed") 59 } 60 } 61 62 // TestGetSetEd448 tests encoding/decoding and setting/getting of the Ed448 key. 63 func TestGetSetEd448(t *testing.T) { 64 var r enr.Record 65 if err := SignV4(&r, privkey); err != nil { 66 t.Fatal(err) 67 } 68 69 var pk Ed448 70 require.NoError(t, r.Load(&pk)) 71 assert.EqualValues(t, privkey.PublicKey(), &pk) 72 }