2020-08-18 15:28:02 +00:00
|
|
|
package object_test
|
|
|
|
|
|
|
|
import (
|
2020-08-18 16:46:55 +00:00
|
|
|
"fmt"
|
2020-09-11 14:35:07 +00:00
|
|
|
"strconv"
|
2020-08-18 15:28:02 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/object"
|
|
|
|
grpc "github.com/nspcc-dev/neofs-api-go/v2/object/grpc"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
2020-08-20 10:41:55 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/session"
|
2020-08-18 15:28:02 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-10-19 18:51:59 +00:00
|
|
|
goproto "google.golang.org/protobuf/proto"
|
2020-08-18 15:28:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestShortHeader_StableMarshal(t *testing.T) {
|
|
|
|
hdrFrom := generateShortHeader("Owner ID")
|
|
|
|
|
|
|
|
t.Run("non empty", func(t *testing.T) {
|
|
|
|
wire, err := hdrFrom.StableMarshal(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-11-13 12:26:23 +00:00
|
|
|
hdrTo := new(object.ShortHeader)
|
|
|
|
require.NoError(t, hdrTo.Unmarshal(wire))
|
2020-08-18 15:28:02 +00:00
|
|
|
|
|
|
|
require.Equal(t, hdrFrom, hdrTo)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAttribute_StableMarshal(t *testing.T) {
|
|
|
|
from := generateAttribute("Key", "Value")
|
|
|
|
|
|
|
|
t.Run("non empty", func(t *testing.T) {
|
|
|
|
wire, err := from.StableMarshal(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-11-13 12:31:15 +00:00
|
|
|
to := new(object.Attribute)
|
|
|
|
require.NoError(t, to.Unmarshal(wire))
|
2020-08-18 15:28:02 +00:00
|
|
|
|
|
|
|
require.Equal(t, from, to)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSplitHeader_StableMarshal(t *testing.T) {
|
|
|
|
from := generateSplit("Split Outside")
|
|
|
|
hdr := generateHeader(123)
|
|
|
|
from.SetParentHeader(hdr)
|
|
|
|
|
|
|
|
t.Run("non empty", func(t *testing.T) {
|
|
|
|
wire, err := from.StableMarshal(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-11-13 12:34:13 +00:00
|
|
|
to := new(object.SplitHeader)
|
|
|
|
require.NoError(t, to.Unmarshal(wire))
|
2020-08-18 15:28:02 +00:00
|
|
|
|
|
|
|
require.Equal(t, from, to)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHeader_StableMarshal(t *testing.T) {
|
|
|
|
insideHeader := generateHeader(100)
|
|
|
|
split := generateSplit("Split")
|
|
|
|
split.SetParentHeader(insideHeader)
|
|
|
|
|
|
|
|
from := generateHeader(500)
|
|
|
|
from.SetSplit(split)
|
|
|
|
|
|
|
|
transport := new(grpc.Header)
|
|
|
|
|
|
|
|
t.Run("non empty", func(t *testing.T) {
|
|
|
|
wire, err := from.StableMarshal(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-10-19 18:51:59 +00:00
|
|
|
err = goproto.Unmarshal(wire, transport)
|
2020-08-18 15:28:02 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
to := object.HeaderFromGRPCMessage(transport)
|
|
|
|
require.Equal(t, from, to)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestObject_StableMarshal(t *testing.T) {
|
|
|
|
from := generateObject("Payload")
|
|
|
|
transport := new(grpc.Object)
|
|
|
|
|
|
|
|
t.Run("non empty", func(t *testing.T) {
|
|
|
|
wire, err := from.StableMarshal(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-10-19 18:51:59 +00:00
|
|
|
err = goproto.Unmarshal(wire, transport)
|
2020-08-18 15:28:02 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
to := object.ObjectFromGRPCMessage(transport)
|
|
|
|
require.Equal(t, from, to)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-08-18 16:46:55 +00:00
|
|
|
func TestGetRequestBody_StableMarshal(t *testing.T) {
|
|
|
|
from := generateGetRequestBody("Container ID", "Object ID")
|
|
|
|
transport := new(grpc.GetRequest_Body)
|
|
|
|
|
|
|
|
t.Run("non empty", func(t *testing.T) {
|
|
|
|
wire, err := from.StableMarshal(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-10-19 18:51:59 +00:00
|
|
|
err = goproto.Unmarshal(wire, transport)
|
2020-08-18 16:46:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
to := object.GetRequestBodyFromGRPCMessage(transport)
|
|
|
|
require.Equal(t, from, to)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetResponseBody_StableMarshal(t *testing.T) {
|
|
|
|
initFrom := generateGetResponseBody(true)
|
|
|
|
chunkFrom := generateGetResponseBody(false)
|
|
|
|
transport := new(grpc.GetResponse_Body)
|
|
|
|
|
|
|
|
t.Run("init non empty", func(t *testing.T) {
|
|
|
|
wire, err := initFrom.StableMarshal(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-10-19 18:51:59 +00:00
|
|
|
err = goproto.Unmarshal(wire, transport)
|
2020-08-18 16:46:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
to := object.GetResponseBodyFromGRPCMessage(transport)
|
|
|
|
require.Equal(t, initFrom, to)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("chunk non empty", func(t *testing.T) {
|
|
|
|
wire, err := chunkFrom.StableMarshal(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-10-19 18:51:59 +00:00
|
|
|
err = goproto.Unmarshal(wire, transport)
|
2020-08-18 16:46:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
to := object.GetResponseBodyFromGRPCMessage(transport)
|
|
|
|
require.Equal(t, chunkFrom, to)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPutRequestBody_StableMarshal(t *testing.T) {
|
|
|
|
initFrom := generatePutRequestBody(true)
|
|
|
|
chunkFrom := generatePutRequestBody(false)
|
|
|
|
transport := new(grpc.PutRequest_Body)
|
|
|
|
|
|
|
|
t.Run("init non empty", func(t *testing.T) {
|
|
|
|
wire, err := initFrom.StableMarshal(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-10-19 18:51:59 +00:00
|
|
|
err = goproto.Unmarshal(wire, transport)
|
2020-08-18 16:46:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
to := object.PutRequestBodyFromGRPCMessage(transport)
|
|
|
|
require.Equal(t, initFrom, to)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("chunk non empty", func(t *testing.T) {
|
|
|
|
wire, err := chunkFrom.StableMarshal(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-10-19 18:51:59 +00:00
|
|
|
err = goproto.Unmarshal(wire, transport)
|
2020-08-18 16:46:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
to := object.PutRequestBodyFromGRPCMessage(transport)
|
|
|
|
require.Equal(t, chunkFrom, to)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPutRequestBody_StableSize(t *testing.T) {
|
|
|
|
from := generatePutResponseBody("Object ID")
|
|
|
|
transport := new(grpc.PutResponse_Body)
|
|
|
|
|
|
|
|
t.Run("non empty", func(t *testing.T) {
|
|
|
|
wire, err := from.StableMarshal(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-10-19 18:51:59 +00:00
|
|
|
err = goproto.Unmarshal(wire, transport)
|
2020-08-18 16:46:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
to := object.PutResponseBodyFromGRPCMessage(transport)
|
|
|
|
require.Equal(t, from, to)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteRequestBody_StableMarshal(t *testing.T) {
|
|
|
|
from := generateDeleteRequestBody("Container ID", "Object ID")
|
|
|
|
transport := new(grpc.DeleteRequest_Body)
|
|
|
|
|
|
|
|
t.Run("non empty", func(t *testing.T) {
|
|
|
|
wire, err := from.StableMarshal(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-10-19 18:51:59 +00:00
|
|
|
err = goproto.Unmarshal(wire, transport)
|
2020-08-18 16:46:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
to := object.DeleteRequestBodyFromGRPCMessage(transport)
|
|
|
|
require.Equal(t, from, to)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteResponseBody_StableMarshal(t *testing.T) {
|
|
|
|
from := generateDeleteResponseBody()
|
|
|
|
transport := new(grpc.DeleteResponse_Body)
|
|
|
|
|
|
|
|
t.Run("non empty", func(t *testing.T) {
|
|
|
|
wire, err := from.StableMarshal(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-10-19 18:51:59 +00:00
|
|
|
err = goproto.Unmarshal(wire, transport)
|
2020-08-18 16:46:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
to := object.DeleteResponseBodyFromGRPCMessage(transport)
|
|
|
|
require.Equal(t, from, to)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSplitHeaderFromGRPCMessage(t *testing.T) {
|
|
|
|
from := generateHeadRequestBody("Container ID", "Object ID")
|
|
|
|
transport := new(grpc.HeadRequest_Body)
|
|
|
|
|
|
|
|
t.Run("non empty", func(t *testing.T) {
|
|
|
|
wire, err := from.StableMarshal(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-10-19 18:51:59 +00:00
|
|
|
err = goproto.Unmarshal(wire, transport)
|
2020-08-18 16:46:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
to := object.HeadRequestBodyFromGRPCMessage(transport)
|
|
|
|
require.Equal(t, from, to)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHeadResponseBody_StableMarshal(t *testing.T) {
|
|
|
|
shortFrom := generateHeadResponseBody(true)
|
|
|
|
fullFrom := generateHeadResponseBody(false)
|
|
|
|
transport := new(grpc.HeadResponse_Body)
|
|
|
|
|
|
|
|
t.Run("short header non empty", func(t *testing.T) {
|
|
|
|
wire, err := shortFrom.StableMarshal(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-10-19 18:51:59 +00:00
|
|
|
err = goproto.Unmarshal(wire, transport)
|
2020-08-18 16:46:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
to := object.HeadResponseBodyFromGRPCMessage(transport)
|
|
|
|
require.Equal(t, shortFrom, to)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("full header non empty", func(t *testing.T) {
|
|
|
|
wire, err := fullFrom.StableMarshal(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-10-19 18:51:59 +00:00
|
|
|
err = goproto.Unmarshal(wire, transport)
|
2020-08-18 16:46:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
to := object.HeadResponseBodyFromGRPCMessage(transport)
|
|
|
|
require.Equal(t, fullFrom, to)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSearchRequestBody_StableMarshal(t *testing.T) {
|
|
|
|
from := generateSearchRequestBody(10, "Container ID")
|
|
|
|
transport := new(grpc.SearchRequest_Body)
|
|
|
|
|
|
|
|
t.Run("non empty", func(t *testing.T) {
|
|
|
|
wire, err := from.StableMarshal(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-10-19 18:51:59 +00:00
|
|
|
err = goproto.Unmarshal(wire, transport)
|
2020-08-18 16:46:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
to := object.SearchRequestBodyFromGRPCMessage(transport)
|
|
|
|
require.Equal(t, from, to)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSearchResponseBody_StableMarshal(t *testing.T) {
|
|
|
|
from := generateSearchResponseBody(10)
|
|
|
|
transport := new(grpc.SearchResponse_Body)
|
|
|
|
|
|
|
|
t.Run("non empty", func(t *testing.T) {
|
|
|
|
wire, err := from.StableMarshal(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-10-19 18:51:59 +00:00
|
|
|
err = goproto.Unmarshal(wire, transport)
|
2020-08-18 16:46:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
to := object.SearchResponseBodyFromGRPCMessage(transport)
|
|
|
|
require.Equal(t, from, to)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetRangeRequestBody_StableMarshal(t *testing.T) {
|
|
|
|
from := generateRangeRequestBody("Container ID", "Object ID")
|
|
|
|
transport := new(grpc.GetRangeRequest_Body)
|
|
|
|
|
|
|
|
t.Run("non empty", func(t *testing.T) {
|
|
|
|
wire, err := from.StableMarshal(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-10-19 18:51:59 +00:00
|
|
|
err = goproto.Unmarshal(wire, transport)
|
2020-08-18 16:46:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
to := object.GetRangeRequestBodyFromGRPCMessage(transport)
|
|
|
|
require.Equal(t, from, to)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetRangeResponseBody_StableMarshal(t *testing.T) {
|
|
|
|
from := generateRangeResponseBody("some data")
|
|
|
|
transport := new(grpc.GetRangeResponse_Body)
|
|
|
|
|
|
|
|
t.Run("non empty", func(t *testing.T) {
|
|
|
|
wire, err := from.StableMarshal(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-10-19 18:51:59 +00:00
|
|
|
err = goproto.Unmarshal(wire, transport)
|
2020-08-18 16:46:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
to := object.GetRangeResponseBodyFromGRPCMessage(transport)
|
|
|
|
require.Equal(t, from, to)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetRangeHashRequestBody_StableMarshal(t *testing.T) {
|
|
|
|
from := generateRangeHashRequestBody("Container ID", "Object ID", 5)
|
|
|
|
transport := new(grpc.GetRangeHashRequest_Body)
|
|
|
|
|
|
|
|
t.Run("non empty", func(t *testing.T) {
|
|
|
|
wire, err := from.StableMarshal(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-10-19 18:51:59 +00:00
|
|
|
err = goproto.Unmarshal(wire, transport)
|
2020-08-18 16:46:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
to := object.GetRangeHashRequestBodyFromGRPCMessage(transport)
|
|
|
|
require.Equal(t, from, to)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetRangeHashResponseBody_StableMarshal(t *testing.T) {
|
|
|
|
from := generateRangeHashResponseBody(5)
|
|
|
|
transport := new(grpc.GetRangeHashResponse_Body)
|
|
|
|
|
|
|
|
t.Run("non empty", func(t *testing.T) {
|
|
|
|
wire, err := from.StableMarshal(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-10-19 18:51:59 +00:00
|
|
|
err = goproto.Unmarshal(wire, transport)
|
2020-08-18 16:46:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
to := object.GetRangeHashResponseBodyFromGRPCMessage(transport)
|
|
|
|
require.Equal(t, from, to)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-08-18 15:28:02 +00:00
|
|
|
func generateOwner(id string) *refs.OwnerID {
|
|
|
|
owner := new(refs.OwnerID)
|
|
|
|
owner.SetValue([]byte(id))
|
|
|
|
|
|
|
|
return owner
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateObjectID(id string) *refs.ObjectID {
|
|
|
|
oid := new(refs.ObjectID)
|
|
|
|
oid.SetValue([]byte(id))
|
|
|
|
|
|
|
|
return oid
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateContainerID(id string) *refs.ContainerID {
|
|
|
|
cid := new(refs.ContainerID)
|
|
|
|
cid.SetValue([]byte(id))
|
|
|
|
|
|
|
|
return cid
|
|
|
|
}
|
|
|
|
|
2020-08-20 09:43:47 +00:00
|
|
|
func generateSignature(k, v string) *refs.Signature {
|
|
|
|
sig := new(refs.Signature)
|
2020-08-18 15:28:02 +00:00
|
|
|
sig.SetKey([]byte(k))
|
|
|
|
sig.SetSign([]byte(v))
|
|
|
|
|
|
|
|
return sig
|
|
|
|
}
|
|
|
|
|
2020-08-20 09:43:47 +00:00
|
|
|
func generateVersion(maj, min uint32) *refs.Version {
|
|
|
|
version := new(refs.Version)
|
2020-08-18 15:28:02 +00:00
|
|
|
version.SetMajor(maj)
|
|
|
|
version.SetMinor(min)
|
|
|
|
|
|
|
|
return version
|
|
|
|
}
|
|
|
|
|
2020-08-18 16:46:55 +00:00
|
|
|
func generateAddress(cid, oid string) *refs.Address {
|
|
|
|
addr := new(refs.Address)
|
|
|
|
addr.SetObjectID(generateObjectID(oid))
|
|
|
|
addr.SetContainerID(generateContainerID(cid))
|
|
|
|
|
|
|
|
return addr
|
|
|
|
}
|
|
|
|
|
2020-08-20 10:41:55 +00:00
|
|
|
func generateSessionToken(id string) *session.SessionToken {
|
|
|
|
lifetime := new(session.TokenLifetime)
|
2020-08-18 15:28:02 +00:00
|
|
|
lifetime.SetExp(1)
|
|
|
|
lifetime.SetNbf(2)
|
|
|
|
lifetime.SetIat(3)
|
|
|
|
|
2020-08-20 10:41:55 +00:00
|
|
|
objectCtx := new(session.ObjectSessionContext)
|
|
|
|
objectCtx.SetVerb(session.ObjectVerbPut)
|
2020-08-18 16:46:55 +00:00
|
|
|
objectCtx.SetAddress(generateAddress("Container ID", "Object ID"))
|
2020-08-18 15:28:02 +00:00
|
|
|
|
2020-08-20 10:41:55 +00:00
|
|
|
tokenBody := new(session.SessionTokenBody)
|
2020-08-18 15:28:02 +00:00
|
|
|
tokenBody.SetID([]byte(id))
|
|
|
|
tokenBody.SetOwnerID(generateOwner("Owner ID"))
|
|
|
|
tokenBody.SetSessionKey([]byte(id))
|
|
|
|
tokenBody.SetLifetime(lifetime)
|
|
|
|
tokenBody.SetContext(objectCtx)
|
|
|
|
|
2020-08-20 10:41:55 +00:00
|
|
|
sessionToken := new(session.SessionToken)
|
2020-08-18 15:28:02 +00:00
|
|
|
sessionToken.SetBody(tokenBody)
|
|
|
|
sessionToken.SetSignature(generateSignature("public key", id))
|
|
|
|
|
|
|
|
return sessionToken
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateShortHeader(id string) *object.ShortHeader {
|
|
|
|
hdr := new(object.ShortHeader)
|
|
|
|
hdr.SetOwnerID(generateOwner(id))
|
|
|
|
hdr.SetVersion(generateVersion(2, 0))
|
|
|
|
hdr.SetCreationEpoch(200)
|
|
|
|
hdr.SetObjectType(object.TypeRegular)
|
|
|
|
hdr.SetPayloadLength(10)
|
|
|
|
|
|
|
|
return hdr
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateAttribute(k, v string) *object.Attribute {
|
|
|
|
attr := new(object.Attribute)
|
|
|
|
attr.SetValue(v)
|
|
|
|
attr.SetKey(k)
|
|
|
|
|
|
|
|
return attr
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateSplit(sig string) *object.SplitHeader {
|
|
|
|
split := new(object.SplitHeader)
|
|
|
|
split.SetChildren([]*refs.ObjectID{
|
|
|
|
generateObjectID("Child 1"),
|
|
|
|
generateObjectID("Child 2"),
|
|
|
|
})
|
|
|
|
split.SetParent(generateObjectID("Parent"))
|
|
|
|
split.SetParentSignature(generateSignature("Key", sig))
|
|
|
|
split.SetPrevious(generateObjectID("Previous"))
|
|
|
|
|
|
|
|
return split
|
|
|
|
}
|
|
|
|
|
2020-08-20 08:48:35 +00:00
|
|
|
func generateChecksum(data string) *refs.Checksum {
|
|
|
|
checksum := new(refs.Checksum)
|
|
|
|
checksum.SetType(refs.TillichZemor)
|
|
|
|
checksum.SetSum([]byte(data))
|
|
|
|
|
|
|
|
return checksum
|
|
|
|
}
|
|
|
|
|
2020-08-18 15:28:02 +00:00
|
|
|
func generateHeader(ln uint64) *object.Header {
|
|
|
|
hdr := new(object.Header)
|
|
|
|
hdr.SetPayloadLength(ln)
|
|
|
|
hdr.SetCreationEpoch(ln / 2)
|
|
|
|
hdr.SetVersion(generateVersion(2, 0))
|
|
|
|
hdr.SetOwnerID(generateOwner("Owner ID"))
|
|
|
|
hdr.SetContainerID(generateContainerID("Contanier ID"))
|
|
|
|
hdr.SetAttributes([]*object.Attribute{
|
|
|
|
generateAttribute("One", "Two"),
|
|
|
|
generateAttribute("Three", "Four"),
|
|
|
|
})
|
2020-08-20 08:48:35 +00:00
|
|
|
hdr.SetHomomorphicHash(generateChecksum("Homomorphic Hash"))
|
2020-08-18 15:28:02 +00:00
|
|
|
hdr.SetObjectType(object.TypeRegular)
|
2020-08-20 08:48:35 +00:00
|
|
|
hdr.SetPayloadHash(generateChecksum("Payload Hash"))
|
2020-09-11 14:35:07 +00:00
|
|
|
hdr.SetSessionToken(generateSessionToken(strconv.Itoa(int(ln))))
|
2020-08-18 15:28:02 +00:00
|
|
|
|
|
|
|
return hdr
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateObject(data string) *object.Object {
|
|
|
|
insideHeader := generateHeader(100)
|
|
|
|
split := generateSplit("Split")
|
|
|
|
split.SetParentHeader(insideHeader)
|
|
|
|
|
|
|
|
outsideHeader := generateHeader(500)
|
|
|
|
outsideHeader.SetSplit(split)
|
|
|
|
|
|
|
|
obj := new(object.Object)
|
|
|
|
obj.SetSignature(generateSignature("Public Key", "Signature"))
|
|
|
|
obj.SetObjectID(generateObjectID("Object ID"))
|
|
|
|
obj.SetPayload([]byte(data))
|
|
|
|
obj.SetHeader(outsideHeader)
|
|
|
|
|
|
|
|
return obj
|
|
|
|
}
|
2020-08-18 16:46:55 +00:00
|
|
|
|
|
|
|
func generateGetRequestBody(cid, oid string) *object.GetRequestBody {
|
|
|
|
req := new(object.GetRequestBody)
|
|
|
|
req.SetAddress(generateAddress(cid, oid))
|
|
|
|
req.SetRaw(true)
|
|
|
|
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateGetResponseBody(flag bool) *object.GetResponseBody {
|
|
|
|
resp := new(object.GetResponseBody)
|
|
|
|
var part object.GetObjectPart
|
|
|
|
|
|
|
|
if flag {
|
|
|
|
init := new(object.GetObjectPartInit)
|
|
|
|
init.SetObjectID(generateObjectID("Object ID"))
|
|
|
|
init.SetSignature(generateSignature("Key", "Signature"))
|
|
|
|
init.SetHeader(generateHeader(10))
|
|
|
|
part = init
|
|
|
|
} else {
|
|
|
|
chunk := new(object.GetObjectPartChunk)
|
|
|
|
chunk.SetChunk([]byte("Some data chunk"))
|
|
|
|
part = chunk
|
|
|
|
}
|
|
|
|
resp.SetObjectPart(part)
|
|
|
|
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
func generatePutRequestBody(flag bool) *object.PutRequestBody {
|
|
|
|
req := new(object.PutRequestBody)
|
|
|
|
var part object.PutObjectPart
|
|
|
|
|
|
|
|
if flag {
|
|
|
|
init := new(object.PutObjectPartInit)
|
|
|
|
init.SetObjectID(generateObjectID("Object ID"))
|
|
|
|
init.SetSignature(generateSignature("Key", "Signature"))
|
|
|
|
init.SetHeader(generateHeader(10))
|
|
|
|
init.SetCopiesNumber(1)
|
|
|
|
part = init
|
|
|
|
} else {
|
|
|
|
chunk := new(object.PutObjectPartChunk)
|
|
|
|
chunk.SetChunk([]byte("Some data chunk"))
|
|
|
|
part = chunk
|
|
|
|
}
|
|
|
|
req.SetObjectPart(part)
|
|
|
|
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
|
|
|
func generatePutResponseBody(oid string) *object.PutResponseBody {
|
|
|
|
resp := new(object.PutResponseBody)
|
|
|
|
resp.SetObjectID(generateObjectID(oid))
|
|
|
|
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateDeleteRequestBody(cid, oid string) *object.DeleteRequestBody {
|
|
|
|
req := new(object.DeleteRequestBody)
|
|
|
|
req.SetAddress(generateAddress(cid, oid))
|
|
|
|
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateDeleteResponseBody() *object.DeleteResponseBody {
|
|
|
|
return new(object.DeleteResponseBody)
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateHeadRequestBody(cid, oid string) *object.HeadRequestBody {
|
|
|
|
req := new(object.HeadRequestBody)
|
|
|
|
req.SetAddress(generateAddress(cid, oid))
|
|
|
|
req.SetRaw(true)
|
|
|
|
req.SetMainOnly(true)
|
|
|
|
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateHeadResponseBody(flag bool) *object.HeadResponseBody {
|
|
|
|
req := new(object.HeadResponseBody)
|
|
|
|
var part object.GetHeaderPart
|
|
|
|
|
|
|
|
if flag {
|
|
|
|
short := new(object.GetHeaderPartShort)
|
|
|
|
short.SetShortHeader(generateShortHeader("short id"))
|
|
|
|
part = short
|
|
|
|
} else {
|
2020-09-08 10:39:13 +00:00
|
|
|
hdrWithSig := new(object.HeaderWithSignature)
|
|
|
|
hdrWithSig.SetHeader(generateHeader(30))
|
|
|
|
hdrWithSig.SetSignature(generateSignature("sig", "key"))
|
|
|
|
|
2020-08-18 16:46:55 +00:00
|
|
|
full := new(object.GetHeaderPartFull)
|
2020-09-08 10:39:13 +00:00
|
|
|
full.SetHeaderWithSignature(hdrWithSig)
|
2020-08-18 16:46:55 +00:00
|
|
|
part = full
|
|
|
|
}
|
|
|
|
|
|
|
|
req.SetHeaderPart(part)
|
|
|
|
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateFilter(k, v string) *object.SearchFilter {
|
|
|
|
f := new(object.SearchFilter)
|
2020-10-27 13:55:19 +00:00
|
|
|
f.SetKey(k)
|
2020-08-18 16:46:55 +00:00
|
|
|
f.SetValue(v)
|
|
|
|
f.SetMatchType(object.MatchStringEqual)
|
|
|
|
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateSearchRequestBody(n int, id string) *object.SearchRequestBody {
|
|
|
|
req := new(object.SearchRequestBody)
|
|
|
|
req.SetContainerID(generateContainerID(id))
|
|
|
|
req.SetVersion(1)
|
|
|
|
|
|
|
|
ff := make([]*object.SearchFilter, n)
|
|
|
|
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
ff[i] = generateFilter("Some Key", fmt.Sprintf("Value %d", i+1))
|
|
|
|
}
|
|
|
|
req.SetFilters(ff)
|
|
|
|
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateSearchResponseBody(n int) *object.SearchResponseBody {
|
|
|
|
resp := new(object.SearchResponseBody)
|
|
|
|
list := make([]*refs.ObjectID, n)
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
list[i] = generateObjectID(fmt.Sprintf("Object ID %d", i+1))
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.SetIDList(list)
|
|
|
|
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateRange(off, ln uint64) *object.Range {
|
|
|
|
r := new(object.Range)
|
|
|
|
r.SetOffset(off)
|
|
|
|
r.SetLength(ln)
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateRangeRequestBody(cid, oid string) *object.GetRangeRequestBody {
|
|
|
|
req := new(object.GetRangeRequestBody)
|
|
|
|
req.SetAddress(generateAddress(cid, oid))
|
|
|
|
req.SetRange(generateRange(10, 20))
|
|
|
|
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateRangeResponseBody(data string) *object.GetRangeResponseBody {
|
|
|
|
resp := new(object.GetRangeResponseBody)
|
|
|
|
resp.SetChunk([]byte(data))
|
|
|
|
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateRangeHashRequestBody(cid, oid string, n int) *object.GetRangeHashRequestBody {
|
|
|
|
req := new(object.GetRangeHashRequestBody)
|
|
|
|
req.SetAddress(generateAddress(cid, oid))
|
|
|
|
|
|
|
|
rngs := make([]*object.Range, n)
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
rngs[i] = generateRange(100, 200+uint64(n))
|
|
|
|
}
|
|
|
|
|
|
|
|
req.SetRanges(rngs)
|
|
|
|
req.SetSalt([]byte("xor salt"))
|
2020-08-20 08:48:35 +00:00
|
|
|
req.SetType(refs.TillichZemor)
|
2020-08-18 16:46:55 +00:00
|
|
|
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateRangeHashResponseBody(n int) *object.GetRangeHashResponseBody {
|
|
|
|
resp := new(object.GetRangeHashResponseBody)
|
|
|
|
|
|
|
|
list := make([][]byte, n)
|
|
|
|
for i := 0; i < n; i++ {
|
2020-09-11 14:35:07 +00:00
|
|
|
list[i] = []byte("Some homomorphic hash data" + strconv.Itoa(n))
|
2020-08-18 16:46:55 +00:00
|
|
|
}
|
|
|
|
|
2020-08-20 08:48:35 +00:00
|
|
|
resp.SetType(refs.TillichZemor)
|
2020-08-18 16:46:55 +00:00
|
|
|
resp.SetHashList(list)
|
|
|
|
|
|
|
|
return resp
|
|
|
|
}
|
2020-09-02 11:46:43 +00:00
|
|
|
|
|
|
|
func TestObject_StableUnmarshal(t *testing.T) {
|
|
|
|
obj := generateObject("some data")
|
|
|
|
|
|
|
|
data, err := obj.StableMarshal(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
obj2 := new(object.Object)
|
|
|
|
require.NoError(t, obj2.StableUnmarshal(data))
|
|
|
|
|
|
|
|
require.Equal(t, obj, obj2)
|
|
|
|
}
|