Пример #1
0
        public async Task SendRequest()
        {
            // Create the object we're going to test.
            TestLogger     logger = new TestLogger();
            TestPort       port   = new TestPort(logger);
            ScanToolDevice device = new ScanToolDevice(port, logger);

            // Specify the sequence of bytes that we would expect to get back from the serial port.
            // Note that the test passes, but the first sequence ends with ">\r\n" and the second ends with "\r\n>"  - this seems suspicious.
            port.EnqueueBytes(Encoding.ASCII.GetBytes("OK>\r\n"));
            port.EnqueueBytes(Encoding.ASCII.GetBytes("6CF0107C01003147315959C3\r\n>"));
            port.BytesToReceive.Position = 0;

            // Send a message.
            Message message     = new Message(new byte[] { 0x6c, 0x10, 0xF0, 0x3C, 0x01 });
            bool    sendSuccess = await device.SendMessage(message);

            // Confirm success.
            Assert.IsTrue(sendSuccess, "Send success.");

            // Confirm that the device sent the bytes we expect it to send.
            Assert.AreEqual("AT SH 6C 10 F0 \r\n", System.Text.Encoding.ASCII.GetString(port.MessagesSent[0]), "Set-header command");
            Assert.AreEqual("3C 01\r\n", Encoding.ASCII.GetString(port.MessagesSent[1]), "Read block 1 command");

            // Confirm that the device interpreted the response as expected.
            Message response = await device.ReceiveMessage();

            Assert.IsNotNull(response, "Response should not be null.");
            Assert.AreEqual("6C F0 10 7C 01 00 31 47 31 59 59", response.GetBytes().ToHex(), "Response message");
        }
Пример #2
0
        public async Task SendRequest()
        {
            // Create the object we're going to test.
            TestLogger logger = new TestLogger();
            TestPort   port   = new TestPort(logger);
            AvtDevice  device = new AvtDevice(port, logger);

            // Specify the sequence of bytes that we would expect to get back from the serial port.
            port.EnqueueBytes(new byte[] { 0x11, 0x0C, 0x00, 0x6c, 0xF0, 0x10, 0x7C, 0x01, 0x00, 0x31, 0x47, 0x31, 0x59, 0x59, 0xC3 });
            port.BytesToReceive.Position = 0;

            // Send a message.
            Message message     = new Message(new byte[] { 0x6c, 0x10, 0xF0, 0x3C, 0x01 });
            bool    sendSuccess = await device.SendMessage(message);

            // Confirm success.
            Assert.IsTrue(sendSuccess, "Send success");

            // Confirm that the device sent the bytes we expect it to send.
            // The 00 00 00 stuff is just placeholders for real data.
            Assert.AreEqual("05", port.MessagesSent[0].ToHex(), "First command");
            Assert.AreEqual(message.GetBytes().ToHex(), port.MessagesSent[1].ToHex(), "Second command");

            // Confirm that the device interpreted the response as expected.
            Message response = await device.ReceiveMessage();

            Assert.IsNotNull(response, "Response should not be null.");
            Assert.AreEqual("6C F0 10 7C 01 00 31 47 31 59 59", response.GetBytes().ToHex(), "Response message");
        }
Пример #3
0
        [Test] public void StringIsWrittenWithLengthPrefix()
        {
            var testSocket   = new TestPort();
            var socketStream = new MessageChannel(testSocket);

            socketStream.Write("h\u2019llo", "{0}:");
            Assert.AreEqual(new byte[] { 55, 58, 104, 226, 128, 153, 108, 108, 111 }, testSocket.GetBytes());
        }
Пример #4
0
        [Test] public void SimpleStringIsWritten()
        {
            var testSocket   = new TestPort();
            var socketStream = new MessageChannel(testSocket);

            socketStream.Write("hello");
            Assert.AreEqual(new byte[] { 104, 101, 108, 108, 111 }, testSocket.GetBytes());
        }
Пример #5
0
        [Test] public void EncodedStringIsRead()
        {
            var testSocket   = new TestPort();
            var socketStream = new MessageChannel(testSocket);

            testSocket.PutBytes(new byte[] { 104, 226, 128, 153, 108, 108, 111 });
            Assert.AreEqual("h\u2019llo", socketStream.Read(7));
        }
Пример #6
0
        [Test] public void SimpleStringIsRead()
        {
            var testSocket   = new TestPort();
            var socketStream = new MessageChannel(testSocket);

            testSocket.PutBytes(new byte[] { 104, 101, 108, 108, 111 });
            Assert.AreEqual("hello", socketStream.Read(5));
        }
Пример #7
0
        [Test] public void StringIsReadInMultipleParts()
        {
            var testSocket   = new TestPort();
            var socketStream = new MessageChannel(testSocket);

            testSocket.PutBytes(new byte[] { 104, 101, 108, 108, 111 });
            testSocket.PutBytes(new byte[] { 32, 119, 111, 114, 108, 100 });
            Assert.AreEqual("hello world", socketStream.Read(11));
        }
Пример #8
0
        private void RunTest(CellProcessor service, string tables)
        {
            var port = new TestPort();

            port.Input = Protocol.FormatInteger(tables.Length);
            port.Input = tables;
            port.Input = Protocol.FormatInteger(0);
            var server = new SocketServer(new FitSocket(new MessageChannel(port), new NullReporter()), service, new NullReporter(), false);

            server.ProcessTestDocuments(new StoryTestStringWriter().ForTables(s => resultTables += s));
            Assert.IsFalse(port.IsOpen);
        }
Пример #9
0
        private static void ConnectionDropedTest()
        {
            // INIT TEST

            var a = new TestTerminal("A", new PhoneNumber("101"));
            var b = new TestTerminal("B", new PhoneNumber("102"));
            var c = new TestTerminal("C", new PhoneNumber("103"));

            var aa = new TestPort("AA");
            var bb = new TestPort("BB");
            var cc = new TestPort("CC");

            var station = new TestStation("AAA", new List<ITerminal> { a, b, c },
                                          new List<IPort> { aa, bb, cc });

            // START TEST

            var t1 = station.GetPreparedTerminal() as TestTerminal;
            var t2 = station.GetPreparedTerminal() as TestTerminal;
            var t3 = station.GetPreparedTerminal() as TestTerminal;

            if (t1 == null || t2 == null || t3 == null) return;

            t1.Plug();
            t2.Plug();
            t3.Plug();

            Console.WriteLine("\n");
            t1.Connect(t2.PhoneNumber);
            t1.SendMessage("Hi!");
            t2.SendMessage("Hallo!");
            t3.Connect(t2.PhoneNumber); // port not bind because target port busy
            t1.SendMessage("How are you?!");
            aa.Close(); // close port of t1 close
            t2.SendMessage("I am ok"); // droped because aa closed
            Console.WriteLine("\n");
            aa.Open();
            t3.Connect(t1.PhoneNumber);
            t1.Unplug();
            t3.SendMessage("Hi"); // droped because t1 unpluged
        }
Пример #10
0
        private static void BillingSystemTest()
        {
            // INIT TEST

            var a = new TestTerminal("A", new PhoneNumber("101"));
            var b = new TestTerminal("B", new PhoneNumber("102"));

            var aa = new TestPort("AA");
            var bb = new TestPort("BB");

            var station = new TestStation("AAA", new List<ITerminal> { a, b },
                                          new List<IPort> { aa, bb });

            var billing = new TestBillingSystem();

            ITariff tariffFirst = new TestTariff(connectCost: 1000, minCost: 300);
            ITariff tariffSecond = new TestTariff(connectCost: 0, minCost: 500);

            var provider = new TestProvider(station, billing);

            // START TEST

            var t1 = provider.SignContract(tariffFirst) as TestTerminal;
            var t2 = provider.SignContract(tariffSecond) as TestTerminal;

            if (t1 == null || t2 == null) return;

            t1.Plug();
            t2.Plug();

            Console.WriteLine("------ FIRST MONTH ------");

            Console.WriteLine("--- First call ---");
            t1.Connect(t2.PhoneNumber);
            t2.SendMessage("Ololololol");
            t2.Disconnect();

            Console.WriteLine(" --- Second call ---");
            t2.Unplug();
            t1.Connect(t2.PhoneNumber);
            t2.Plug();

            Console.WriteLine(" --- third call ---");
            t2.Connect(t1.PhoneNumber);
            t2.SendMessage("Ololol");
            t1.Disconnect();

            Console.WriteLine("--- statistic ---");
            var calls = provider.GetStatisticForPeriod(t1.PhoneNumber);
            foreach (var call in calls)
                Console.WriteLine(call);
            Console.WriteLine($"Debt of {t1.PhoneNumber} - {provider.GetDebt(t1.PhoneNumber)}");
            Console.WriteLine($"Debt of {t2.PhoneNumber} - {provider.GetDebt(t2.PhoneNumber)}");

            billing.StartNewPeriod();
            Console.WriteLine("------ SECOND MONTH ------");

            Console.WriteLine("--- statistic ---");
            calls = provider.GetStatisticForPeriod(t1.PhoneNumber);
            foreach (var call in calls)
                Console.WriteLine(call);
            Console.WriteLine($"Debt of {t1.PhoneNumber} - {provider.GetDebt(t1.PhoneNumber)}");
            Console.WriteLine($"Debt of {t2.PhoneNumber} - {provider.GetDebt(t2.PhoneNumber)}");

            Console.WriteLine($"--- {t1.PhoneNumber} changing tariff ---");
            if (provider.TryChangeTariff(t1.PhoneNumber, tariffSecond))
                Console.WriteLine("tariff changed successfully");
            else
                Console.WriteLine("tariff not changed");

            Console.WriteLine($"--- {t1.PhoneNumber} changing tariff for the second time ---");
            if (provider.TryChangeTariff(t1.PhoneNumber, tariffFirst))
                Console.WriteLine("tariff changed successfully");
            else
                Console.WriteLine("tariff not changed");
        }
Пример #11
0
 public void SetUp()
 {
     port        = new TestPort();
     interpreter = new Interpreter(new Messenger(new MessageChannel(port)), string.Empty, Builder.Service());
 }
Пример #12
0
 public void SetUp()
 {
     port      = new TestPort();
     messenger = new Messenger(new MessageChannel(port));
 }