2019-08-26 17:49:42 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package overlay_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"storj.io/storj/pkg/identity"
|
|
|
|
"storj.io/storj/pkg/storj"
|
2019-11-14 19:46:15 +00:00
|
|
|
"storj.io/storj/private/testcontext"
|
|
|
|
"storj.io/storj/private/testidentity"
|
2019-08-26 17:49:42 +01:00
|
|
|
"storj.io/storj/satellite"
|
|
|
|
"storj.io/storj/satellite/satellitedb/satellitedbtest"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPeerIdentities(t *testing.T) {
|
|
|
|
satellitedbtest.Run(t, func(t *testing.T, db satellite.DB) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
encode := identity.EncodePeerIdentity
|
|
|
|
|
|
|
|
idents := db.PeerIdentities()
|
|
|
|
|
|
|
|
{ // basic tests
|
|
|
|
ca, err := testidentity.NewTestCA(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
leafFirst, err := ca.NewIdentity()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
leafSecond, err := ca.NewIdentity()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// sanity check
|
|
|
|
require.Equal(t, leafFirst.ID, leafSecond.ID)
|
|
|
|
|
|
|
|
{ // add entry
|
|
|
|
err := idents.Set(ctx, leafFirst.ID, leafFirst.PeerIdentity())
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // get the entry
|
|
|
|
got, err := idents.Get(ctx, leafFirst.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, encode(leafFirst.PeerIdentity()), encode(got))
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // update entry
|
|
|
|
err := idents.Set(ctx, leafSecond.ID, leafSecond.PeerIdentity())
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // get the entry
|
|
|
|
got, err := idents.Get(ctx, leafFirst.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, encode(leafSecond.PeerIdentity()), encode(got))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-18 14:50:33 +01:00
|
|
|
{ // get multiple with invalid
|
2019-08-26 17:49:42 +01:00
|
|
|
list := make(map[storj.NodeID]*identity.PeerIdentity)
|
2019-09-18 14:50:33 +01:00
|
|
|
ids := storj.NodeIDList{}
|
|
|
|
savedIDs := make(map[storj.NodeID]bool)
|
|
|
|
unsavedIDs := make(map[storj.NodeID]bool)
|
2019-08-26 17:49:42 +01:00
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
ident := testidentity.MustPregeneratedSignedIdentity(i, storj.LatestIDVersion())
|
|
|
|
list[ident.ID] = ident.PeerIdentity()
|
2019-09-18 14:50:33 +01:00
|
|
|
ids = append(ids, ident.ID)
|
2019-08-26 17:49:42 +01:00
|
|
|
|
2019-09-18 14:50:33 +01:00
|
|
|
// only place half in the table
|
|
|
|
if i%2 == 0 {
|
|
|
|
unsavedIDs[ident.ID] = true
|
|
|
|
continue
|
|
|
|
}
|
2019-08-26 17:49:42 +01:00
|
|
|
err := idents.Set(ctx, ident.ID, ident.PeerIdentity())
|
|
|
|
require.NoError(t, err)
|
2019-09-18 14:50:33 +01:00
|
|
|
savedIDs[ident.ID] = true
|
2019-08-26 17:49:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
got, err := idents.BatchGet(ctx, ids)
|
|
|
|
require.NoError(t, err)
|
2019-09-18 14:50:33 +01:00
|
|
|
require.Len(t, got, len(savedIDs))
|
2019-08-26 17:49:42 +01:00
|
|
|
for _, gotIdent := range got {
|
|
|
|
require.Equal(t, encode(list[gotIdent.ID]), encode(gotIdent))
|
2019-09-18 14:50:33 +01:00
|
|
|
require.True(t, savedIDs[gotIdent.ID])
|
|
|
|
require.False(t, unsavedIDs[gotIdent.ID])
|
2019-08-26 17:49:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|