From d0e04d414c36898ca851d095e60ea3cd47d5b3d4 Mon Sep 17 00:00:00 2001
From: "Richard T. Carback III" <rick.carback@gmail.com>
Date: Thu, 21 Apr 2022 22:19:44 +0000
Subject: [PATCH] Add stringer interface for message processors. SendUnsafe and
 precan E2E are now working

---
 auth/receivedConfirm.go     |  7 +++++++
 auth/receivedRequest.go     |  8 +++++++-
 cmd/callbacks.go            |  2 +-
 cmd/root.go                 |  1 +
 cmix/message/handler.go     | 11 ++++++++++-
 cmix/message/processor.go   |  7 ++++++-
 cmix/message/services.go    | 23 +++++++++++++++++------
 e2e/processor.go            |  9 ++++++++-
 e2e/sendUnsafe.go           |  3 +++
 e2e/unsafeProcessor.go      | 15 ++++++++++++++-
 fileTransfer/processor.go   |  6 ++++++
 groupChat/receive.go        |  8 +++++++-
 single/listener.go          |  6 ++++++
 single/responseProcessor.go |  7 +++++++
 14 files changed, 100 insertions(+), 13 deletions(-)

diff --git a/auth/receivedConfirm.go b/auth/receivedConfirm.go
index 07e12df9b..dd06dbbf1 100644
--- a/auth/receivedConfirm.go
+++ b/auth/receivedConfirm.go
@@ -3,6 +3,7 @@ package auth
 import (
 	"encoding/base64"
 	"fmt"
+
 	jww "github.com/spf13/jwalterweatherman"
 	"gitlab.com/elixxir/client/auth/store"
 	"gitlab.com/elixxir/client/cmix/identity/receptionID"
@@ -109,3 +110,9 @@ func (rcs *receivedConfirmService) Process(msg format.Message,
 	}
 	state.callbacks.Confirm(c, receptionID, round)
 }
+
+func (rcs *receivedConfirmService) String() string {
+	return fmt.Sprintf("authConfirm(%s, %s, %s)",
+		rcs.s.e2e.GetReceptionID(), rcs.GetPartner(),
+		rcs.GetFingerprint())
+}
diff --git a/auth/receivedRequest.go b/auth/receivedRequest.go
index 45fe574d2..50e8d261c 100644
--- a/auth/receivedRequest.go
+++ b/auth/receivedRequest.go
@@ -3,6 +3,8 @@ package auth
 import (
 	"encoding/base64"
 	"fmt"
+	"strings"
+
 	"github.com/cloudflare/circl/dh/sidh"
 	"github.com/pkg/errors"
 	jww "github.com/spf13/jwalterweatherman"
@@ -15,7 +17,6 @@ import (
 	"gitlab.com/elixxir/primitives/fact"
 	"gitlab.com/elixxir/primitives/format"
 	"gitlab.com/xx_network/primitives/id"
-	"strings"
 )
 
 const dummyerr = "dummy error so we dont delete the request"
@@ -248,6 +249,11 @@ func (rrs *receivedRequestService) Process(message format.Message,
 	}
 }
 
+func (rrs *receivedRequestService) String() string {
+	return fmt.Sprintf("authRequest(%s)",
+		rrs.s.e2e.GetReceptionID())
+}
+
 func processDecryptedMessage(b []byte) (*id.ID, *sidh.PublicKey, fact.FactList,
 	[]byte, error) {
 	//decode the ecr format
diff --git a/cmd/callbacks.go b/cmd/callbacks.go
index 6c4b1a9a7..f6839fce0 100644
--- a/cmd/callbacks.go
+++ b/cmd/callbacks.go
@@ -74,7 +74,7 @@ func (a *authCallbacks) Reset(requestor contact.Contact,
 func registerMessageListener(client *api.Client) chan receive.Message {
 	recvCh := make(chan receive.Message, 10000)
 	listenerID := client.RegisterListenerChannel("DefaultCLIReceiver",
-		receive.AnyUser(), catalog.XxMessage, recvCh)
+		receive.AnyUser(), catalog.NoType, recvCh)
 	jww.INFO.Printf("Message ListenerID: %v", listenerID)
 	return recvCh
 }
diff --git a/cmd/root.go b/cmd/root.go
index 7bcfff2ac..06a209aff 100644
--- a/cmd/root.go
+++ b/cmd/root.go
@@ -211,6 +211,7 @@ var rootCmd = &cobra.Command{
 			recipientContact = user.GetContact()
 		}
 
+		client.GetE2EHandler().EnableUnsafeReception()
 		recvCh := registerMessageListener(client)
 
 		err := client.StartNetworkFollower(5 * time.Second)
diff --git a/cmix/message/handler.go b/cmix/message/handler.go
index 9d4c1a1f1..6a67e983e 100644
--- a/cmix/message/handler.go
+++ b/cmix/message/handler.go
@@ -9,11 +9,12 @@ package message
 
 import (
 	"fmt"
+	"sync"
+
 	jww "github.com/spf13/jwalterweatherman"
 	"gitlab.com/elixxir/client/stoppable"
 	"gitlab.com/elixxir/primitives/format"
 	"gitlab.com/xx_network/primitives/netTime"
-	"sync"
 )
 
 func (p *handler) handleMessages(stop *stoppable.Single) {
@@ -28,6 +29,8 @@ func (p *handler) handleMessages(stop *stoppable.Single) {
 				wg.Add(len(bundle.Messages))
 				for i := range bundle.Messages {
 					msg := bundle.Messages[i]
+					jww.TRACE.Printf("handle IterMsgs: %s",
+						msg.Digest())
 
 					go func() {
 						count, ts := p.inProcess.Add(
@@ -68,8 +71,12 @@ func (p *handler) handleMessage(ecrMsg format.Message, bundle Bundle) bool {
 	identity := bundle.Identity
 	round := bundle.RoundInfo
 
+	jww.INFO.Printf("handleMessage(%s)", ecrMsg.Digest())
+
 	// If we have a fingerprint, process it
 	if proc, exists := p.pop(identity.Source, fingerprint); exists {
+		jww.DEBUG.Printf("handleMessage found fingerprint: %s",
+			ecrMsg.Digest())
 		proc.Process(ecrMsg, identity, round)
 		return true
 	}
@@ -78,6 +85,8 @@ func (p *handler) handleMessage(ecrMsg format.Message, bundle Bundle) bool {
 		identity.Source, ecrMsg.GetSIH(), ecrMsg.GetContents())
 	if exists {
 		for _, t := range services {
+			jww.DEBUG.Printf("handleMessage service found: %s, %s",
+				ecrMsg.Digest(), t)
 			go t.Process(ecrMsg, identity, round)
 		}
 		if len(services) == 0 {
diff --git a/cmix/message/processor.go b/cmix/message/processor.go
index c4107476d..5d454df5c 100644
--- a/cmix/message/processor.go
+++ b/cmix/message/processor.go
@@ -1,8 +1,10 @@
 package message
 
 import (
-	"gitlab.com/elixxir/client/cmix/rounds"
+	"fmt"
+
 	"gitlab.com/elixxir/client/cmix/identity/receptionID"
+	"gitlab.com/elixxir/client/cmix/rounds"
 	"gitlab.com/elixxir/primitives/format"
 )
 
@@ -16,4 +18,7 @@ type Processor interface {
 	// and can lead to compromise of message contents and integrity.
 	Process(message format.Message, receptionID receptionID.EphemeralIdentity,
 		round rounds.Round)
+
+	// Implement the stringer interface String() string for debugging
+	fmt.Stringer
 }
diff --git a/cmix/message/services.go b/cmix/message/services.go
index ed11ce56b..8bd95daa2 100644
--- a/cmix/message/services.go
+++ b/cmix/message/services.go
@@ -78,25 +78,32 @@ func (sm *ServicesManager) get(clientID *id.ID, receivedSIH,
 	if !exists {
 		return nil, false
 	}
+
+	// NOTE: We exit on the first service match
 	for _, s := range services {
 		// Check if the SIH matches this service
 		if s.ForMe(ecrMsgContents, receivedSIH) {
 			if s.defaultList == nil && s.Tag != sih.Default {
 				//skip if the processor is nil
-				if s.Processor != nil {
+				if s.Processor == nil {
+					jww.ERROR.Printf("<nil> processor: %s",
+						s.Tag)
 					return []Processor{}, true
 				}
-				// Return this service directly if not the default service
+				// Return this service directly if not
+				// the default service
 				return []Processor{s}, true
 
 			} else if s.defaultList != nil {
-				// If it is default and the default list is not empty, then
-				// return the default list
+				// If it is default and the default
+				// list is not empty, then return the
+				// default list
 				return s.defaultList, true
 			}
 
-			// Return false if it is for me, but I have nothing registered to
-			// respond to default queries
+			// Return false if it is for me, but I have
+			// nothing registered to respond to default
+			// queries
 			return []Processor{}, false
 		}
 	}
@@ -198,3 +205,7 @@ func (sm *ServicesManager) DeleteClientService(clientID *id.ID) {
 
 	delete(sm.tmap, *clientID)
 }
+
+func (s service) String() string {
+	return s.Service.String()
+}
diff --git a/e2e/processor.go b/e2e/processor.go
index e57359960..ea7d4aef4 100644
--- a/e2e/processor.go
+++ b/e2e/processor.go
@@ -1,9 +1,11 @@
 package e2e
 
 import (
+	"fmt"
+
 	jww "github.com/spf13/jwalterweatherman"
-	"gitlab.com/elixxir/client/cmix/rounds"
 	"gitlab.com/elixxir/client/cmix/identity/receptionID"
+	"gitlab.com/elixxir/client/cmix/rounds"
 	"gitlab.com/elixxir/client/e2e/ratchet/partner/session"
 	"gitlab.com/elixxir/primitives/format"
 )
@@ -37,3 +39,8 @@ func (p *processor) Process(ecrMsg format.Message,
 		p.m.Switchboard.Speak(message)
 	}
 }
+
+func (p *processor) String() string {
+	return fmt.Sprintf("E2E(%s): %s",
+		p.m.myID, p.cy.GetSession())
+}
diff --git a/e2e/sendUnsafe.go b/e2e/sendUnsafe.go
index 9ad2b48ad..4451f16c4 100644
--- a/e2e/sendUnsafe.go
+++ b/e2e/sendUnsafe.go
@@ -62,6 +62,9 @@ func (m *manager) sendUnsafe(mt catalog.MessageType, recipient *id.ID,
 			unencryptedMAC, fp := e2e.SetUnencrypted(payload,
 				m.myID)
 
+			jww.TRACE.Printf("sendUnsafe contents: %v, fp: %v, mac: %v",
+				payload, fp, unencryptedMAC)
+
 			var err error
 			roundIds[i], _, err = m.net.Send(recipient, fp,
 				srvc, payload, unencryptedMAC,
diff --git a/e2e/unsafeProcessor.go b/e2e/unsafeProcessor.go
index 5a9e84390..3cd173d52 100644
--- a/e2e/unsafeProcessor.go
+++ b/e2e/unsafeProcessor.go
@@ -1,9 +1,11 @@
 package e2e
 
 import (
+	"fmt"
+
 	jww "github.com/spf13/jwalterweatherman"
-	"gitlab.com/elixxir/client/cmix/rounds"
 	"gitlab.com/elixxir/client/cmix/identity/receptionID"
+	"gitlab.com/elixxir/client/cmix/rounds"
 	"gitlab.com/elixxir/crypto/e2e"
 	"gitlab.com/elixxir/primitives/format"
 )
@@ -17,7 +19,14 @@ func (up *UnsafeProcessor) Process(ecrMsg format.Message,
 	receptionID receptionID.EphemeralIdentity,
 	round rounds.Round) {
 	//check if the message is unencrypted
+	jww.INFO.Printf("Unsafe PRocessed received: contents: %v, fp: %v, mac: %v, sih: %v",
+		ecrMsg.GetContents(), ecrMsg.GetKeyFP(), ecrMsg.GetMac(), ecrMsg.GetSIH())
 	unencrypted, sender := e2e.IsUnencrypted(ecrMsg)
+	if !unencrypted && sender == nil {
+		jww.ERROR.Printf("unencrypted message failed MAC check: %v",
+			ecrMsg)
+		return
+	}
 	if !unencrypted {
 		jww.ERROR.Printf("Received a non unencrypted message in e2e "+
 			"service %s, A message might have dropped!", up.tag)
@@ -35,3 +44,7 @@ func (up *UnsafeProcessor) Process(ecrMsg format.Message,
 		up.m.Switchboard.Speak(message)
 	}
 }
+
+func (up *UnsafeProcessor) String() string {
+	return fmt.Sprintf("Unsafe(%s)", up.m.myID)
+}
diff --git a/fileTransfer/processor.go b/fileTransfer/processor.go
index f4c2432c3..a1bb17db8 100644
--- a/fileTransfer/processor.go
+++ b/fileTransfer/processor.go
@@ -8,6 +8,8 @@
 package fileTransfer
 
 import (
+	"fmt"
+
 	jww "github.com/spf13/jwalterweatherman"
 	"gitlab.com/elixxir/client/cmix/identity/receptionID"
 	"gitlab.com/elixxir/client/cmix/rounds"
@@ -62,3 +64,7 @@ func (p *processor) Process(msg format.Message,
 	// Call callback with updates
 	p.callbacks.Call(p.TransferID(), nil)
 }
+
+func (p *processor) String() string {
+	return fmt.Sprintf("FileTransfer(%s)", p.myID)
+}
diff --git a/groupChat/receive.go b/groupChat/receive.go
index 503a52fa7..244883cc3 100644
--- a/groupChat/receive.go
+++ b/groupChat/receive.go
@@ -8,6 +8,9 @@
 package groupChat
 
 import (
+	"fmt"
+	"time"
+
 	"github.com/pkg/errors"
 	jww "github.com/spf13/jwalterweatherman"
 	"gitlab.com/elixxir/client/cmix/identity/receptionID"
@@ -16,7 +19,6 @@ import (
 	"gitlab.com/elixxir/crypto/group"
 	"gitlab.com/elixxir/primitives/format"
 	"gitlab.com/elixxir/primitives/states"
-	"time"
 )
 
 // Error messages.
@@ -77,6 +79,10 @@ func (p *receptionProcessor) Process(message format.Message, receptionID recepti
 	p.m.receiveFunc(result)
 }
 
+func (p *receptionProcessor) String() string {
+	return fmt.Sprintf("GroupChatReception(%s)", p.m.receptionId)
+}
+
 // decryptMessage decrypts the group message payload and returns its message ID,
 // timestamp, sender ID, and message contents.
 func decryptMessage(g gs.Group, fingerprint format.Fingerprint, key group.CryptKey, payload []byte) (
diff --git a/single/listener.go b/single/listener.go
index 5501915df..bd5f7dab6 100644
--- a/single/listener.go
+++ b/single/listener.go
@@ -1,6 +1,8 @@
 package single
 
 import (
+	"fmt"
+
 	jww "github.com/spf13/jwalterweatherman"
 	"gitlab.com/elixxir/client/cmix"
 	"gitlab.com/elixxir/client/cmix/identity/receptionID"
@@ -119,3 +121,7 @@ func (l *listener) Stop() {
 	}
 	l.net.DeleteService(l.myId, svc, l)
 }
+
+func (l *listener) String() string {
+	return fmt.Sprintf("SingleUse(%s)", l.myId)
+}
diff --git a/single/responseProcessor.go b/single/responseProcessor.go
index a6f0d080a..62f526a49 100644
--- a/single/responseProcessor.go
+++ b/single/responseProcessor.go
@@ -1,6 +1,8 @@
 package single
 
 import (
+	"fmt"
+
 	jww "github.com/spf13/jwalterweatherman"
 	"gitlab.com/elixxir/client/cmix/identity/receptionID"
 	"gitlab.com/elixxir/client/cmix/rounds"
@@ -48,3 +50,8 @@ func (rsp *responseProcessor) Process(ecrMsg format.Message,
 		rsp.callback(payload, receptionID, round, nil)
 	}
 }
+
+func (rsp *responseProcessor) String() string {
+	return fmt.Sprintf("SingleUseFP(%s, %s)",
+		rsp.sendingID, rsp.recipient.ID)
+}
-- 
GitLab