using FrostFS.Refs; using FrostFS.SDK.ClientV2; using FrostFS.SDK.ClientV2.Interfaces; using FrostFS.SDK.ClientV2.Mappers.GRPC; using FrostFS.SDK.ClientV2.Parameters; using FrostFS.SDK.Cryptography; using FrostFS.SDK.ModelsV2; using FrostFS.SDK.ModelsV2.Enums; using FrostFS.SDK.ModelsV2.Netmap; using Google.Protobuf; using Microsoft.Extensions.Options; using System.Security.Cryptography; using System.Text; namespace FrostFS.SDK.Tests; public abstract class ObjectTestsBase { protected static readonly string key = "KwHDAJ66o8FoLBjVbjP2sWBmgBMGjt7Vv4boA7xQrBoAYBE397Aq"; protected IOptions Settings { get; set; } protected ContainerId ContainerId { get; set; } protected NetworkMocker NetworkMocker { get; set; } = new NetworkMocker(key); protected SessionMocker SessionMocker { get; set; } = new SessionMocker(key); protected ContainerMocker ContainerMocker { get; set; } = new ContainerMocker(key); protected ObjectMocker Mocker { get; set; } protected ObjectTestsBase() { var ecdsaKey = key.LoadWif(); Settings = Options.Create(new SingleOwnerClientSettings { Key = key, Host = "http://localhost:8080" }); Mocker = new ObjectMocker(key) { PlacementPolicy = new PlacementPolicy(true, new Replica(1)), Version = new ModelsV2.Version(2, 13), ContainerGuid = Guid.NewGuid() }; ContainerId = new ContainerId(Base58.Encode(Mocker.ContainerGuid.ToBytes())); Mocker.ObjectHeader = new(ContainerId, ModelsV2.Enums.ObjectType.Regular, [new ObjectAttribute("k", "v")]) { Version = new ModelsV2.Version(2, 13), OwnerId = OwnerId.FromKey(ecdsaKey) }; } protected IFrostFSClient GetClient() { return Client.GetTestInstance( Settings, null, NetworkMocker.GetMock().Object, SessionMocker.GetMock().Object, ContainerMocker.GetMock().Object, Mocker.GetMock().Object); } } public class ObjectTest : ObjectTestsBase { [Fact] public async void GetObjectTest() { var client = GetClient(); var ecdsaKey = key.LoadWif(); var ctx = new Context { Key = ecdsaKey, OwnerId = OwnerId.FromKey(ecdsaKey), Version = new ModelsV2.Version(2, 13) }; var objectId = client.CalculateObjectId(Mocker.ObjectHeader!, ctx); var result = await client.GetObjectAsync(new PrmObjectGet(ContainerId, objectId) { Context = ctx }); Assert.NotNull(result); Assert.Equal(Mocker.ObjectHeader!.ContainerId.Value, result.Header.ContainerId.Value); Assert.Equal(Mocker.ObjectHeader!.OwnerId!.Value, result.Header.OwnerId!.Value); Assert.Equal(Mocker.ObjectHeader.PayloadLength, result.Header.PayloadLength); Assert.Single(result.Header.Attributes); Assert.Equal(Mocker.ObjectHeader.Attributes[0].Key, result.Header.Attributes[0].Key); Assert.Equal(Mocker.ObjectHeader.Attributes[0].Value,result.Header.Attributes[0].Value); } [Fact] public async void PutObjectTest() { Mocker.ResultObjectIds = new([SHA256.HashData([])]); Random rnd = new(); var bytes = new byte[1024]; rnd.NextBytes(bytes); var param = new PrmObjectPut { Header = Mocker.ObjectHeader, Payload = new MemoryStream(bytes), ClientCut = false }; var result = await GetClient().PutObjectAsync(param); var sentMessages = Mocker.ClientStreamWriter!.Messages; var body1 = sentMessages.ElementAt(0).GetBody() as Object.PutRequest.Types.Body; var body2 = sentMessages.ElementAt(1).GetBody() as Object.PutRequest.Types.Body; Assert.NotNull(result); Assert.Equal(Mocker.ResultObjectIds.First(), result.ToHash()); Assert.True(Mocker.ClientStreamWriter.CompletedTask); Assert.Equal(0, body1!.Chunk.Length); Assert.Equal(Object.PutRequest.Types.Body.ObjectPartOneofCase.Init, body1!.ObjectPartCase); Assert.Equal(1024, body2!.Chunk.Length); Assert.Equal(Object.PutRequest.Types.Body.ObjectPartOneofCase.Chunk, body2!.ObjectPartCase); } [Fact] public async void ClientCutTest() { NetworkMocker.Parameters = new Dictionary() { { "MaxObjectSize", [0x0, 0xa] } }; var blockSize = 2560; byte[] bytes = File.ReadAllBytes(@".\..\..\..\TestData\cat.jpg"); var fileLength = bytes.Length; var param = new PrmObjectPut { Header = Mocker.ObjectHeader, Payload = new MemoryStream(bytes), BufferMaxSize = 1024, ClientCut = true }; Random rnd = new(); List objIds = new([new byte[32], new byte[32], new byte[32]]); rnd.NextBytes(objIds.ElementAt(0)); rnd.NextBytes(objIds.ElementAt(1)); rnd.NextBytes(objIds.ElementAt(2)); Mocker.ResultObjectIds = objIds; var result = await GetClient().PutObjectAsync(param); var singleObjects = Mocker.PutSingleRequests.ToArray(); var streamObjects = Mocker.ClientStreamWriter.Messages.ToArray(); Assert.Single(singleObjects); Assert.Equal(11, streamObjects.Length); var bodies = streamObjects.Select(o => ((Object.PutRequest)o).Body).ToArray(); Assert.Equal(3, bodies.Count(b => b.Init != null)); Assert.Equal(5, bodies.Count(b => b.Chunk.Length == 1024)); Assert.Equal(596, ((Object.PutRequest)streamObjects[10]).Body.Chunk.Length); var linkObject = singleObjects[0].Body.Object; var header1 = bodies[0].Init.Header; var header2 = bodies[4].Init.Header; var header3 = bodies[8].Init.Header; var payload1 = bodies[1].Chunk .Concat(bodies[2].Chunk) .Concat(bodies[3].Chunk) .ToArray(); var payload2 = bodies[5].Chunk .Concat(bodies[6].Chunk) .Concat(bodies[7].Chunk) .ToArray(); var payload3 = bodies[9].Chunk .Concat(bodies[10].Chunk) .ToArray(); Assert.NotNull(header1.Split.SplitId); Assert.Null(header1.Split.Previous); Assert.Equal(bytes[..blockSize], payload1); Assert.True(header1.Attributes.Count == 0); Assert.Equal(header1.Split.SplitId, header2.Split.SplitId); Assert.Equal(objIds.ElementAt(0), header2.Split.Previous.Value); Assert.Equal(bytes[blockSize..(blockSize * 2)], payload2); Assert.True(header2.Attributes.Count == 0); // last part Assert.NotNull(header3.Split.Parent); Assert.NotNull(header3.Split.ParentHeader); Assert.NotNull(header3.Split.ParentSignature); Assert.Equal(header2.Split.SplitId, header3.Split.SplitId); Assert.Equal(bytes[((fileLength / blockSize) * blockSize)..fileLength], payload3); Assert.True(header3.Attributes.Count == 0); //link object Assert.Equal(header3.Split.Parent, linkObject.Header.Split.Parent); Assert.Equal(header3.Split.ParentHeader, linkObject.Header.Split.ParentHeader); Assert.Equal(header3.Split.SplitId, linkObject.Header.Split.SplitId); Assert.Equal(0, (int)linkObject.Header.PayloadLength); Assert.True(header3.Attributes.Count == 0); Assert.Single(linkObject.Header.Split.ParentHeader.Attributes); Assert.Equal("k", linkObject.Header.Split.ParentHeader.Attributes[0].Key); Assert.Equal("v", linkObject.Header.Split.ParentHeader.Attributes[0].Value); var modelObjId = ObjectId.FromHash(linkObject.Header.Split.Parent.Value.ToByteArray()); Assert.Equal(result.Value, modelObjId.ToString()); } [Fact] public async void DeleteObject() { Mocker.ObjectId = new ObjectID { Value = ByteString.CopyFrom(SHA256.HashData(Encoding.UTF8.GetBytes("test"))) }.ToModel(); await GetClient().DeleteObjectAsync(new PrmObjectDelete(ContainerId, Mocker.ObjectId)); var request = Mocker.DeleteRequests.FirstOrDefault(); Assert.NotNull(request); Assert.Equal(ContainerId.ToMessage().Value, request.Body.Address.ContainerId.Value); Assert.Equal(Mocker.ObjectId.ToMessage().Value, request.Body.Address.ObjectId.Value); } [Fact] public async void GetHeaderTest() { Mocker.ObjectId = new ObjectID { Value = ByteString.CopyFrom(SHA256.HashData(Encoding.UTF8.GetBytes("test"))) }.ToModel(); var response = await GetClient().GetObjectHeadAsync(new PrmObjectHeadGet(ContainerId, Mocker.ObjectId)); var request = Mocker.HeadRequests.FirstOrDefault(); Assert.NotNull(request); Assert.Equal(ContainerId.ToMessage(), request.Body.Address.ContainerId); Assert.Equal(Mocker.ObjectId.ToMessage(), request.Body.Address.ObjectId); Assert.NotNull(response); Assert.Equal(ContainerId.Value, response.ContainerId.Value); Assert.Equal(Mocker.ObjectHeader!.OwnerId!.Value, response.OwnerId!.Value); Assert.Equal(Mocker.ObjectHeader!.Version!.ToString(), response.Version!.ToString()); Assert.Equal(Mocker.HeadResponse!.PayloadLength, response.PayloadLength); Assert.Equal(ObjectType.Regular, response.ObjectType); Assert.Single(response.Attributes); Assert.Equal(Mocker.HeadResponse.Attributes[0].Key, response.Attributes.First().Key); Assert.Equal(Mocker.HeadResponse.Attributes[0].Value, response.Attributes.First().Value); Assert.Null(response.Split); } }