Skip to content
Snippets Groups Projects
Commit cc4813b0 authored by Jake Taylor's avatar Jake Taylor
Browse files

fix tests

parent f9c187ea
No related branches found
No related tags found
3 merge requests!510Release,!220Xx 3916/restlike conn,!207WIP: Client Restructure
......@@ -17,15 +17,15 @@ import (
"google.golang.org/protobuf/proto"
)
// processor is the reception handler for a RestServer
type connectReceiver struct {
// receiver is the reception handler for a RestServer
type receiver struct {
conn connect.Connection
endpoints *restlike.Endpoints
}
// Hear handles connect.Connection message reception for a RestServer
// Automatically responds to invalid endpoint requests
func (c connectReceiver) Hear(item receive.Message) {
func (c receiver) Hear(item receive.Message) {
// Unmarshal the request payload
newMessage := &restlike.Message{}
err := proto.Unmarshal(item.Payload, newMessage)
......@@ -37,24 +37,24 @@ func (c connectReceiver) Hear(item receive.Message) {
var respondErr error
if cb, err := c.endpoints.Get(restlike.URI(newMessage.GetUri()), restlike.Method(newMessage.GetMethod())); err == nil {
// Send the payload to the proper Callback if it exists and singleRespond with the result
respondErr = connRespond(cb(newMessage), c.conn)
respondErr = respond(cb(newMessage), c.conn)
} else {
// If no callback, automatically send an error response
respondErr = connRespond(&restlike.Message{Error: err.Error()}, c.conn)
respondErr = respond(&restlike.Message{Error: err.Error()}, c.conn)
}
if respondErr != nil {
jww.ERROR.Printf("Unable to singleRespond to request: %+v", err)
}
}
// connRespond to connect.Connection with the given Message
func connRespond(response *restlike.Message, conn connect.Connection) error {
// respond to connect.Connection with the given Message
func respond(response *restlike.Message, conn connect.Connection) error {
payload, err := proto.Marshal(response)
if err != nil {
return errors.Errorf("unable to marshal restlike response message: %+v", err)
}
// TODO: Parameterize params and timeout
// TODO: Parameterize params
_, _, _, err = conn.SendE2E(catalog.XxMessage, payload, e2e.GetDefaultParams())
if err != nil {
return errors.Errorf("unable to send restlike response message: %+v", err)
......@@ -63,6 +63,6 @@ func connRespond(response *restlike.Message, conn connect.Connection) error {
}
// Name is used for debugging
func (c connectReceiver) Name() string {
func (c receiver) Name() string {
return "Restlike"
}
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2022 Privategrity Corporation /
// /
// All rights reserved. /
////////////////////////////////////////////////////////////////////////////////
package connect
import (
"gitlab.com/elixxir/client/e2e/receive"
"gitlab.com/elixxir/client/restlike"
"testing"
)
// Test failure of proto unmarshal
func TestSingleReceiver_Callback_FailUnmarshal(t *testing.T) {
ep := restlike.NewEndpoints()
r := receiver{endpoints: ep}
r.Hear(receive.Message{Payload: []byte("test")})
}
// Test happy path
//func TestSingleReceiver_Callback(t *testing.T) {
// ep := &Endpoints{endpoints: make(map[URI]map[Method]Callback)}
// resultChan := make(chan interface{}, 1)
// cb := func(*Message) *Message {
// resultChan <- ""
// return nil
// }
// testPath := URI("test/path")
// testMethod := Get
// testMessage := &Message{
// Content: []byte("test"),
// Headers: nil,
// Method: uint32(testMethod),
// Uri: string(testPath),
// Error: "",
// }
//
// err := ep.Add(testPath, testMethod, cb)
// if err != nil {
// t.Errorf(err.Error())
// }
// receiver := singleReceiver{endpoints: ep}
//
// testPayload, err := proto.Marshal(testMessage)
// if err != nil {
// t.Errorf(err.Error())
// }
// testReq := single.BuildTestRequest(testPayload, t)
// receiver.Callback(testReq, receptionID.EphemeralIdentity{}, nil)
//
// select {
// case _ = <-resultChan:
// case <-time.After(3 * time.Second):
// t.Errorf("Test SingleReceiver timed out!")
// }
//}
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2022 Privategrity Corporation /
// /
// All rights reserved. /
////////////////////////////////////////////////////////////////////////////////
package connect
import (
"bytes"
"gitlab.com/elixxir/client/e2e/receive"
"gitlab.com/elixxir/client/restlike"
"google.golang.org/protobuf/proto"
"testing"
"time"
)
// Test happy path
func TestSingleResponse_Callback(t *testing.T) {
resultChan := make(chan *restlike.Message, 1)
cb := func(input *restlike.Message) {
resultChan <- input
}
testPath := "test/path"
testMethod := restlike.Get
testMessage := &restlike.Message{
Content: []byte("test"),
Headers: nil,
Method: uint32(testMethod),
Uri: testPath,
Error: "",
}
r := response{cb}
testPayload, err := proto.Marshal(testMessage)
if err != nil {
t.Errorf(err.Error())
}
r.Hear(receive.Message{Payload: testPayload})
select {
case result := <-resultChan:
if result.Uri != testPath {
t.Errorf("Mismatched uri")
}
if result.Method != uint32(testMethod) {
t.Errorf("Mismatched method")
}
if !bytes.Equal(testMessage.Content, result.Content) {
t.Errorf("Mismatched content")
}
case <-time.After(3 * time.Second):
t.Errorf("Test SingleResponse timed out!")
}
}
// Test proto error path
func TestSingleResponse_Callback_ProtoErr(t *testing.T) {
resultChan := make(chan *restlike.Message, 1)
cb := func(input *restlike.Message) {
resultChan <- input
}
r := response{cb}
r.Hear(receive.Message{Payload: []byte("test")})
select {
case result := <-resultChan:
if len(result.Error) == 0 {
t.Errorf("Expected cb proto error!")
}
case <-time.After(3 * time.Second):
t.Errorf("Test SingleResponse proto error timed out!")
}
}
......@@ -33,7 +33,7 @@ func NewServer(receptionId *id.ID, privKey *cyclic.Int,
// Callback for connection requests
cb := func(conn connect.Connection) {
handler := connectReceiver{endpoints: newServer.endpoints, conn: conn}
handler := receiver{endpoints: newServer.endpoints, conn: conn}
conn.RegisterListener(catalog.XxMessage, handler)
}
......
......@@ -18,14 +18,14 @@ import (
"time"
)
// processor is the reception handler for a RestServer
type singleReceiver struct {
// receiver is the reception handler for a RestServer
type receiver struct {
endpoints *restlike.Endpoints
}
// Callback is the handler for single-use message reception for a RestServer
// Automatically responds to invalid endpoint requests
func (s *singleReceiver) Callback(req *single.Request, receptionId receptionID.EphemeralIdentity, rounds []rounds.Round) {
func (s *receiver) Callback(req *single.Request, receptionId receptionID.EphemeralIdentity, rounds []rounds.Round) {
// Unmarshal the request payload
newMessage := &restlike.Message{}
err := proto.Unmarshal(req.GetPayload(), newMessage)
......
......@@ -16,10 +16,10 @@ import (
// Test failure of proto unmarshal
func TestSingleReceiver_Callback_FailUnmarshal(t *testing.T) {
ep := restlike.NewEndpoints()
receiver := singleReceiver{endpoints: ep}
r := receiver{endpoints: ep}
testReq := single.BuildTestRequest(make([]byte, 0), t)
receiver.Callback(testReq, receptionID.EphemeralIdentity{}, nil)
r.Callback(testReq, receptionID.EphemeralIdentity{}, nil)
}
// Test happy path
......@@ -44,7 +44,7 @@ func TestSingleReceiver_Callback_FailUnmarshal(t *testing.T) {
// if err != nil {
// t.Errorf(err.Error())
// }
// receiver := singleReceiver{endpoints: ep}
// receiver := receiver{endpoints: ep}
//
// testPayload, err := proto.Marshal(testMessage)
// if err != nil {
......
......@@ -32,13 +32,13 @@ func TestSingleResponse_Callback(t *testing.T) {
Error: "",
}
response := response{cb}
r := response{cb}
testPayload, err := proto.Marshal(testMessage)
if err != nil {
t.Errorf(err.Error())
}
response.Callback(testPayload, receptionID.EphemeralIdentity{}, nil, nil)
r.Callback(testPayload, receptionID.EphemeralIdentity{}, nil, nil)
select {
case result := <-resultChan:
......@@ -62,9 +62,9 @@ func TestSingleResponse_Callback_Err(t *testing.T) {
cb := func(input *restlike.Message) {
resultChan <- input
}
response := response{cb}
r := response{cb}
response.Callback(nil, receptionID.EphemeralIdentity{}, nil, errors.New("test"))
r.Callback(nil, receptionID.EphemeralIdentity{}, nil, errors.New("test"))
select {
case result := <-resultChan:
......@@ -82,9 +82,9 @@ func TestSingleResponse_Callback_ProtoErr(t *testing.T) {
cb := func(input *restlike.Message) {
resultChan <- input
}
response := response{cb}
r := response{cb}
response.Callback([]byte("test"), receptionID.EphemeralIdentity{}, nil, nil)
r.Callback([]byte("test"), receptionID.EphemeralIdentity{}, nil, nil)
select {
case result := <-resultChan:
......
......@@ -29,7 +29,7 @@ func NewServer(receptionId *id.ID, privKey *cyclic.Int, net single.ListenCmix, e
endpoints: restlike.NewEndpoints(),
}
newServer.listener = single.Listen(catalog.RestLike, receptionId, privKey,
net, e2eGrp, &singleReceiver{newServer.endpoints})
net, e2eGrp, &receiver{newServer.endpoints})
return newServer
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment