You've already forked UnrealEngineUWP
mirror of
https://github.com/izzy2lost/UnrealEngineUWP.git
synced 2026-03-26 18:15:20 -07:00
Also fixed up inconsistent whitespace. #jira UE-158872 #preflight https://horde.devtools.epicgames.com/job/62c80bc6f671e8d2fac950f7 [CL 21004958 by Joakim Lindqvist in ue5-main branch]
132 lines
3.5 KiB
C#
132 lines
3.5 KiB
C#
// Copyright Epic Games, Inc. All Rights Reserved.
|
|
|
|
using System;
|
|
using System.IO;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using EpicGames.Core;
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
|
|
namespace EpicGames.Serialization.Tests
|
|
{
|
|
class TestObject
|
|
{
|
|
[CbField]
|
|
public CbBinaryAttachment BinaryAttachment { get; set; }
|
|
|
|
[CbField]
|
|
public CbObjectAttachment ObjectAttachment { get; set; }
|
|
}
|
|
|
|
class SimpleObject
|
|
{
|
|
[CbField] public int A { get; set; } = 0;
|
|
|
|
[CbField]
|
|
public string B { get; set; } = String.Empty;
|
|
}
|
|
|
|
[TestClass]
|
|
public class CbPackageTests
|
|
{
|
|
[TestMethod]
|
|
public async Task RoundTrip()
|
|
{
|
|
TestObject root = new TestObject()
|
|
{
|
|
|
|
};
|
|
CbObject rootObject = CbSerializer.Serialize(root);
|
|
IoHash rootHash = IoHash.Compute(rootObject.GetView().Span);
|
|
|
|
CbPackageBuilder builder = new();
|
|
await builder.AddAttachment(rootHash, CbPackageAttachmentFlags.IsObject, rootObject.GetView().ToArray());
|
|
byte[] bytes = builder.ToByteArray();
|
|
|
|
await using MemoryStream ms = new MemoryStream(bytes);
|
|
CbPackageReader reader = await CbPackageReader.Create(ms);
|
|
|
|
Assert.AreEqual(rootHash, reader.RootHash);
|
|
Assert.AreEqual(rootObject, reader.RootObject);
|
|
|
|
await foreach ((CbPackageAttachmentEntry, byte[]) _ in reader.IterateAttachments())
|
|
{
|
|
Assert.Fail("No Attachments expected");
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task RoundTripComplex()
|
|
{
|
|
byte[] blob = Encoding.UTF8.GetBytes("This is blob contents");
|
|
IoHash blobHash = IoHash.Compute(blob);
|
|
|
|
SimpleObject simple = new SimpleObject();
|
|
CbObject simpleObject = CbSerializer.Serialize(simple);
|
|
IoHash simpleHash = IoHash.Compute(simpleObject.GetView().Span);
|
|
|
|
TestObject root = new TestObject()
|
|
{
|
|
BinaryAttachment = blobHash,
|
|
ObjectAttachment = simpleHash,
|
|
};
|
|
CbObject rootObject = CbSerializer.Serialize(root);
|
|
IoHash rootHash = IoHash.Compute(rootObject.GetView().Span);
|
|
|
|
CbPackageBuilder builder = new();
|
|
await builder.AddAttachment(rootHash, CbPackageAttachmentFlags.IsObject, rootObject.GetView().ToArray());
|
|
await builder.AddAttachment(simpleHash, CbPackageAttachmentFlags.IsObject, simpleObject.GetView().ToArray());
|
|
await builder.AddAttachment(blobHash, 0, blob);
|
|
|
|
byte[] bytes = builder.ToByteArray();
|
|
|
|
await using MemoryStream ms = new MemoryStream(bytes);
|
|
CbPackageReader reader = await CbPackageReader.Create(ms);
|
|
|
|
Assert.AreEqual(rootHash, reader.RootHash);
|
|
Assert.AreEqual(rootObject, reader.RootObject);
|
|
|
|
int countOfAttachments = 0;
|
|
await foreach ((CbPackageAttachmentEntry entry, byte[] attachmentBytes) in reader.IterateAttachments())
|
|
{
|
|
countOfAttachments++;
|
|
if (entry.AttachmentHash == blobHash)
|
|
{
|
|
Assert.AreEqual((ulong)blob.Length, entry.PayloadSize);
|
|
CollectionAssert.AreEqual(blob, attachmentBytes);
|
|
continue;
|
|
}
|
|
|
|
if (entry.AttachmentHash == simpleHash)
|
|
{
|
|
Assert.AreEqual((ulong)simpleObject.GetView().Length, entry.PayloadSize);
|
|
|
|
CbObject roundTrippedObject = new CbObject(attachmentBytes);
|
|
Assert.AreEqual(simpleObject, roundTrippedObject);
|
|
continue;
|
|
}
|
|
|
|
Assert.Fail($"Unknown attachment {entry.AttachmentHash}");
|
|
}
|
|
Assert.AreEqual(2, countOfAttachments);
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task ReadNoAttachments()
|
|
{
|
|
CbPackageBuilder packageBuilder = new CbPackageBuilder();
|
|
byte[] buf = packageBuilder.ToByteArray();
|
|
await using MemoryStream ms = new MemoryStream(buf);
|
|
|
|
try
|
|
{
|
|
await CbPackageReader.Create(ms);
|
|
Assert.Fail("Exception should be thrown");
|
|
}
|
|
catch (Exception)
|
|
{
|
|
}
|
|
}
|
|
}
|
|
}
|