diff --git a/bearer/bearer_test.go b/bearer/bearer_test.go index 26394c8a..11194c6d 100644 --- a/bearer/bearer_test.go +++ b/bearer/bearer_test.go @@ -11,7 +11,7 @@ import ( bearertest "github.com/nspcc-dev/neofs-sdk-go/bearer/test" cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test" neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto" - "github.com/nspcc-dev/neofs-sdk-go/crypto/test" + neofscryptotest "github.com/nspcc-dev/neofs-sdk-go/crypto/test" "github.com/nspcc-dev/neofs-sdk-go/eacl" eacltest "github.com/nspcc-dev/neofs-sdk-go/eacl/test" usertest "github.com/nspcc-dev/neofs-sdk-go/user/test" @@ -257,11 +257,11 @@ func TestToken_Sign(t *testing.T) { require.False(t, val.VerifySignature()) - signer := test.RandomSignerRFC6979() + usr := usertest.User() val = bearertest.Token(t) - require.NoError(t, val.Sign(signer)) + require.NoError(t, val.Sign(usr)) require.True(t, val.VerifySignature()) @@ -294,11 +294,11 @@ func TestToken_SignedData(t *testing.T) { require.NoError(t, dec.UnmarshalSignedData(signedData)) require.Equal(t, val, dec) - signer := test.RandomSignerRFC6979() + usr := usertest.User() val = bearertest.Token(t) - val.SetIssuer(signer.UserID()) + val.SetIssuer(usr.UserID()) - test.SignedDataComponentUser(t, signer, &val) + usertest.TestSignedData(t, usr, &val) } func TestToken_ReadFromV2(t *testing.T) { @@ -360,11 +360,9 @@ func TestToken_ReadFromV2(t *testing.T) { require.True(t, val.AssertUser(usr)) require.False(t, val.AssertUser(usr2)) - signer := test.RandomSigner() - var s neofscrypto.Signature - require.NoError(t, s.CalculateMarshalled(signer, &body, nil)) + require.NoError(t, s.CalculateMarshalled(neofscryptotest.Signer(), &body, nil)) s.WriteToV2(&sig) @@ -374,7 +372,7 @@ func TestToken_ReadFromV2(t *testing.T) { } func TestResolveIssuer(t *testing.T) { - signer := test.RandomSignerRFC6979() + usr := usertest.User() var val bearer.Token @@ -391,12 +389,12 @@ func TestResolveIssuer(t *testing.T) { require.Zero(t, val.ResolveIssuer()) - require.NoError(t, val.Sign(signer)) + require.NoError(t, val.Sign(usr)) - usr := signer.UserID() + usrID := usr.UserID() - require.Equal(t, usr, val.ResolveIssuer()) - require.Equal(t, usr, val.Issuer()) + require.Equal(t, usrID, val.ResolveIssuer()) + require.Equal(t, usrID, val.Issuer()) } func TestToken_Issuer(t *testing.T) { diff --git a/client/container_statistic_test.go b/client/container_statistic_test.go index 434b7e84..f55476b9 100644 --- a/client/container_statistic_test.go +++ b/client/container_statistic_test.go @@ -25,7 +25,6 @@ import ( "github.com/nspcc-dev/neofs-sdk-go/container/acl" cid "github.com/nspcc-dev/neofs-sdk-go/container/id" neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto" - "github.com/nspcc-dev/neofs-sdk-go/crypto/test" "github.com/nspcc-dev/neofs-sdk-go/eacl" "github.com/nspcc-dev/neofs-sdk-go/netmap" "github.com/nspcc-dev/neofs-sdk-go/object" @@ -34,6 +33,7 @@ import ( session2 "github.com/nspcc-dev/neofs-sdk-go/session" "github.com/nspcc-dev/neofs-sdk-go/stat" "github.com/nspcc-dev/neofs-sdk-go/user" + usertest "github.com/nspcc-dev/neofs-sdk-go/user/test" "github.com/stretchr/testify/require" ) @@ -150,7 +150,7 @@ func testEaclTable(containerID cid.ID) eacl.Table { } func TestClientStatistic_AccountBalance(t *testing.T) { - signer := test.RandomSignerRFC6979() + usr := usertest.User() ctx := context.Background() c := newClient(t, nil) @@ -165,7 +165,7 @@ func TestClientStatistic_AccountBalance(t *testing.T) { resp.SetBody(&body) resp.SetMetaHeader(&meta) - err := signServiceMessage(signer, &resp, nil) + err := signServiceMessage(usr, &resp, nil) if err != nil { panic(fmt.Sprintf("sign response: %v", err)) } @@ -177,7 +177,7 @@ func TestClientStatistic_AccountBalance(t *testing.T) { c.prm.statisticCallback = collector.Collect var prm PrmBalanceGet - prm.SetAccount(*randAccount(signer)) + prm.SetAccount(*randAccount(usr)) _, err := c.BalanceGet(ctx, prm) require.NoError(t, err) @@ -185,7 +185,7 @@ func TestClientStatistic_AccountBalance(t *testing.T) { } func TestClientStatistic_ContainerPut(t *testing.T) { - signer := test.RandomSignerRFC6979() + usr := usertest.User() ctx := context.Background() c := newClient(t, nil) @@ -199,7 +199,7 @@ func TestClientStatistic_ContainerPut(t *testing.T) { resp.SetBody(&body) resp.SetMetaHeader(&meta) - err := signServiceMessage(signer, &resp, nil) + err := signServiceMessage(usr.RFC6979, &resp, nil) if err != nil { panic(fmt.Sprintf("sign response: %v", err)) } @@ -207,20 +207,20 @@ func TestClientStatistic_ContainerPut(t *testing.T) { return &resp, nil } - cont := prepareContainer(*randAccount(signer)) + cont := prepareContainer(*randAccount(usr)) collector := newCollector() c.prm.statisticCallback = collector.Collect var prm PrmContainerPut - _, err := c.ContainerPut(ctx, cont, signer, prm) + _, err := c.ContainerPut(ctx, cont, usr.RFC6979, prm) require.NoError(t, err) require.Equal(t, 1, collector.methods[stat.MethodContainerPut].requests) } func TestClientStatistic_ContainerGet(t *testing.T) { - signer := test.RandomSignerRFC6979() + usr := usertest.User() ctx := context.Background() c := newClient(t, nil) @@ -232,7 +232,7 @@ func TestClientStatistic_ContainerGet(t *testing.T) { var resp v2container.GetResponse var meta session.ResponseMetaHeader - cont.SetOwnerID(randOwner(signer)) + cont.SetOwnerID(randOwner(usr)) cont.SetVersion(&ver) nonce, err := uuid.New().MarshalBinary() @@ -251,7 +251,7 @@ func TestClientStatistic_ContainerGet(t *testing.T) { resp.SetBody(&body) resp.SetMetaHeader(&meta) - if err = signServiceMessage(signer, &resp, nil); err != nil { + if err = signServiceMessage(usr, &resp, nil); err != nil { panic(fmt.Sprintf("sign response: %v", err)) } @@ -269,7 +269,7 @@ func TestClientStatistic_ContainerGet(t *testing.T) { } func TestClientStatistic_ContainerList(t *testing.T) { - signer := test.RandomSignerRFC6979() + usr := usertest.User() ctx := context.Background() c := newClient(t, nil) @@ -281,7 +281,7 @@ func TestClientStatistic_ContainerList(t *testing.T) { resp.SetBody(&body) resp.SetMetaHeader(&meta) - if err := signServiceMessage(signer, &resp, nil); err != nil { + if err := signServiceMessage(usr, &resp, nil); err != nil { panic(fmt.Sprintf("sign response: %v", err)) } @@ -292,14 +292,14 @@ func TestClientStatistic_ContainerList(t *testing.T) { c.prm.statisticCallback = collector.Collect var prm PrmContainerList - _, err := c.ContainerList(ctx, *randAccount(signer), prm) + _, err := c.ContainerList(ctx, *randAccount(usr), prm) require.NoError(t, err) require.Equal(t, 1, collector.methods[stat.MethodContainerList].requests) } func TestClientStatistic_ContainerDelete(t *testing.T) { - signer := test.RandomSignerRFC6979() + usr := usertest.User() ctx := context.Background() c := newClient(t, nil) @@ -311,7 +311,7 @@ func TestClientStatistic_ContainerDelete(t *testing.T) { resp.SetBody(&body) resp.SetMetaHeader(&meta) - if err := signServiceMessage(signer, &resp, nil); err != nil { + if err := signServiceMessage(usr, &resp, nil); err != nil { panic(fmt.Sprintf("sign response: %v", err)) } @@ -322,14 +322,14 @@ func TestClientStatistic_ContainerDelete(t *testing.T) { c.prm.statisticCallback = collector.Collect var prm PrmContainerDelete - err := c.ContainerDelete(ctx, cid.ID{}, signer, prm) + err := c.ContainerDelete(ctx, cid.ID{}, usr, prm) require.NoError(t, err) require.Equal(t, 1, collector.methods[stat.MethodContainerDelete].requests) } func TestClientStatistic_ContainerEacl(t *testing.T) { - signer := test.RandomSignerRFC6979() + usr := usertest.User() ctx := context.Background() c := newClient(t, nil) @@ -344,7 +344,7 @@ func TestClientStatistic_ContainerEacl(t *testing.T) { resp.SetBody(&body) resp.SetMetaHeader(&meta) - if err := signServiceMessage(signer, &resp, nil); err != nil { + if err := signServiceMessage(usr, &resp, nil); err != nil { panic(fmt.Sprintf("sign response: %v", err)) } @@ -362,7 +362,7 @@ func TestClientStatistic_ContainerEacl(t *testing.T) { } func TestClientStatistic_ContainerSetEacl(t *testing.T) { - signer := test.RandomSignerRFC6979() + usr := usertest.User() ctx := context.Background() c := newClient(t, nil) @@ -374,7 +374,7 @@ func TestClientStatistic_ContainerSetEacl(t *testing.T) { resp.SetBody(&body) resp.SetMetaHeader(&meta) - if err := signServiceMessage(signer, &resp, nil); err != nil { + if err := signServiceMessage(usr, &resp, nil); err != nil { panic(fmt.Sprintf("sign response: %v", err)) } @@ -386,14 +386,14 @@ func TestClientStatistic_ContainerSetEacl(t *testing.T) { var prm PrmContainerSetEACL table := testEaclTable(cid.ID{}) - err := c.ContainerSetEACL(ctx, table, signer, prm) + err := c.ContainerSetEACL(ctx, table, usr, prm) require.NoError(t, err) require.Equal(t, 1, collector.methods[stat.MethodContainerSetEACL].requests) } func TestClientStatistic_ContainerAnnounceUsedSpace(t *testing.T) { - signer := test.RandomSignerRFC6979() + usr := usertest.User() ctx := context.Background() c := newClient(t, nil) @@ -405,7 +405,7 @@ func TestClientStatistic_ContainerAnnounceUsedSpace(t *testing.T) { resp.SetBody(&body) resp.SetMetaHeader(&meta) - if err := signServiceMessage(signer, &resp, nil); err != nil { + if err := signServiceMessage(usr, &resp, nil); err != nil { panic(fmt.Sprintf("sign response: %v", err)) } @@ -428,7 +428,7 @@ func TestClientStatistic_ContainerAnnounceUsedSpace(t *testing.T) { } func TestClientStatistic_ContainerSyncContainerWithNetwork(t *testing.T) { - signer := test.RandomSignerRFC6979() + usr := usertest.User() ctx := context.Background() c := newClient(t, nil) @@ -451,7 +451,7 @@ func TestClientStatistic_ContainerSyncContainerWithNetwork(t *testing.T) { resp.SetBody(&body) resp.SetMetaHeader(&meta) - if err := signServiceMessage(signer, &resp, nil); err != nil { + if err := signServiceMessage(usr, &resp, nil); err != nil { panic(fmt.Sprintf("sign response: %v", err)) } @@ -461,7 +461,7 @@ func TestClientStatistic_ContainerSyncContainerWithNetwork(t *testing.T) { collector := newCollector() c.prm.statisticCallback = collector.Collect - cont := prepareContainer(*randAccount(signer)) + cont := prepareContainer(*randAccount(usr)) err := SyncContainerWithNetwork(ctx, &cont, c) require.NoError(t, err) @@ -470,7 +470,7 @@ func TestClientStatistic_ContainerSyncContainerWithNetwork(t *testing.T) { } func TestClientStatistic_ContainerEndpointInfo(t *testing.T) { - signer := test.RandomSignerRFC6979() + usr := usertest.User() ctx := context.Background() c := newClient(t, nil) @@ -480,7 +480,7 @@ func TestClientStatistic_ContainerEndpointInfo(t *testing.T) { var ver refs.Version var nodeInfo netmapv2.NodeInfo - nodeInfo.SetPublicKey(neofscrypto.PublicKeyBytes(signer.Public())) + nodeInfo.SetPublicKey(neofscrypto.PublicKeyBytes(usr.Public())) nodeInfo.SetAddresses("https://some-endpont.com") body := netmapv2.LocalNodeInfoResponseBody{} @@ -490,7 +490,7 @@ func TestClientStatistic_ContainerEndpointInfo(t *testing.T) { resp.SetBody(&body) resp.SetMetaHeader(&meta) - if err := signServiceMessage(signer, &resp, nil); err != nil { + if err := signServiceMessage(usr, &resp, nil); err != nil { panic(fmt.Sprintf("sign response: %v", err)) } @@ -507,7 +507,7 @@ func TestClientStatistic_ContainerEndpointInfo(t *testing.T) { } func TestClientStatistic_ContainerNetMapSnapshot(t *testing.T) { - signer := test.RandomSignerRFC6979() + usr := usertest.User() ctx := context.Background() c := newClient(t, nil) @@ -522,7 +522,7 @@ func TestClientStatistic_ContainerNetMapSnapshot(t *testing.T) { resp.SetBody(&body) resp.SetMetaHeader(&meta) - if err := signServiceMessage(signer, &resp, nil); err != nil { + if err := signServiceMessage(usr, &resp, nil); err != nil { panic(fmt.Sprintf("sign response: %v", err)) } @@ -540,7 +540,7 @@ func TestClientStatistic_ContainerNetMapSnapshot(t *testing.T) { } func TestClientStatistic_CreateSession(t *testing.T) { - signer := test.RandomSignerRFC6979() + usr := usertest.User() ctx := context.Background() c := newClient(t, nil) @@ -551,12 +551,12 @@ func TestClientStatistic_CreateSession(t *testing.T) { body := session.CreateResponseBody{} body.SetID(randBytes(10)) - body.SetSessionKey(neofscrypto.PublicKeyBytes(signer.Public())) + body.SetSessionKey(neofscrypto.PublicKeyBytes(usr.Public())) resp.SetBody(&body) resp.SetMetaHeader(&meta) - if err := signServiceMessage(signer, &resp, nil); err != nil { + if err := signServiceMessage(usr, &resp, nil); err != nil { panic(fmt.Sprintf("sign response: %v", err)) } @@ -569,7 +569,7 @@ func TestClientStatistic_CreateSession(t *testing.T) { var prm PrmSessionCreate - _, err := c.SessionCreate(ctx, signer, prm) + _, err := c.SessionCreate(ctx, usr, prm) require.NoError(t, err) require.Equal(t, 1, collector.methods[stat.MethodSessionCreate].requests) @@ -578,7 +578,7 @@ func TestClientStatistic_CreateSession(t *testing.T) { func TestClientStatistic_ObjectPut(t *testing.T) { t.Skip("need changes to api-go, to set `wc client.MessageWriterCloser` in rpcapi.PutRequestWriter") - signer := test.RandomSignerRFC6979() + usr := usertest.User() ctx := context.Background() c := newClient(t, nil) @@ -589,7 +589,7 @@ func TestClientStatistic_ObjectPut(t *testing.T) { } containerID := *randContainerID() - account := randAccount(signer) + account := randAccount(usr) collector := newCollector() c.prm.statisticCallback = collector.Collect @@ -600,10 +600,10 @@ func TestClientStatistic_ObjectPut(t *testing.T) { tokenSession.SetExp(1) tokenSession.BindContainer(containerID) tokenSession.ForVerb(session2.VerbObjectPut) - tokenSession.SetAuthKey(signer.Public()) + tokenSession.SetAuthKey(usr.Public()) tokenSession.SetIssuer(*account) - err := tokenSession.Sign(signer) + err := tokenSession.Sign(usr) require.NoError(t, err) var prm PrmObjectPutInit @@ -613,7 +613,7 @@ func TestClientStatistic_ObjectPut(t *testing.T) { hdr.SetOwnerID(account) hdr.SetContainerID(containerID) - writer, err := c.ObjectPutInit(ctx, hdr, signer, prm) + writer, err := c.ObjectPutInit(ctx, hdr, usr, prm) require.NoError(t, err) _, err = writer.Write(randBytes(10)) @@ -626,7 +626,7 @@ func TestClientStatistic_ObjectPut(t *testing.T) { } func TestClientStatistic_ObjectDelete(t *testing.T) { - signer := test.RandomSignerRFC6979() + usr := usertest.User() ctx := context.Background() c := newClient(t, nil) @@ -648,7 +648,7 @@ func TestClientStatistic_ObjectDelete(t *testing.T) { resp.SetBody(&body) resp.SetMetaHeader(&meta) - if err := signServiceMessage(signer, &resp, nil); err != nil { + if err := signServiceMessage(usr, &resp, nil); err != nil { panic(fmt.Sprintf("sign response: %v", err)) } @@ -663,7 +663,7 @@ func TestClientStatistic_ObjectDelete(t *testing.T) { var prm PrmObjectDelete - _, err := c.ObjectDelete(ctx, containerID, objectID, signer, prm) + _, err := c.ObjectDelete(ctx, containerID, objectID, usr, prm) require.NoError(t, err) require.Equal(t, 1, collector.methods[stat.MethodObjectDelete].requests) @@ -672,7 +672,7 @@ func TestClientStatistic_ObjectDelete(t *testing.T) { func TestClientStatistic_ObjectGet(t *testing.T) { t.Skip("need changes to api-go, to set `r client.MessageReader` in rpcapi.GetResponseReader") - signer := test.RandomSignerRFC6979() + usr := usertest.User() ctx := context.Background() c := newClient(t, nil) @@ -692,7 +692,7 @@ func TestClientStatistic_ObjectGet(t *testing.T) { var prm PrmObjectGet - _, reader, err := c.ObjectGetInit(ctx, containerID, objectID, signer, prm) + _, reader, err := c.ObjectGetInit(ctx, containerID, objectID, usr, prm) require.NoError(t, err) buff := make([]byte, 32) @@ -703,7 +703,7 @@ func TestClientStatistic_ObjectGet(t *testing.T) { } func TestClientStatistic_ObjectHead(t *testing.T) { - signer := test.RandomSignerRFC6979() + usr := usertest.User() ctx := context.Background() c := newClient(t, nil) @@ -718,7 +718,7 @@ func TestClientStatistic_ObjectHead(t *testing.T) { resp.SetBody(&body) resp.SetMetaHeader(&meta) - if err := signServiceMessage(signer, &resp, nil); err != nil { + if err := signServiceMessage(usr, &resp, nil); err != nil { panic(fmt.Sprintf("sign response: %v", err)) } @@ -733,7 +733,7 @@ func TestClientStatistic_ObjectHead(t *testing.T) { var prm PrmObjectHead - _, err := c.ObjectHead(ctx, containerID, objectID, signer, prm) + _, err := c.ObjectHead(ctx, containerID, objectID, usr, prm) require.NoError(t, err) require.Equal(t, 1, collector.methods[stat.MethodObjectHead].requests) @@ -742,7 +742,7 @@ func TestClientStatistic_ObjectHead(t *testing.T) { func TestClientStatistic_ObjectRange(t *testing.T) { t.Skip("need changes to api-go, to set `r client.MessageReader` in rpcapi.ObjectRangeResponseReader") - signer := test.RandomSignerRFC6979() + usr := usertest.User() ctx := context.Background() c := newClient(t, nil) @@ -762,7 +762,7 @@ func TestClientStatistic_ObjectRange(t *testing.T) { var prm PrmObjectRange - reader, err := c.ObjectRangeInit(ctx, containerID, objectID, 0, 1, signer, prm) + reader, err := c.ObjectRangeInit(ctx, containerID, objectID, 0, 1, usr, prm) require.NoError(t, err) buff := make([]byte, 32) @@ -773,7 +773,7 @@ func TestClientStatistic_ObjectRange(t *testing.T) { } func TestClientStatistic_ObjectHash(t *testing.T) { - signer := test.RandomSignerRFC6979() + usr := usertest.User() ctx := context.Background() c := newClient(t, nil) @@ -789,7 +789,7 @@ func TestClientStatistic_ObjectHash(t *testing.T) { resp.SetBody(&body) resp.SetMetaHeader(&meta) - if err := signServiceMessage(signer, &resp, nil); err != nil { + if err := signServiceMessage(usr, &resp, nil); err != nil { panic(fmt.Sprintf("sign response: %v", err)) } @@ -805,7 +805,7 @@ func TestClientStatistic_ObjectHash(t *testing.T) { var prm PrmObjectHash prm.SetRangeList(0, 2) - _, err := c.ObjectHash(ctx, containerID, objectID, signer, prm) + _, err := c.ObjectHash(ctx, containerID, objectID, usr, prm) require.NoError(t, err) require.Equal(t, 1, collector.methods[stat.MethodObjectHash].requests) @@ -814,7 +814,7 @@ func TestClientStatistic_ObjectHash(t *testing.T) { func TestClientStatistic_ObjectSearch(t *testing.T) { t.Skip("need changes to api-go, to set `r client.MessageReader` in rpcapi.SearchResponseReader") - signer := test.RandomSignerRFC6979() + usr := usertest.User() ctx := context.Background() c := newClient(t, nil) @@ -833,7 +833,7 @@ func TestClientStatistic_ObjectSearch(t *testing.T) { var prm PrmObjectSearch - reader, err := c.ObjectSearchInit(ctx, containerID, signer, prm) + reader, err := c.ObjectSearchInit(ctx, containerID, usr, prm) require.NoError(t, err) iterator := func(oid.ID) bool { @@ -847,7 +847,7 @@ func TestClientStatistic_ObjectSearch(t *testing.T) { } func TestClientStatistic_AnnounceIntermediateTrust(t *testing.T) { - signer := test.RandomSignerRFC6979() + usr := usertest.User() ctx := context.Background() c := newClient(t, nil) @@ -859,7 +859,7 @@ func TestClientStatistic_AnnounceIntermediateTrust(t *testing.T) { resp.SetBody(&body) resp.SetMetaHeader(&meta) - if err := signServiceMessage(signer, &resp, nil); err != nil { + if err := signServiceMessage(usr, &resp, nil); err != nil { panic(fmt.Sprintf("sign response: %v", err)) } @@ -879,7 +879,7 @@ func TestClientStatistic_AnnounceIntermediateTrust(t *testing.T) { } func TestClientStatistic_MethodAnnounceLocalTrust(t *testing.T) { - signer := test.RandomSignerRFC6979() + usr := usertest.User() ctx := context.Background() c := newClient(t, nil) @@ -891,7 +891,7 @@ func TestClientStatistic_MethodAnnounceLocalTrust(t *testing.T) { resp.SetBody(&body) resp.SetMetaHeader(&meta) - if err := signServiceMessage(signer, &resp, nil); err != nil { + if err := signServiceMessage(usr, &resp, nil); err != nil { panic(fmt.Sprintf("sign response: %v", err)) } diff --git a/client/netmap_test.go b/client/netmap_test.go index 2c858e1b..a1a218e5 100644 --- a/client/netmap_test.go +++ b/client/netmap_test.go @@ -11,7 +11,7 @@ import ( "github.com/nspcc-dev/neofs-api-go/v2/session" apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status" neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto" - "github.com/nspcc-dev/neofs-sdk-go/crypto/test" + neofscryptotest "github.com/nspcc-dev/neofs-sdk-go/crypto/test" "github.com/nspcc-dev/neofs-sdk-go/netmap" "github.com/stretchr/testify/require" ) @@ -75,7 +75,7 @@ func TestClient_NetMapSnapshot(t *testing.T) { var res netmap.NetMap var srv serverNetMap - signer := test.RandomSignerRFC6979() + signer := neofscryptotest.Signer() srv.signer = signer diff --git a/client/object_put_test.go b/client/object_put_test.go index f34420d9..ad6d9c76 100644 --- a/client/object_put_test.go +++ b/client/object_put_test.go @@ -11,9 +11,9 @@ import ( "github.com/nspcc-dev/neofs-api-go/v2/rpc/client" v2session "github.com/nspcc-dev/neofs-api-go/v2/session" apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status" - "github.com/nspcc-dev/neofs-sdk-go/crypto/test" "github.com/nspcc-dev/neofs-sdk-go/object" "github.com/nspcc-dev/neofs-sdk-go/user" + usertest "github.com/nspcc-dev/neofs-sdk-go/user/test" "github.com/nspcc-dev/neofs-sdk-go/version" "github.com/stretchr/testify/require" ) @@ -53,13 +53,13 @@ func (t *testPutStreamAccessDenied) Close() error { func TestClient_ObjectPutInit(t *testing.T) { t.Run("EOF-on-status-return", func(t *testing.T) { c := newClient(t, nil) - signer := test.RandomSignerRFC6979() + usr := usertest.User() rpcAPIPutObject = func(_ *client.Client, r *v2object.PutResponse, _ ...client.CallOption) (objectWriter, error) { - return &testPutStreamAccessDenied{resp: r, signer: signer, t: t}, nil + return &testPutStreamAccessDenied{resp: r, signer: usr, t: t}, nil } - w, err := c.ObjectPutInit(context.Background(), object.Object{}, signer, PrmObjectPutInit{}) + w, err := c.ObjectPutInit(context.Background(), object.Object{}, usr, PrmObjectPutInit{}) require.NoError(t, err) n, err := w.Write([]byte{1}) diff --git a/client/object_replicate_test.go b/client/object_replicate_test.go index c52853f7..ed9ab663 100644 --- a/client/object_replicate_test.go +++ b/client/object_replicate_test.go @@ -14,7 +14,7 @@ import ( status "github.com/nspcc-dev/neofs-api-go/v2/status/grpc" apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status" neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto" - "github.com/nspcc-dev/neofs-sdk-go/crypto/test" + neofscryptotest "github.com/nspcc-dev/neofs-sdk-go/crypto/test" "github.com/nspcc-dev/neofs-sdk-go/object" oidtest "github.com/nspcc-dev/neofs-sdk-go/object/id/test" objecttest "github.com/nspcc-dev/neofs-sdk-go/object/test" @@ -152,7 +152,7 @@ func serveObjectReplication(tb testing.TB, clientSigner neofscrypto.Signer, clie func TestClient_ReplicateObject(t *testing.T) { ctx := context.Background() - signer := test.RandomSigner() + signer := neofscryptotest.Signer() obj := objecttest.Object() id := oidtest.ID() obj.SetID(id) diff --git a/client/object_search_test.go b/client/object_search_test.go index 7276acf9..bbd0bec7 100644 --- a/client/object_search_test.go +++ b/client/object_search_test.go @@ -10,7 +10,7 @@ import ( "github.com/nspcc-dev/neofs-api-go/v2/refs" cid "github.com/nspcc-dev/neofs-sdk-go/container/id" neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto" - "github.com/nspcc-dev/neofs-sdk-go/crypto/test" + neofscryptotest "github.com/nspcc-dev/neofs-sdk-go/crypto/test" oid "github.com/nspcc-dev/neofs-sdk-go/object/id" oidtest "github.com/nspcc-dev/neofs-sdk-go/object/id/test" "github.com/stretchr/testify/require" @@ -144,7 +144,7 @@ func TestClient_ObjectSearch(t *testing.T) { } func testListReaderResponse() (neofscrypto.Signer, *ObjectListReader) { - return test.RandomSigner(), &ObjectListReader{ + return neofscryptotest.Signer(), &ObjectListReader{ cancelCtxStream: func() {}, client: &Client{}, tail: nil, diff --git a/client/session_test.go b/client/session_test.go index 869c1f8c..c94994dc 100644 --- a/client/session_test.go +++ b/client/session_test.go @@ -8,7 +8,7 @@ import ( "github.com/nspcc-dev/neofs-api-go/v2/rpc/client" "github.com/nspcc-dev/neofs-api-go/v2/session" neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto" - "github.com/nspcc-dev/neofs-sdk-go/crypto/test" + usertest "github.com/nspcc-dev/neofs-sdk-go/user/test" "github.com/stretchr/testify/require" ) @@ -37,7 +37,7 @@ func (m sessionAPIServer) createSession(*client.Client, *session.CreateRequest, func TestClient_SessionCreate(t *testing.T) { ctx := context.Background() - signer := test.RandomSignerRFC6979() + usr := usertest.User() c := newClient(t, nil) @@ -45,22 +45,22 @@ func TestClient_SessionCreate(t *testing.T) { prmSessionCreate.SetExp(1) t.Run("missing session id", func(t *testing.T) { - c.setNeoFSAPIServer(&sessionAPIServer{signer: signer, setBody: func(body *session.CreateResponseBody) { + c.setNeoFSAPIServer(&sessionAPIServer{signer: usr, setBody: func(body *session.CreateResponseBody) { body.SetSessionKey([]byte{1}) }}) - result, err := c.SessionCreate(ctx, signer, prmSessionCreate) + result, err := c.SessionCreate(ctx, usr, prmSessionCreate) require.Nil(t, result) require.ErrorIs(t, err, ErrMissingResponseField) require.Equal(t, "missing session id field in the response", err.Error()) }) t.Run("missing session key", func(t *testing.T) { - c.setNeoFSAPIServer(&sessionAPIServer{signer: signer, setBody: func(body *session.CreateResponseBody) { + c.setNeoFSAPIServer(&sessionAPIServer{signer: usr, setBody: func(body *session.CreateResponseBody) { body.SetID([]byte{1}) }}) - result, err := c.SessionCreate(ctx, signer, prmSessionCreate) + result, err := c.SessionCreate(ctx, usr, prmSessionCreate) require.Nil(t, result) require.ErrorIs(t, err, ErrMissingResponseField) require.Equal(t, "missing session key field in the response", err.Error()) diff --git a/client/sign_test.go b/client/sign_test.go index 01fbb95b..9b7fc421 100644 --- a/client/sign_test.go +++ b/client/sign_test.go @@ -8,7 +8,8 @@ import ( "github.com/nspcc-dev/neofs-api-go/v2/refs" "github.com/nspcc-dev/neofs-api-go/v2/session" neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto" - "github.com/nspcc-dev/neofs-sdk-go/crypto/test" + neofscryptotest "github.com/nspcc-dev/neofs-sdk-go/crypto/test" + usertest "github.com/nspcc-dev/neofs-sdk-go/user/test" "github.com/stretchr/testify/require" ) @@ -99,15 +100,15 @@ func testResponseMeta(t *testing.T, meta *session.ResponseMetaHeader, req servic } func TestEmptyMessage(t *testing.T) { - signer := test.RandomSignerRFC6979() + signer := neofscryptotest.Signer() require.NoError(t, verifyServiceMessage(nil)) require.NoError(t, signServiceMessage(signer, nil, nil)) } func TestBalanceRequest(t *testing.T) { - signer := test.RandomSignerRFC6979() - id := signer.UserID() + signer := neofscryptotest.Signer() + id := usertest.ID() var ownerID refs.OwnerID id.WriteToV2(&ownerID) @@ -131,7 +132,7 @@ func TestBalanceRequest(t *testing.T) { } func TestBalanceResponse(t *testing.T) { - signer := test.RandomSignerRFC6979() + signer := neofscryptotest.Signer() dec := new(accounting.Decimal) dec.SetValue(100) @@ -163,8 +164,8 @@ func TestBalanceResponse(t *testing.T) { } func TestCreateRequest(t *testing.T) { - signer := test.RandomSignerRFC6979() - id := signer.UserID() + signer := neofscryptotest.Signer() + id := usertest.ID() var ownerID refs.OwnerID id.WriteToV2(&ownerID) @@ -199,7 +200,7 @@ func TestCreateRequest(t *testing.T) { } func TestCreateResponse(t *testing.T) { - signer := test.RandomSignerRFC6979() + signer := neofscryptotest.Signer() id := make([]byte, 8) _, err := rand.Read(id) diff --git a/container/container_test.go b/container/container_test.go index 4f686e23..35409aef 100644 --- a/container/container_test.go +++ b/container/container_test.go @@ -15,7 +15,7 @@ import ( cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test" containertest "github.com/nspcc-dev/neofs-sdk-go/container/test" neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto" - "github.com/nspcc-dev/neofs-sdk-go/crypto/test" + neofscryptotest "github.com/nspcc-dev/neofs-sdk-go/crypto/test" netmaptest "github.com/nspcc-dev/neofs-sdk-go/netmap/test" usertest "github.com/nspcc-dev/neofs-sdk-go/user/test" "github.com/nspcc-dev/neofs-sdk-go/version" @@ -344,8 +344,9 @@ func TestCalculateSignature(t *testing.T) { var sig neofscrypto.Signature - require.Error(t, val.CalculateSignature(&sig, test.RandomSigner())) - require.NoError(t, val.CalculateSignature(&sig, test.RandomSignerRFC6979())) + signer := neofscryptotest.Signer() + require.Error(t, val.CalculateSignature(&sig, signer)) + require.NoError(t, val.CalculateSignature(&sig, signer.RFC6979)) var msg refs.Signature sig.WriteToV2(&msg) diff --git a/crypto/signature_test.go b/crypto/signature_test.go index d425a8dc..17580863 100644 --- a/crypto/signature_test.go +++ b/crypto/signature_test.go @@ -5,7 +5,7 @@ import ( "github.com/nspcc-dev/neofs-api-go/v2/refs" neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto" - "github.com/nspcc-dev/neofs-sdk-go/crypto/test" + neofscryptotest "github.com/nspcc-dev/neofs-sdk-go/crypto/test" "github.com/stretchr/testify/require" ) @@ -13,7 +13,7 @@ const anyUnsupportedScheme = neofscrypto.ECDSA_WALLETCONNECT + 1 func TestSignatureLifecycle(t *testing.T) { data := []byte("Hello, world!") - signer := test.RandomSigner() + signer := neofscryptotest.Signer() scheme := signer.Scheme() pubKey := signer.Public() bPubKey := neofscrypto.PublicKeyBytes(pubKey) @@ -68,7 +68,7 @@ func TestSignatureLifecycle(t *testing.T) { } func TestNewSignature(t *testing.T) { - signer := test.RandomSigner() + signer := neofscryptotest.Signer() scheme := signer.Scheme() pubKey := signer.Public() val := []byte("Hello, world!") // may be any for this test diff --git a/crypto/test/tests.go b/crypto/test/tests.go index 9d1705d3..71f4ce80 100644 --- a/crypto/test/tests.go +++ b/crypto/test/tests.go @@ -1,60 +1,31 @@ /* -Package tests provides special help functions for testing NeoFS API and its environment. - -All functions accepting `t *testing.T` that emphasize there are only for tests purposes. +Package neofscryptotest provides helper functions to test code importing [neofscrypto]. */ -package test +package neofscryptotest import ( + "crypto/ecdsa" + "errors" "fmt" + "math/rand" "testing" "github.com/nspcc-dev/neo-go/pkg/crypto/keys" neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto" neofsecdsa "github.com/nspcc-dev/neofs-sdk-go/crypto/ecdsa" - "github.com/nspcc-dev/neofs-sdk-go/user" "github.com/stretchr/testify/require" ) -// RandomSigner return neofscrypto.Signer ONLY for TESTs purposes. -// It may be used like helper to get new neofscrypto.Signer if you need it in yours tests. -func RandomSigner() neofscrypto.Signer { - p, err := keys.NewPrivateKey() - if err != nil { - panic(fmt.Errorf("failed to randomize private key: %w", err)) - } - - return neofsecdsa.Signer(p.PrivateKey) -} - -// RandomSignerRFC6979 return [user.Signer] ONLY for TESTs purposes. -// It may be used like helper to get new [user.Signer] if you need it in yours tests. -func RandomSignerRFC6979() user.Signer { - p, err := keys.NewPrivateKey() - if err != nil { - panic(fmt.Errorf("failed to randomize private key: %w", err)) - } - - return user.NewAutoIDSignerRFC6979(p.PrivateKey) -} - -// SignedComponent describes component which can signed and the signature may be verified. +// SignedComponent contains data to be signed and its signature to be verified. type SignedComponent interface { SignedData() []byte Sign(neofscrypto.Signer) error VerifySignature() bool } -// SignedComponentUserSigner is the same as [SignedComponent] but uses [user.Signer] instead of [neofscrypto.Signer]. -// It helps to cover all cases. -type SignedComponentUserSigner interface { - SignedData() []byte - Sign(user.Signer) error - VerifySignature() bool -} - -// SignedDataComponent tests [SignedComponent] for valid data generation by SignedData function. -func SignedDataComponent(tb testing.TB, signer neofscrypto.Signer, cmp SignedComponent) { +// TestSignedData tests signing and verification of +// [SignedComponent.SignedData]. +func TestSignedData(tb testing.TB, signer neofscrypto.Signer, cmp SignedComponent) { data := cmp.SignedData() sig, err := signer.Sign(data) @@ -68,17 +39,52 @@ func SignedDataComponent(tb testing.TB, signer neofscrypto.Signer, cmp SignedCom require.True(tb, cmp.VerifySignature()) } -// SignedDataComponentUser tests [SignedComponentUserSigner] for valid data generation by SignedData function. -func SignedDataComponentUser(tb testing.TB, signer user.Signer, cmp SignedComponentUserSigner) { - data := cmp.SignedData() +type failedSigner struct { + neofscrypto.Signer +} - sig, err := signer.Sign(data) - require.NoError(tb, err) +func (x failedSigner) Sign([]byte) ([]byte, error) { return nil, errors.New("[test] failed to sign") } - static := neofscrypto.NewStaticSigner(signer.Scheme(), sig, signer.Public()) +// FailSigner wraps s to always return error from Sign method. +func FailSigner(s neofscrypto.Signer) neofscrypto.Signer { + return failedSigner{s} +} - err = cmp.Sign(user.NewSigner(static, signer.UserID())) - require.NoError(tb, err) +// Signature returns random neofscrypto.Signature. +func Signature() neofscrypto.Signature { + sig := make([]byte, rand.Int()%128) + rand.Read(sig) + return neofscrypto.NewSignature(neofscrypto.Scheme(rand.Uint32()%3), Signer().Public(), sig) +} - require.True(tb, cmp.VerifySignature()) +// ECDSAPrivateKey returns random ECDSA private key. +func ECDSAPrivateKey() ecdsa.PrivateKey { + p, err := keys.NewPrivateKey() + if err != nil { + panic(fmt.Errorf("unexpected private key randomizaiton failure: %w", err)) + } + return p.PrivateKey +} + +// VariableSigner unites various elements of NeoFS cryptography frequently used +// in testing. +type VariableSigner struct { + ECDSAPrivateKey ecdsa.PrivateKey + // Components calculated for ECDSAPrivateKey. + PublicKeyBytes []byte + neofscrypto.Signer + RFC6979 neofscrypto.Signer + WalletConnect neofscrypto.Signer +} + +// Signer returns random VariableSigner. +func Signer() VariableSigner { + pk := ECDSAPrivateKey() + return VariableSigner{ + ECDSAPrivateKey: pk, + Signer: neofsecdsa.Signer(pk), + RFC6979: neofsecdsa.SignerRFC6979(pk), + WalletConnect: neofsecdsa.SignerWalletConnect(pk), + PublicKeyBytes: neofscrypto.PublicKeyBytes((*neofsecdsa.PublicKey)(&pk.PublicKey)), + } } diff --git a/crypto/test/tests_test.go b/crypto/test/tests_test.go new file mode 100644 index 00000000..04bcacdb --- /dev/null +++ b/crypto/test/tests_test.go @@ -0,0 +1,48 @@ +package neofscryptotest_test + +import ( + "testing" + + "github.com/nspcc-dev/neofs-api-go/v2/refs" + neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto" + neofsecdsa "github.com/nspcc-dev/neofs-sdk-go/crypto/ecdsa" + neofscryptotest "github.com/nspcc-dev/neofs-sdk-go/crypto/test" + "github.com/stretchr/testify/require" +) + +func TestSignature(t *testing.T) { + s := neofscryptotest.Signature() + require.NotEqual(t, s, neofscryptotest.Signature()) + + var m refs.Signature + s.WriteToV2(&m) + var s2 neofscrypto.Signature + require.NoError(t, s2.ReadFromV2(m)) + require.Equal(t, s, s2) +} + +func TestECDSAPrivateKey(t *testing.T) { + require.NotEqual(t, neofscryptotest.ECDSAPrivateKey(), neofscryptotest.ECDSAPrivateKey()) +} + +func TestFailSigner(t *testing.T) { + s := neofscryptotest.FailSigner(neofscryptotest.Signer()) + _, err := s.Sign(nil) + require.EqualError(t, err, "[test] failed to sign") +} + +func TestSigner(t *testing.T) { + s := neofscryptotest.Signer() + require.NotEqual(t, s, neofscryptotest.Signer()) + + require.EqualValues(t, s.Signer, s.ECDSAPrivateKey) + require.Equal(t, neofscrypto.ECDSA_SHA512, s.Scheme()) + require.EqualValues(t, s.RFC6979, s.ECDSAPrivateKey) + require.Equal(t, neofscrypto.ECDSA_DETERMINISTIC_SHA256, s.RFC6979.Scheme()) + require.EqualValues(t, s.WalletConnect, s.ECDSAPrivateKey) + require.Equal(t, neofscrypto.ECDSA_WALLETCONNECT, s.WalletConnect.Scheme()) + + var pub neofsecdsa.PublicKey + require.NoError(t, pub.Decode(s.PublicKeyBytes)) + require.EqualValues(t, s.ECDSAPrivateKey.PublicKey, pub) +} diff --git a/object/fmt_test.go b/object/fmt_test.go index 620fc437..5f939877 100644 --- a/object/fmt_test.go +++ b/object/fmt_test.go @@ -5,7 +5,7 @@ import ( "testing" cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test" - "github.com/nspcc-dev/neofs-sdk-go/crypto/test" + neofscryptotest "github.com/nspcc-dev/neofs-sdk-go/crypto/test" usertest "github.com/nspcc-dev/neofs-sdk-go/user/test" "github.com/stretchr/testify/require" ) @@ -19,7 +19,7 @@ func TestVerificationFields(t *testing.T) { obj.SetPayload(payload) obj.SetPayloadSize(uint64(len(payload))) - require.NoError(t, obj.SetVerificationFields(test.RandomSigner())) + require.NoError(t, obj.SetVerificationFields(neofscryptotest.Signer())) require.NoError(t, obj.CheckVerificationFields()) @@ -65,7 +65,7 @@ func TestVerificationFields(t *testing.T) { } func TestObject_SignedData(t *testing.T) { - signer := test.RandomSigner() + signer := neofscryptotest.Signer() uid := usertest.ID() rf := RequiredFields{ @@ -78,5 +78,5 @@ func TestObject_SignedData(t *testing.T) { require.NoError(t, val.SetVerificationFields(signer)) - test.SignedDataComponent(t, signer, &val) + neofscryptotest.TestSignedData(t, signer, &val) } diff --git a/object/object_internal_test.go b/object/object_internal_test.go index 087c3fc1..e57406cf 100644 --- a/object/object_internal_test.go +++ b/object/object_internal_test.go @@ -9,7 +9,6 @@ import ( "github.com/nspcc-dev/neofs-api-go/v2/refs" cid "github.com/nspcc-dev/neofs-sdk-go/container/id" cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test" - "github.com/nspcc-dev/neofs-sdk-go/crypto/test" oidtest "github.com/nspcc-dev/neofs-sdk-go/object/id/test" "github.com/nspcc-dev/neofs-sdk-go/session" "github.com/nspcc-dev/neofs-sdk-go/user" @@ -39,7 +38,7 @@ func parenObject(cnr cid.ID, owner user.ID) *Object { } func TestObject_CopyTo(t *testing.T) { - signer := test.RandomSignerRFC6979() + usr := usertest.User() var obj Object cnr := cidtest.ID() @@ -69,7 +68,7 @@ func TestObject_CopyTo(t *testing.T) { obj.SetVersion(&v) require.NoError(t, obj.CalculateAndSetID()) - require.NoError(t, obj.Sign(signer)) + require.NoError(t, obj.Sign(usr)) t.Run("copy", func(t *testing.T) { var dst Object @@ -136,7 +135,7 @@ func TestObject_CopyTo(t *testing.T) { var dst Object require.NoError(t, dst.CalculateAndSetID()) - require.NoError(t, dst.Sign(signer)) + require.NoError(t, dst.Sign(usr)) require.NotNil(t, dst.Signature()) local.CopyTo(&dst) @@ -146,7 +145,7 @@ func TestObject_CopyTo(t *testing.T) { checkObjectEquals(t, local, dst) require.NoError(t, dst.CalculateAndSetID()) - require.NoError(t, dst.Sign(signer)) + require.NoError(t, dst.Sign(usr)) require.NotNil(t, dst.Signature()) require.Nil(t, local.Signature()) }) @@ -269,7 +268,7 @@ func TestObject_CopyTo(t *testing.T) { t.Run("header, set session owner", func(t *testing.T) { var local Object sess := sessionToken(cnr) - sess.SetIssuer(signer.UserID()) + sess.SetIssuer(usr.UserID()) local.SetSessionToken(sess) @@ -289,7 +288,7 @@ func TestObject_CopyTo(t *testing.T) { local.SetSessionToken(sessionToken(cnr)) sess := sessionToken(cnr) - sess.SetIssuer(signer.UserID()) + sess.SetIssuer(usr.UserID()) var dst Object dst.SetSessionToken(sess) diff --git a/object/slicer/slicer_test.go b/object/slicer/slicer_test.go index d25b2cdb..a29890a1 100644 --- a/object/slicer/slicer_test.go +++ b/object/slicer/slicer_test.go @@ -19,7 +19,7 @@ import ( "github.com/nspcc-dev/neofs-sdk-go/client" cid "github.com/nspcc-dev/neofs-sdk-go/container/id" cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test" - "github.com/nspcc-dev/neofs-sdk-go/crypto/test" + neofscryptotest "github.com/nspcc-dev/neofs-sdk-go/crypto/test" "github.com/nspcc-dev/neofs-sdk-go/netmap" "github.com/nspcc-dev/neofs-sdk-go/object" oid "github.com/nspcc-dev/neofs-sdk-go/object/id" @@ -83,7 +83,7 @@ func benchmarkSliceDataIntoObjects(b *testing.B, size, sizeLimit uint64) { ctx := context.Background() in, opts := randomInput(b, size, sizeLimit) - obj := sessiontest.ObjectSigned(test.RandomSignerRFC6979()) + obj := sessiontest.ObjectSigned(usertest.User()) s, err := slicer.New( ctx, discardObject{opts: opts}, @@ -236,7 +236,7 @@ func randomInput(tb testing.TB, size, sizeLimit uint64) (input, slicer.Options) var opts slicer.Options if rand.Int()%2 == 0 { - tok := sessiontest.ObjectSigned(test.RandomSignerRFC6979()) + tok := sessiontest.ObjectSigned(usertest.User()) in.sessionToken = &tok opts.SetSession(*in.sessionToken) } else { @@ -842,8 +842,8 @@ func TestSlicedObjectsHaveSplitID(t *testing.T) { require.NoError(t, err) containerID.Encode(id) - signer := test.RandomSignerRFC6979() - ownerID := signer.UserID() + usr := usertest.User() + usrID := usr.UserID() opts := slicer.Options{} opts.SetObjectPayloadLimit(maxObjectSize) @@ -859,7 +859,7 @@ func TestSlicedObjectsHaveSplitID(t *testing.T) { writer := &memoryWriter{ opts: opts, } - sl, err := slicer.New(context.Background(), writer, signer, containerID, ownerID, nil) + sl, err := slicer.New(context.Background(), writer, usr, containerID, usrID, nil) require.NoError(t, err) payload := make([]byte, maxObjectSize*overheadAmount) @@ -892,7 +892,7 @@ func TestSlicedObjectsHaveSplitID(t *testing.T) { writer := &memoryWriter{ opts: opts, } - sl, err := slicer.New(context.Background(), writer, signer, containerID, ownerID, nil) + sl, err := slicer.New(context.Background(), writer, usr, containerID, usrID, nil) require.NoError(t, err) payloadWriter, err := sl.InitPut(ctx, nil) @@ -931,7 +931,7 @@ func TestSlicedObjectsHaveSplitID(t *testing.T) { writer := &memoryWriter{ opts: opts, } - sl, err := slicer.New(context.Background(), writer, signer, containerID, ownerID, nil) + sl, err := slicer.New(context.Background(), writer, usr, containerID, usrID, nil) require.NoError(t, err) payload := make([]byte, maxObjectSize-1) @@ -1187,7 +1187,7 @@ func BenchmarkKnownPayloadSize(b *testing.B) { b.Run("read", func(b *testing.B) { obj := objecttest.Object() hdr := *obj.CutPayload() - signer := user.NewSigner(test.RandomSigner(), usertest.ID()) + signer := user.NewSigner(neofscryptotest.Signer(), usertest.ID()) payload := make([]byte, tc.size) //nolint:staticcheck rand.Read(payload) @@ -1208,7 +1208,7 @@ func BenchmarkKnownPayloadSize(b *testing.B) { b.Run("write", func(b *testing.B) { obj := objecttest.Object() hdr := *obj.CutPayload() - signer := user.NewSigner(test.RandomSigner(), usertest.ID()) + signer := user.NewSigner(neofscryptotest.Signer(), usertest.ID()) payload := make([]byte, tc.size) //nolint:staticcheck rand.Read(payload) diff --git a/pool/cache_test.go b/pool/cache_test.go index 0ba3c1cc..9ef6ffa6 100644 --- a/pool/cache_test.go +++ b/pool/cache_test.go @@ -3,9 +3,9 @@ package pool import ( "testing" - "github.com/nspcc-dev/neofs-sdk-go/crypto/test" "github.com/nspcc-dev/neofs-sdk-go/session" sessiontest "github.com/nspcc-dev/neofs-sdk-go/session/test" + usertest "github.com/nspcc-dev/neofs-sdk-go/user/test" "github.com/stretchr/testify/require" ) @@ -25,7 +25,7 @@ func TestSessionCache_GetUnmodifiedToken(t *testing.T) { require.True(t, ok) check(t, value, "before sign") - err = value.Sign(test.RandomSignerRFC6979()) + err = value.Sign(usertest.User()) require.NoError(t, err) value, ok = cache.Get(key) diff --git a/pool/pool_test.go b/pool/pool_test.go index 95840703..a2b9b91a 100644 --- a/pool/pool_test.go +++ b/pool/pool_test.go @@ -10,10 +10,10 @@ import ( "github.com/nspcc-dev/neofs-sdk-go/client" apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status" cid "github.com/nspcc-dev/neofs-sdk-go/container/id" - neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto" - "github.com/nspcc-dev/neofs-sdk-go/crypto/test" + neofscryptotest "github.com/nspcc-dev/neofs-sdk-go/crypto/test" oid "github.com/nspcc-dev/neofs-sdk-go/object/id" "github.com/nspcc-dev/neofs-sdk-go/session" + usertest "github.com/nspcc-dev/neofs-sdk-go/user/test" "github.com/stretchr/testify/require" "go.uber.org/zap" ) @@ -23,7 +23,7 @@ func TestBuildPoolClientFailed(t *testing.T) { return nil, errors.New("oops") } mockClientBuilder2 := func(addr string) (internalClient, error) { - mockCli := newMockClient(addr, test.RandomSignerRFC6979()) + mockCli := newMockClient(addr, neofscryptotest.Signer()) mockCli.errOnDial() return mockCli, nil } @@ -34,7 +34,7 @@ func TestBuildPoolClientFailed(t *testing.T) { } { t.Run(name, func(t *testing.T) { opts := InitParameters{ - signer: test.RandomSignerRFC6979(), + signer: usertest.User().RFC6979, nodeParams: []NodeParam{{1, "peer0", 1}}, } opts.setClientBuilder(b) @@ -53,24 +53,21 @@ func TestBuildPoolOneNodeFailed(t *testing.T) { {2, "peer1", 1}, } - var clientKeys []neofscrypto.Signer mockClientBuilder := func(addr string) (internalClient, error) { - key := test.RandomSignerRFC6979() - clientKeys = append(clientKeys, key) - + signer := neofscryptotest.Signer() if addr == nodes[0].address { - mockCli := newMockClient(addr, key) + mockCli := newMockClient(addr, signer) mockCli.errOnEndpointInfo() return mockCli, nil } - return newMockClient(addr, key), nil + return newMockClient(addr, signer), nil } log, err := zap.NewProduction() require.NoError(t, err) opts := InitParameters{ - signer: test.RandomSignerRFC6979(), + signer: usertest.User().RFC6979, clientRebalanceInterval: 1000 * time.Millisecond, logger: log, nodeParams: nodes, @@ -97,7 +94,7 @@ func TestBuildPoolOneNodeFailed(t *testing.T) { func TestBuildPoolZeroNodes(t *testing.T) { opts := InitParameters{ - signer: test.RandomSignerRFC6979(), + signer: usertest.User(), } _, err := NewPool(opts) require.Error(t, err) @@ -110,20 +107,20 @@ func TestBuildPoolNoSigner(t *testing.T) { func TestBuildPoolWrongSigner(t *testing.T) { opts := InitParameters{ - signer: test.RandomSignerRFC6979(), + signer: usertest.User(), } _, err := NewPool(opts) require.Error(t, err) } func TestOneNode(t *testing.T) { - key1 := test.RandomSignerRFC6979() + signer1 := neofscryptotest.Signer() mockClientBuilder := func(addr string) (internalClient, error) { - return newMockClient(addr, key1), nil + return newMockClient(addr, signer1), nil } opts := InitParameters{ - signer: test.RandomSignerRFC6979(), + signer: usertest.User().RFC6979, nodeParams: []NodeParam{{1, "peer0", 1}}, } opts.setClientBuilder(mockClientBuilder) @@ -140,15 +137,12 @@ func TestOneNode(t *testing.T) { } func TestTwoNodes(t *testing.T) { - var clientKeys []neofscrypto.Signer mockClientBuilder := func(addr string) (internalClient, error) { - key := test.RandomSignerRFC6979() - clientKeys = append(clientKeys, key) - return newMockClient(addr, key), nil + return newMockClient(addr, neofscryptotest.Signer()), nil } opts := InitParameters{ - signer: test.RandomSignerRFC6979(), + signer: usertest.User().RFC6979, nodeParams: []NodeParam{ {1, "peer0", 1}, {1, "peer1", 1}, @@ -182,23 +176,20 @@ func TestOneOfTwoFailed(t *testing.T) { {9, "peer1", 1}, } - var clientKeys []neofscrypto.Signer mockClientBuilder := func(addr string) (internalClient, error) { - key := test.RandomSignerRFC6979() - clientKeys = append(clientKeys, key) - + signer := neofscryptotest.Signer() if addr == nodes[0].address { - return newMockClient(addr, key), nil + return newMockClient(addr, signer), nil } - mockCli := newMockClient(addr, key) + mockCli := newMockClient(addr, signer) mockCli.errOnEndpointInfo() mockCli.errOnNetworkInfo() return mockCli, nil } opts := InitParameters{ - signer: test.RandomSignerRFC6979(), + signer: usertest.User().RFC6979, nodeParams: nodes, clientRebalanceInterval: 200 * time.Millisecond, } @@ -222,17 +213,14 @@ func TestOneOfTwoFailed(t *testing.T) { } func TestTwoFailed(t *testing.T) { - var clientKeys []neofscrypto.Signer mockClientBuilder := func(addr string) (internalClient, error) { - key := test.RandomSignerRFC6979() - clientKeys = append(clientKeys, key) - mockCli := newMockClient(addr, key) + mockCli := newMockClient(addr, neofscryptotest.Signer()) mockCli.errOnEndpointInfo() return mockCli, nil } opts := InitParameters{ - signer: test.RandomSignerRFC6979(), + signer: usertest.User().RFC6979, nodeParams: []NodeParam{ {1, "peer0", 1}, {1, "peer1", 1}, @@ -256,16 +244,16 @@ func TestTwoFailed(t *testing.T) { } func TestSessionCache(t *testing.T) { - signer := test.RandomSignerRFC6979() + usr := usertest.User() var mockCli *mockClient mockClientBuilder := func(addr string) (internalClient, error) { - mockCli = newMockClient(addr, signer) + mockCli = newMockClient(addr, usr) return mockCli, nil } opts := InitParameters{ - signer: signer, + signer: usr.RFC6979, nodeParams: []NodeParam{ {1, "peer0", 1}, }, @@ -291,23 +279,23 @@ func TestSessionCache(t *testing.T) { t.Run("no session token after pool creation", func(t *testing.T) { st, ok := pool.cache.Get(cacheKey) require.False(t, ok) - require.False(t, st.AssertAuthKey(signer.Public())) + require.False(t, st.AssertAuthKey(usr.Public())) }) t.Run("session token was created after request", func(t *testing.T) { - _, _, err = pool.ObjectGetInit(ctx, containerID, oid.ID{}, signer, client.PrmObjectGet{}) + _, _, err = pool.ObjectGetInit(ctx, containerID, oid.ID{}, usr, client.PrmObjectGet{}) require.NoError(t, err) st, ok := pool.cache.Get(cacheKey) require.True(t, ok) - require.True(t, st.AssertAuthKey(signer.Public())) + require.True(t, st.AssertAuthKey(usr.Public())) }) t.Run("session is not removed", func(t *testing.T) { // error on the next request to the node mockCli.statusOnGetObject(errors.New("some error")) - _, _, err = pool.ObjectGetInit(ctx, cid.ID{}, oid.ID{}, signer, client.PrmObjectGet{}) + _, _, err = pool.ObjectGetInit(ctx, cid.ID{}, oid.ID{}, usr, client.PrmObjectGet{}) require.Error(t, err) _, ok := pool.cache.Get(cacheKey) @@ -319,7 +307,7 @@ func TestSessionCache(t *testing.T) { mockCli.statusOnGetObject(apistatus.SessionTokenNotFound{}) // make request, - _, _, err = pool.ObjectGetInit(ctx, cid.ID{}, oid.ID{}, signer, client.PrmObjectGet{}) + _, _, err = pool.ObjectGetInit(ctx, cid.ID{}, oid.ID{}, usr, client.PrmObjectGet{}) require.Error(t, err) // cache must not contain session token @@ -332,7 +320,7 @@ func TestSessionCache(t *testing.T) { t.Run("session created again", func(t *testing.T) { mockCli.statusOnGetObject(nil) - _, _, err = pool.ObjectGetInit(ctx, cid.ID{}, oid.ID{}, signer, client.PrmObjectGet{}) + _, _, err = pool.ObjectGetInit(ctx, cid.ID{}, oid.ID{}, usr, client.PrmObjectGet{}) require.NoError(t, err) _, ok := pool.cache.Get(cacheKey) @@ -346,22 +334,20 @@ func TestPriority(t *testing.T) { {2, "peer1", 100}, } - var clientKeys []neofscrypto.Signer mockClientBuilder := func(addr string) (internalClient, error) { - key := test.RandomSignerRFC6979() - clientKeys = append(clientKeys, key) + signer := neofscryptotest.Signer() if addr == nodes[0].address { - mockCli := newMockClient(addr, key) + mockCli := newMockClient(addr, signer) mockCli.errOnEndpointInfo() return mockCli, nil } - return newMockClient(addr, key), nil + return newMockClient(addr, signer), nil } opts := InitParameters{ - signer: test.RandomSignerRFC6979(), + signer: usertest.User().RFC6979, nodeParams: nodes, clientRebalanceInterval: 1500 * time.Millisecond, } @@ -394,14 +380,12 @@ func TestPriority(t *testing.T) { } func TestSessionCacheWithKey(t *testing.T) { - key := test.RandomSignerRFC6979() - mockClientBuilder := func(addr string) (internalClient, error) { - return newMockClient(addr, key), nil + return newMockClient(addr, neofscryptotest.Signer()), nil } opts := InitParameters{ - signer: test.RandomSignerRFC6979(), + signer: usertest.User().RFC6979, nodeParams: []NodeParam{ {1, "peer0", 1}, }, @@ -421,7 +405,7 @@ func TestSessionCacheWithKey(t *testing.T) { require.NoError(t, err) var prm client.PrmObjectDelete - anonSigner := test.RandomSignerRFC6979() + anonSigner := usertest.User() _, err = pool.ObjectDelete(ctx, cid.ID{}, oid.ID{}, anonSigner, prm) require.NoError(t, err) @@ -432,12 +416,11 @@ func TestSessionCacheWithKey(t *testing.T) { func TestSessionTokenOwner(t *testing.T) { mockClientBuilder := func(addr string) (internalClient, error) { - key := test.RandomSignerRFC6979() - return newMockClient(addr, key), nil + return newMockClient(addr, neofscryptotest.Signer()), nil } opts := InitParameters{ - signer: test.RandomSignerRFC6979(), + signer: usertest.User().RFC6979, nodeParams: []NodeParam{ {1, "peer0", 1}, }, @@ -456,7 +439,7 @@ func TestSessionTokenOwner(t *testing.T) { cp, err := p.connection() require.NoError(t, err) - anonSigner := test.RandomSignerRFC6979() + anonSigner := usertest.User() var containerID cid.ID @@ -568,25 +551,22 @@ func TestSwitchAfterErrorThreshold(t *testing.T) { errorThreshold := 5 - var clientKeys []neofscrypto.Signer mockClientBuilder := func(addr string) (internalClient, error) { - key := test.RandomSignerRFC6979() - clientKeys = append(clientKeys, key) - + signer := neofscryptotest.Signer() if addr == nodes[0].address { - mockCli := newMockClient(addr, key) + mockCli := newMockClient(addr, signer) mockCli.setThreshold(uint32(errorThreshold)) mockCli.statusOnGetObject(apistatus.ServerInternal{}) return mockCli, nil } - return newMockClient(addr, key), nil + return newMockClient(addr, signer), nil } - signer := test.RandomSignerRFC6979() + usr := usertest.User() opts := InitParameters{ - signer: signer, + signer: usr.RFC6979, nodeParams: nodes, clientRebalanceInterval: 30 * time.Second, } @@ -607,7 +587,7 @@ func TestSwitchAfterErrorThreshold(t *testing.T) { require.Equal(t, nodes[0].address, conn.address()) sdkClient, err := conn.getClient() require.NoError(t, err) - _, _, err = sdkClient.ObjectGetInit(ctx, cid.ID{}, oid.ID{}, signer, client.PrmObjectGet{}) + _, _, err = sdkClient.ObjectGetInit(ctx, cid.ID{}, oid.ID{}, usr, client.PrmObjectGet{}) require.Error(t, err) } @@ -618,6 +598,6 @@ func TestSwitchAfterErrorThreshold(t *testing.T) { sdkClient, err := conn.getClient() require.NoError(t, err) - _, _, err = sdkClient.ObjectGetInit(ctx, cid.ID{}, oid.ID{}, signer, client.PrmObjectGet{}) + _, _, err = sdkClient.ObjectGetInit(ctx, cid.ID{}, oid.ID{}, usr, client.PrmObjectGet{}) require.NoError(t, err) } diff --git a/pool/sampler_test.go b/pool/sampler_test.go index 1b234cbe..6cd5a5ed 100644 --- a/pool/sampler_test.go +++ b/pool/sampler_test.go @@ -5,7 +5,8 @@ import ( "math/rand" "testing" - "github.com/nspcc-dev/neofs-sdk-go/crypto/test" + neofscryptotest "github.com/nspcc-dev/neofs-sdk-go/crypto/test" + usertest "github.com/nspcc-dev/neofs-sdk-go/user/test" "github.com/stretchr/testify/require" ) @@ -51,10 +52,10 @@ func TestHealthyReweight(t *testing.T) { cache, err := newCache(defaultSessionCacheSize) require.NoError(t, err) - client1 := newMockClient(names[0], test.RandomSigner()) + client1 := newMockClient(names[0], neofscryptotest.Signer()) client1.errOnDial() - client2 := newMockClient(names[1], test.RandomSigner()) + client2 := newMockClient(names[1], neofscryptotest.Signer()) inner := &innerPool{ sampler: newSampler(weights, rand.NewSource(0)), @@ -63,7 +64,7 @@ func TestHealthyReweight(t *testing.T) { p := &Pool{ innerPools: []*innerPool{inner}, cache: cache, - signer: test.RandomSignerRFC6979(), + signer: usertest.User(), rebalanceParams: rebalanceParameters{nodesParams: []*nodesParam{{weights: weights}}}, } @@ -82,7 +83,7 @@ func TestHealthyReweight(t *testing.T) { // enabled first node again inner.lock.Lock() - inner.clients[0] = newMockClient(names[0], test.RandomSigner()) + inner.clients[0] = newMockClient(names[0], neofscryptotest.Signer()) inner.lock.Unlock() p.updateInnerNodesHealth(context.TODO(), 0, buffer) @@ -105,8 +106,8 @@ func TestHealthyNoReweight(t *testing.T) { inner := &innerPool{ sampler: sampl, clients: []internalClient{ - newMockClient(names[0], test.RandomSigner()), - newMockClient(names[1], test.RandomSigner()), + newMockClient(names[0], neofscryptotest.Signer()), + newMockClient(names[1], neofscryptotest.Signer()), }, } p := &Pool{ diff --git a/reputation/test/generate.go b/reputation/test/generate.go index 0ce31f81..858762e9 100644 --- a/reputation/test/generate.go +++ b/reputation/test/generate.go @@ -4,7 +4,7 @@ import ( "fmt" "github.com/nspcc-dev/neo-go/pkg/crypto/keys" - "github.com/nspcc-dev/neofs-sdk-go/crypto/test" + neofscryptotest "github.com/nspcc-dev/neofs-sdk-go/crypto/test" "github.com/nspcc-dev/neofs-sdk-go/reputation" ) @@ -44,7 +44,7 @@ func GlobalTrust() (v reputation.GlobalTrust) { func SignedGlobalTrust() reputation.GlobalTrust { gt := GlobalTrust() - if err := gt.Sign(test.RandomSignerRFC6979()); err != nil { + if err := gt.Sign(neofscryptotest.Signer()); err != nil { panic(fmt.Sprintf("unexpected error from GlobalTrust.Sign: %v", err)) } diff --git a/reputation/trust_test.go b/reputation/trust_test.go index e9d157f0..ea9c3708 100644 --- a/reputation/trust_test.go +++ b/reputation/trust_test.go @@ -5,7 +5,7 @@ import ( "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" + neofscryptotest "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" @@ -177,7 +177,7 @@ func TestGlobalTrust_Sign(t *testing.T) { require.False(t, val.VerifySignature()) - require.NoError(t, val.Sign(test.RandomSigner())) + require.NoError(t, val.Sign(neofscryptotest.Signer())) var valV2 v2reputation.GlobalTrust val.WriteToV2(&valV2) @@ -194,9 +194,8 @@ func TestGlobalTrust_SignedData(t *testing.T) { val := reputationtest.GlobalTrust() require.False(t, val.VerifySignature()) - signer := test.RandomSigner() - test.SignedDataComponent(t, signer, &val) + neofscryptotest.TestSignedData(t, neofscryptotest.Signer(), &val) } func TestGlobalTrustEncoding(t *testing.T) { diff --git a/session/common_test.go b/session/common_test.go index 4b5f637b..def0c32f 100644 --- a/session/common_test.go +++ b/session/common_test.go @@ -7,7 +7,7 @@ import ( "github.com/google/uuid" "github.com/nspcc-dev/neofs-api-go/v2/refs" "github.com/nspcc-dev/neofs-api-go/v2/session" - "github.com/nspcc-dev/neofs-sdk-go/crypto/test" + usertest "github.com/nspcc-dev/neofs-sdk-go/user/test" "github.com/stretchr/testify/require" ) @@ -18,13 +18,13 @@ func Test_commonData_copyTo(t *testing.T) { sig.SetSign([]byte("sign")) sig.SetScheme(refs.ECDSA_SHA512) - signer := test.RandomSignerRFC6979() + usr := usertest.User() data := commonData{ idSet: true, id: uuid.New(), issuerSet: true, - issuer: signer.UserID(), + issuer: usr.UserID(), lifetimeSet: true, iat: 1, nbf: 2, @@ -98,7 +98,7 @@ func Test_commonData_copyTo(t *testing.T) { require.Equal(t, data.issuerSet, dst.issuerSet) require.True(t, data.issuer.Equals(dst.issuer)) - dst.SetIssuer(test.RandomSignerRFC6979().UserID()) + dst.SetIssuer(usertest.OtherID(usr.ID)) require.Equal(t, data.issuerSet, dst.issuerSet) require.False(t, data.issuer.Equals(dst.issuer)) @@ -109,7 +109,7 @@ func Test_commonData_copyTo(t *testing.T) { require.False(t, local.issuerSet) var dst commonData - dst.SetIssuer(test.RandomSignerRFC6979().UserID()) + dst.SetIssuer(usertest.OtherID(usr.ID)) require.True(t, dst.issuerSet) local.copyTo(&dst) @@ -124,7 +124,7 @@ func Test_commonData_copyTo(t *testing.T) { require.Equal(t, local.issuerSet, dst.issuerSet) require.True(t, local.issuer.Equals(dst.issuer)) - dst.SetIssuer(test.RandomSignerRFC6979().UserID()) + dst.SetIssuer(usertest.OtherID(usr.ID)) require.False(t, local.issuerSet) require.True(t, dst.issuerSet) @@ -218,7 +218,7 @@ func Test_commonData_copyTo(t *testing.T) { } var dst commonData - require.NoError(t, dst.sign(signer, emptyWriter)) + require.NoError(t, dst.sign(usr, emptyWriter)) require.True(t, dst.sigSet) local.copyTo(&dst) @@ -227,7 +227,7 @@ func Test_commonData_copyTo(t *testing.T) { require.True(t, bytes.Equal(local.marshal(emptyWriter), dst.marshal(emptyWriter))) - require.NoError(t, dst.sign(signer, emptyWriter)) + require.NoError(t, dst.sign(usr, emptyWriter)) require.False(t, local.sigSet) require.True(t, dst.sigSet) }) diff --git a/session/container_test.go b/session/container_test.go index 4d5df119..d3cc1f79 100644 --- a/session/container_test.go +++ b/session/container_test.go @@ -12,7 +12,7 @@ import ( v2session "github.com/nspcc-dev/neofs-api-go/v2/session" cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test" neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto" - "github.com/nspcc-dev/neofs-sdk-go/crypto/test" + neofscryptotest "github.com/nspcc-dev/neofs-sdk-go/crypto/test" "github.com/nspcc-dev/neofs-sdk-go/session" sessiontest "github.com/nspcc-dev/neofs-sdk-go/session/test" "github.com/nspcc-dev/neofs-sdk-go/user" @@ -36,9 +36,9 @@ func TestContainerProtocolV2(t *testing.T) { restoreID() // Owner - usr := usertest.ID() + usrID := usertest.ID() var usrV2 refs.OwnerID - usr.WriteToV2(&usrV2) + usrID.WriteToV2(&usrV2) restoreUser := func() { body.SetOwnerID(&usrV2) } @@ -55,8 +55,8 @@ func TestContainerProtocolV2(t *testing.T) { restoreLifetime() // Session key - signer := test.RandomSignerRFC6979() - authKey := signer.Public() + usr := usertest.User() + authKey := usr.Public() binAuthKey := neofscrypto.PublicKeyBytes(authKey) restoreAuthKey := func() { body.SetSessionKey(binAuthKey) @@ -134,7 +134,7 @@ func TestContainerProtocolV2(t *testing.T) { }, restore: restoreUser, assert: func(val session.Container) { - require.Equal(t, usr, val.Issuer()) + require.Equal(t, usrID, val.Issuer()) }, breakSign: func(m *v2session.Token) { id := m.GetBody().GetOwnerID().GetValue() @@ -225,7 +225,7 @@ func TestContainerProtocolV2(t *testing.T) { } if testcase.breakSign != nil { - require.NoError(t, val.Sign(signer), testcase.name) + require.NoError(t, val.Sign(usr), testcase.name) require.True(t, val.VerifySignature(), testcase.name) var signedV2 v2session.Token @@ -265,14 +265,14 @@ func TestContainer_WriteToV2(t *testing.T) { }) // Owner/Signature - signer := test.RandomSignerRFC6979() + usr := usertest.User() - require.NoError(t, val.Sign(signer)) + require.NoError(t, val.Sign(usr)) - usr := signer.UserID() + usrID := usr.UserID() var usrV2 refs.OwnerID - usr.WriteToV2(&usrV2) + usrID.WriteToV2(&usrV2) assert(func(m v2session.Token) { require.Equal(t, &usrV2, m.GetBody().GetOwnerID()) @@ -428,7 +428,7 @@ func TestContainer_ID(t *testing.T) { func TestContainer_AssertAuthKey(t *testing.T) { var x session.Container - key := test.RandomSignerRFC6979().Public() + key := neofscryptotest.Signer().Public() require.False(t, x.AssertAuthKey(key)) @@ -511,7 +511,7 @@ func TestIssuedBy(t *testing.T) { var ( token session.Container issuer user.ID - signer = test.RandomSignerRFC6979() + signer = usertest.User() ) issuer = signer.UserID() @@ -526,20 +526,19 @@ func TestContainer_Issuer(t *testing.T) { t.Run("signer", func(t *testing.T) { var token session.Container - signer := test.RandomSignerRFC6979() + usr := usertest.User() require.Zero(t, token.Issuer()) - require.NoError(t, token.Sign(signer)) + require.NoError(t, token.Sign(usr)) - issuer := signer.UserID() + issuer := usr.UserID() require.True(t, token.Issuer().Equals(issuer)) }) t.Run("external", func(t *testing.T) { var token session.Container - signer := test.RandomSignerRFC6979() - issuer := signer.UserID() + issuer := usertest.ID() token.SetIssuer(issuer) require.True(t, token.Issuer().Equals(issuer)) @@ -548,40 +547,40 @@ func TestContainer_Issuer(t *testing.T) { t.Run("public key", func(t *testing.T) { var token session.Container - signer := test.RandomSignerRFC6979() + usr := usertest.User() require.Nil(t, token.IssuerPublicKeyBytes()) - require.NoError(t, token.Sign(signer)) + require.NoError(t, token.Sign(usr)) - require.Equal(t, neofscrypto.PublicKeyBytes(signer.Public()), token.IssuerPublicKeyBytes()) + require.Equal(t, neofscrypto.PublicKeyBytes(usr.Public()), token.IssuerPublicKeyBytes()) }) } func TestContainer_Sign(t *testing.T) { val := sessiontest.Container() - require.NoError(t, val.SetSignature(test.RandomSignerRFC6979())) + require.NoError(t, val.SetSignature(neofscryptotest.Signer())) require.Zero(t, val.Issuer()) require.True(t, val.VerifySignature()) - require.NoError(t, val.Sign(test.RandomSignerRFC6979())) + require.NoError(t, val.Sign(usertest.User())) require.True(t, val.VerifySignature()) t.Run("issue#546", func(t *testing.T) { - signer1 := test.RandomSignerRFC6979() - signer2 := test.RandomSignerRFC6979() - require.False(t, signer1.UserID().Equals(signer2.UserID())) + usr1 := usertest.User() + usr2 := usertest.User() + require.False(t, usr1.UserID().Equals(usr2.UserID())) token1 := sessiontest.Container() - require.NoError(t, token1.Sign(signer1)) - require.Equal(t, signer1.UserID(), token1.Issuer()) + require.NoError(t, token1.Sign(usr1)) + require.Equal(t, usr1.UserID(), token1.Issuer()) // copy token and re-sign var token2 session.Container token1.CopyTo(&token2) - require.NoError(t, token2.Sign(signer2)) - require.Equal(t, signer2.UserID(), token2.Issuer()) + require.NoError(t, token2.Sign(usr2)) + require.Equal(t, usr2.UserID(), token2.Issuer()) }) } @@ -596,12 +595,12 @@ func TestContainer_SignedData(t *testing.T) { require.NoError(t, dec.UnmarshalSignedData(signedData)) require.Equal(t, val, dec) - signer := user.NewSigner(test.RandomSigner(), id) - test.SignedDataComponentUser(t, signer, &val) + signer := user.NewSigner(neofscryptotest.Signer(), id) + usertest.TestSignedData(t, signer, &val) } func TestContainer_VerifyDataSignature(t *testing.T) { - signer := test.RandomSignerRFC6979() + signer := neofscryptotest.Signer().RFC6979 var tok session.Container diff --git a/session/object_test.go b/session/object_test.go index b4e103e4..fae907aa 100644 --- a/session/object_test.go +++ b/session/object_test.go @@ -12,7 +12,7 @@ import ( v2session "github.com/nspcc-dev/neofs-api-go/v2/session" cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test" neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto" - "github.com/nspcc-dev/neofs-sdk-go/crypto/test" + neofscryptotest "github.com/nspcc-dev/neofs-sdk-go/crypto/test" oidtest "github.com/nspcc-dev/neofs-sdk-go/object/id/test" "github.com/nspcc-dev/neofs-sdk-go/session" sessiontest "github.com/nspcc-dev/neofs-sdk-go/session/test" @@ -36,9 +36,9 @@ func TestObjectProtocolV2(t *testing.T) { restoreID() // Owner - usr := usertest.ID() + usrID := usertest.ID() var usrV2 refs.OwnerID - usr.WriteToV2(&usrV2) + usrID.WriteToV2(&usrV2) restoreUser := func() { body.SetOwnerID(&usrV2) } @@ -55,8 +55,8 @@ func TestObjectProtocolV2(t *testing.T) { restoreLifetime() // Session key - signer := test.RandomSignerRFC6979() - authKey := signer.Public() + usr := usertest.User() + authKey := usr.Public() binAuthKey := neofscrypto.PublicKeyBytes(authKey) restoreAuthKey := func() { body.SetSessionKey(binAuthKey) @@ -139,7 +139,7 @@ func TestObjectProtocolV2(t *testing.T) { }, restore: restoreUser, assert: func(val session.Object) { - require.Equal(t, usr, val.Issuer()) + require.Equal(t, usrID, val.Issuer()) }, breakSign: func(m *v2session.Token) { id := m.GetBody().GetOwnerID().GetValue() @@ -235,7 +235,7 @@ func TestObjectProtocolV2(t *testing.T) { } if testcase.breakSign != nil { - require.NoError(t, val.Sign(signer), testcase.name) + require.NoError(t, val.Sign(usr), testcase.name) require.True(t, val.VerifySignature(), testcase.name) var signedV2 v2session.Token @@ -275,14 +275,14 @@ func TestObject_WriteToV2(t *testing.T) { }) // Owner/Signature - signer := test.RandomSignerRFC6979() + usr := usertest.User() - require.NoError(t, val.Sign(signer)) + require.NoError(t, val.Sign(usr)) - usr := signer.UserID() + usrID := usr.UserID() var usrV2 refs.OwnerID - usr.WriteToV2(&usrV2) + usrID.WriteToV2(&usrV2) assert(func(m v2session.Token) { require.Equal(t, &usrV2, m.GetBody().GetOwnerID()) @@ -521,7 +521,7 @@ func TestObject_ID(t *testing.T) { func TestObject_AssertAuthKey(t *testing.T) { var x session.Object - key := test.RandomSignerRFC6979().Public() + key := neofscryptotest.Signer().Public() require.False(t, x.AssertAuthKey(key)) @@ -607,68 +607,68 @@ func TestObject_AssertVerb(t *testing.T) { func TestObject_Issuer(t *testing.T) { var token session.Object - signer := test.RandomSignerRFC6979() + usr := usertest.User() require.Zero(t, token.Issuer()) require.Nil(t, token.IssuerPublicKeyBytes()) - require.NoError(t, token.Sign(signer)) + require.NoError(t, token.Sign(usr)) - issuer := signer.UserID() + issuer := usr.UserID() require.True(t, token.Issuer().Equals(issuer)) - require.Equal(t, neofscrypto.PublicKeyBytes(signer.Public()), token.IssuerPublicKeyBytes()) + require.Equal(t, neofscrypto.PublicKeyBytes(usr.Public()), token.IssuerPublicKeyBytes()) } func TestObject_Sign(t *testing.T) { val := sessiontest.Object() - require.NoError(t, val.SetSignature(test.RandomSignerRFC6979())) + require.NoError(t, val.SetSignature(neofscryptotest.Signer())) require.Zero(t, val.Issuer()) require.True(t, val.VerifySignature()) - require.NoError(t, val.Sign(test.RandomSignerRFC6979())) + require.NoError(t, val.Sign(usertest.User())) require.True(t, val.VerifySignature()) t.Run("issue#546", func(t *testing.T) { - signer1 := test.RandomSignerRFC6979() - signer2 := test.RandomSignerRFC6979() - require.False(t, signer1.UserID().Equals(signer2.UserID())) + usr1 := usertest.User() + usr2 := usertest.User() + require.False(t, usr1.UserID().Equals(usr2.UserID())) token1 := sessiontest.Object() - require.NoError(t, token1.Sign(signer1)) - require.Equal(t, signer1.UserID(), token1.Issuer()) + require.NoError(t, token1.Sign(usr1)) + require.Equal(t, usr1.UserID(), token1.Issuer()) // copy token and re-sign var token2 session.Object token1.CopyTo(&token2) - require.NoError(t, token2.Sign(signer2)) - require.Equal(t, signer2.UserID(), token2.Issuer()) + require.NoError(t, token2.Sign(usr2)) + require.Equal(t, usr2.UserID(), token2.Issuer()) }) } func TestObject_SignedData(t *testing.T) { - issuerSigner := test.RandomSignerRFC6979() - issuer := issuerSigner.UserID() + issuer := usertest.User() + issuerID := issuer.UserID() var tokenSession session.Object tokenSession.SetID(uuid.New()) tokenSession.SetExp(100500) tokenSession.BindContainer(cidtest.ID()) tokenSession.ForVerb(session.VerbObjectPut) - tokenSession.SetAuthKey(test.RandomSignerRFC6979().Public()) - tokenSession.SetIssuer(issuer) + tokenSession.SetAuthKey(neofscryptotest.Signer().Public()) + tokenSession.SetIssuer(issuerID) signedData := tokenSession.SignedData() var dec session.Object require.NoError(t, dec.UnmarshalSignedData(signedData)) require.Equal(t, tokenSession, dec) - sign, err := issuerSigner.Sign(signedData) + sign, err := issuer.RFC6979.Sign(signedData) require.NoError(t, err) - require.NoError(t, tokenSession.Sign(issuerSigner)) + require.NoError(t, tokenSession.Sign(issuer.RFC6979)) require.True(t, tokenSession.VerifySignature()) var m v2session.Token @@ -676,5 +676,5 @@ func TestObject_SignedData(t *testing.T) { require.Equal(t, m.GetSignature().GetSign(), sign) - test.SignedDataComponentUser(t, issuerSigner, &tokenSession) + usertest.TestSignedData(t, issuer, &tokenSession) } diff --git a/user/test/id.go b/user/test/id.go index 32f88ae2..399dc162 100644 --- a/user/test/id.go +++ b/user/test/id.go @@ -1,10 +1,16 @@ package usertest import ( + "crypto/ecdsa" + "errors" "math/rand" + "testing" "github.com/nspcc-dev/neo-go/pkg/util" + neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto" + neofscryptotest "github.com/nspcc-dev/neofs-sdk-go/crypto/test" "github.com/nspcc-dev/neofs-sdk-go/user" + "github.com/stretchr/testify/require" ) // ID returns random user.ID. @@ -39,3 +45,61 @@ func IDs(n int) []user.ID { } return res } + +// UserSigner represents NeoFS user credentials. +type UserSigner struct { + ID user.ID + ECDSAPrivateKey ecdsa.PrivateKey + // Components calculated for ECDSAPrivateKey. + PublicKeyBytes []byte + user.Signer + RFC6979 user.Signer + WalletConnect user.Signer +} + +func User() UserSigner { + cs := neofscryptotest.Signer() + s := user.NewAutoIDSigner(cs.ECDSAPrivateKey) + return UserSigner{ + ID: s.UserID(), + ECDSAPrivateKey: cs.ECDSAPrivateKey, + PublicKeyBytes: cs.PublicKeyBytes, + Signer: s, + RFC6979: user.NewAutoIDSignerRFC6979(cs.ECDSAPrivateKey), + WalletConnect: user.NewSigner(cs.WalletConnect, s.UserID()), + } +} + +type failedSigner struct { + user.Signer +} + +func (x failedSigner) Sign([]byte) ([]byte, error) { return nil, errors.New("[test] failed to sign") } + +// FailSigner returns wraps s to always return error from Sign method. +func FailSigner(s user.Signer) user.Signer { + return failedSigner{s} +} + +// SignedComponent contains data to be signed and its signature to be verified. +type SignedComponent interface { + SignedData() []byte + Sign(user.Signer) error + VerifySignature() bool +} + +// TestSignedData SignedDataComponentUser signing and verification of +// [SignedComponent.SignedData]. +func TestSignedData(tb testing.TB, signer user.Signer, cmp SignedComponent) { + data := cmp.SignedData() + + sig, err := signer.Sign(data) + require.NoError(tb, err) + + static := neofscrypto.NewStaticSigner(signer.Scheme(), sig, signer.Public()) + + err = cmp.Sign(user.NewSigner(static, signer.UserID())) + require.NoError(tb, err) + + require.True(tb, cmp.VerifySignature()) +} diff --git a/user/test/id_test.go b/user/test/id_test.go index 67820682..18f00781 100644 --- a/user/test/id_test.go +++ b/user/test/id_test.go @@ -6,6 +6,8 @@ import ( "github.com/nspcc-dev/neofs-api-go/v2/refs" cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test" + neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto" + neofsecdsa "github.com/nspcc-dev/neofs-sdk-go/crypto/ecdsa" "github.com/nspcc-dev/neofs-sdk-go/user" usertest "github.com/nspcc-dev/neofs-sdk-go/user/test" "github.com/stretchr/testify/require" @@ -30,3 +32,38 @@ func TestOtherID(t *testing.T) { ids := usertest.IDs(100) require.NotContains(t, ids, usertest.OtherID(ids...)) } + +func TestFailSigner(t *testing.T) { + s := usertest.FailSigner(usertest.User()) + _, err := s.Sign(nil) + require.EqualError(t, err, "[test] failed to sign") +} + +func TestUser(t *testing.T) { + s := usertest.User() + require.NotEqual(t, s, usertest.User()) + + require.Equal(t, s.ID, s.UserID()) + require.Equal(t, s.ID, user.ResolveFromECDSAPublicKey(s.ECDSAPrivateKey.PublicKey)) + + var pub neofsecdsa.PublicKey + require.NoError(t, pub.Decode(s.PublicKeyBytes)) + require.EqualValues(t, s.ECDSAPrivateKey.PublicKey, pub) + + data := []byte("Hello, world!") + + sig, err := s.Sign(data) + require.NoError(t, err) + require.True(t, pub.Verify(data, sig)) + require.Equal(t, neofscrypto.ECDSA_SHA512, s.Scheme()) + + sig, err = s.RFC6979.Sign(data) + require.NoError(t, err) + require.True(t, (*neofsecdsa.PublicKeyRFC6979)(&pub).Verify(data, sig)) + require.Equal(t, neofscrypto.ECDSA_DETERMINISTIC_SHA256, s.RFC6979.Scheme()) + + sig, err = s.WalletConnect.Sign(data) + require.NoError(t, err) + require.True(t, (*neofsecdsa.PublicKeyWalletConnect)(&pub).Verify(data, sig)) + require.Equal(t, neofscrypto.ECDSA_WALLETCONNECT, s.WalletConnect.Scheme()) +}