Skip to content

Commit

Permalink
Update PR and add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gammazero committed May 21, 2019
1 parent 6bae361 commit aa177bb
Show file tree
Hide file tree
Showing 4 changed files with 235 additions and 96 deletions.
47 changes: 18 additions & 29 deletions router/realm.go
Original file line number Diff line number Diff line change
Expand Up @@ -789,24 +789,20 @@ func (r *realm) metaProcedureHandler() {
func (r *realm) sessionCount(msg *wamp.Invocation) wamp.Message {
var filter []string
if len(msg.Arguments) != 0 {
filterList, isOk := wamp.AsList(msg.Arguments[0])

if isOk {
filterList, ok := wamp.AsList(msg.Arguments[0])
if !ok {
return &wamp.Error{
Type: wamp.INVOCATION,
Error: wamp.ErrInvalidArgument,
Request: msg.Request,
}
}

for _, x := range filterList {
role, ok := x.(string)
if ok {
filter = append(filter, role)
} else {
return &wamp.Error{
Error: wamp.ErrInvalidArgument,
Request: msg.Request,
}
filter, ok = wamp.ListToStrings(filterList)
if !ok {
return &wamp.Error{
Type: wamp.INVOCATION,
Error: wamp.ErrInvalidArgument,
Request: msg.Request,
}
}
}
Expand Down Expand Up @@ -845,28 +841,23 @@ func (r *realm) sessionCount(msg *wamp.Invocation) wamp.Message {
func (r *realm) sessionList(msg *wamp.Invocation) wamp.Message {
var filter []string
if len(msg.Arguments) != 0 {
filterList, isOk := wamp.AsList(msg.Arguments[0])

if isOk {
filterList, ok := wamp.AsList(msg.Arguments[0])
if !ok {
return &wamp.Error{
Type: wamp.INVOCATION,
Error: wamp.ErrInvalidArgument,
Request: msg.Request,
}
}

for _, x := range filterList {
role, ok := x.(string)
if ok {
filter = append(filter, role)
} else {
return &wamp.Error{
Error: wamp.ErrInvalidArgument,
Request: msg.Request,
}
filter, ok = wamp.ListToStrings(filterList)
if !ok {
return &wamp.Error{
Type: wamp.INVOCATION,
Error: wamp.ErrInvalidArgument,
Request: msg.Request,
}
}
}

retChan := make(chan []wamp.ID)

if len(filter) == 0 {
Expand Down Expand Up @@ -1100,12 +1091,10 @@ func (r *realm) testamentAdd(msg *wamp.Invocation) wamp.Message {
}
topic, ok := wamp.AsURI(msg.Arguments[0])
if !ok {
fmt.Printf("invalid topic")
return makeError(msg.Request, wamp.ErrInvalidArgument)
}
args, ok := wamp.AsList(msg.Arguments[1])
if !ok {
fmt.Printf("invalid args")
return makeError(msg.Request, wamp.ErrInvalidArgument)
}
kwargs, ok := wamp.AsDict(msg.Arguments[2])
Expand Down
253 changes: 186 additions & 67 deletions router/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,119 @@ func TestRouterCall(t *testing.T) {
}
}

func TestSessionMetaProcedures(t *testing.T) {
func TestSessionCountMetaProcedure(t *testing.T) {
defer leaktest.Check(t)()
r, err := newTestRouter()
if err != nil {
t.Error(err)
}
defer r.Close()

caller, err := testClient(r)
if err != nil {
t.Fatal(err)
}

// Call wamp.MetaProcSessionCount
req := &wamp.Call{Request: wamp.GlobalID(), Procedure: wamp.MetaProcSessionCount}
caller.Send(req)
msg, err := wamp.RecvTimeout(caller, time.Second)
if err != nil {
t.Fatal(err)
}
result, ok := msg.(*wamp.Result)
if !ok {
t.Fatal("expected RESULT, got", msg.MessageType())
}
if result.Request != req.Request {
t.Fatal("wrong result ID")
}
if len(result.Arguments) == 0 {
t.Fatal("missing expected arguemnt")
}
count, ok := result.Arguments[0].(int)
if !ok {
t.Fatal("expected int arguemnt")
}
if count != 1 {
t.Fatal("wrong session count")
}

// Call wamp.MetaProcSessionCount with invalid argument
req = &wamp.Call{
Request: wamp.GlobalID(),
Procedure: wamp.MetaProcSessionCount,
Arguments: wamp.List{"should-be-a-list"},
}
caller.Send(req)
msg, err = wamp.RecvTimeout(caller, time.Second)
if err != nil {
t.Fatal(err)
}
errResult, ok := msg.(*wamp.Error)
if !ok {
t.Fatal("expected ERROR, got", msg.MessageType())
}
if errResult.Request != req.Request {
t.Fatal("wrong result ID")
}

// Call wamp.MetaProcSessionCount with non-matching filter
filter := wamp.List{"user", "def"}
req = &wamp.Call{
Request: wamp.GlobalID(),
Procedure: wamp.MetaProcSessionCount,
Arguments: wamp.List{filter},
}
caller.Send(req)
msg, err = wamp.RecvTimeout(caller, time.Second)
if err != nil {
t.Fatal(err)
}
result, ok = msg.(*wamp.Result)
if !ok {
t.Fatal("expected RESULT, got", msg.MessageType())
}
if result.Request != req.Request {
t.Fatal("wrong result ID")
}
if len(result.Arguments) == 0 {
t.Fatal("missing expected arguemnt")
}
count = result.Arguments[0].(int)
if count != 0 {
t.Fatal("wrong session count")
}

// Call wamp.MetaProcSessionCount with matching filter
filter = wamp.List{"trusted", "user", "def"}
req = &wamp.Call{
Request: wamp.GlobalID(),
Procedure: wamp.MetaProcSessionCount,
Arguments: wamp.List{filter},
}
caller.Send(req)
msg, err = wamp.RecvTimeout(caller, time.Second)
if err != nil {
t.Fatal(err)
}
result, ok = msg.(*wamp.Result)
if !ok {
t.Fatal("expected RESULT, got", msg.MessageType())
}
if result.Request != req.Request {
t.Fatal("wrong result ID")
}
if len(result.Arguments) == 0 {
t.Fatal("missing expected arguemnt")
}
count = result.Arguments[0].(int)
if count != 1 {
t.Fatal("wrong session count")
}
}

func TestListSessionMetaProcedures(t *testing.T) {
defer leaktest.Check(t)()
r, err := newTestRouter()
if err != nil {
Expand All @@ -410,77 +522,84 @@ func TestSessionMetaProcedures(t *testing.T) {
}
sessID := caller.ID

// Call session meta-procedure to get session count.
sessionCountRequests := []*wamp.Call{
// Normal call
&wamp.Call{Request: wamp.GlobalID(), Procedure: wamp.MetaProcSessionCount},
// Call with extra arguments (but invalid)
&wamp.Call{Request: wamp.GlobalID(), Procedure: wamp.MetaProcSessionCount, Arguments: wamp.List{"invalidarg"}},
}
var msg wamp.Message
for _, req := range sessionCountRequests {
callID := req.Request
caller.Send(req)
msg, err = wamp.RecvTimeout(caller, time.Second)
if err != nil {
t.Fatal(err)
}
result, ok := msg.(*wamp.Result)
if !ok {
t.Fatal("expected RESULT, got", msg.MessageType())
}
if result.Request != callID {
t.Fatal("wrong result ID")
}
// Call wamp.MetaProcSessionList to get session list.
req := &wamp.Call{
Request: wamp.GlobalID(),
Procedure: wamp.MetaProcSessionList,
}
caller.Send(req)
msg, err := wamp.RecvTimeout(caller, time.Second)
if err != nil {
t.Fatal(err)
}
result, ok := msg.(*wamp.Result)
if !ok {
t.Fatal("expected RESULT, got", msg.MessageType())
}
if result.Request != req.Request {
t.Fatal("wrong result ID")
}
if len(result.Arguments) == 0 {
t.Fatal("missing expected arguemnt")
}
ids, ok := result.Arguments[0].([]wamp.ID)
if !ok {
t.Fatal("wrong arg type")
}
if len(ids) != 1 {
t.Fatal("wrong number of session IDs")
}
if sessID != ids[0] {
t.Fatal("wrong session ID")
}

if len(result.Arguments) == 0 {
t.Fatal("missing expected arguemnt")
}
count, ok := result.Arguments[0].(int)
if !ok {
t.Fatal("expected int arguemnt")
}
if count != 1 {
t.Fatal("wrong session count")
}
// Call wamp.MetaProcSessionList with matching filter
filter := wamp.List{"trusted"}
req = &wamp.Call{
Request: wamp.GlobalID(),
Procedure: wamp.MetaProcSessionList,
Arguments: wamp.List{filter},
}
caller.Send(req)
msg, err = wamp.RecvTimeout(caller, time.Second)
if err != nil {
t.Fatal(err)
}
result, ok = msg.(*wamp.Result)
if !ok {
t.Fatal("expected RESULT, got", msg.MessageType())
}
if result.Request != req.Request {
t.Fatal("wrong result ID")
}
if len(result.Arguments) == 0 {
t.Fatal("missing expected arguemnt")
}
ids, ok = result.Arguments[0].([]wamp.ID)
if !ok {
t.Fatal("wrong arg type")
}
if len(ids) != 1 {
t.Fatal("wrong number of session IDs")
}
if sessID != ids[0] {
t.Fatal("wrong session ID")
}
}

// Call session meta-procedure to get session list.
sessionListRequests := []*wamp.Call{
// Normal call
&wamp.Call{Request: wamp.GlobalID(), Procedure: wamp.MetaProcSessionList},
// Call with extra arguments (but invalid)
&wamp.Call{Request: wamp.GlobalID(), Procedure: wamp.MetaProcSessionList, Arguments: wamp.List{"invalidarg"}},
func TestGetSessionMetaProcedures(t *testing.T) {
defer leaktest.Check(t)()
r, err := newTestRouter()
if err != nil {
t.Error(err)
}
for _, req := range sessionListRequests {
callID := req.Request
caller.Send(&wamp.Call{Request: callID, Procedure: wamp.MetaProcSessionList})
msg, err = wamp.RecvTimeout(caller, time.Second)
if err != nil {
t.Fatal(err)
}
result, ok := msg.(*wamp.Result)
if !ok {
t.Fatal("expected RESULT, got", msg.MessageType())
}
if result.Request != callID {
t.Fatal("wrong result ID")
}
defer r.Close()

if len(result.Arguments) == 0 {
t.Fatal("missing expected arguemnt")
}
ids, ok := result.Arguments[0].([]wamp.ID)
if !ok {
t.Fatal("wrong arg type")
}
if len(ids) != 1 {
t.Fatal("wrong number of session IDs")
}
if sessID != ids[0] {
t.Fatal("wrong session ID")
}
caller, err := testClient(r)
if err != nil {
t.Fatal(err)
}
sessID := caller.ID

// Call session meta-procedure with bad session ID
callID := wamp.GlobalID()
Expand All @@ -489,7 +608,7 @@ func TestSessionMetaProcedures(t *testing.T) {
Procedure: wamp.MetaProcSessionGet,
Arguments: wamp.List{wamp.ID(123456789)},
})
msg, err = wamp.RecvTimeout(caller, time.Second)
msg, err := wamp.RecvTimeout(caller, time.Second)
if err != nil {
t.Fatal(err)
}
Expand Down
17 changes: 17 additions & 0 deletions wamp/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,23 @@ func AsList(v interface{}) (List, bool) {
return list, true
}

// ListToStrings converts a List to a slice of string. Returns the string
// slice and a boolean indicating if the conversion was successful.
func ListToStrings(list List) ([]string, bool) {
if len(list) == 0 {
return nil, true
}
strs := make([]string, len(list))
for i := range list {
s, ok := AsString(list[i])
if !ok {
return nil, false
}
strs[i] = s
}
return strs, true
}

// OptionString returns named value as string; empty string if missing or not
// string type.
func OptionString(opts Dict, optionName string) string {
Expand Down
Loading

0 comments on commit aa177bb

Please sign in to comment.