diff --git a/bindings/notifications.go b/bindings/notifications.go index 311673a25493390a7ed5bcf1c1fc4493b926d107..27d927deef0011750635556d3c0008b247df56c0 100644 --- a/bindings/notifications.go +++ b/bindings/notifications.go @@ -8,14 +8,11 @@ package bindings import ( - "encoding/base64" - "encoding/csv" "encoding/json" "github.com/pkg/errors" "gitlab.com/elixxir/client/storage/edge" pb "gitlab.com/elixxir/comms/mixmessages" "gitlab.com/elixxir/crypto/fingerprint" - "strings" ) type NotificationForMeReport struct { @@ -41,7 +38,7 @@ type ManyNotificationForMeReport struct { } func (mnfmr *ManyNotificationForMeReport) Get(i int) (*NotificationForMeReport, error) { - if len(mnfmr.many)>=i{ + if i>=len(mnfmr.many){ return nil, errors.New("Cannot get, too long") } return mnfmr.many[i], nil @@ -72,7 +69,7 @@ func NotificationsForMe(notifCSV, preimages string) (*ManyNotificationForMeRepor "cannot check if notification is for me") } - list, err := DecodeNotificationsCSV(notifCSV) + list, err := pb.DecodeNotificationsCSV(notifCSV) if err != nil { return nil, err } @@ -93,8 +90,8 @@ func NotificationsForMe(notifCSV, preimages string) (*ManyNotificationForMeRepor tYpe: preimage.Type, source: preimage.Source, } + break } - break } notifList = append(notifList, n) } @@ -113,29 +110,4 @@ func (c *Client) UnregisterForNotifications() error { return c.api.UnregisterForNotifications() } -func DecodeNotificationsCSV(data string)([]*pb.NotificationData, error){ - r := csv.NewReader(strings.NewReader(data)) - read, err := r.ReadAll() - if err!=nil{ - return nil, errors.WithMessage(err,"Failed to decode notifications CSV") - } - - l := make([]*pb.NotificationData, len(read)) - for i, touple := range read{ - messageHash, err := base64.StdEncoding.DecodeString(touple[0]) - if err!=nil{ - return nil, errors.WithMessage(err,"Failed decode an element") - } - identityFP, err := base64.StdEncoding.DecodeString(touple[1]) - if err!=nil{ - return nil, errors.WithMessage(err,"Failed decode an element") - } - l[i] = &pb.NotificationData{ - EphemeralID: 0, - IdentityFP: identityFP, - MessageHash: messageHash, - } - } - return l, nil -} diff --git a/bindings/notifications_test.go b/bindings/notifications_test.go index 3d8f78d33206de6e6eeffa25c2a973f8605970d1..53e5c911071ea1029aa4a7dfa729802d6e95acc0 100644 --- a/bindings/notifications_test.go +++ b/bindings/notifications_test.go @@ -1,21 +1,126 @@ package bindings import ( + "bytes" + "encoding/json" + "gitlab.com/elixxir/client/storage/edge" + "gitlab.com/elixxir/comms/mixmessages" + "gitlab.com/elixxir/crypto/fingerprint" + "math/rand" "testing" ) -// FIXME: this test needs to be fixed func TestNotificationForMe(t *testing.T) { - // payload := []byte("I'm a payload") - // hash := fingerprint.GetMessageHash(payload) - // rid := id.NewIdFromString("zezima", id.User, t) - // fp := fingerprint.IdentityFP(payload, rid) - // - // ok, err := NotificationForMe(base64.StdEncoding.EncodeToString(hash), base64.StdEncoding.EncodeToString(fp), rid.Bytes()) - // if err != nil { - // t.Errorf("Failed to check notification: %+v", err) - // } - // if !ok { - // t.Error("Should have gotten ok response") - // } + + const numPreimages = 5 + + types := []string{"default", "request", "silent", "e2e", "group"} + sourceList := [][]byte{{0}, {1}, {2}, {3}, {4}} + + preimageList := make([]edge.Preimage, 0, numPreimages) + + rng := rand.New(rand.NewSource(42)) + + for i := 0; i < numPreimages; i++ { + piData := make([]byte, 32) + rng.Read(piData) + + pi := edge.Preimage{ + Data: piData, + Type: types[i], + Source: sourceList[i], + } + + preimageList = append(preimageList, pi) + } + + preimagesJson, _ := json.Marshal(&preimageList) + + dataSources := []int{0, 1, -1, 2, 3, 4, -1, 0, 1, 2, 3, 4, -1, 2, 2, 2} + + notifData := make([]*mixmessages.NotificationData,0,len(dataSources)) + + for _, index := range dataSources{ + var preimage []byte + if index==-1{ + preimage = make([]byte,32) + rng.Read(preimage) + }else{ + preimage = preimageList[index].Data + } + + msg := make([]byte,32) + rng.Read(msg) + msgHash := fingerprint.GetMessageHash(msg) + + identityFP := fingerprint.IdentityFP(msg,preimage) + + n := &mixmessages.NotificationData{ + EphemeralID: 0, + IdentityFP: identityFP, + MessageHash: msgHash, + } + + notifData = append(notifData,n) + } + + notfsCSV := mixmessages.MakeNotificationsCSV(notifData) + + + notifsForMe, err := NotificationsForMe(notfsCSV,string(preimagesJson)) + if err!=nil{ + t.Errorf("Got error from NotificationsForMe: %+v", err) + } + + for i:=0;i<notifsForMe.Len();i++{ + nfm, err := notifsForMe.Get(i) + if err!=nil{ + t.Errorf("Got error in getting notif: %+v", err) + } + if dataSources[i]==-1{ + if nfm.ForMe(){ + t.Errorf("Notification %d should not be for me", i) + } + if nfm.Type()!=""{ + t.Errorf("Notification %d shoudl not have a type, " + + "has: %s", i, nfm.Type()) + } + if nfm.Source()!=nil{ + t.Errorf("Notification %d shoudl not have a source, " + + "has: %v", i, nfm.Source()) + } + }else{ + if !nfm.ForMe(){ + t.Errorf("Notification %d should be for me", i) + }else{ + expectedType := types[dataSources[i]] + if nfm.Type()!=expectedType{ + t.Errorf("Notification %d has the wrong type, " + + "Expected: %s, Received: %s", i, nfm.Type(), expectedType) + } + expectedSource := sourceList[dataSources[i]] + if !bytes.Equal(nfm.Source(),expectedSource){ + t.Errorf("Notification %d source does not match: " + + "Expected: %v, Received: %v", i, expectedSource, + nfm.Source()) + } + } + } + } +} + +func TestManyNotificationForMeReport_Get(t *testing.T) { + ManyNotificationForMeReport := &ManyNotificationForMeReport{many: make([]*NotificationForMeReport, 10)} + + //not too long + _, err := ManyNotificationForMeReport.Get(2) + if err!=nil{ + t.Errorf("Got error when not too long: %+v", err) + } + + //too long + _, err = ManyNotificationForMeReport.Get(69) + if err==nil{ + t.Errorf("Didnt get error when too long") + } } diff --git a/go.mod b/go.mod index 23115e484daabb8bbaea3fc0d6a5d85ff67b5d71..5bf815bc62c73b33813e131d44979a7cc07c4572 100644 --- a/go.mod +++ b/go.mod @@ -18,7 +18,7 @@ require ( github.com/spf13/jwalterweatherman v1.1.0 github.com/spf13/viper v1.7.1 gitlab.com/elixxir/bloomfilter v0.0.0-20200930191214-10e9ac31b228 - gitlab.com/elixxir/comms v0.0.4-0.20211220224127-670d882e4067 + gitlab.com/elixxir/comms v0.0.4-0.20211222154743-2f5bc6365f8d gitlab.com/elixxir/crypto v0.0.7-0.20211220224022-1f518df56c0f gitlab.com/elixxir/ekv v0.1.5 gitlab.com/elixxir/primitives v0.0.3-0.20211220223949-1fdbc43d8269 diff --git a/go.sum b/go.sum index b23a3edc3e6207920e2dfd3fb8cf4c7b2e98459d..1756055ca9ec7b322655dccd1b6af049add0807b 100644 --- a/go.sum +++ b/go.sum @@ -257,6 +257,8 @@ gitlab.com/elixxir/bloomfilter v0.0.0-20200930191214-10e9ac31b228 h1:Gi6rj4mAlK0 gitlab.com/elixxir/bloomfilter v0.0.0-20200930191214-10e9ac31b228/go.mod h1:H6jztdm0k+wEV2QGK/KYA+MY9nj9Zzatux/qIvDDv3k= gitlab.com/elixxir/comms v0.0.4-0.20211220224127-670d882e4067 h1:FlZjW7xdzFkivwTu+D/ZSvH7JLqDYEICuf7OPePNs6U= gitlab.com/elixxir/comms v0.0.4-0.20211220224127-670d882e4067/go.mod h1:kvn6jAHZcRTXYdo9LvZDbO+Nw8V7Gn749fpbyUYlSN8= +gitlab.com/elixxir/comms v0.0.4-0.20211222154743-2f5bc6365f8d h1:jcEIpFn2DISLNe1nWiCocJCBgd81KUoxVCe4EVP+QUk= +gitlab.com/elixxir/comms v0.0.4-0.20211222154743-2f5bc6365f8d/go.mod h1:kvn6jAHZcRTXYdo9LvZDbO+Nw8V7Gn749fpbyUYlSN8= gitlab.com/elixxir/crypto v0.0.0-20200804182833-984246dea2c4/go.mod h1:ucm9SFKJo+K0N2GwRRpaNr+tKXMIOVWzmyUD0SbOu2c= gitlab.com/elixxir/crypto v0.0.3/go.mod h1:ZNgBOblhYToR4m8tj4cMvJ9UsJAUKq+p0gCp07WQmhA= gitlab.com/elixxir/crypto v0.0.7-0.20211220224022-1f518df56c0f h1:/VKJYB++EcVy5WvCRm/yIOWHmtqNYDodWZiNKc4bsXg=