2021-11-08 08:41:39 +00:00
|
|
|
package reputation_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
|
|
|
v2reputation "github.com/nspcc-dev/neofs-api-go/v2/reputation"
|
|
|
|
neofsecdsa "github.com/nspcc-dev/neofs-sdk-go/crypto/ecdsa"
|
2021-11-08 08:41:39 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/reputation"
|
|
|
|
reputationtest "github.com/nspcc-dev/neofs-sdk-go/reputation/test"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/version"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
func TestTrust_Peer(t *testing.T) {
|
|
|
|
var trust reputation.Trust
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
require.Zero(t, trust.Peer())
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
trust = reputationtest.Trust()
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
peer := reputationtest.PeerID()
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
trust.SetPeer(peer)
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
var peerV2 v2reputation.PeerID
|
|
|
|
peer.WriteToV2(&peerV2)
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
var trustV2 v2reputation.Trust
|
|
|
|
trust.WriteToV2(&trustV2)
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
require.Equal(t, &peerV2, trustV2.GetPeer())
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
var val2 reputation.Trust
|
|
|
|
require.NoError(t, val2.ReadFromV2(trustV2))
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
require.Equal(t, peer, val2.Peer())
|
|
|
|
}
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
func TestTrust_Value(t *testing.T) {
|
|
|
|
var val reputation.Trust
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
require.Zero(t, val.Value())
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
val = reputationtest.Trust()
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
const value = 0.75
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
val.SetValue(value)
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
var trustV2 v2reputation.Trust
|
|
|
|
val.WriteToV2(&trustV2)
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
require.EqualValues(t, value, trustV2.GetValue())
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
var val2 reputation.Trust
|
|
|
|
require.NoError(t, val2.ReadFromV2(trustV2))
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
require.EqualValues(t, value, val2.Value())
|
2021-11-08 08:41:39 +00:00
|
|
|
}
|
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
func TestPeerToPeerTrust_TrustingPeer(t *testing.T) {
|
|
|
|
var val reputation.PeerToPeerTrust
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
require.Zero(t, val.TrustingPeer())
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
val = reputationtest.PeerToPeerTrust()
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
peer := reputationtest.PeerID()
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
val.SetTrustingPeer(peer)
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
var peerV2 v2reputation.PeerID
|
|
|
|
peer.WriteToV2(&peerV2)
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
var trustV2 v2reputation.PeerToPeerTrust
|
|
|
|
val.WriteToV2(&trustV2)
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
require.Equal(t, &peerV2, trustV2.GetTrustingPeer())
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
var val2 reputation.PeerToPeerTrust
|
|
|
|
require.NoError(t, val2.ReadFromV2(trustV2))
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
require.Equal(t, peer, val2.TrustingPeer())
|
|
|
|
}
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
func TestPeerToPeerTrust_Trust(t *testing.T) {
|
|
|
|
var val reputation.PeerToPeerTrust
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
require.Zero(t, val.Trust())
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
val = reputationtest.PeerToPeerTrust()
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
trust := reputationtest.Trust()
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
val.SetTrust(trust)
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
var trustV2 v2reputation.Trust
|
|
|
|
trust.WriteToV2(&trustV2)
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
var valV2 v2reputation.PeerToPeerTrust
|
|
|
|
val.WriteToV2(&valV2)
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
require.Equal(t, &trustV2, valV2.GetTrust())
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
var val2 reputation.PeerToPeerTrust
|
|
|
|
require.NoError(t, val2.ReadFromV2(valV2))
|
|
|
|
|
|
|
|
require.Equal(t, trust, val2.Trust())
|
2021-11-08 08:41:39 +00:00
|
|
|
}
|
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
func TestGlobalTrust_Init(t *testing.T) {
|
|
|
|
var val reputation.GlobalTrust
|
|
|
|
val.Init()
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
var valV2 v2reputation.GlobalTrust
|
|
|
|
val.WriteToV2(&valV2)
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
var verV2 refs.Version
|
|
|
|
version.Current().WriteToV2(&verV2)
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
require.Equal(t, &verV2, valV2.GetVersion())
|
2021-11-08 08:41:39 +00:00
|
|
|
}
|
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
func TestGlobalTrust_Manager(t *testing.T) {
|
|
|
|
var val reputation.GlobalTrust
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
require.Zero(t, val.Manager())
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
val = reputationtest.SignedGlobalTrust()
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
peer := reputationtest.PeerID()
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
val.SetManager(peer)
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
var peerV2 v2reputation.PeerID
|
|
|
|
peer.WriteToV2(&peerV2)
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
var trustV2 v2reputation.GlobalTrust
|
|
|
|
val.WriteToV2(&trustV2)
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
require.Equal(t, &peerV2, trustV2.GetBody().GetManager())
|
|
|
|
|
|
|
|
var val2 reputation.GlobalTrust
|
|
|
|
require.NoError(t, val2.ReadFromV2(trustV2))
|
|
|
|
|
|
|
|
require.Equal(t, peer, val2.Manager())
|
2021-11-08 08:41:39 +00:00
|
|
|
}
|
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
func TestGlobalTrust_Trust(t *testing.T) {
|
|
|
|
var val reputation.GlobalTrust
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
require.Zero(t, val.Trust())
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
val = reputationtest.SignedGlobalTrust()
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
trust := reputationtest.Trust()
|
|
|
|
|
|
|
|
val.SetTrust(trust)
|
|
|
|
|
|
|
|
var trustV2 v2reputation.Trust
|
|
|
|
trust.WriteToV2(&trustV2)
|
|
|
|
|
|
|
|
var valV2 v2reputation.GlobalTrust
|
|
|
|
val.WriteToV2(&valV2)
|
|
|
|
|
|
|
|
require.Equal(t, &trustV2, valV2.GetBody().GetTrust())
|
|
|
|
|
|
|
|
var val2 reputation.GlobalTrust
|
|
|
|
require.NoError(t, val2.ReadFromV2(valV2))
|
|
|
|
|
|
|
|
require.Equal(t, trust, val2.Trust())
|
2021-11-08 08:41:39 +00:00
|
|
|
}
|
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
func TestGlobalTrust_Sign(t *testing.T) {
|
|
|
|
k, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
val := reputationtest.GlobalTrust()
|
|
|
|
|
|
|
|
require.False(t, val.VerifySignature())
|
|
|
|
|
|
|
|
require.NoError(t, val.Sign(neofsecdsa.Signer(k.PrivateKey)))
|
|
|
|
|
|
|
|
var valV2 v2reputation.GlobalTrust
|
|
|
|
val.WriteToV2(&valV2)
|
|
|
|
|
|
|
|
require.NotZero(t, valV2.GetSignature())
|
|
|
|
|
|
|
|
var val2 reputation.GlobalTrust
|
|
|
|
require.NoError(t, val2.ReadFromV2(valV2))
|
|
|
|
|
|
|
|
require.True(t, val2.VerifySignature())
|
|
|
|
}
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
func TestGlobalTrustEncoding(t *testing.T) {
|
|
|
|
val := reputationtest.SignedGlobalTrust()
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
t.Run("binary", func(t *testing.T) {
|
|
|
|
data := val.Marshal()
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
var val2 reputation.GlobalTrust
|
|
|
|
require.NoError(t, val2.Unmarshal(data))
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
require.Equal(t, val, val2)
|
2021-11-08 08:41:39 +00:00
|
|
|
})
|
|
|
|
}
|