github.com/oskarth/go-ethereum@v1.6.8-0.20191013093314-dac24a9d3494/p2p/enode/node_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  	"encoding/hex"
    21  	"fmt"
    22  	"testing"
    23  
    24  	"github.com/ethereum/go-ethereum/p2p/enr"
    25  	"github.com/ethereum/go-ethereum/rlp"
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  var pyRecord, _ = hex.DecodeString("f884b8407098ad865b00a582051940cb9cf36836572411a47278783077011599ed5cd16b76f2635f4e234738f30813a89eb9137e3e3df5266e3a1f11df72ecf1145ccb9c01826964827634826970847f00000189736563703235366b31a103ca634cae0d49acb401d8a4c6b6fe8c55b70d115bf400769cc1400f3258cd31388375647082765f")
    30  
    31  // TestPythonInterop checks that we can decode and verify a record produced by the Python
    32  // implementation.
    33  func TestPythonInterop(t *testing.T) {
    34  	var r enr.Record
    35  	if err := rlp.DecodeBytes(pyRecord, &r); err != nil {
    36  		t.Fatalf("can't decode: %v", err)
    37  	}
    38  	n, err := New(ValidSchemes, &r)
    39  	if err != nil {
    40  		t.Fatalf("can't verify record: %v", err)
    41  	}
    42  
    43  	var (
    44  		wantID  = HexID("a448f24c6d18e575453db13171562b71999873db5b286df957af199ec94617f7")
    45  		wantSeq = uint64(1)
    46  		wantIP  = enr.IP{127, 0, 0, 1}
    47  		wantUDP = enr.UDP(30303)
    48  	)
    49  	if n.Seq() != wantSeq {
    50  		t.Errorf("wrong seq: got %d, want %d", n.Seq(), wantSeq)
    51  	}
    52  	if n.ID() != wantID {
    53  		t.Errorf("wrong id: got %x, want %x", n.ID(), wantID)
    54  	}
    55  	want := map[enr.Entry]interface{}{new(enr.IP): &wantIP, new(enr.UDP): &wantUDP}
    56  	for k, v := range want {
    57  		desc := fmt.Sprintf("loading key %q", k.ENRKey())
    58  		if assert.NoError(t, n.Load(k), desc) {
    59  			assert.Equal(t, k, v, desc)
    60  		}
    61  	}
    62  }