diff --git a/auth/fmt_test.go b/auth/fmt_test.go new file mode 100644 index 0000000000000000000000000000000000000000..9e87d2f587da56e586e16578f94f915e6178c61b --- /dev/null +++ b/auth/fmt_test.go @@ -0,0 +1,472 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// + +package auth + +import ( + "bytes" + "gitlab.com/xx_network/primitives/id" + "math/rand" + "reflect" + "testing" +) + +// Tests newBaseFormat +func TestNewBaseFormat(t *testing.T) { + // Construct message + pubKeySize := 256 + payloadSize := saltSize + pubKeySize + baseMsg := newBaseFormat(payloadSize, pubKeySize) + + // Check that the base format was constructed properly + if !bytes.Equal(baseMsg.pubkey, make([]byte, pubKeySize)) { + t.Errorf("NewBaseFormat error: "+ + "Unexpected pubkey field in base format."+ + "\n\tExpected: %v"+ + "\n\tReceived: %v", make([]byte, pubKeySize), baseMsg.pubkey) + } + + if !bytes.Equal(baseMsg.salt, make([]byte, saltSize)) { + t.Errorf("NewBaseFormat error: "+ + "Unexpected salt field in base format."+ + "\n\tExpected: %v"+ + "\n\tReceived: %v", make([]byte, saltSize), baseMsg.salt) + } + + expectedEcrPayloadSize := payloadSize - (pubKeySize + saltSize) + if !bytes.Equal(baseMsg.ecrPayload, make([]byte, expectedEcrPayloadSize)) { + t.Errorf("NewBaseFormat error: "+ + "Unexpected payload field in base format."+ + "\n\tExpected: %v"+ + "\n\tReceived: %v", make([]byte, expectedEcrPayloadSize), baseMsg.ecrPayload) + } + + // Error case, where payload size is less than the public key plus salt + defer func() { + if r := recover(); r == nil { + t.Error("newBaseFormat() did not panic when the size of " + + "the payload is smaller than the size of the public key.") + } + }() + + newBaseFormat(0, pubKeySize) +} + +/* Tests the setter/getter methods for baseFormat */ + +// Set/Get PubKey tests +func TestBaseFormat_SetGetPubKey(t *testing.T) { + // Construct message + pubKeySize := 256 + payloadSize := saltSize + pubKeySize + baseMsg := newBaseFormat(payloadSize, pubKeySize) + + // Test setter + grp := getGroup() + pubKey := grp.NewInt(25) + baseMsg.SetPubKey(pubKey) + expectedBytes := pubKey.LeftpadBytes(uint64(len(baseMsg.pubkey))) + if !bytes.Equal(baseMsg.pubkey, expectedBytes) { + t.Errorf("SetPubKey() error: "+ + "Public key field does not have expected value."+ + "\n\tExpected: %v\n\tReceived: %v", expectedBytes, baseMsg.pubkey) + } + + // Test getter + receivedKey := baseMsg.GetPubKey(grp) + if !bytes.Equal(pubKey.Bytes(), receivedKey.Bytes()) { + t.Errorf("GetPubKey() error: "+ + "Public key retrieved does not have expected value."+ + "\n\tExpected: %v\n\tReceived: %v", pubKey, receivedKey) + } + +} + +// Set/Get salt tests +func TestBaseFormat_SetGetSalt(t *testing.T) { + // Construct message + pubKeySize := 256 + payloadSize := saltSize + pubKeySize + baseMsg := newBaseFormat(payloadSize, pubKeySize) + + // Test setter + salt := newSalt("salt") + baseMsg.SetSalt(salt) + if !bytes.Equal(salt, baseMsg.salt) { + t.Errorf("SetSalt() error: "+ + "Salt field does not have expected value."+ + "\n\tExpected: %v\n\tReceived: %v", salt, baseMsg.salt) + } + + // Test getter + receivedSalt := baseMsg.GetSalt() + if !bytes.Equal(salt, receivedSalt) { + t.Errorf("GetSalt() error: "+ + "Salt retrieved does not have expected value."+ + "\n\tExpected: %v\n\tReceived: %v", salt, receivedSalt) + } + + // Test setter error path: Setting salt of incorrect size + defer func() { + if r := recover(); r == nil { + t.Error("SetSalt() did not panic when the size of " + + "the salt is smaller than the required salt size.") + } + }() + + baseMsg.SetSalt([]byte("salt")) +} + +// Set/Get EcrPayload tests +func TestBaseFormat_SetGetEcrPayload(t *testing.T) { + // Construct message + pubKeySize := 256 + payloadSize := (saltSize + pubKeySize) * 2 + baseMsg := newBaseFormat(payloadSize, pubKeySize) + + // Test setter + ecrPayloadSize := payloadSize - (pubKeySize + saltSize) + ecrPayload := newPayload(ecrPayloadSize, "ecrPayload") + baseMsg.SetEcrPayload(ecrPayload) + if !bytes.Equal(ecrPayload, baseMsg.ecrPayload) { + t.Errorf("SetEcrPayload() error: "+ + "EcrPayload field does not have expected value."+ + "\n\tExpected: %v\n\tReceived: %v", ecrPayload, baseMsg.ecrPayload) + + } + + // Test Getter + receivedEcrPayload := baseMsg.GetEcrPayload() + if !bytes.Equal(receivedEcrPayload, ecrPayload) { + t.Errorf("GetEcrPayload() error: "+ + "EcrPayload retrieved does not have expected value."+ + "\n\tExpected: %v\n\tReceived: %v", ecrPayload, receivedEcrPayload) + } + + // Setter error path: Setting ecrPayload that + // does not completely fill field + defer func() { + if r := recover(); r == nil { + t.Error("SetEcrPayload() did not panic when the size of " + + "the ecrPayload is smaller than the pre-constructed field.") + } + }() + baseMsg.SetEcrPayload([]byte("ecrPayload")) +} + +// Marshal/ unmarshal tests +func TestBaseFormat_MarshalUnmarshal(t *testing.T) { + // Construct a fully populated message + pubKeySize := 256 + payloadSize := (saltSize + pubKeySize) * 2 + baseMsg := newBaseFormat(payloadSize, pubKeySize) + ecrPayloadSize := payloadSize - (pubKeySize + saltSize) + ecrPayload := newPayload(ecrPayloadSize, "ecrPayload") + baseMsg.SetEcrPayload(ecrPayload) + salt := newSalt("salt") + baseMsg.SetSalt(salt) + grp := getGroup() + pubKey := grp.NewInt(25) + baseMsg.SetPubKey(pubKey) + + // Test marshal + data := baseMsg.Marshal() + if !bytes.Equal(data, baseMsg.data) { + t.Errorf("baseFormat.Marshal() error: "+ + "Marshalled data is not expected."+ + "\n\tExpected: %v\n\tReceived: %v", baseMsg.data, data) + } + + // Test unmarshal + newMsg, err := unmarshalBaseFormat(data, pubKeySize) + if err != nil { + t.Errorf("unmarshalBaseFormat() error: "+ + "Could not unmarshal into baseFormat: %v", err) + } + + if !reflect.DeepEqual(newMsg, baseMsg) { + t.Errorf("unmarshalBaseFormat() error: "+ + "Unmarshalled message does not match originally marshalled message."+ + "\n\tExpected: %v\n\tRecieved: %v", baseMsg, newMsg) + } + + // Unmarshal error test: Invalid size parameter + _, err = unmarshalBaseFormat(make([]byte, 0), pubKeySize) + if err == nil { + t.Errorf("unmarshalBaseFormat() error: " + + "Should not be able to unmarshal when baseFormat is too small") + } + +} + +// Tests newEcrFormat +func TestNewEcrFormat(t *testing.T) { + // Construct message + payloadSize := ownershipSize * 2 + ecrMsg := newEcrFormat(payloadSize) + + // Check that the ecrFormat was constructed properly + if !bytes.Equal(ecrMsg.ownership, make([]byte, ownershipSize)) { + t.Errorf("newEcrFormat error: "+ + "Unexpected ownership field in ecrFormat."+ + "\n\tExpected: %v"+ + "\n\tReceived: %v", make([]byte, payloadSize), ecrMsg.ownership) + } + + if !bytes.Equal(ecrMsg.payload, make([]byte, payloadSize-ownershipSize)) { + t.Errorf("newEcrFormat error: "+ + "Unexpected ownership field in ecrFormat."+ + "\n\tExpected: %v"+ + "\n\tReceived: %v", make([]byte, payloadSize-ownershipSize), ecrMsg.payload) + } + + // Error case, where payload size is less than the public key plus salt + defer func() { + if r := recover(); r == nil { + t.Error("newEcrFormat() did not panic when the size of " + + "the payload is smaller than the size of the ownership") + } + }() + + newEcrFormat(0) +} + +/* Tests the setter/getter methods for ecrFormat */ + +// Set/Get ownership tests +func TestEcrFormat_SetGetOwnership(t *testing.T) { + // Construct message + payloadSize := ownershipSize * 2 + ecrMsg := newEcrFormat(payloadSize) + + // Test setter + ownership := newOwnership("owner") + ecrMsg.SetOwnership(ownership) + if !bytes.Equal(ownership, ecrMsg.ownership) { + t.Errorf("SetOwnership() error: "+ + "Ownership field does not have expected value."+ + "\n\tExpected: %v\n\tReceived: %v", ownership, ecrMsg.ownership) + + } + + // Test getter + receivedOwnership := ecrMsg.GetOwnership() + if !bytes.Equal(receivedOwnership, ecrMsg.ownership) { + t.Errorf("GetOwnership() error: "+ + "Ownership retrieved does not have expected value."+ + "\n\tExpected: %v\n\tReceived: %v", ownership, receivedOwnership) + + } + + // Test setter error path: Setting ownership of incorrect size + defer func() { + if r := recover(); r == nil { + t.Error("SetOwnership() did not panic when the size of " + + "the ownership is smaller than the required ownership size.") + } + }() + + ecrMsg.SetOwnership([]byte("ownership")) +} + +// Set/Get payload tests +func TestEcrFormat_SetGetPayload(t *testing.T) { + // Construct message + payloadSize := ownershipSize * 2 + ecrMsg := newEcrFormat(payloadSize) + + // Test set + expectedPayload := newPayload(payloadSize-ownershipSize, "ownership") + ecrMsg.SetPayload(expectedPayload) + + if !bytes.Equal(expectedPayload, ecrMsg.payload) { + t.Errorf("SetPayload() error: "+ + "Payload field does not have expected value."+ + "\n\tExpected: %v\n\tReceived: %v", expectedPayload, ecrMsg.payload) + } + + // Test get + receivedPayload := ecrMsg.GetPayload() + if !bytes.Equal(receivedPayload, expectedPayload) { + t.Errorf("GetPayload() error: "+ + "Payload retrieved does not have expected value."+ + "\n\tExpected: %v\n\tReceived: %v", expectedPayload, receivedPayload) + + } + + // Test setter error path: Setting payload of incorrect size + defer func() { + if r := recover(); r == nil { + t.Error("SetPayload() did not panic when the size of " + + "the payload is smaller than the required payload size.") + } + }() + + ecrMsg.SetPayload([]byte("payload")) +} + +// Marshal/ unmarshal tests +func TestEcrFormat_MarshalUnmarshal(t *testing.T) { + // Construct message + payloadSize := ownershipSize * 2 + ecrMsg := newEcrFormat(payloadSize) + expectedPayload := newPayload(payloadSize-ownershipSize, "ownership") + ecrMsg.SetPayload(expectedPayload) + ownership := newOwnership("owner") + ecrMsg.SetOwnership(ownership) + + // Test marshal + data := ecrMsg.Marshal() + if !bytes.Equal(data, ecrMsg.data) { + t.Errorf("ecrFormat.Marshal() error: "+ + "Marshalled data is not expected."+ + "\n\tExpected: %v\n\tReceived: %v", ecrMsg.data, data) + } + + // Test unmarshal + newMsg, err := unmarshalEcrFormat(data) + if err != nil { + t.Errorf("unmarshalEcrFormat() error: "+ + "Could not unmarshal into ecrFormat: %v", err) + } + + if !reflect.DeepEqual(newMsg, ecrMsg) { + t.Errorf("unmarshalBaseFormat() error: "+ + "Unmarshalled message does not match originally marshalled message."+ + "\n\tExpected: %v\n\tRecieved: %v", ecrMsg, newMsg) + } + + // Unmarshal error test: Invalid size parameter + _, err = unmarshalEcrFormat(make([]byte, 0)) + if err == nil { + t.Errorf("unmarshalEcrFormat() error: " + + "Should not be able to unmarshal when ecrFormat is too small") + } + +} + +// Tests newRequestFormat +func TestNewRequestFormat(t *testing.T) { + // Construct message + payloadSize := id.ArrIDLen*2 - 1 + ecrMsg := newEcrFormat(payloadSize) + expectedPayload := newPayload(id.ArrIDLen, "ownership") + ecrMsg.SetPayload(expectedPayload) + reqMsg, err := newRequestFormat(ecrMsg) + if err != nil { + t.Fatalf("newRequestFormat() error: "+ + "Failed to construct message: %v", err) + } + + // Check that the requestFormat was constructed properly + if !bytes.Equal(reqMsg.id, expectedPayload) { + t.Errorf("newRequestFormat() error: "+ + "Unexpected id field in requestFormat."+ + "\n\tExpected: %v"+ + "\n\tReceived: %v", make([]byte, id.ArrIDLen), reqMsg.id) + } + + if !bytes.Equal(reqMsg.msgPayload, make([]byte, 0)) { + t.Errorf("newRequestFormat() error: "+ + "Unexpected msgPayload field in requestFormat."+ + "\n\tExpected: %v"+ + "\n\tReceived: %v", make([]byte, 0), reqMsg.msgPayload) + } + + payloadSize = ownershipSize * 2 + ecrMsg = newEcrFormat(payloadSize) + reqMsg, err = newRequestFormat(ecrMsg) + if err == nil { + t.Errorf("Expecter error: Should be invalid size when calling newRequestFormat") + } + +} + +/* Setter/Getter tests for RequestFormat */ + +// Unit test for Get/SetID +func TestRequestFormat_SetGetID(t *testing.T) { + // Construct message + payloadSize := id.ArrIDLen*2 - 1 + ecrMsg := newEcrFormat(payloadSize) + expectedPayload := newPayload(id.ArrIDLen, "ownership") + ecrMsg.SetPayload(expectedPayload) + reqMsg, err := newRequestFormat(ecrMsg) + if err != nil { + t.Fatalf("newRequestFormat() error: "+ + "Failed to construct message: %v", err) + } + + // Test SetID + prng := rand.New(rand.NewSource(42)) + expectedId := randID(prng, id.User) + reqMsg.SetID(expectedId) + if !bytes.Equal(reqMsg.id, expectedId.Bytes()) { + t.Errorf("SetID() error: "+ + "Id field does not have expected value."+ + "\n\tExpected: %v\n\tReceived: %v", expectedId, reqMsg.msgPayload) + } + + // Test GetID + receivedId, err := reqMsg.GetID() + if err != nil { + t.Fatalf("GetID() error: "+ + "Retrieved id does not match expected value:"+ + "\n\tExpected: %v\n\tReceived: %v", expectedId, receivedId) + } + + // Test GetID error: unmarshal-able ID in requestFormat + reqMsg.id = []byte("badId") + receivedId, err = reqMsg.GetID() + if err == nil { + t.Errorf("GetID() error: " + + "Should not be able get ID from request message ") + } + +} + +// Unit test for Get/SetMsgPayload +func TestRequestFormat_SetGetMsgPayload(t *testing.T) { + // Construct message + payloadSize := id.ArrIDLen*3 - 1 + ecrMsg := newEcrFormat(payloadSize) + expectedPayload := newPayload(id.ArrIDLen*2, "ownership") + ecrMsg.SetPayload(expectedPayload) + reqMsg, err := newRequestFormat(ecrMsg) + if err != nil { + t.Fatalf("newRequestFormat() error: "+ + "Failed to construct message: %v", err) + } + + // Test SetMsgPayload + msgPayload := newPayload(id.ArrIDLen, "msgPayload") + reqMsg.SetMsgPayload(msgPayload) + if !bytes.Equal(reqMsg.msgPayload, msgPayload) { + t.Errorf("SetMsgPayload() error: "+ + "MsgPayload has unexpected value: "+ + "\n\tExpected: %v\n\tReceived: %v", msgPayload, reqMsg.msgPayload) + } + + // Test GetMsgPayload + retrievedMsgPayload := reqMsg.GetMsgPayload() + if !bytes.Equal(retrievedMsgPayload, msgPayload) { + t.Errorf("GetMsgPayload() error: "+ + "MsgPayload has unexpected value: "+ + "\n\tExpected: %v\n\tReceived: %v", msgPayload, retrievedMsgPayload) + + } + + // Test SetMsgPayload error: Invalid message payload size + defer func() { + if r := recover(); r == nil { + t.Error("SetMsgPayload() did not panic when the size of " + + "the payload is the incorrect size.") + } + }() + expectedPayload = append(expectedPayload, expectedPayload...) + reqMsg.SetMsgPayload(expectedPayload) +} diff --git a/auth/utils_test.go b/auth/utils_test.go new file mode 100644 index 0000000000000000000000000000000000000000..95ff91489d0b6a8c98f9417265f8fe09ef1680ff --- /dev/null +++ b/auth/utils_test.go @@ -0,0 +1,50 @@ +package auth + +import ( + "gitlab.com/elixxir/crypto/cyclic" + "gitlab.com/xx_network/crypto/large" + "gitlab.com/xx_network/primitives/id" + "math/rand" +) + +func getGroup() *cyclic.Group { + return cyclic.NewGroup( + large.NewIntFromString("E2EE983D031DC1DB6F1A7A67DF0E9A8E5561DB8E8D4941"+ + "3394C049B7A8ACCEDC298708F121951D9CF920EC5D146727AA4AE535B0922C688"+ + "B55B3DD2AEDF6C01C94764DAB937935AA83BE36E67760713AB44A6337C20E7861"+ + "575E745D31F8B9E9AD8412118C62A3E2E29DF46B0864D0C951C394A5CBBDC6ADC"+ + "718DD2A3E041023DBB5AB23EBB4742DE9C1687B5B34FA48C3521632C4A530E8FF"+ + "B1BC51DADDF453B0B2717C2BC6669ED76B4BDD5C9FF558E88F26E5785302BEDBC"+ + "A23EAC5ACE92096EE8A60642FB61E8F3D24990B8CB12EE448EEF78E184C7242DD"+ + "161C7738F32BF29A841698978825B4111B4BC3E1E198455095958333D776D8B2B"+ + "EEED3A1A1A221A6E37E664A64B83981C46FFDDC1A45E3D5211AAF8BFBC072768C"+ + "4F50D7D7803D2D4F278DE8014A47323631D7E064DE81C0C6BFA43EF0E6998860F"+ + "1390B5D3FEACAF1696015CB79C3F9C2D93D961120CD0E5F12CBB687EAB045241F"+ + "96789C38E89D796138E6319BE62E35D87B1048CA28BE389B575E994DCA7554715"+ + "84A09EC723742DC35873847AEF49F66E43873", 16), + large.NewIntFromString("2", 16)) +} + +// randID returns a new random ID of the specified type. +func randID(rng *rand.Rand, t id.Type) *id.ID { + newID, _ := id.NewRandomID(rng, t) + return newID +} + +func newSalt(s string) []byte { + salt := make([]byte, saltSize) + copy(salt[:], s) + return salt +} + +func newPayload(size int, s string) []byte { + b := make([]byte, size) + copy(b[:], s) + return b +} + +func newOwnership(s string) []byte { + ownership := make([]byte, ownershipSize) + copy(ownership[:], s) + return ownership +} diff --git a/network/rounds/remoteFilters.go b/network/rounds/remoteFilters.go index e2a225632c088a9082a63ed08367b90114f80a50..7a434be073e51118d22adae682c27d2e3ad83b50 100644 --- a/network/rounds/remoteFilters.go +++ b/network/rounds/remoteFilters.go @@ -1,3 +1,10 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// + package rounds import ( diff --git a/network/rounds/remoteFilters_test.go b/network/rounds/remoteFilters_test.go new file mode 100644 index 0000000000000000000000000000000000000000..5a5e381e0b8bc24140a2e9dc5686b8ff81f00ddc --- /dev/null +++ b/network/rounds/remoteFilters_test.go @@ -0,0 +1,202 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// + +package rounds + +import ( + bloom "gitlab.com/elixxir/bloomfilter" + "gitlab.com/elixxir/client/interfaces" + "gitlab.com/elixxir/client/storage/reception" + "gitlab.com/elixxir/comms/mixmessages" + "gitlab.com/xx_network/primitives/id" + "gitlab.com/xx_network/primitives/id/ephemeral" + "reflect" + "testing" + "time" +) + +// Unit test NewRemoteFilter +func TestNewRemoteFilter(t *testing.T) { + bloomFilter := &mixmessages.ClientBloom{ + Filter: nil, + FirstRound: 0, + RoundRange: 0, + } + + rf := NewRemoteFilter(bloomFilter) + if !reflect.DeepEqual(rf.data, bloomFilter) { + t.Fatalf("NewRemoteFilter() error: "+ + "RemoteFilter not initialized as expected."+ + "\n\tExpected: %v\n\tReceived: %v", bloomFilter, rf.data) + } +} + +// Unit test GetFilter +func TestRemoteFilter_GetFilter(t *testing.T) { + testFilter, err := bloom.InitByParameters(interfaces.BloomFilterSize, + interfaces.BloomFilterHashes) + if err != nil { + t.Fatalf("GetFilter error: " + + "Cannot initialize bloom filter for setup: %v", err) + } + + data, err := testFilter.MarshalBinary() + if err != nil { + t.Fatalf("GetFilter error: " + + "Cannot marshal filter for setup: %v", err) + } + + bloomFilter := &mixmessages.ClientBloom{ + Filter: data, + FirstRound: 0, + RoundRange: 0, + } + + rf := NewRemoteFilter(bloomFilter) + retrievedFilter := rf.GetFilter() + if !reflect.DeepEqual(retrievedFilter, testFilter) { + t.Fatalf("GetFilter error: " + + "Did not retrieve expected filter." + + "\n\tExpected: %v\n\tReceived: %v", testFilter, retrievedFilter) + } +} + +// Unit test fro FirstRound and LastRound +func TestRemoteFilter_FirstLastRound(t *testing.T) { + firstRound := uint64(25) + roundRange := uint32(75) + bloomFilter := &mixmessages.ClientBloom{ + Filter: nil, + FirstRound: firstRound, + RoundRange: roundRange, + } + rf := NewRemoteFilter(bloomFilter) + + // Test FirstRound + receivedFirstRound := rf.FirstRound() + if receivedFirstRound != id.Round(firstRound) { + t.Fatalf("FirstRound error: " + + "Did not receive expected round." + + "\n\tExpected: %v\n\tReceived: %v", firstRound, receivedFirstRound) + } + + // Test LastRound + receivedLastRound := rf.LastRound() + if receivedLastRound != id.Round(firstRound + uint64(roundRange)) { + t.Fatalf("LastRound error: " + + "Did not receive expected round." + + "\n\tExpected: %v\n\tReceived: %v", receivedLastRound, firstRound + uint64(roundRange)) + } + +} + +// In bounds test +func TestValidFilterRange(t *testing.T) { + firstRound := uint64(25) + roundRange := uint32(75) + testFilter, err := bloom.InitByParameters(interfaces.BloomFilterSize, + interfaces.BloomFilterHashes) + if err != nil { + t.Fatalf("GetFilter error: " + + "Cannot initialize bloom filter for setup: %v", err) + } + + data, err := testFilter.MarshalBinary() + if err != nil { + t.Fatalf("GetFilter error: " + + "Cannot marshal filter for setup: %v", err) + } + + // Construct an in bounds value + expectedEphID := ephemeral.Id{1, 2, 3, 4, 5, 6, 7, 8} + requestGateway := id.NewIdFromString(ReturningGateway, id.Gateway, t) + iu := reception.IdentityUse{ + Identity: reception.Identity{ + EphId: expectedEphID, + Source: requestGateway, + StartValid: time.Now().Add(12*time.Hour), + EndValid: time.Now().Add(24*time.Hour), + }, + } + + + bloomFilter := &mixmessages.ClientBloom{ + Filter: data, + FirstRound: firstRound, + RoundRange: roundRange, + } + + msg := &mixmessages.ClientBlooms{ + Period: int64(12 * time.Hour ), + FirstTimestamp: time.Now().UnixNano(), + Filters: []*mixmessages.ClientBloom{bloomFilter}, + } + + start, end, outOfBounds := ValidFilterRange(iu, msg) + if outOfBounds { + t.Errorf("ValidFilterRange error: " + + "Range should not be out of bounds") + } + + if start != 0 && end != 1 { + t.Errorf("ValidFilterRange error: " + + "Unexpected indices returned. " + + "\n\tExpected start: %v\n\tReceived start: %v" + + "\n\tExpected end: %v\n\tReceived end: %v", 0, start, 1, end) + } + +} + +// out of bounds test +func TestValidFilterRange_OutBounds(t *testing.T) { + firstRound := uint64(25) + roundRange := uint32(75) + testFilter, err := bloom.InitByParameters(interfaces.BloomFilterSize, + interfaces.BloomFilterHashes) + if err != nil { + t.Fatalf("GetFilter error: " + + "Cannot initialize bloom filter for setup: %v", err) + } + + data, err := testFilter.MarshalBinary() + if err != nil { + t.Fatalf("GetFilter error: " + + "Cannot marshal filter for setup: %v", err) + } + + // Construct an in bounds value + expectedEphID := ephemeral.Id{1, 2, 3, 4, 5, 6, 7, 8} + requestGateway := id.NewIdFromString(ReturningGateway, id.Gateway, t) + iu := reception.IdentityUse{ + Identity: reception.Identity{ + EphId: expectedEphID, + Source: requestGateway, + StartValid: time.Now().Add(-24*time.Hour), + EndValid: time.Now().Add(-36*time.Hour), + }, + } + + + bloomFilter := &mixmessages.ClientBloom{ + Filter: data, + FirstRound: firstRound, + RoundRange: roundRange, + } + + msg := &mixmessages.ClientBlooms{ + Period: int64(12 * time.Hour ), + FirstTimestamp: time.Now().UnixNano(), + Filters: []*mixmessages.ClientBloom{bloomFilter}, + } + + _, _, outOfBounds := ValidFilterRange(iu, msg) + if !outOfBounds { + t.Errorf("ValidFilterRange error: " + + "Range should be out of bounds") + } + +} \ No newline at end of file diff --git a/storage/ndf_test.go b/storage/ndf_test.go new file mode 100644 index 0000000000000000000000000000000000000000..ece36461e5368cd110b0004d95fc09bf3f28837f --- /dev/null +++ b/storage/ndf_test.go @@ -0,0 +1,33 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// + +package storage + +import ( + "reflect" + "testing" +) + +func TestSession_SetGetNDF(t *testing.T) { + sess := InitTestingSession(t) + testNdf := getNDF() + sess.SetNDF(testNdf) + + if !reflect.DeepEqual(testNdf, sess.ndf) { + t.Errorf("SetNDF error: "+ + "Unexpected value after setting ndf:"+ + "Expected: %v\n\tReceived: %v", testNdf, sess.ndf) + } + + receivedNdf := sess.GetNDF() + if !reflect.DeepEqual(testNdf, receivedNdf) { + t.Errorf("GetNDF error: "+ + "Unexpected value retrieved from GetNdf:"+ + "Expected: %v\n\tReceived: %v", testNdf, receivedNdf) + + } +} diff --git a/storage/utils_test.go b/storage/utils_test.go new file mode 100644 index 0000000000000000000000000000000000000000..6e9cc93343c47ea97587b76bb0951bc9e3e8ac94 --- /dev/null +++ b/storage/utils_test.go @@ -0,0 +1,55 @@ +package storage + +import ( + "gitlab.com/xx_network/primitives/id" + "gitlab.com/xx_network/primitives/ndf" + "math/rand" +) + +// randID returns a new random ID of the specified type. +func randID(rng *rand.Rand, t id.Type) *id.ID { + newID, _ := id.NewRandomID(rng, t) + return newID +} + +func getNDF() *ndf.NetworkDefinition { + return &ndf.NetworkDefinition{ + E2E: ndf.Group{ + Prime: "E2EE983D031DC1DB6F1A7A67DF0E9A8E5561DB8E8D49413394C049B7A" + + "8ACCEDC298708F121951D9CF920EC5D146727AA4AE535B0922C688B55B3D" + + "D2AEDF6C01C94764DAB937935AA83BE36E67760713AB44A6337C20E78615" + + "75E745D31F8B9E9AD8412118C62A3E2E29DF46B0864D0C951C394A5CBBDC" + + "6ADC718DD2A3E041023DBB5AB23EBB4742DE9C1687B5B34FA48C3521632C" + + "4A530E8FFB1BC51DADDF453B0B2717C2BC6669ED76B4BDD5C9FF558E88F2" + + "6E5785302BEDBCA23EAC5ACE92096EE8A60642FB61E8F3D24990B8CB12EE" + + "448EEF78E184C7242DD161C7738F32BF29A841698978825B4111B4BC3E1E" + + "198455095958333D776D8B2BEEED3A1A1A221A6E37E664A64B83981C46FF" + + "DDC1A45E3D5211AAF8BFBC072768C4F50D7D7803D2D4F278DE8014A47323" + + "631D7E064DE81C0C6BFA43EF0E6998860F1390B5D3FEACAF1696015CB79C" + + "3F9C2D93D961120CD0E5F12CBB687EAB045241F96789C38E89D796138E63" + + "19BE62E35D87B1048CA28BE389B575E994DCA755471584A09EC723742DC3" + + "5873847AEF49F66E43873", + Generator: "2", + }, + CMIX: ndf.Group{ + Prime: "9DB6FB5951B66BB6FE1E140F1D2CE5502374161FD6538DF1648218642" + + "F0B5C48C8F7A41AADFA187324B87674FA1822B00F1ECF8136943D7C55757" + + "264E5A1A44FFE012E9936E00C1D3E9310B01C7D179805D3058B2A9F4BB6F" + + "9716BFE6117C6B5B3CC4D9BE341104AD4A80AD6C94E005F4B993E14F091E" + + "B51743BF33050C38DE235567E1B34C3D6A5C0CEAA1A0F368213C3D19843D" + + "0B4B09DCB9FC72D39C8DE41F1BF14D4BB4563CA28371621CAD3324B6A2D3" + + "92145BEBFAC748805236F5CA2FE92B871CD8F9C36D3292B5509CA8CAA77A" + + "2ADFC7BFD77DDA6F71125A7456FEA153E433256A2261C6A06ED3693797E7" + + "995FAD5AABBCFBE3EDA2741E375404AE25B", + Generator: "5C7FF6B06F8F143FE8288433493E4769C4D988ACE5BE25A0E2480" + + "9670716C613D7B0CEE6932F8FAA7C44D2CB24523DA53FBE4F6EC3595892D" + + "1AA58C4328A06C46A15662E7EAA703A1DECF8BBB2D05DBE2EB956C142A33" + + "8661D10461C0D135472085057F3494309FFA73C611F78B32ADBB5740C361" + + "C9F35BE90997DB2014E2EF5AA61782F52ABEB8BD6432C4DD097BC5423B28" + + "5DAFB60DC364E8161F4A2A35ACA3A10B1C4D203CC76A470A33AFDCBDD929" + + "59859ABD8B56E1725252D78EAC66E71BA9AE3F1DD2487199874393CD4D83" + + "2186800654760E1E34C09E4D155179F9EC0DC4473F996BDCE6EED1CABED8" + + "B6F116F7AD9CF505DF0F998E34AB27514B0FFE7", + }, + } +}