2020-05-06 10:16:15 +00:00
|
|
|
package object
|
|
|
|
|
|
|
|
import (
|
2020-05-18 09:48:04 +00:00
|
|
|
"crypto/rand"
|
2020-05-06 10:16:15 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/service"
|
|
|
|
"github.com/nspcc-dev/neofs-crypto/test"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSignVerifyRequests(t *testing.T) {
|
|
|
|
sk := test.DecodeKey(0)
|
|
|
|
|
|
|
|
type sigType interface {
|
2020-06-10 17:22:34 +00:00
|
|
|
service.RequestData
|
2020-05-06 10:16:15 +00:00
|
|
|
service.SignKeyPairAccumulator
|
|
|
|
service.SignKeyPairSource
|
|
|
|
SetToken(*Token)
|
|
|
|
}
|
|
|
|
|
|
|
|
items := []struct {
|
2020-05-06 10:33:03 +00:00
|
|
|
constructor func() sigType
|
|
|
|
payloadCorrupt []func(sigType)
|
2020-05-06 10:16:15 +00:00
|
|
|
}{
|
|
|
|
{ // PutRequest.PutHeader
|
|
|
|
constructor: func() sigType {
|
|
|
|
return MakePutRequestHeader(new(Object))
|
|
|
|
},
|
2020-05-06 10:33:03 +00:00
|
|
|
payloadCorrupt: []func(sigType){
|
2020-05-06 10:16:15 +00:00
|
|
|
func(s sigType) {
|
|
|
|
obj := s.(*PutRequest).GetR().(*PutRequest_Header).Header.GetObject()
|
|
|
|
obj.SystemHeader.PayloadLength++
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{ // PutRequest.Chunk
|
|
|
|
constructor: func() sigType {
|
|
|
|
return MakePutRequestChunk(make([]byte, 10))
|
|
|
|
},
|
2020-05-06 10:33:03 +00:00
|
|
|
payloadCorrupt: []func(sigType){
|
2020-05-06 10:16:15 +00:00
|
|
|
func(s sigType) {
|
|
|
|
h := s.(*PutRequest).GetR().(*PutRequest_Chunk)
|
|
|
|
h.Chunk[0]++
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-05-06 10:33:03 +00:00
|
|
|
{ // GetRequest
|
|
|
|
constructor: func() sigType {
|
|
|
|
return new(GetRequest)
|
|
|
|
},
|
|
|
|
payloadCorrupt: []func(sigType){
|
|
|
|
func(s sigType) {
|
|
|
|
s.(*GetRequest).Address.CID[0]++
|
|
|
|
},
|
|
|
|
func(s sigType) {
|
|
|
|
s.(*GetRequest).Address.ObjectID[0]++
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-05-06 10:54:13 +00:00
|
|
|
{ // HeadRequest
|
|
|
|
constructor: func() sigType {
|
|
|
|
return new(HeadRequest)
|
|
|
|
},
|
|
|
|
payloadCorrupt: []func(sigType){
|
|
|
|
func(s sigType) {
|
|
|
|
s.(*HeadRequest).Address.CID[0]++
|
|
|
|
},
|
|
|
|
func(s sigType) {
|
|
|
|
s.(*HeadRequest).Address.ObjectID[0]++
|
|
|
|
},
|
|
|
|
func(s sigType) {
|
|
|
|
s.(*HeadRequest).FullHeaders = true
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-05-06 11:15:07 +00:00
|
|
|
{ // DeleteRequest
|
|
|
|
constructor: func() sigType {
|
|
|
|
return new(DeleteRequest)
|
|
|
|
},
|
|
|
|
payloadCorrupt: []func(sigType){
|
|
|
|
func(s sigType) {
|
|
|
|
s.(*DeleteRequest).OwnerID[0]++
|
|
|
|
},
|
|
|
|
func(s sigType) {
|
|
|
|
s.(*DeleteRequest).Address.CID[0]++
|
|
|
|
},
|
|
|
|
func(s sigType) {
|
|
|
|
s.(*DeleteRequest).Address.ObjectID[0]++
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-05-06 11:38:39 +00:00
|
|
|
{ // GetRangeRequest
|
|
|
|
constructor: func() sigType {
|
|
|
|
return new(GetRangeRequest)
|
|
|
|
},
|
|
|
|
payloadCorrupt: []func(sigType){
|
|
|
|
func(s sigType) {
|
|
|
|
s.(*GetRangeRequest).Range.Length++
|
|
|
|
},
|
|
|
|
func(s sigType) {
|
|
|
|
s.(*GetRangeRequest).Range.Offset++
|
|
|
|
},
|
|
|
|
func(s sigType) {
|
|
|
|
s.(*GetRangeRequest).Address.CID[0]++
|
|
|
|
},
|
|
|
|
func(s sigType) {
|
|
|
|
s.(*GetRangeRequest).Address.ObjectID[0]++
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-05-06 11:58:29 +00:00
|
|
|
{ // GetRangeHashRequest
|
|
|
|
constructor: func() sigType {
|
|
|
|
return &GetRangeHashRequest{
|
|
|
|
Ranges: []Range{{}},
|
|
|
|
Salt: []byte{1, 2, 3},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
payloadCorrupt: []func(sigType){
|
|
|
|
func(s sigType) {
|
|
|
|
s.(*GetRangeHashRequest).Address.CID[0]++
|
|
|
|
},
|
|
|
|
func(s sigType) {
|
|
|
|
s.(*GetRangeHashRequest).Address.ObjectID[0]++
|
|
|
|
},
|
|
|
|
func(s sigType) {
|
|
|
|
s.(*GetRangeHashRequest).Salt[0]++
|
|
|
|
},
|
|
|
|
func(s sigType) {
|
|
|
|
s.(*GetRangeHashRequest).Ranges[0].Length++
|
|
|
|
},
|
|
|
|
func(s sigType) {
|
|
|
|
s.(*GetRangeHashRequest).Ranges[0].Offset++
|
|
|
|
},
|
|
|
|
func(s sigType) {
|
|
|
|
s.(*GetRangeHashRequest).Ranges = nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-05-06 12:09:31 +00:00
|
|
|
{ // GetRangeHashRequest
|
|
|
|
constructor: func() sigType {
|
|
|
|
return &SearchRequest{
|
|
|
|
Query: []byte{1, 2, 3},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
payloadCorrupt: []func(sigType){
|
|
|
|
func(s sigType) {
|
|
|
|
s.(*SearchRequest).ContainerID[0]++
|
|
|
|
},
|
|
|
|
func(s sigType) {
|
|
|
|
s.(*SearchRequest).Query[0]++
|
|
|
|
},
|
|
|
|
func(s sigType) {
|
|
|
|
s.(*SearchRequest).QueryVersion++
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-05-06 10:16:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range items {
|
|
|
|
{ // token corruptions
|
|
|
|
v := item.constructor()
|
|
|
|
|
|
|
|
token := new(Token)
|
|
|
|
v.SetToken(token)
|
|
|
|
|
2020-06-10 17:22:34 +00:00
|
|
|
require.NoError(t, service.SignRequestData(sk, v))
|
2020-05-06 10:16:15 +00:00
|
|
|
|
2020-06-10 17:22:34 +00:00
|
|
|
require.NoError(t, service.VerifyRequestData(v))
|
2020-05-06 10:16:15 +00:00
|
|
|
|
|
|
|
token.SetSessionKey(append(token.GetSessionKey(), 1))
|
|
|
|
|
2020-06-10 17:22:34 +00:00
|
|
|
require.Error(t, service.VerifyRequestData(v))
|
2020-05-06 10:16:15 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 10:33:03 +00:00
|
|
|
{ // payload corruptions
|
|
|
|
for _, corruption := range item.payloadCorrupt {
|
2020-05-06 10:16:15 +00:00
|
|
|
v := item.constructor()
|
|
|
|
|
2020-06-10 17:22:34 +00:00
|
|
|
require.NoError(t, service.SignRequestData(sk, v))
|
2020-05-06 10:16:15 +00:00
|
|
|
|
2020-06-10 17:22:34 +00:00
|
|
|
require.NoError(t, service.VerifyRequestData(v))
|
2020-05-06 10:16:15 +00:00
|
|
|
|
|
|
|
corruption(v)
|
|
|
|
|
2020-06-10 17:22:34 +00:00
|
|
|
require.Error(t, service.VerifyRequestData(v))
|
2020-05-06 10:16:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-18 09:47:13 +00:00
|
|
|
|
|
|
|
func TestHeadRequest_ReadSignedData(t *testing.T) {
|
|
|
|
t.Run("full headers", func(t *testing.T) {
|
|
|
|
req := new(HeadRequest)
|
|
|
|
|
2020-05-18 09:48:04 +00:00
|
|
|
// unset FullHeaders flag
|
|
|
|
req.SetFullHeaders(false)
|
2020-05-18 09:47:13 +00:00
|
|
|
|
2020-05-18 09:48:04 +00:00
|
|
|
// allocate two different buffers for reading
|
|
|
|
buf1 := testData(t, req.SignedDataSize())
|
|
|
|
buf2 := testData(t, req.SignedDataSize())
|
2020-05-18 09:47:13 +00:00
|
|
|
|
2020-05-18 09:48:04 +00:00
|
|
|
// read to both buffers
|
|
|
|
n1, err := req.ReadSignedData(buf1)
|
2020-05-18 09:47:13 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-05-18 09:48:04 +00:00
|
|
|
n2, err := req.ReadSignedData(buf2)
|
2020-05-18 09:47:13 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-05-18 09:48:04 +00:00
|
|
|
require.Equal(t, buf1[:n1], buf2[:n2])
|
2020-05-18 09:47:13 +00:00
|
|
|
})
|
|
|
|
}
|
2020-05-18 09:48:04 +00:00
|
|
|
|
|
|
|
func testData(t *testing.T, sz int) []byte {
|
|
|
|
data := make([]byte, sz)
|
|
|
|
|
|
|
|
_, err := rand.Read(data)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return data
|
|
|
|
}
|
2020-05-18 10:44:32 +00:00
|
|
|
|
|
|
|
func TestIntegrityHeaderSignMethods(t *testing.T) {
|
|
|
|
// create new IntegrityHeader
|
|
|
|
s := new(IntegrityHeader)
|
|
|
|
|
|
|
|
// set test headers checksum
|
|
|
|
s.SetHeadersChecksum([]byte{1, 2, 3})
|
|
|
|
|
|
|
|
data, err := s.SignedData()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, data, s.GetHeadersChecksum())
|
|
|
|
|
|
|
|
// add signature
|
|
|
|
sig := []byte{4, 5, 6}
|
|
|
|
s.AddSignKey(sig, nil)
|
|
|
|
|
|
|
|
require.Equal(t, sig, s.GetSignature())
|
|
|
|
}
|