From c5ce86b569b2179c5b9564978e2a10f5e1738f5b Mon Sep 17 00:00:00 2001
From: Jake Taylor <jake@elixxir.io>
Date: Wed, 27 Apr 2022 11:13:56 -0500
Subject: [PATCH] add some service tests

---
 cmix/message/fingerprints_test.go   |  4 ---
 cmix/message/serviceInterface.go    |  9 -------
 cmix/message/serviceTracker_test.go | 41 +++++++++++++++++++++++++++++
 cmix/message/services.go            | 14 ++++++----
 cmix/message/services_test.go       | 40 ++++++++++++++++++++++++++++
 5 files changed, 90 insertions(+), 18 deletions(-)
 create mode 100644 cmix/message/serviceTracker_test.go
 create mode 100644 cmix/message/services_test.go

diff --git a/cmix/message/fingerprints_test.go b/cmix/message/fingerprints_test.go
index 2dc046216..813e48a5a 100644
--- a/cmix/message/fingerprints_test.go
+++ b/cmix/message/fingerprints_test.go
@@ -181,7 +181,3 @@ func (mock *MockMsgProcessor) Process(
 func (mock *MockMsgProcessor) String() string {
 	return ""
 }
-
-func (mock *MockMsgProcessor) String() string {
-	return ""
-}
diff --git a/cmix/message/serviceInterface.go b/cmix/message/serviceInterface.go
index ca1cbee4e..346ff4b7f 100644
--- a/cmix/message/serviceInterface.go
+++ b/cmix/message/serviceInterface.go
@@ -2,7 +2,6 @@ package message
 
 import (
 	"encoding/base64"
-	"encoding/json"
 	"fmt"
 	"gitlab.com/elixxir/crypto/sih"
 )
@@ -44,14 +43,6 @@ func (si Service) ForMeFromMessageHash(messageHash, hash []byte) bool {
 	return sih.ForMeFromMessageHash(si.preimage(), messageHash, hash)
 }
 
-func (si Service) MarshalJSON() ([]byte, error) {
-	return json.Marshal(&si)
-}
-
-func (si Service) UnmarshalJSON(b []byte) error {
-	return json.Unmarshal(b, &si)
-}
-
 func (si Service) String() string {
 	p := si.preimage()
 	return fmt.Sprintf("Tag: %s, Identifier: %s, source: %s, "+
diff --git a/cmix/message/serviceTracker_test.go b/cmix/message/serviceTracker_test.go
new file mode 100644
index 000000000..c7b06b46a
--- /dev/null
+++ b/cmix/message/serviceTracker_test.go
@@ -0,0 +1,41 @@
+////////////////////////////////////////////////////////////////////////////////
+// Copyright © 2022 Privategrity Corporation                                   /
+//                                                                             /
+// All rights reserved.                                                        /
+////////////////////////////////////////////////////////////////////////////////
+
+package message
+
+import (
+	"gitlab.com/xx_network/primitives/id"
+	"testing"
+)
+
+func TestServiceList_Marshal_UnmarshalJSON(t *testing.T) {
+	var sl ServiceList = make(map[id.ID][]Service)
+	numServices := 10
+	testString := "test"
+	for i := 0; i < numServices; i++ {
+		uid := id.NewIdFromUInt(uint64(i), id.User, t)
+		sl[*uid] = []Service{{Tag: testString}}
+	}
+	jsonResult, err := sl.MarshalJSON()
+	if err != nil {
+		t.Errorf(err.Error())
+	}
+
+	sl = make(map[id.ID][]Service)
+	err = sl.UnmarshalJSON(jsonResult)
+	if err != nil {
+		t.Errorf(err.Error())
+	}
+
+	if len(sl) != numServices {
+		t.Errorf("Unexpected # of services: Got %d, expected %d", len(sl), numServices)
+	}
+	for _, newService := range sl {
+		if newService[0].Tag != testString {
+			t.Errorf("Unexpected service tag: Got %s, expected %s", newService[0].Tag, testString)
+		}
+	}
+}
diff --git a/cmix/message/services.go b/cmix/message/services.go
index 04c57321e..412fc1850 100644
--- a/cmix/message/services.go
+++ b/cmix/message/services.go
@@ -39,6 +39,7 @@ must be re-added before StartNetworkFollower is called.
 */
 
 type ServicesManager struct {
+	// Map reception ID to sih.Preimage to service
 	tmap        map[id.ID]map[sih.Preimage]service
 	trackers    []ServicesTracker
 	numServices uint
@@ -115,13 +116,12 @@ func (sm *ServicesManager) get(clientID *id.ID, receivedSIH,
 // for the same identifier/tag pair.
 //   preimage - the preimage which is triggered on
 //   type - a descriptive string of the service. Generally used in notifications
-//   source - a byte buffer of related data. Generally used in notifications.
+//   source - a byte buffer of related data. Mostly used in notifications.
 //     Example: Sender ID
-// There can be multiple "default" services, the must use the "default" tag
+// There can be multiple "default" services, they must use the "default" tag
 // and the identifier must be the client reception ID.
 // A service may have a nil response unless it is default.
-func (sm *ServicesManager) AddService(clientID *id.ID, newService Service,
-	response Processor) {
+func (sm *ServicesManager) AddService(clientID *id.ID, newService Service, response Processor) {
 	sm.Lock()
 	defer sm.Unlock()
 
@@ -131,10 +131,12 @@ func (sm *ServicesManager) AddService(clientID *id.ID, newService Service,
 		defaultList: nil,
 	}
 
+	// Initialize the map for the ID if needed
 	if _, exists := sm.tmap[*clientID]; !exists {
 		sm.tmap[*clientID] = make(map[sih.Preimage]service)
 	}
 
+	// Handle default tag behavior
 	if newService.Tag == sih.Default {
 		if !bytes.Equal(newService.Identifier, clientID[:]) {
 			jww.FATAL.Panicf("Cannot accept a malformed 'Default' " +
@@ -152,9 +154,11 @@ func (sm *ServicesManager) AddService(clientID *id.ID, newService Service,
 			"service already exists", newService.Tag)
 	}
 
+	// Add the service to the internal map
 	sm.tmap[*clientID][newService.preimage()] = newEntry
-
 	sm.numServices++
+
+	// Signal that a new service was added
 	sm.triggerServiceTracking()
 }
 
diff --git a/cmix/message/services_test.go b/cmix/message/services_test.go
new file mode 100644
index 000000000..e3cad7bed
--- /dev/null
+++ b/cmix/message/services_test.go
@@ -0,0 +1,40 @@
+////////////////////////////////////////////////////////////////////////////////
+// Copyright © 2022 Privategrity Corporation                                   /
+//                                                                             /
+// All rights reserved.                                                        /
+////////////////////////////////////////////////////////////////////////////////
+
+package message
+
+import (
+	"gitlab.com/elixxir/crypto/sih"
+	"gitlab.com/xx_network/primitives/id"
+	"testing"
+)
+
+func TestServicesManager_Add_DeleteService(t *testing.T) {
+	s := NewServices()
+
+	testId := id.NewIdFromUInt(0, id.User, t)
+	testService := Service{
+		Identifier: testId.Bytes(),
+		Tag:        sih.Default,
+	}
+	s.AddService(testId, testService, nil)
+
+	if s.numServices != 1 {
+		t.Errorf("Expected successful service add increment")
+	}
+	if len(s.tmap[*testId]) != 1 {
+		t.Errorf("Expected successful service add")
+	}
+
+	s.DeleteService(testId, testService, nil)
+
+	if s.numServices != 0 {
+		t.Errorf("Expected successful service remove decrement")
+	}
+	if len(s.tmap[*testId]) != 0 {
+		t.Errorf("Expected successful service remove")
+	}
+}
-- 
GitLab