From 0512bb766497ee9e8c1b954e044919bd3a27562c Mon Sep 17 00:00:00 2001 From: Stephen McQuay Date: Mon, 26 Aug 2013 23:09:39 -0700 Subject: [PATCH] added test for out of order conf, found bug that needs fixing --- protocol.go | 3 +-- protocol_test.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/protocol.go b/protocol.go index e4eb53e..c19e0df 100644 --- a/protocol.go +++ b/protocol.go @@ -3,7 +3,6 @@ package main import ( "code.google.com/p/go.net/websocket" "errors" - "log" ) // > identify @@ -114,9 +113,9 @@ func Negociate(ws *websocket.Conn, id string, width, height float64) (*Config, e for { err = websocket.JSON.Receive(ws, &conf) if err != nil { - log.Print(err) return nil, err } + // TODO: verify conf's type if conf.Stats.valid() { _ = websocket.JSON.Send(ws, NewHandshake(id, true)) break diff --git a/protocol_test.go b/protocol_test.go index c1acf03..d1d09ef 100644 --- a/protocol_test.go +++ b/protocol_test.go @@ -169,3 +169,55 @@ func TestBadUseragent(t *testing.T) { t.Errorf("tried sending bad type, didn't detect it") } } + +func TestOutOfOrderProtocol(t *testing.T) { + ws, err := DummyServer() + + // send early here: + err = websocket.JSON.Send(ws, ClientID{ + "robot", + "smcquay test robot 2", + "unittest", + }) + if err != nil { + t.Error(err) + } + + var idreq IdRequest + err = websocket.JSON.Receive(ws, &idreq) + if idreq.Type != "idreq" { + t.Errorf("Server skipped a step (idreq)") + } + + // XXX: this thing gets interpreted server-side as a Config ... + err = websocket.JSON.Send(ws, ClientID{ + Type: "why doesn't this matter?", + Name: "smcquay test robot 2", + Useragent: "unittest", + }) + if err != nil { + t.Error(err) + } + + var gameparam GameParam + err = websocket.JSON.Receive(ws, &gameparam) + if gameparam.Type != "gameparam" { + t.Errorf("didn't receive a good gameparam") + } + + err = websocket.JSON.Send(ws, &Config{ + "bogus", + stats{ + Hp: 200000, + Speed: 10000, + WeaponRadius: 5000, + ScannerRadius: 50, + }, + }) + + var handshake Handshake + websocket.JSON.Receive(ws, &handshake) + if !handshake.Success { + t.Errorf("failed to validate correct stats request") + } +}