github.com/annchain/OG@v0.0.9/p2p/onode/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 onode 18 19 import ( 20 "bytes" 21 "crypto/ecdsa" 22 "encoding/hex" 23 ogcrypto2 "github.com/annchain/OG/deprecated/ogcrypto" 24 "math/big" 25 "testing" 26 27 "github.com/annchain/OG/p2p/enr" 28 "github.com/stretchr/testify/assert" 29 "github.com/stretchr/testify/require" 30 ) 31 32 var ( 33 privkey, _ = ogcrypto2.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") 34 pubkey = &privkey.PublicKey 35 ) 36 37 func TestEmptyNodeID(t *testing.T) { 38 var r enr.Record 39 if addr := ValidSchemes.NodeAddr(&r); addr != nil { 40 t.Errorf("wrong address on empty record: got %v, want %v", addr, nil) 41 } 42 43 require.NoError(t, SignV4(&r, privkey)) 44 expected := "a448f24c6d18e575453db13171562b71999873db5b286df957af199ec94617f7" 45 assert.Equal(t, expected, hex.EncodeToString(ValidSchemes.NodeAddr(&r))) 46 } 47 48 // Checks that failure to sign leaves the record unmodified. 49 func TestSignError(t *testing.T) { 50 invalidKey := &ecdsa.PrivateKey{D: new(big.Int), PublicKey: *pubkey} 51 52 var r enr.Record 53 emptyEnc, _ := r.Encode(nil) 54 if err := SignV4(&r, invalidKey); err == nil { 55 t.Fatal("expected error from SignV4") 56 } 57 newEnc, _ := r.Encode(nil) 58 if !bytes.Equal(newEnc, emptyEnc) { 59 t.Fatal("record modified even though signing failed") 60 } 61 } 62 63 // TestGetSetSecp256k1 tests encoding/decoding and setting/getting of the Secp256k1 key. 64 func TestGetSetSecp256k1(t *testing.T) { 65 var r enr.Record 66 if err := SignV4(&r, privkey); err != nil { 67 t.Fatal(err) 68 } 69 70 var pk Secp256k1 71 require.NoError(t, r.Load(&pk)) 72 assert.EqualValues(t, pubkey, &pk) 73 }