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

add tests for handler

parent 4fd6555b
Branches
Tags
2 merge requests!510Release,!207WIP: Client Restructure
......@@ -181,3 +181,7 @@ func (mock *MockMsgProcessor) Process(
func (mock *MockMsgProcessor) String() string {
return ""
}
func (mock *MockMsgProcessor) String() string {
return ""
}
......@@ -14,6 +14,7 @@ import (
"gitlab.com/xx_network/primitives/id"
"strconv"
"sync"
"time"
jww "github.com/spf13/jwalterweatherman"
"gitlab.com/elixxir/client/stoppable"
......@@ -103,7 +104,8 @@ func (h *handler) StartProcesses() stoppable.Stoppable {
return multi
}
//
// handleMessages is a long-running thread that receives each Bundle from messageReception
// and processes the messages in the Bundle
func (h *handler) handleMessages(stop *stoppable.Single) {
for {
select {
......@@ -123,7 +125,21 @@ func (h *handler) handleMessages(stop *stoppable.Single) {
count, ts := h.inProcess.Add(
msg, bundle.RoundInfo.Raw, bundle.Identity)
wg.Done()
success := h.handleMessage(msg, bundle)
h.handleMessage(count, ts, msg, bundle)
}()
}
wg.Wait()
bundle.Finish()
}()
}
}
}
// handleMessage processes an individual message in the Bundle
// and handles the inProcess logic
func (h *handler) handleMessage(count uint, ts time.Time, msg format.Message, bundle Bundle) {
success := h.handleMessageHelper(msg, bundle)
if success {
h.inProcess.Remove(
msg, bundle.RoundInfo.Raw, bundle.Identity)
......@@ -140,20 +156,12 @@ func (h *handler) handleMessages(stop *stoppable.Single) {
h.inProcess.Failed(
msg, bundle.RoundInfo.Raw, bundle.Identity)
}
}
}()
}
wg.Wait()
bundle.Finish()
}()
}
}
}
func (h *handler) handleMessage(ecrMsg format.Message, bundle Bundle) bool {
// handleMessageHelper determines if any services or fingerprints match the given message
// and runs the processor, returning whether a processor was found
func (h *handler) handleMessageHelper(ecrMsg format.Message, bundle Bundle) bool {
fingerprint := ecrMsg.GetKeyFP()
identity := bundle.Identity
round := bundle.RoundInfo
......
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2022 Privategrity Corporation /
// /
// All rights reserved. /
////////////////////////////////////////////////////////////////////////////////
package message
import (
"gitlab.com/elixxir/client/cmix/identity/receptionID"
"gitlab.com/elixxir/client/cmix/rounds"
"gitlab.com/elixxir/client/event"
"gitlab.com/elixxir/client/storage/versioned"
pb "gitlab.com/elixxir/comms/mixmessages"
"gitlab.com/elixxir/crypto/sih"
"gitlab.com/elixxir/ekv"
"gitlab.com/elixxir/primitives/format"
"gitlab.com/elixxir/primitives/states"
"gitlab.com/xx_network/primitives/id"
"testing"
"time"
)
type testProcessor struct {
}
func (t *testProcessor) Process(message format.Message, receptionID receptionID.EphemeralIdentity, round rounds.Round) {
}
func (t *testProcessor) String() string {
return ""
}
// Test that a message is handled when no service or fingerprint exists and message needs to be removed
func Test_handler_handleMessage_NoResult(t *testing.T) {
garbled, err := NewOrLoadMeteredCmixMessageBuffer(versioned.NewKV(ekv.MakeMemstore()), inProcessKey)
if err != nil {
t.Errorf("Failed to load or new the Garbled Messages system: %v", err)
}
m := handler{
events: event.NewEventManager(),
param: GetDefaultParams(),
inProcess: garbled,
}
testId := id.NewIdFromUInt(2, id.User, t)
m.ServicesManager = *NewServices()
contents := []byte{4, 4}
lazyPreimage := sih.MakePreimage(contents, "test")
ecrMsg := format.NewMessage(2056)
ecrMsg.SetContents(contents)
ecrMsg.SetSIH(sih.Hash(lazyPreimage, ecrMsg.GetContents()))
testRound := rounds.Round{
Timestamps: make(map[states.Round]time.Time),
AddressSpaceSize: 18,
Raw: &pb.RoundInfo{Timestamps: make([]uint64, states.NUM_STATES)},
}
testRound.Timestamps[states.QUEUED] = time.Now()
testRound.Raw.Timestamps[states.QUEUED] = uint64(time.Now().Unix())
ephId := receptionID.BuildIdentityFromRound(testId, testRound)
bundle := Bundle{
Identity: ephId,
RoundInfo: testRound,
}
m.inProcess.Add(ecrMsg, bundle.RoundInfo.Raw, bundle.Identity)
m.handleMessage(m.param.MaxChecksInProcessMessage, time.Now().Add(-2*m.param.InProcessMessageWait),
ecrMsg, bundle)
if len(m.inProcess.mb.GetMessages()) != 0 {
t.Errorf("Expected to remove message from inProgress!")
}
}
// Test that a message is handled correctly via fingerprinting
func Test_handler_handleMessageHelper(t *testing.T) {
m := handler{
events: event.NewEventManager(),
}
testId := id.NewIdFromUInt(2, id.User, t)
m.FingerprintsManager = *newFingerprints(testId)
ecrMsg := format.NewMessage(2056)
fp := format.NewFingerprint([]byte{0, 2})
ecrMsg.SetKeyFP(fp)
testRound := rounds.Round{
Timestamps: make(map[states.Round]time.Time),
AddressSpaceSize: 18,
}
testRound.Timestamps[states.QUEUED] = time.Now()
ephId := receptionID.BuildIdentityFromRound(testId, testRound)
bundle := Bundle{
Identity: ephId,
RoundInfo: testRound,
}
err := m.AddFingerprint(testId, fp, &testProcessor{})
if err != nil {
t.Errorf("Unexpected failure to AddFignerprint: %+v", err)
}
result := m.handleMessageHelper(ecrMsg, bundle)
if !result {
t.Errorf("Expected handleMessage success!")
}
}
// Test that a message is handled when no service or fingerprint exists
func Test_handler_handleMessageHelper_NoResult(t *testing.T) {
m := handler{
events: event.NewEventManager(),
}
testId := id.NewIdFromUInt(2, id.User, t)
m.ServicesManager = *NewServices()
contents := []byte{4, 4}
lazyPreimage := sih.MakePreimage(contents, "test")
ecrMsg := format.NewMessage(2056)
ecrMsg.SetContents(contents)
ecrMsg.SetSIH(sih.Hash(lazyPreimage, ecrMsg.GetContents()))
testRound := rounds.Round{
Timestamps: make(map[states.Round]time.Time),
AddressSpaceSize: 18,
}
testRound.Timestamps[states.QUEUED] = time.Now()
ephId := receptionID.BuildIdentityFromRound(testId, testRound)
bundle := Bundle{
Identity: ephId,
RoundInfo: testRound,
}
result := m.handleMessageHelper(ecrMsg, bundle)
if result {
t.Errorf("Expected handleMessage failure!")
}
}
// Test that a message is handled correctly via services
func Test_handler_handleMessageHelper_Service(t *testing.T) {
m := handler{
events: event.NewEventManager(),
}
testId := id.NewIdFromUInt(2, id.User, t)
m.ServicesManager = *NewServices()
contents := []byte{4, 4}
lazyPreimage := sih.MakePreimage(contents, "test")
ecrMsg := format.NewMessage(2056)
ecrMsg.SetContents(contents)
ecrMsg.SetSIH(sih.Hash(lazyPreimage, ecrMsg.GetContents()))
testRound := rounds.Round{
Timestamps: make(map[states.Round]time.Time),
AddressSpaceSize: 18,
}
testRound.Timestamps[states.QUEUED] = time.Now()
ephId := receptionID.BuildIdentityFromRound(testId, testRound)
bundle := Bundle{
Identity: ephId,
RoundInfo: testRound,
}
s := Service{
Identifier: nil,
Tag: "test",
lazyPreimage: &lazyPreimage,
}
m.AddService(testId, s, nil)
result := m.handleMessageHelper(ecrMsg, bundle)
if !result {
t.Errorf("Expected handleMessage success!")
}
}
......@@ -68,8 +68,7 @@ func NewServices() *ServicesManager {
// These services are returned to the caller along with a true boolean.
// If the map has been exhausted with no matches found, it returns nil and false.
func (sm *ServicesManager) get(clientID *id.ID, receivedSIH,
ecrMsgContents []byte) ([]Processor,
bool) {
ecrMsgContents []byte) ([]Processor, bool) {
sm.Lock()
defer sm.Unlock()
cid := *clientID
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment