frostfs-sdk-go/reputation/trust_test.go
Evgenii Baidakov 36b1e8442c
tests: Use dedicated function to generate signers in tests
Signed-off-by: Evgenii Baidakov <evgenii@nspcc.io>
2023-04-26 15:03:55 +04:00

204 lines
4.2 KiB
Go

package reputation_test
import (
"testing"
"github.com/nspcc-dev/neofs-api-go/v2/refs"
v2reputation "github.com/nspcc-dev/neofs-api-go/v2/reputation"
"github.com/nspcc-dev/neofs-sdk-go/crypto/test"
"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"
)
func TestTrust_Peer(t *testing.T) {
var trust reputation.Trust
require.Zero(t, trust.Peer())
trust = reputationtest.Trust()
peer := reputationtest.PeerID()
trust.SetPeer(peer)
var peerV2 v2reputation.PeerID
peer.WriteToV2(&peerV2)
var trustV2 v2reputation.Trust
trust.WriteToV2(&trustV2)
require.Equal(t, &peerV2, trustV2.GetPeer())
var val2 reputation.Trust
require.NoError(t, val2.ReadFromV2(trustV2))
require.Equal(t, peer, val2.Peer())
}
func TestTrust_Value(t *testing.T) {
var val reputation.Trust
require.Zero(t, val.Value())
val = reputationtest.Trust()
const value = 0.75
val.SetValue(value)
var trustV2 v2reputation.Trust
val.WriteToV2(&trustV2)
require.EqualValues(t, value, trustV2.GetValue())
var val2 reputation.Trust
require.NoError(t, val2.ReadFromV2(trustV2))
require.EqualValues(t, value, val2.Value())
}
func TestPeerToPeerTrust_TrustingPeer(t *testing.T) {
var val reputation.PeerToPeerTrust
require.Zero(t, val.TrustingPeer())
val = reputationtest.PeerToPeerTrust()
peer := reputationtest.PeerID()
val.SetTrustingPeer(peer)
var peerV2 v2reputation.PeerID
peer.WriteToV2(&peerV2)
var trustV2 v2reputation.PeerToPeerTrust
val.WriteToV2(&trustV2)
require.Equal(t, &peerV2, trustV2.GetTrustingPeer())
var val2 reputation.PeerToPeerTrust
require.NoError(t, val2.ReadFromV2(trustV2))
require.Equal(t, peer, val2.TrustingPeer())
}
func TestPeerToPeerTrust_Trust(t *testing.T) {
var val reputation.PeerToPeerTrust
require.Zero(t, val.Trust())
val = reputationtest.PeerToPeerTrust()
trust := reputationtest.Trust()
val.SetTrust(trust)
var trustV2 v2reputation.Trust
trust.WriteToV2(&trustV2)
var valV2 v2reputation.PeerToPeerTrust
val.WriteToV2(&valV2)
require.Equal(t, &trustV2, valV2.GetTrust())
var val2 reputation.PeerToPeerTrust
require.NoError(t, val2.ReadFromV2(valV2))
require.Equal(t, trust, val2.Trust())
}
func TestGlobalTrust_Init(t *testing.T) {
var val reputation.GlobalTrust
val.Init()
var valV2 v2reputation.GlobalTrust
val.WriteToV2(&valV2)
var verV2 refs.Version
version.Current().WriteToV2(&verV2)
require.Equal(t, &verV2, valV2.GetVersion())
}
func TestGlobalTrust_Manager(t *testing.T) {
var val reputation.GlobalTrust
require.Zero(t, val.Manager())
val = reputationtest.SignedGlobalTrust(t)
peer := reputationtest.PeerID()
val.SetManager(peer)
var peerV2 v2reputation.PeerID
peer.WriteToV2(&peerV2)
var trustV2 v2reputation.GlobalTrust
val.WriteToV2(&trustV2)
require.Equal(t, &peerV2, trustV2.GetBody().GetManager())
var val2 reputation.GlobalTrust
require.NoError(t, val2.ReadFromV2(trustV2))
require.Equal(t, peer, val2.Manager())
}
func TestGlobalTrust_Trust(t *testing.T) {
var val reputation.GlobalTrust
require.Zero(t, val.Trust())
val = reputationtest.SignedGlobalTrust(t)
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())
}
func TestGlobalTrust_Sign(t *testing.T) {
val := reputationtest.GlobalTrust()
require.False(t, val.VerifySignature())
require.NoError(t, val.Sign(test.RandomSigner(t)))
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())
}
func TestGlobalTrustEncoding(t *testing.T) {
val := reputationtest.SignedGlobalTrust(t)
t.Run("binary", func(t *testing.T) {
data := val.Marshal()
var val2 reputation.GlobalTrust
require.NoError(t, val2.Unmarshal(data))
require.Equal(t, val, val2)
})
}