ethwire.ClientIdentity now handles Client info sent in handshake + test

This commit is contained in:
zelig 2014-07-03 17:28:27 +01:00
parent de2da4fd19
commit c64629964f
2 changed files with 84 additions and 0 deletions

View File

@ -0,0 +1,54 @@
package ethwire
import (
"fmt"
"runtime"
)
// should be used in Peer handleHandshake, incorporate Caps, ProtocolVersion, Pubkey etc.
type ClientIdentity interface {
String() string
}
type SimpleClientIdentity struct {
clientString string
clientIdentifier string
version string
customIdentifier string
os string
implementation string
}
func NewSimpleClientIdentity(clientIdentifier string, version string, customIdentifier string) *SimpleClientIdentity {
clientIdentity := &SimpleClientIdentity{
clientIdentifier: clientIdentifier,
version: version,
customIdentifier: customIdentifier,
os: runtime.GOOS,
implementation: "Go",
}
clientIdentity.init()
return clientIdentity
}
func (c *SimpleClientIdentity) init() {
c.clientString = fmt.Sprintf("%s/v%s/%s/%s/%s",
c.clientIdentifier,
c.version,
c.customIdentifier,
c.os,
c.implementation)
}
func (c *SimpleClientIdentity) String() string {
return c.clientString
}
func (c *SimpleClientIdentity) SetCustomIdentifier(customIdentifier string) {
c.customIdentifier = customIdentifier
c.init()
}
func (c *SimpleClientIdentity) GetCustomIdentifier() string {
return c.customIdentifier
}

View File

@ -0,0 +1,30 @@
package ethwire
import (
"fmt"
"runtime"
"testing"
)
func TestClientIdentity(t *testing.T) {
clientIdentity := NewSimpleClientIdentity("Ethereum(G)", "0.5.16", "test")
clientString := clientIdentity.String()
expected := fmt.Sprintf("Ethereum(G)/v0.5.16/test/%s/Go", runtime.GOOS)
if clientString != expected {
t.Error("Expected clientIdentity to be %v, got %v", expected, clientString)
}
customIdentifier := clientIdentity.GetCustomIdentifier()
if customIdentifier != "test" {
t.Error("Expected clientIdentity.GetCustomIdentifier() to be 'test', got %v", customIdentifier)
}
clientIdentity.SetCustomIdentifier("test2")
customIdentifier = clientIdentity.GetCustomIdentifier()
if customIdentifier != "test2" {
t.Error("Expected clientIdentity.GetCustomIdentifier() to be 'test2', got %v", customIdentifier)
}
clientString = clientIdentity.String()
expected = fmt.Sprintf("Ethereum(G)/v0.5.16/test2/%s/Go", runtime.GOOS)
if clientString != expected {
t.Error("Expected clientIdentity to be %v, got %v", expected, clientString)
}
}