Пример #1
0
        void TestModeForSendOperation(XModemProtocol.XModemMode mode)
        {
            var communicator = new ComSendCollection();
            var context      = new Context {
                Mode         = mode,
                Data         = _rdg.GetRandomData(40000).ToList(),
                Communicator = communicator,
            };
            var dts = new List <List <byte> > {
                new List <byte> {
                    mode == XModemProtocol.XModemMode.Checksum ? context.Options.NAK : context.Options.C
                }
            };
            double payloadCount = mode == XModemProtocol.XModemMode.OneK ? 1024.0 : 128.0;
            int    ackCount     = (int)Math.Ceiling(context.Data.Count / payloadCount) + 1;

            dts.AddRange(Enumerable.Repeat(new List <byte> {
                context.Options.ACK
            }, ackCount));
            communicator.CollectionToSend = dts;
            IOperation operation = new SendOperation();

            operation.Go(context);
            var expectedData = new List <List <byte> >(context.Packets);

            expectedData.Add(new List <byte> {
                context.Options.EOT
            });
            Assert.AreEqual(expectedData, communicator.BytesRead);
        }
Пример #2
0
 private void TestModeForInvokeReceive(XModemMode mode)
 {
     RunTestForInvokeReceive(_randomDataGenerator.GetRandomData(128), mode);
     RunTestForInvokeReceive(_randomDataGenerator.GetRandomData(130), mode);
     RunTestForInvokeReceive(_randomDataGenerator.GetRandomData(10000), mode);
     RunTestForInvokeReceive(_randomDataGenerator.GetRandomData(10240), mode);
 }
Пример #3
0
        public void FinalizeReceiveTest()
        {
            var      data         = _rdg.GetRandomData(20).ToList();
            var      expectedData = new List <byte>(data);
            IContext context      = new Context();

            data.AddRange(Enumerable.Repeat(context.Options.SUB, 15));
            context.Data = data;
            IFinalizer finalizer = new FinalizeReceive();

            // Test to see if finalizer strips away SUB.
            finalizer.Finalize(context);
            Assert.AreEqual(expectedData, context.Data);
            context.Data = new List <byte>();
            // Test to see if finalizer ends safely if no data present.
            finalizer.Finalize(context);
        }
Пример #4
0
        public void DetectorTest()
        {
            var rand = new RandomDataGenerator();
            ICancellationDetector detector = new CancellationDetector();
            var message = new List <byte>();
            XModemProtocolOptions options = new XModemProtocolOptions {
                CancellationBytesRequired = 10
            };

            message.Add(0x43);
            // Test to see if detector returns false under the condition that the message is too short.
            Assert.IsFalse(detector.CancellationDetected(message, options));

            message.AddRange(rand.GetRandomData(50));
            // Test to see if detector returns false under the condition that the message does not contain a cancel bytes.
            Assert.IsFalse(detector.CancellationDetected(message, options));

            message.AddRange(Enumerable.Repeat(options.CAN, 9));
            // Test to see if detector returns false under the condition that the message does not contain enough cancel bytes.
            Assert.IsFalse(detector.CancellationDetected(message, options));

            for (int i = 0; i < 3; i++)
            {
                message.Add(0x43);
                message.AddRange(Enumerable.Repeat(options.CAN, 9));
            }
            // Test to see if detector returns false under the condition that the message does not contain enough cancel bytes
            // contiguously even though it contains enough overall.
            Assert.IsFalse(detector.CancellationDetected(message, options));

            options.CancellationBytesRequired = 5;
            // Test to see if detector returns true under the condition that the message contains enough cancel bytes contiguously.
            Assert.IsTrue(detector.CancellationDetected(message, options));

            options.CancellationBytesRequired = 10;
            message.Add(0x43);
            message.AddRange(Enumerable.Repeat(options.CAN, 10));
            // Test to see if detector returns true under the condition that the message contains enough cancel bytes contiguously.
            Assert.IsTrue(detector.CancellationDetected(message, options));
        }
Пример #5
0
        void RunTest(Tuple <IPacketBuilder, IXModemProtocolOptions, int> options)
        {
            var  builder     = options.Item1;
            var  testOptions = options.Item2;
            int  dataLength  = options.Item3;
            int  packetSize;
            int  payloadSize = 128;
            byte header      = testOptions.SOH;

            if (builder is NormalPacketBuilder)
            {
                packetSize = 132;
            }
            else if (builder is CRCPacketBuilder)
            {
                packetSize = 133;
            }
            else
            {
                if (testOptions.SenderOneKPacketSize == OneKPacketSize.Mixed &&
                    dataLength < 129)
                {
                    packetSize = 133;
                }
                else
                {
                    packetSize  = 1029;
                    payloadSize = 1024;
                    header      = testOptions.STX;
                }
            }
            var packets     = builder.GetPackets(_rdg.GetRandomData(dataLength), testOptions);
            int packetCount = (int)Math.Ceiling((double)dataLength / payloadSize);

            Assert.AreEqual(packetCount, packets.Count);
            Assert.AreEqual(packetSize, packets[0].Count);
            Assert.AreEqual(header, packets[0][0]);
        }
Пример #6
0
        public void PlayCatch()
        {
            var _sender   = new XModemProtocol.XModemCommunicator();
            var _receiver = new XModemProtocol.XModemCommunicator();

#if External
            var Port1 = new SerialPort {
                BaudRate     = 230400,
                ReadTimeout  = 10000,
                WriteTimeout = 10000,
                DataBits     = 8,
                Parity       = Parity.Even,
                StopBits     = StopBits.One,
                PortName     = "COM14"
            };
            var Port2 = new SerialPort {
                BaudRate     = 230400,
                ReadTimeout  = 10000,
                WriteTimeout = 10000,
                DataBits     = 8,
                Parity       = Parity.Even,
                StopBits     = StopBits.One,
                PortName     = "COM8"
            };
            Port1.Open();
            Port2.Open();
            _sender.Port   = Port1;
            _receiver.Port = Port2;
#else
            InterPlayComm _senderCom = new InterPlayComm {
                Name = "Port 1"
            };
            InterPlayComm _receiverCom = new InterPlayComm {
                Name = "Port 2"
            };

            _senderCom.Partner   = _receiverCom;
            _receiverCom.Partner = _senderCom;

            _sender.Communicator   = _senderCom;
            _receiver.Communicator = _receiverCom;
#endif

            _data = _rdg.GetRandomData(10000);
            _receiver.Polynomial = 0x1021;
            _sender.Polynomial   = 0x1021;

            SetEvents(_sender, "Port 1");
            SetEvents(_receiver, "Port 2");

            _sender.PacketsBuilt += (s, e) => WriteLine($"Port 1 : {e.Packets.Count}");

            _sender.Data = _data;

            var tasks = new Task[] {
                Task.Run(() => _sender.Send()),
                Task.Run(() => _receiver.Receive()),
            };

            Task.WaitAll(tasks);
            ReadLine();
        }