using System.Collections.ObjectModel; using System.Diagnostics.CodeAnalysis; using System.Security.Cryptography; using System.Text; using FrostFS.Refs; using FrostFS.SDK.Client; using FrostFS.SDK.Client.Mappers.GRPC; using Google.Protobuf; namespace FrostFS.SDK.Tests.Unit; [SuppressMessage("Reliability", "CA2007:Consider calling ConfigureAwait on the awaited task", Justification = "Default Value is correct for tests")] [SuppressMessage("Security", "CA5394:Do not use insecure randomness", Justification = "No secure purpose")] public class ObjectTest : ObjectTestsBase { [Fact] public async void PutObjectTest() { Mocker.ResultObjectIds!.Add(SHA256.HashData([])); Random rnd = new(); var bytes = new byte[1024]; rnd.NextBytes(bytes); var param = new PrmObjectPut( Mocker.ObjectHeader, payload: new MemoryStream(bytes), clientCut: false); var result = await GetClient().PutObjectAsync(param, default); 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.Add("MaxObjectSize", [0x0, 0xa]); var blockSize = 2560; byte[] bytes = File.ReadAllBytes(@".\..\..\..\cat.jpg"); var fileLength = bytes.Length; var param = new PrmObjectPut( Mocker.ObjectHeader, payload: new MemoryStream(bytes), bufferMaxSize: 1024, clientCut: true); Random rnd = new(); Collection 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)); foreach (var objId in objIds) Mocker.ResultObjectIds!.Add(objId); var result = await GetClient().PutObjectAsync(param, default); var singleObjects = Mocker.PutSingleRequests.ToArray(); Assert.NotNull(Mocker.ClientStreamWriter?.Messages); 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 = FrostFsObjectId.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), default); 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 res = await GetClient().GetObjectHeadAsync(new PrmObjectHeadGet(ContainerId, Mocker.ObjectId), default); var objHeader = res.HeaderInfo; Assert.NotNull(objHeader); 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(objHeader); Assert.Equal(ContainerId.GetValue(), objHeader.ContainerId.GetValue()); Assert.Equal(Mocker.ObjectHeader!.OwnerId!.Value, objHeader.OwnerId!.Value); Assert.Equal(Mocker.ObjectHeader!.Version!.ToString(), objHeader.Version!.ToString()); Assert.Equal(Mocker.HeadResponse!.PayloadLength, objHeader.PayloadLength); Assert.Equal(FrostFsObjectType.Regular, objHeader.ObjectType); Assert.NotNull(objHeader.Attributes); Assert.Single(objHeader.Attributes); Assert.Equal(Mocker.HeadResponse.Attributes[0].Key, objHeader.Attributes.First().Key); Assert.Equal(Mocker.HeadResponse.Attributes[0].Value, objHeader.Attributes.First().Value); Assert.Null(objHeader.Split); } [Fact] public async void GetRangeTest() { Mocker.ResultObjectIds!.Add(SHA256.HashData([])); Random rnd = new(); var bytes = new byte[1024]; rnd.NextBytes(bytes); Mocker.RangeResponse = bytes; Mocker.ObjectId = new ObjectID { Value = ByteString.CopyFrom(SHA256.HashData(Encoding.UTF8.GetBytes("test"))) }.ToModel(); var param = new PrmRangeGet(ContainerId, Mocker.ObjectId, new FrostFsRange(100, (ulong)Mocker.RangeResponse.Length)); var result = await GetClient().GetRangeAsync(param, default); Assert.NotNull(Mocker.GetRangeRequest); Assert.Equal(param.Range.Offset, Mocker.GetRangeRequest.Body.Range.Offset); Assert.Equal(param.Range.Length, Mocker.GetRangeRequest.Body.Range.Length); Assert.NotNull(result); var chunk = await result.ReadChunk(); var chunkBytes = chunk.Value.Span.ToArray(); Assert.Equal(chunkBytes.Length, Mocker.RangeResponse.Length); Assert.Equal(SHA256.HashData(bytes), SHA256.HashData(Mocker.RangeResponse)); } [Fact] public async void GetRangeHashTest() { Mocker.ResultObjectIds!.Add(SHA256.HashData([])); Random rnd = new(); var bytes = new byte[1024]; rnd.NextBytes(bytes); var salt = new byte[32]; rnd.NextBytes(salt); var hash = new byte[32]; rnd.NextBytes(hash); Mocker.RangeResponse = bytes; var len = (ulong)bytes.Length; Mocker.RangeHashResponses.Add(ByteString.CopyFrom(hash)); Mocker.ObjectId = new ObjectID { Value = ByteString.CopyFrom(SHA256.HashData(Encoding.UTF8.GetBytes("test"))) }.ToModel(); var param = new PrmRangeHashGet(ContainerId, Mocker.ObjectId, [new FrostFsRange(100, len)], salt); var result = await GetClient().GetRangeHashAsync(param, default); Assert.NotNull(Mocker.GetRangeHashRequest); Assert.Equal(param.Ranges[0].Offset, Mocker.GetRangeHashRequest.Body.Ranges[0].Offset); Assert.Equal(param.Ranges[0].Length, Mocker.GetRangeHashRequest.Body.Ranges[0].Length); Assert.NotNull(result); Assert.Single(result); Assert.Equal(SHA256.HashData(hash), SHA256.HashData(result.First().ToArray())); } [Fact] public async void PatchTest() { Mocker.ObjectId = new ObjectID { Value = ByteString.CopyFrom(SHA256.HashData(Encoding.UTF8.GetBytes("test"))) }.ToModel(); var address = new FrostFsAddress(ContainerId, Mocker.ObjectId); Mocker.ResultObjectIds!.Add(SHA256.HashData([])); Random rnd = new(); var patch = new byte[32]; rnd.NextBytes(patch); var range = new FrostFsRange(8, (ulong)patch.Length); var param = new PrmObjectPatch( address, payload: new MemoryStream(patch), maxChunkLength: 32, range: range); var result = await GetClient().PatchObjectAsync(param, default); Assert.NotNull(result); Assert.NotNull(result.Value); Assert.NotNull(Mocker.PatchStreamWriter); Assert.Single(Mocker.PatchStreamWriter.Messages); var sentMessages = Mocker.PatchStreamWriter!.Messages; var body = sentMessages.First().GetBody() as Object.PatchRequest.Types.Body; Assert.NotNull(body); Assert.True(Mocker.PatchStreamWriter.CompletedTask); Assert.Equal(address.ContainerId, body.Address.ContainerId); Assert.Equal(address.ObjectId, body.Address.ObjectId); Assert.Equal(32, body.Patch.Chunk.Length); Assert.Equal(SHA256.HashData(patch), SHA256.HashData(body.Patch.Chunk.ToArray())); } }