github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/p2p/enode/node_test.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 19:16:41</date>
    10  //</624450104551542784>
    11  
    12  
    13  package enode
    14  
    15  import (
    16  	"encoding/hex"
    17  	"fmt"
    18  	"testing"
    19  
    20  	"github.com/ethereum/go-ethereum/p2p/enr"
    21  	"github.com/ethereum/go-ethereum/rlp"
    22  	"github.com/stretchr/testify/assert"
    23  )
    24  
    25  var pyRecord, _ = hex.DecodeString("f884b8407098ad865b00a582051940cb9cf36836572411a47278783077011599ed5cd16b76f2635f4e234738f30813a89eb9137e3e3df5266e3a1f11df72ecf1145ccb9c01826964827634826970847f00000189736563703235366b31a103ca634cae0d49acb401d8a4c6b6fe8c55b70d115bf400769cc1400f3258cd31388375647082765f")
    26  
    27  //testpythorintrop检查是否可以解码和验证由python生成的记录
    28  //实施。
    29  func TestPythonInterop(t *testing.T) {
    30  	var r enr.Record
    31  	if err := rlp.DecodeBytes(pyRecord, &r); err != nil {
    32  		t.Fatalf("can't decode: %v", err)
    33  	}
    34  	n, err := New(ValidSchemes, &r)
    35  	if err != nil {
    36  		t.Fatalf("can't verify record: %v", err)
    37  	}
    38  
    39  	var (
    40  		wantID  = HexID("a448f24c6d18e575453db13171562b71999873db5b286df957af199ec94617f7")
    41  		wantSeq = uint64(1)
    42  		wantIP  = enr.IP{127, 0, 0, 1}
    43  		wantUDP = enr.UDP(30303)
    44  	)
    45  	if n.Seq() != wantSeq {
    46  		t.Errorf("wrong seq: got %d, want %d", n.Seq(), wantSeq)
    47  	}
    48  	if n.ID() != wantID {
    49  		t.Errorf("wrong id: got %x, want %x", n.ID(), wantID)
    50  	}
    51  	want := map[enr.Entry]interface{}{new(enr.IP): &wantIP, new(enr.UDP): &wantUDP}
    52  	for k, v := range want {
    53  		desc := fmt.Sprintf("loading key %q", k.ENRKey())
    54  		if assert.NoError(t, n.Load(k), desc) {
    55  			assert.Equal(t, k, v, desc)
    56  		}
    57  	}
    58  }
    59