public static void CreateFrames() { // Set up the non-empty layer data we'll use. Bond.IBonded goodLayerObject = new Bond.Bonded<Dummy>(dummyObject); var outputBuffer = new OutputBuffer(); var compactWriter = new CompactBinaryWriter<OutputBuffer>(outputBuffer); compactWriter.WriteVersion(); goodLayerObject.Serialize(compactWriter); goodLayerData = outputBuffer.Data; // Good frames, from which we can pull good framelets to build bad frames. goodRequestFrame = EpoxyConnection.MessageToFrame( GoodRequestId, GoodService, GoodMethod, EpoxyMessageType.REQUEST, meaninglessPayload, null, LoggerTests.BlackHole); goodRequestLayerDataFrame = EpoxyConnection.MessageToFrame( GoodRequestId, GoodService, GoodMethod, EpoxyMessageType.REQUEST, meaninglessPayload, goodLayerObject, LoggerTests.BlackHole); goodResponseFrame = EpoxyConnection.MessageToFrame( GoodResponseId, GoodService, GoodMethod, EpoxyMessageType.RESPONSE, meaninglessPayload, null, LoggerTests.BlackHole); goodErrorResponseFrame = EpoxyConnection.MessageToFrame( GoodResponseId, GoodService, GoodMethod, EpoxyMessageType.RESPONSE, meaninglessError, null, LoggerTests.BlackHole); goodEventFrame = EpoxyConnection.MessageToFrame( GoodRequestId, GoodService, GoodMethod, EpoxyMessageType.EVENT, meaninglessPayload, null, LoggerTests.BlackHole); configFrame = EpoxyConnection.MakeConfigFrame(LoggerTests.BlackHole); protocolErrorFrame = EpoxyConnection.MakeProtocolErrorFrame(MeaninglessErrorCode, null, LoggerTests.BlackHole); var goodFrameletCount = goodRequestFrame.Count; // Bad frames made of good framelets. shortRequestFrame = new Frame(goodFrameletCount - 1, LoggerTests.BlackHole); for (var i = 0; i < goodFrameletCount - 1; i++) { shortRequestFrame.Add(goodRequestFrame.Framelets[i]); } doubleHeadersRequestFrame = new Frame(goodFrameletCount + 1, LoggerTests.BlackHole); doubleHeadersRequestFrame.Add(goodRequestFrame.Framelets[0]); for (var i = 0; i < goodFrameletCount; i++) { doubleHeadersRequestFrame.Add(goodRequestFrame.Framelets[i]); } headersConfigRequestFrame = new Frame(2, LoggerTests.BlackHole); headersConfigRequestFrame.Add(goodRequestFrame.Framelets[0]); headersConfigRequestFrame.Add(configFrame.Framelets[0]); doublePayloadRequestFrame = new Frame(goodFrameletCount + 1, LoggerTests.BlackHole); for (var i = 0; i < goodFrameletCount; i++) { doublePayloadRequestFrame.Add(goodRequestFrame.Framelets[i]); } doublePayloadRequestFrame.Add(goodRequestFrame.Framelets[goodFrameletCount - 1]); backwardsRequestFrame = new Frame(goodFrameletCount, LoggerTests.BlackHole); foreach (var framelet in goodRequestFrame.Framelets.Reverse()) { backwardsRequestFrame.Add(framelet); } doubleProtocolErrorFrame = EpoxyConnection.MakeProtocolErrorFrame(MeaninglessErrorCode, null, LoggerTests.BlackHole); doubleProtocolErrorFrame.Add(doubleProtocolErrorFrame.Framelets[0]); configFrameExtra = EpoxyConnection.MakeConfigFrame(LoggerTests.BlackHole); configFrameExtra.Add(goodRequestFrame.Framelets[0]); // Bad frames made of bad framelets. var invalidConfigData = new ArraySegment<byte>(new byte[] { 0x01 }); configFrameBadConfigData = new Frame(1, LoggerTests.BlackHole); configFrameBadConfigData.Add(new Framelet(FrameletType.EpoxyConfig, invalidConfigData)); }
public void Frame_Add_FrameletsCanBeRetreived() { var AnyOtherContents = new ArraySegment<byte>(new[] {(byte)0x00}); var expectedFramelets = new[] { new Framelet(FrameletType.EpoxyConfig, AnyContents), new Framelet(FrameletType.EpoxyConfig, AnyOtherContents), new Framelet(FrameletType.LayerData, AnyContents), new Framelet(FrameletType.EpoxyConfig, AnyContents), }; var frame = new Frame(4, LoggerTests.BlackHole); frame.Add(new Framelet(FrameletType.EpoxyConfig, AnyContents)); frame.Add(new Framelet(FrameletType.EpoxyConfig, AnyOtherContents)); frame.Add(new Framelet(FrameletType.LayerData, AnyContents)); frame.Add(new Framelet(FrameletType.EpoxyConfig, AnyContents)); Assert.AreEqual(4, frame.Count); Assert.AreEqual(frame.Framelets.Count, frame.Count); CollectionAssert.AreEqual(expectedFramelets, frame.Framelets); }
public static async Task<Frame> ReadAsync(Stream stream, CancellationToken ct, Logger logger) { try { var frameletCount = await ReadUInt16Async(stream, ct); if (frameletCount == 0) { throw new EpoxyProtocolErrorException("Zero framelets"); } var frame = new Frame(frameletCount, logger); while (frameletCount > 0) { if (ct.IsCancellationRequested) { return null; } var frameletType = await ReadUInt16Async(stream, ct); if (!Framelet.IsKnownType(frameletType)) { throw new EpoxyProtocolErrorException("Unknown framelet type: " + frameletType); } var frameletLength = await ReadUInt32Async(stream, ct); if (frameletLength > int.MaxValue) { throw new EpoxyProtocolErrorException("Framelet too big: " + frameletLength); } byte[] frameletContents = await ReadBufferAsync(stream, unchecked((int)frameletLength), ct); frame.Add(new Framelet((FrameletType)frameletType, new ArraySegment<byte>(frameletContents))); --frameletCount; } return frame; } catch (Exception ex) when (ex is OperationCanceledException || ex is EndOfStreamException || ex is ObjectDisposedException) { return null; } }
public async Task Frame_RoundTrip_Works() { var expectedFramelets = new[] { new Framelet(FrameletType.EpoxyConfig, AnyContents), new Framelet(FrameletType.LayerData, AnyContents), new Framelet(FrameletType.EpoxyConfig, AnyContents), }; var frame = new Frame(LoggerTests.BlackHole); foreach (var framelet in expectedFramelets) { frame.Add(framelet); } var memStream = new MemoryStream(); await frame.WriteAsync(memStream); memStream.Seek(0, SeekOrigin.Begin); var resultFrame = await Frame.ReadAsync(memStream, CancellationToken.None, LoggerTests.BlackHole); CollectionAssert.AreEqual(expectedFramelets, resultFrame.Framelets, DeepFrameletComparer.Instance); }
public async Task Frame_WriteAsync_OneFramelet_ContentsExpected() { var frame = new Frame(LoggerTests.BlackHole); frame.Add(new Framelet(FrameletType.EpoxyConfig, AnyContents)); var memStream = new MemoryStream(); await frame.WriteAsync(memStream); var expectedBytes = new[] { 0x01, 0x00, // frame count 0x43, 0x47, // EpoxyConfig framelet type 0x04, 0x00, 0x00, 0x00, // framelet length 0x62, 0x6F, 0x6E, 0x64 // AnyContents bytes }; CollectionAssert.AreEqual(expectedBytes, memStream.ToArray()); }
public void Frame_Add_AddMoreThanInt32TotalSize_Throws() { var largeContents = new ArraySegment<byte>(new byte[2 * 65535]); int numFramesToAdd = Int32.MaxValue/largeContents.Count; var frame = new Frame(numFramesToAdd, LoggerTests.BlackHole); for (int i = 0; i < numFramesToAdd - 1; ++i) { frame.Add(new Framelet(FrameletType.LayerData, largeContents)); } Assert.That( () => frame.Add(new Framelet(FrameletType.LayerData, largeContents)), Throws.InvalidOperationException.With.Message.ContainsSubstring("Exceeded maximum size of frame")); }
public void Frame_Add_AddMoreThanUInt16Framelets_Throws() { var frame = new Frame((int)UInt16.MaxValue + 1, LoggerTests.BlackHole); for (int i = 0; i < UInt16.MaxValue; ++i) { frame.Add(new Framelet(FrameletType.LayerData, AnyContents)); } Assert.That( () => frame.Add(new Framelet(FrameletType.PayloadData, AnyContents)), Throws.InvalidOperationException.With.Message.ContainsSubstring("Exceeded maximum allowed count of framelets")); }
public void Frame_Add_TotalCount_Updated() { var frame = new Frame(2, LoggerTests.BlackHole); int expectedSize = 2; Assert.AreEqual(expectedSize, frame.TotalSize); frame.Add(new Framelet(FrameletType.LayerData, AnyContents)); expectedSize += 2 + 4 + AnyContents.Count; Assert.AreEqual(expectedSize, frame.TotalSize); frame.Add(new Framelet(FrameletType.ProtocolError, AnyContents)); expectedSize += 2 + 4 + AnyContents.Count; Assert.AreEqual(expectedSize, frame.TotalSize); }