Skip to content

Commit

Permalink
add testcase: handshake, handshaketimeout, handshakewrongmsg, heartbeat
Browse files Browse the repository at this point in the history
  • Loading branch information
dylenfu committed May 11, 2020
1 parent c42fa9c commit 705486c
Show file tree
Hide file tree
Showing 12 changed files with 213 additions and 51 deletions.
59 changes: 45 additions & 14 deletions common/test_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,62 @@ import "time"
// handshake test cases
const (
HandshakeNormal = iota
Handshake_StopAfterSendVersion
Handshake_StopAfterReceiveVersion
Handshake_StopAfterUpdateKad
Handshake_StopAfterReadKad
Handshake_StopAfterSendAck
Handshake_StopAfterReadAck
Handshake_StopClientAfterSendVersion
Handshake_StopClientAfterReceiveVersion
Handshake_StopClientAfterUpdateKad
Handshake_StopClientAfterReadKad
Handshake_StopClientAfterSendAck
Handshake_StopServerAfterSendVersion
Handshake_StopServerAfterReceiveVersion
Handshake_StopServerAfterUpdateKad
Handshake_StopServerAfterReadKad
Handshake_StopServerAfterReadAck
)

var (
HandshakeLevel uint8 = HandshakeNormal // default normal
HandshakeDuration time.Duration = time.Duration(10) * time.Second // default value: 10 sec
HandshakeLevel uint8
HandshakeWrongMsg bool
HandshakeDuration time.Duration
HeartbeatBlockHeight uint64
)

func SetHandshakeLevel(lvl uint8) {
var (
DefHandshakeStopLevel uint8 = HandshakeNormal
DefHandshakeWrongMsg = false
DefHandshakeTimeout = time.Duration(10) * time.Second
DefHeartbeatBlockHeight uint64= 9442
)

func InitializeTestParams() {
HandshakeLevel = DefHandshakeStopLevel
HandshakeWrongMsg = DefHandshakeWrongMsg
HandshakeDuration = DefHandshakeTimeout
HeartbeatBlockHeight = DefHeartbeatBlockHeight
}

func Reset() {
InitializeTestParams()
}

// handshake stop level
func SetHandshakeStopLevel(lvl uint8) {
HandshakeLevel = lvl
}
func StopHandshake(lvl uint8) bool {
func ValidateHandshakeStopLevel(lvl uint8) bool {
return HandshakeLevel == lvl
}
func SetHandshakeDuraion(sec int) {

// handshake wrong msg
func SetHandshakeWrongMsg(active bool) {
HandshakeWrongMsg = active
}

// handshake timeout
func SetHandshakeTestDuraion(sec int) {
HandshakeDuration = time.Duration(sec) * time.Second
}

// heartbeat test cases
var HeartbeatBlockHeight uint64 = 358 // default 100000
func SetHeartbeatBlockHeight(height uint64) {
// heartbeat
func SetHeartbeatTestBlockHeight(height uint64) {
HeartbeatBlockHeight = height
}
8 changes: 8 additions & 0 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@ import (
var OntTool = NewOntologyTool()

type Method func() bool
type GcFunc func()

type OntologyTool struct {
//Map name to method
methodsMap map[string]Method
//Map method result
methodsRes map[string]bool
//gc func
gc GcFunc
}

func NewOntologyTool() *OntologyTool {
Expand All @@ -44,6 +47,10 @@ func (this *OntologyTool) RegMethod(name string, method Method) {
this.methodsMap[name] = method
}

func (this *OntologyTool) RegGCFunc(fn GcFunc) {
this.gc = fn
}

//Start run
func (this *OntologyTool) Start(methodsList []string) {
if len(methodsList) > 0 {
Expand All @@ -69,6 +76,7 @@ func (this *OntologyTool) runMethod(index int, methodName string) {
ok := method()
this.onAfterMethodFinish(index, methodName, ok)
this.methodsRes[methodName] = ok
this.gc()
}
}

Expand Down
7 changes: 7 additions & 0 deletions methods/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,17 @@
package methods

import (
"github.com/ontio/ontology-tool/common"
"github.com/ontio/ontology-tool/core"
)

func init() {
common.InitializeTestParams()
core.OntTool.RegGCFunc(reset)

core.OntTool.RegMethod("demo", Demo)
core.OntTool.RegMethod("handshake", Handshake)
core.OntTool.RegMethod("handshakeTimeout", HandshakeTimeout)
core.OntTool.RegMethod("handshakeWrongMsg", HandshakeWrongMsg)
core.OntTool.RegMethod("heartbeat", Heartbeat)
}
108 changes: 94 additions & 14 deletions methods/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ var (
tr *timer.Timer
)

func Demo() bool {
log4.Info("hello, dht demo")
return true
}

func setup(protocol p2p.Protocol) {
var err error

Expand All @@ -51,36 +46,121 @@ func setup(protocol p2p.Protocol) {
tr = timer.NewTimer(2)
}

func reset() {
ns.Stop()
common.Reset()
ns = nil
tr = nil
}

// methods
func Demo() bool {
log4.Info("hello, dht demo")
return true
}

func Handshake() bool {

// 1. get params from json file
var params struct {
Remote string
HeartbeatTime int
Remote string
TestCase uint8
}
if err := getParamsFromJsonFile("./params/Handshake.json", &params); err != nil {
log4.Error("%s", err)
_ = log4.Error("%s", err)
return false
}

// 2. set common params
common.SetHandshakeDuraion(10)
common.SetHandshakeLevel(common.HandshakeNormal)
common.SetHeartbeatBlockHeight(358)
common.SetHandshakeStopLevel(params.TestCase)

// 3. setup p2p.protocols
protocol := protocols.NewOnlyHeartbeatMsgHandler()
setup(protocol)

// 4. connect and handshake
if err := ns.Connect(params.Remote); err != nil {
log4.Debug("connecting to %s failed, err: %s", params.Remote, err)
_ = log4.Error("connecting to %s failed, err: %s", params.Remote, err)
return false
}

// 5. dispatch
dispatch(params.HeartbeatTime)
log4.Info("handshake end!")

return true
}

func HandshakeWrongMsg() bool {

// 1. get params from json file
var params struct {
Remote string
WrongMsg bool
}
if err := getParamsFromJsonFile("./params/HandshakeWrongMsg.json", &params); err != nil {
_ = log4.Error("%s", err)
return false
}

protocol := protocols.NewOnlyHeartbeatMsgHandler()
setup(protocol)

common.SetHandshakeWrongMsg(params.WrongMsg)
if err := ns.Connect(params.Remote); err != nil {
_ = log4.Error("connecting to %s failed, err: %s", params.Remote, err)
return false
}

log4.Info("handshakeWrongMsg end!")

return true
}

func HandshakeTimeout() bool {
var params struct {
Remote string
Timeout int
}
if err := getParamsFromJsonFile("./params/HandshakeTimeout.json", &params); err != nil {
_ = log4.Error("%s", err)
return false
}

protocol := protocols.NewOnlyHeartbeatMsgHandler()
setup(protocol)

common.SetHandshakeTestDuraion(params.Timeout)
if err := ns.Connect(params.Remote); err != nil {
_ = log4.Error("connecting to %s failed, err: %s", params.Remote, err)
return false
}

log4.Info("handshakeTimeout end!")

return true
}

func Heartbeat() bool {
var params struct {
Remote string
InitBlockHeight uint64
DispatchTime int
}
if err := getParamsFromJsonFile("./params/Heartbeat.json", &params); err != nil {
_ = log4.Error("%s", err)
return false
}

protocol := protocols.NewOnlyHeartbeatMsgHandler()
setup(protocol)

common.SetHeartbeatTestBlockHeight(params.InitBlockHeight)
if err := ns.Connect(params.Remote); err != nil {
_ = log4.Error("connecting to %s failed, err: %s", params.Remote, err)
return false
}

dispatch(params.DispatchTime)

log4.Info("heartbeat end!")
return true
}
34 changes: 21 additions & 13 deletions p2pserver/handshake/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,20 @@ func HandshakeClient(info *peer.PeerInfo, selfId *common.PeerKeyId, conn net.Con
}()

// 1. sendMsg version
err := sendMsg(conn, version)
if err != nil {
return nil, err
if tcm.HandshakeWrongMsg {
err := sendMsg(conn, &types.Addr{})
if err != nil {
return nil, err
}
} else {
err := sendMsg(conn, version)
if err != nil {
return nil, err
}
}

// mark:
if tcm.StopHandshake(tcm.Handshake_StopAfterSendVersion) {
if tcm.ValidateHandshakeStopLevel(tcm.Handshake_StopClientAfterSendVersion) {
return nil, fmt.Errorf("client handshake stopped after send version")
}

Expand All @@ -58,7 +66,7 @@ func HandshakeClient(info *peer.PeerInfo, selfId *common.PeerKeyId, conn net.Con
return nil, fmt.Errorf("expected version message, but got message type: %s", msg.CmdType())
}
// mark:
if tcm.StopHandshake(tcm.Handshake_StopAfterReceiveVersion) {
if tcm.ValidateHandshakeStopLevel(tcm.Handshake_StopClientAfterReceiveVersion) {
return nil, fmt.Errorf("client handshake stopped after receive version")
}

Expand All @@ -70,7 +78,7 @@ func HandshakeClient(info *peer.PeerInfo, selfId *common.PeerKeyId, conn net.Con
return nil, err
}
// mark:
if tcm.StopHandshake(tcm.Handshake_StopAfterUpdateKad) {
if tcm.ValidateHandshakeStopLevel(tcm.Handshake_StopClientAfterUpdateKad) {
return nil, fmt.Errorf("client handshake stopped after update kad")
}

Expand All @@ -84,7 +92,7 @@ func HandshakeClient(info *peer.PeerInfo, selfId *common.PeerKeyId, conn net.Con
return nil, fmt.Errorf("handshake failed, expect kad id message, got %s", msg.CmdType())
}
// mark:
if tcm.StopHandshake(tcm.Handshake_StopAfterReadKad) {
if tcm.ValidateHandshakeStopLevel(tcm.Handshake_StopClientAfterReadKad) {
return nil, fmt.Errorf("client handshake stopped after read kad")
}

Expand All @@ -97,7 +105,7 @@ func HandshakeClient(info *peer.PeerInfo, selfId *common.PeerKeyId, conn net.Con
return nil, err
}
// mark:
if tcm.StopHandshake(tcm.Handshake_StopAfterSendAck) {
if tcm.ValidateHandshakeStopLevel(tcm.Handshake_StopClientAfterSendAck) {
return nil, fmt.Errorf("client handshake stopped after send ack")
}

Expand Down Expand Up @@ -132,7 +140,7 @@ func HandshakeServer(info *peer.PeerInfo, selfId *common.PeerKeyId, conn net.Con
}
version := msg.(*types.Version)
// mark:
if tcm.StopHandshake(tcm.Handshake_StopAfterReceiveVersion) {
if tcm.ValidateHandshakeStopLevel(tcm.Handshake_StopServerAfterReceiveVersion) {
return nil, fmt.Errorf("server handshake stopped after receive version")
}

Expand All @@ -142,7 +150,7 @@ func HandshakeServer(info *peer.PeerInfo, selfId *common.PeerKeyId, conn net.Con
return nil, err
}
// mark:
if tcm.StopHandshake(tcm.Handshake_StopAfterSendVersion) {
if tcm.ValidateHandshakeStopLevel(tcm.Handshake_StopServerAfterSendVersion) {
return nil, fmt.Errorf("server handshake stopped after send version")
}

Expand All @@ -159,7 +167,7 @@ func HandshakeServer(info *peer.PeerInfo, selfId *common.PeerKeyId, conn net.Con
}
kid = kadkeyId.KadKeyId.Id
// mark:
if tcm.StopHandshake(tcm.Handshake_StopAfterReadKad) {
if tcm.ValidateHandshakeStopLevel(tcm.Handshake_StopServerAfterReadKad) {
return nil, fmt.Errorf("server handshake stopped after read kad")
}

Expand All @@ -169,7 +177,7 @@ func HandshakeServer(info *peer.PeerInfo, selfId *common.PeerKeyId, conn net.Con
return nil, err
}
// mark:
if tcm.StopHandshake(tcm.Handshake_StopAfterUpdateKad) {
if tcm.ValidateHandshakeStopLevel(tcm.Handshake_StopServerAfterUpdateKad) {
return nil, fmt.Errorf("server handshake stopped after update kad")
}
}
Expand All @@ -183,7 +191,7 @@ func HandshakeServer(info *peer.PeerInfo, selfId *common.PeerKeyId, conn net.Con
return nil, fmt.Errorf("[HandshakeServer] expected version ack message")
}
// mark:
if tcm.StopHandshake(tcm.Handshake_StopAfterReadAck) {
if tcm.ValidateHandshakeStopLevel(tcm.Handshake_StopServerAfterReadAck) {
return nil, fmt.Errorf("server handshake stopped after read ack")
}

Expand Down
Loading

0 comments on commit 705486c

Please sign in to comment.