Skip to content
Snippets Groups Projects
Commit 223718df authored by Jono Wenger's avatar Jono Wenger
Browse files

Merge remote-tracking branch 'origin/release' into XX-4277/fileTransferCrashDebug

parents 41a135fc 1aecd3a8
No related branches found
No related tags found
1 merge request!430XX-4277 / Fix file transfer crashes
......@@ -14,6 +14,7 @@ import (
"gitlab.com/xx_network/primitives/id/ephemeral"
"gitlab.com/xx_network/primitives/netTime"
"math/rand"
"reflect"
"testing"
"time"
)
......@@ -109,3 +110,109 @@ func TestIdentity_Equal(t *testing.T) {
"\na: %s\nc: %s", a, c)
}
}
// TestIdentity_store_loadProcessNext tests that when an Identity is stored,
// Identity.ProcessNext is loaded. This test is exhaustive by making a reasonably
// long Identity.ProcessNext linked list, and checking if all Identity's are loaded.
func TestIdentity_store_loadProcessNext(t *testing.T) {
kv := versioned.NewKV(ekv.MakeMemstore())
const numTests = 10
// Construct the first identity, which will be stored
ephId := ephemeral.Id{}
copy(ephId[:], []byte{0})
first := &Identity{
EphemeralIdentity: EphemeralIdentity{
EphId: ephId,
Source: &id.Permissioning,
},
AddressSize: 0,
End: netTime.Now().Round(0),
ExtraChecks: 0,
StartValid: netTime.Now().Round(0),
EndValid: netTime.Now().Round(0),
Ephemeral: false,
}
// Build the linked list with unique identities. Use temp as a temporary
// head, such that the previous node in the linked list can have its next
// set with the next identity, and then set temp to the next identity for
// the next iteration
temp := first
for i := 1; i < numTests; i++ {
// Ensure uniqueness of every identity by having the ephemeral ID
// contain the number of the iteration (ie the value of i)
ephId = ephemeral.Id{}
copy(ephId[:], []byte{byte(i)})
next := &Identity{
EphemeralIdentity: EphemeralIdentity{
EphId: ephId,
Source: &id.Permissioning,
},
AddressSize: 25,
End: netTime.Now().Round(0),
ExtraChecks: 16,
StartValid: netTime.Now().Round(0),
EndValid: netTime.Now().Round(0),
Ephemeral: false,
}
temp.ProcessNext = next
temp = next
}
// Save the first identity. This should be the head of
// the created linked list, and thus all nodes (Identity's) should be saved.
err := first.store(kv)
if err != nil {
t.Errorf("Failed to store: %s", err)
}
// Load the identity
loadedIdentity, err := loadIdentity(kv)
if err != nil {
t.Errorf("Failed to load: %+v", err)
}
// Smoke test: Check that there is a next element in the linked list
if loadedIdentity.ProcessNext == nil {
t.Fatalf("Failed to load processNext for identity!")
}
// Serialize the linked list, such that we can iterate over
// it to check for expected values later
temp = &loadedIdentity
serializedList := make([]*Identity, 0)
for temp != nil {
serializedList = append(serializedList, temp)
temp = temp.ProcessNext
}
// Smoke test: Check that the number of loaded identities is of the
// expected quantity
if len(serializedList) != numTests {
t.Fatalf("Bad number of identities from Identity.ProcessNext."+
"\nExpected: %d"+
"\nReceived: %d", numTests, len(serializedList))
}
// Go through every identity to make sure it has the expected value
for i := 0; i < len(serializedList); i++ {
// The loaded identity should have an ephemeral ID
// contain the number of the iteration (ie the value of i)
ephId = ephemeral.Id{}
copy(ephId[:], []byte{byte(i)})
received := serializedList[i]
if !reflect.DeepEqual(ephId, received.EphId) {
t.Errorf("Identity #%d loaded is not expected."+
"\nExpected: %+v"+
"\nReceived: %+v", i, ephId, received.EphId)
}
}
}
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