github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/p2pserver/peer/peer_test.go (about)

     1  /*
     2   * Copyright (C) 2018 The ontology Authors
     3   * This file is part of The ontology library.
     4   *
     5   * The ontology 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 ontology 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 ontology.  If not, see <http://www.gnu.org/licenses/>.
    17   */
    18  
    19  package peer
    20  
    21  import (
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/ontio/ontology-crypto/keypair"
    26  	"github.com/ontio/ontology/account"
    27  	"github.com/ontio/ontology/common/log"
    28  )
    29  
    30  var p *Peer
    31  var key keypair.PublicKey
    32  
    33  func init() {
    34  	log.Init(log.Stdout)
    35  	p = NewPeer()
    36  	p.base.version = 1
    37  	p.base.services = 1
    38  	p.base.syncPort = 10338
    39  	p.base.consPort = 10339
    40  	p.base.relay = true
    41  	p.base.height = 123355
    42  	p.base.id = 29357734007
    43  	acct := account.NewAccount("SHA256withECDSA")
    44  	key = acct.PubKey()
    45  	p.SetBookKeeperAddr(key)
    46  }
    47  func TestGetPeerComInfo(t *testing.T) {
    48  	p.DumpInfo()
    49  	if p.base.GetVersion() != 1 {
    50  		t.Errorf("PeerCom GetVersion error")
    51  	} else {
    52  		p.base.SetVersion(2)
    53  		if p.base.GetVersion() != 2 {
    54  			t.Errorf("PeerCom SetVersion error")
    55  		}
    56  	}
    57  
    58  	if p.base.GetServices() != 1 {
    59  		t.Errorf("PeerCom GetServices error")
    60  	} else {
    61  		p.base.SetServices(2)
    62  		if p.base.GetServices() != 2 {
    63  			t.Errorf("PeerCom SetServices error")
    64  		}
    65  	}
    66  
    67  	if p.base.GetSyncPort() != 10338 {
    68  		t.Errorf("PeerCom GetSyncPort error")
    69  	} else {
    70  		p.base.SetSyncPort(20338)
    71  		if p.base.GetSyncPort() != 20338 {
    72  			t.Errorf("PeerCom SetSyncPort error")
    73  		}
    74  	}
    75  
    76  	if p.base.GetConsPort() != 10339 {
    77  		t.Errorf("PeerCom GetConsPort error")
    78  	} else {
    79  		p.base.SetConsPort(20339)
    80  		if p.base.GetConsPort() != 20339 {
    81  			t.Errorf("PeerCom SetConsPort error")
    82  		}
    83  	}
    84  
    85  	if p.base.GetRelay() != true {
    86  		t.Errorf("PeerCom GetRelay error")
    87  	} else {
    88  		p.base.SetRelay(false)
    89  		if p.base.GetRelay() != false {
    90  			t.Errorf("PeerCom SetRelay error")
    91  		}
    92  	}
    93  
    94  	if p.base.GetHeight() != 123355 {
    95  		t.Errorf("PeerCom GetHeight error")
    96  	} else {
    97  		p.base.SetHeight(234343497)
    98  		if p.base.GetHeight() != 234343497 {
    99  			t.Errorf("PeerCom SetHeight error")
   100  		}
   101  	}
   102  
   103  	if p.base.GetID() != 29357734007 {
   104  		t.Errorf("PeerCom GetID error")
   105  	} else {
   106  		p.base.SetID(1224322422)
   107  		if p.base.GetID() != 1224322422 {
   108  			t.Errorf("PeerCom SetID error")
   109  		}
   110  	}
   111  	if p.base.GetPubKey() != key {
   112  		t.Errorf("PeerCom GetPubKey error")
   113  	}
   114  }
   115  
   116  func TestUpdatePeer(t *testing.T) {
   117  	p.UpdateInfo(time.Now(), 3, 3, 30334, 30335, 0x7533345, 0, 7322222)
   118  	p.SetConsState(2)
   119  	p.SetSyncState(3)
   120  	p.SetHttpInfoState(true)
   121  	p.SyncLink.SetAddr("127.0.0.1:20338")
   122  	p.DumpInfo()
   123  
   124  }