From f8df0ac76afe301177a1a9aac4853a50e3a52d62 Mon Sep 17 00:00:00 2001
From: Benjamin Wenger <ben@elixxir.ioo>
Date: Fri, 8 Apr 2022 09:25:28 -0700
Subject: [PATCH] improvements to e2e/ratchet/partner/manager interface

---
 auth/interface.go                   |  4 +--
 auth/state.go                       |  4 +--
 e2e/ratchet/partner/interface.go    | 37 +++++++++++++-------
 e2e/ratchet/partner/manager.go      | 53 ++++++++++++++++++++++++-----
 e2e/ratchet/partner/manager_test.go | 20 +++++------
 e2e/ratchet/partner/relationship.go | 31 -----------------
 e2e/ratchet/ratchet.go              |  2 +-
 e2e/ratchet/utils_test.go           |  4 +--
 8 files changed, 87 insertions(+), 68 deletions(-)

diff --git a/auth/interface.go b/auth/interface.go
index ac0b1c4b8..f44e221ac 100644
--- a/auth/interface.go
+++ b/auth/interface.go
@@ -57,7 +57,7 @@ type State interface {
 	// This will not be useful if either side has ratcheted
 	ReplayConfirm(partner *id.ID) (id.Round, error)
 
-	// CallReceivedRequests will iterate through all pending contact requests
+	// CallAllReceivedRequests will iterate through all pending contact requests
 	// and replay them on the callbacks.
-	CallReceivedRequests()
+	CallAllReceivedRequests()
 }
diff --git a/auth/state.go b/auth/state.go
index ef311d23a..d4a8fdb87 100644
--- a/auth/state.go
+++ b/auth/state.go
@@ -112,9 +112,9 @@ func NewStateLegacy(kv *versioned.KV, net cmix.Client, e2e e2e.Handler,
 	return s, nil
 }
 
-// CallReceivedRequests will iterate through all pending contact requests and replay
+// CallAllReceivedRequests will iterate through all pending contact requests and replay
 // them on the callbacks.
-func (s *state) CallReceivedRequests() {
+func (s *state) CallAllReceivedRequests() {
 	rrList := s.store.GetAllReceivedRequests()
 	for i := range rrList {
 		rr := rrList[i]
diff --git a/e2e/ratchet/partner/interface.go b/e2e/ratchet/partner/interface.go
index 6877291c4..06a3aadc6 100644
--- a/e2e/ratchet/partner/interface.go
+++ b/e2e/ratchet/partner/interface.go
@@ -10,26 +10,39 @@ import (
 )
 
 type Manager interface {
+	//accessors
+	GetPartnerID() *id.ID
+	GetMyID() *id.ID
+	GetMyOriginPrivateKey() *cyclic.Int
+	GetPartnerOriginPublicKey() *cyclic.Int
+
+	GetSendRelationshipFingerprint() []byte
+	GetReceiveRelationshipFingerprint() []byte
+
+	GetConnectionFingerprintBytes() []byte
+	GetConnectionFingerprint() string
+	GetContact() contact.Contact
+
+	//sending and receving
+	PopSendCypher() (*session.Cypher, error)
+	PopRekeyCypher() (*session.Cypher, error)
+
+	//Ratcheting
 	NewReceiveSession(partnerPubKey *cyclic.Int,
 		partnerSIDHPubKey *sidh.PublicKey, e2eParams session.Params,
 		source *session.Session) (*session.Session, bool)
 	NewSendSession(myDHPrivKey *cyclic.Int, mySIDHPrivateKey *sidh.PrivateKey,
 		e2eParams session.Params, source *session.Session) *session.Session
-	PopSendCypher() (*session.Cypher, error)
-	PopRekeyCypher() (*session.Cypher, error)
-	GetPartnerID() *id.ID
-	GetMyID() *id.ID
 	GetSendSession(sid session.SessionID) *session.Session
+
+	//state machine
 	GetReceiveSession(sid session.SessionID) *session.Session
 	Confirm(sid session.SessionID) error
 	TriggerNegotiations() []*session.Session
-	GetMyOriginPrivateKey() *cyclic.Int
-	GetPartnerOriginPublicKey() *cyclic.Int
-	GetSendRelationshipFingerprint() []byte
-	GetRelationshipFingerprintBytes() []byte
-	GetRelationshipFingerprint() string
+
+	//services
 	MakeService(tag string) message.Service
-	GetContact() contact.Contact
-	DeleteRelationship() error
-	ClearManager() error
+
+	//storage
+	Delete() error
 }
diff --git a/e2e/ratchet/partner/manager.go b/e2e/ratchet/partner/manager.go
index a8e482cc4..789bb3b2e 100644
--- a/e2e/ratchet/partner/manager.go
+++ b/e2e/ratchet/partner/manager.go
@@ -141,11 +141,11 @@ func LoadManager(kv *versioned.KV, myID, partnerID *id.ID,
 	return m, nil
 }
 
-// ClearManager removes the relationship between the partner
+// Delete removes the relationship between the partner
 // and deletes the Send and Receive sessions. This includes the
 // sessions and the key vectors
-func (m *manager) ClearManager() error {
-	if err := m.DeleteRelationship(); err != nil {
+func (m *manager) Delete() error {
+	if err := m.deleteRelationships(); err != nil {
 		return errors.WithMessage(err,
 			"Failed to delete relationship")
 	}
@@ -159,6 +159,37 @@ func (m *manager) ClearManager() error {
 	return nil
 }
 
+// deleteRelationships removes all relationship and
+// relationship adjacent information from storage
+func (m *manager) deleteRelationships() error {
+
+	// Delete the send information
+	sendKv := m.kv.Prefix(session.Send.Prefix())
+	m.send.Delete()
+	if err := deleteRelationshipFingerprint(sendKv); err != nil {
+		return err
+	}
+	if err := sendKv.Delete(relationshipKey,
+		currentRelationshipVersion); err != nil {
+		return errors.Errorf("cannot delete send relationship: %v",
+			err)
+	}
+
+	// Delete the receive information
+	receiveKv := m.kv.Prefix(session.Receive.Prefix())
+	m.receive.Delete()
+	if err := deleteRelationshipFingerprint(receiveKv); err != nil {
+		return err
+	}
+	if err := receiveKv.Delete(relationshipKey,
+		currentRelationshipVersion); err != nil {
+		return errors.Errorf("cannot delete receive relationship: %v",
+			err)
+	}
+
+	return nil
+}
+
 // NewReceiveSession creates a new Receive session using the latest private key
 // this user has sent and the new public key received from the partner. If the
 // session already exists, then it will not be overwritten and the extant
@@ -235,12 +266,18 @@ func (m *manager) GetReceiveSession(sid session.SessionID) *session.Session {
 	return m.receive.GetByID(sid)
 }
 
-// GetSendSession gets the Send session of the passed ID. Returns nil if no
+// GetSendRelationshipFingerprint gets the Send session of the passed ID. Returns nil if no
 // session is found.
 func (m *manager) GetSendRelationshipFingerprint() []byte {
 	return m.send.fingerprint
 }
 
+// GetReceiveRelationshipFingerprint gets the receive session of the passed ID.
+// Returns nil if no session is found.
+func (m *manager) GetReceiveRelationshipFingerprint() []byte {
+	return m.receive.fingerprint
+}
+
 // Confirm confirms a Send session is known about by the partner.
 func (m *manager) Confirm(sid session.SessionID) error {
 	return m.send.Confirm(sid)
@@ -265,16 +302,16 @@ const relationshipFpLength = 15
 // GetRelationshipFingerprint returns a unique fingerprint for an E2E
 // relationship. The fingerprint is a base 64 encoded hash of of the two
 // relationship fingerprints truncated to 15 characters.
-func (m *manager) GetRelationshipFingerprint() string {
+func (m *manager) GetConnectionFingerprint() string {
 
 	// Base 64 encode hash and truncate
 	return base64.StdEncoding.EncodeToString(
-		m.GetRelationshipFingerprintBytes())[:relationshipFpLength]
+		m.GetConnectionFingerprintBytes())[:relationshipFpLength]
 }
 
 // GetRelationshipFingerprintBytes returns a unique fingerprint for an E2E
 // relationship. used for the e2e preimage.
-func (m *manager) GetRelationshipFingerprintBytes() []byte {
+func (m *manager) GetConnectionFingerprintBytes() []byte {
 	// Sort fingerprints
 	var fps [][]byte
 
@@ -299,7 +336,7 @@ func (m *manager) GetRelationshipFingerprintBytes() []byte {
 // the metadata with the partner
 func (m *manager) MakeService(tag string) message.Service {
 	return message.Service{
-		Identifier: m.GetRelationshipFingerprintBytes(),
+		Identifier: m.GetConnectionFingerprintBytes(),
 		Tag:        tag,
 		Metadata:   m.partner[:],
 	}
diff --git a/e2e/ratchet/partner/manager_test.go b/e2e/ratchet/partner/manager_test.go
index f0ffd05b9..944121e06 100644
--- a/e2e/ratchet/partner/manager_test.go
+++ b/e2e/ratchet/partner/manager_test.go
@@ -80,7 +80,7 @@ func TestManager_ClearManager(t *testing.T) {
 	// Set up expected and test values
 	expectedM, kv := newTestManager(t)
 
-	err := expectedM.ClearManager()
+	err := expectedM.Delete()
 	if err != nil {
 		t.Fatalf("clearManager returned an error: %v", err)
 	}
@@ -345,18 +345,18 @@ func TestManager_GetRelationshipFingerprint(t *testing.T) {
 	h.Write(append(m.receive.fingerprint, m.send.fingerprint...))
 	expected := base64.StdEncoding.EncodeToString(h.Sum(nil))[:relationshipFpLength]
 
-	fp := m.GetRelationshipFingerprint()
+	fp := m.GetConnectionFingerprint()
 	if fp != expected {
-		t.Errorf("GetRelationshipFingerprint did not return the expected "+
+		t.Errorf("GetConnectionFingerprint did not return the expected "+
 			"fingerprint.\nexpected: %s\nreceived: %s", expected, fp)
 	}
 
 	// Flip the order and show that the output is the same.
 	m.receive.fingerprint, m.send.fingerprint = m.send.fingerprint, m.receive.fingerprint
 
-	fp = m.GetRelationshipFingerprint()
+	fp = m.GetConnectionFingerprint()
 	if fp != expected {
-		t.Errorf("GetRelationshipFingerprint did not return the expected "+
+		t.Errorf("GetConnectionFingerprint did not return the expected "+
 			"fingerprint.\nexpected: %s\nreceived: %s", expected, fp)
 	}
 }
@@ -379,18 +379,18 @@ func TestManager_GetRelationshipFingerprint_Consistency(t *testing.T) {
 		prng.Read(m.receive.fingerprint)
 		prng.Read(m.send.fingerprint)
 
-		fp := m.GetRelationshipFingerprint()
+		fp := m.GetConnectionFingerprint()
 		if fp != expected {
-			t.Errorf("GetRelationshipFingerprint did not return the expected "+
+			t.Errorf("GetConnectionFingerprint did not return the expected "+
 				"fingerprint (%d).\nexpected: %s\nreceived: %s", i, expected, fp)
 		}
 
 		// Flip the order and show that the output is the same.
 		m.receive.fingerprint, m.send.fingerprint = m.send.fingerprint, m.receive.fingerprint
 
-		fp = m.GetRelationshipFingerprint()
+		fp = m.GetConnectionFingerprint()
 		if fp != expected {
-			t.Errorf("GetRelationshipFingerprint did not return the expected "+
+			t.Errorf("GetConnectionFingerprint did not return the expected "+
 				"fingerprint (%d).\nexpected: %s\nreceived: %s", i, expected, fp)
 		}
 
@@ -402,7 +402,7 @@ func TestManager_MakeService(t *testing.T) {
 	m, _ := newTestManager(t)
 	tag := "hunter2"
 	expected := message.Service{
-		Identifier: m.GetRelationshipFingerprintBytes(),
+		Identifier: m.GetConnectionFingerprintBytes(),
 		Tag:        tag,
 		Metadata:   m.partner[:],
 	}
diff --git a/e2e/ratchet/partner/relationship.go b/e2e/ratchet/partner/relationship.go
index e41b36811..6c54b5921 100644
--- a/e2e/ratchet/partner/relationship.go
+++ b/e2e/ratchet/partner/relationship.go
@@ -146,37 +146,6 @@ func LoadRelationship(kv *versioned.KV, t session.RelationshipType, myID,
 	return r, nil
 }
 
-// DeleteRelationship removes all relationship and
-// relationship adjacent information from storage
-func (m *manager) DeleteRelationship() error {
-
-	// Delete the send information
-	sendKv := m.kv.Prefix(session.Send.Prefix())
-	m.send.Delete()
-	if err := deleteRelationshipFingerprint(sendKv); err != nil {
-		return err
-	}
-	if err := sendKv.Delete(relationshipKey,
-		currentRelationshipVersion); err != nil {
-		return errors.Errorf("cannot delete send relationship: %v",
-			err)
-	}
-
-	// Delete the receive information
-	receiveKv := m.kv.Prefix(session.Receive.Prefix())
-	m.receive.Delete()
-	if err := deleteRelationshipFingerprint(receiveKv); err != nil {
-		return err
-	}
-	if err := receiveKv.Delete(relationshipKey,
-		currentRelationshipVersion); err != nil {
-		return errors.Errorf("cannot delete receive relationship: %v",
-			err)
-	}
-
-	return nil
-}
-
 func (r *relationship) save() error {
 
 	now := netTime.Now()
diff --git a/e2e/ratchet/ratchet.go b/e2e/ratchet/ratchet.go
index fd1d6f289..3bc73f3b9 100644
--- a/e2e/ratchet/ratchet.go
+++ b/e2e/ratchet/ratchet.go
@@ -152,7 +152,7 @@ func (r *Ratchet) DeletePartner(partnerID *id.ID) error {
 		return errors.New(NoPartnerErrorStr)
 	}
 
-	if err := m.ClearManager(); err != nil {
+	if err := m.Delete(); err != nil {
 		return errors.WithMessagef(err,
 			"Could not remove partner %s from store",
 			partnerID)
diff --git a/e2e/ratchet/utils_test.go b/e2e/ratchet/utils_test.go
index 9c2b763e9..c34978720 100644
--- a/e2e/ratchet/utils_test.go
+++ b/e2e/ratchet/utils_test.go
@@ -52,10 +52,10 @@ func managersEqual(expected, received partner.Manager, t *testing.T) bool {
 		equal = false
 	}
 
-	if !strings.EqualFold(expected.GetRelationshipFingerprint(), received.GetRelationshipFingerprint()) {
+	if !strings.EqualFold(expected.GetConnectionFingerprint(), received.GetConnectionFingerprint()) {
 		t.Errorf("Did not Receive expected Manager.Receive."+
 			"\n\texpected: %+v\n\treceived: %+v",
-			expected.GetRelationshipFingerprint(), received.GetRelationshipFingerprint())
+			expected.GetConnectionFingerprint(), received.GetConnectionFingerprint())
 		equal = false
 	}
 	if !reflect.DeepEqual(expected.GetMyID(), received.GetMyID()) {
-- 
GitLab