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

add some service tests

parent 5ee8906f
No related branches found
No related tags found
2 merge requests!510Release,!207WIP: Client Restructure
...@@ -181,7 +181,3 @@ func (mock *MockMsgProcessor) Process( ...@@ -181,7 +181,3 @@ func (mock *MockMsgProcessor) Process(
func (mock *MockMsgProcessor) String() string { func (mock *MockMsgProcessor) String() string {
return "" return ""
} }
func (mock *MockMsgProcessor) String() string {
return ""
}
...@@ -2,7 +2,6 @@ package message ...@@ -2,7 +2,6 @@ package message
import ( import (
"encoding/base64" "encoding/base64"
"encoding/json"
"fmt" "fmt"
"gitlab.com/elixxir/crypto/sih" "gitlab.com/elixxir/crypto/sih"
) )
...@@ -44,14 +43,6 @@ func (si Service) ForMeFromMessageHash(messageHash, hash []byte) bool { ...@@ -44,14 +43,6 @@ func (si Service) ForMeFromMessageHash(messageHash, hash []byte) bool {
return sih.ForMeFromMessageHash(si.preimage(), messageHash, hash) 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 { func (si Service) String() string {
p := si.preimage() p := si.preimage()
return fmt.Sprintf("Tag: %s, Identifier: %s, source: %s, "+ return fmt.Sprintf("Tag: %s, Identifier: %s, source: %s, "+
......
////////////////////////////////////////////////////////////////////////////////
// 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)
}
}
}
...@@ -39,6 +39,7 @@ must be re-added before StartNetworkFollower is called. ...@@ -39,6 +39,7 @@ must be re-added before StartNetworkFollower is called.
*/ */
type ServicesManager struct { type ServicesManager struct {
// Map reception ID to sih.Preimage to service
tmap map[id.ID]map[sih.Preimage]service tmap map[id.ID]map[sih.Preimage]service
trackers []ServicesTracker trackers []ServicesTracker
numServices uint numServices uint
...@@ -115,13 +116,12 @@ func (sm *ServicesManager) get(clientID *id.ID, receivedSIH, ...@@ -115,13 +116,12 @@ func (sm *ServicesManager) get(clientID *id.ID, receivedSIH,
// for the same identifier/tag pair. // for the same identifier/tag pair.
// preimage - the preimage which is triggered on // preimage - the preimage which is triggered on
// type - a descriptive string of the service. Generally used in notifications // 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 // 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. // and the identifier must be the client reception ID.
// A service may have a nil response unless it is default. // A service may have a nil response unless it is default.
func (sm *ServicesManager) AddService(clientID *id.ID, newService Service, func (sm *ServicesManager) AddService(clientID *id.ID, newService Service, response Processor) {
response Processor) {
sm.Lock() sm.Lock()
defer sm.Unlock() defer sm.Unlock()
...@@ -131,10 +131,12 @@ func (sm *ServicesManager) AddService(clientID *id.ID, newService Service, ...@@ -131,10 +131,12 @@ func (sm *ServicesManager) AddService(clientID *id.ID, newService Service,
defaultList: nil, defaultList: nil,
} }
// Initialize the map for the ID if needed
if _, exists := sm.tmap[*clientID]; !exists { if _, exists := sm.tmap[*clientID]; !exists {
sm.tmap[*clientID] = make(map[sih.Preimage]service) sm.tmap[*clientID] = make(map[sih.Preimage]service)
} }
// Handle default tag behavior
if newService.Tag == sih.Default { if newService.Tag == sih.Default {
if !bytes.Equal(newService.Identifier, clientID[:]) { if !bytes.Equal(newService.Identifier, clientID[:]) {
jww.FATAL.Panicf("Cannot accept a malformed 'Default' " + jww.FATAL.Panicf("Cannot accept a malformed 'Default' " +
...@@ -152,9 +154,11 @@ func (sm *ServicesManager) AddService(clientID *id.ID, newService Service, ...@@ -152,9 +154,11 @@ func (sm *ServicesManager) AddService(clientID *id.ID, newService Service,
"service already exists", newService.Tag) "service already exists", newService.Tag)
} }
// Add the service to the internal map
sm.tmap[*clientID][newService.preimage()] = newEntry sm.tmap[*clientID][newService.preimage()] = newEntry
sm.numServices++ sm.numServices++
// Signal that a new service was added
sm.triggerServiceTracking() sm.triggerServiceTracking()
} }
......
////////////////////////////////////////////////////////////////////////////////
// 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")
}
}
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