private void TestModeForInvokeSend(IEnumerable <byte> _data, XModemProtocol.XModemMode mode) { IInvoker invoker = new InvokeSend(); var cts = new CancellationTokenSource(); var communicator = new ComSendCollection(); IContext context = new Context { Token = cts.Token, Communicator = communicator, Mode = mode, }; var expectedData = new List <List <byte> >(context.Packets); expectedData.Add(new List <byte> { context.Options.EOT }); communicator.BytesToSend = new List <byte> { context.Options.ACK }; communicator.BytesRead = new List <List <byte> >(); invoker.Invoke(context); Assert.AreEqual(expectedData, communicator.BytesRead); }
public void NAKResendTest() { var cts = new CancellationTokenSource(); var communicator = new ComSendCollection(); IInvoker invoker = new InvokeSend(); IContext context = new Context { Communicator = communicator, Token = cts.Token, }; context.Data = _randomDataGenerator.GetRandomData(10000).ToList(); // Construct responses. // 1). Refuse first packet twice before accepting. // 2). Refuse second packet. // 3). Send non-sensical byte. // 4). Send cancellation request. var nakCollection = new List <byte> { context.Options.NAK }; var ackCollection = new List <byte> { context.Options.ACK }; var canCollection = Enumerable.Repeat(context.Options.CAN, context.Options.CancellationBytesRequired); communicator.CollectionToSend = new List <List <byte> > { nakCollection, nakCollection, ackCollection, nakCollection, new List <byte> { context.Options.SUB }, canCollection.ToList() }; // Construct expected data to be received. // 1). First packet should be sent thrice. // 2). Second packet should be sent twice. var expectedData = new List <List <byte> >(); for (int i = 0; i < 3; i++) { expectedData.Add(context.Packets[0]); } for (int i = 0; i < 2; i++) { expectedData.Add(context.Packets[1]); } bool excThrown = false; try { communicator.BytesRead = new List <List <byte> >(); invoker.Invoke(context); } catch (XModemProtocolException ex) { excThrown = ex.AbortArgs.Reason == XModemProtocol.XModemAbortReason.CancellationRequestReceived; } Assert.IsTrue(excThrown); Assert.AreEqual(expectedData, communicator.BytesRead); }