summary refs log tree commit diff
path: root/crypto/test
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2022-11-05 23:04:19 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2022-11-05 23:04:19 +0700
commit4ff3ad2363c111d8723821687b14c1189ecac675 (patch)
tree0c8683c876c827dcf2457ba425af85c74c4e582c /crypto/test
parentAdd TlsPeer.IgnoreCorruptDtlsRecords (diff)
downloadBouncyCastle.NET-ed25519-4ff3ad2363c111d8723821687b14c1189ecac675.tar.xz
Add Span-based variants in DTLS
Diffstat (limited to 'crypto/test')
-rw-r--r--crypto/test/src/tls/test/LoggingDatagramTransport.cs62
-rw-r--r--crypto/test/src/tls/test/MockDatagramAssociation.cs59
-rw-r--r--crypto/test/src/tls/test/UnreliableDatagramTransport.cs47
3 files changed, 168 insertions, 0 deletions
diff --git a/crypto/test/src/tls/test/LoggingDatagramTransport.cs b/crypto/test/src/tls/test/LoggingDatagramTransport.cs
index f675b72fc..0ad15e065 100644
--- a/crypto/test/src/tls/test/LoggingDatagramTransport.cs
+++ b/crypto/test/src/tls/test/LoggingDatagramTransport.cs
@@ -34,25 +34,86 @@ namespace Org.BouncyCastle.Tls.Tests
 
         public virtual int Receive(byte[] buf, int off, int len, int waitMillis)
         {
+//#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+#if NET6_0_OR_GREATER
+            return Receive(buf.AsSpan(off, len), waitMillis);
+#else
             int length = m_transport.Receive(buf, off, len, waitMillis);
             if (length >= 0)
             {
                 DumpDatagram("Received", buf, off, length);
             }
             return length;
+#endif
         }
 
+//#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+#if NET6_0_OR_GREATER
+        public virtual int Receive(Span<byte> buffer, int waitMillis)
+        {
+            int length = m_transport.Receive(buffer, waitMillis);
+            if (length >= 0)
+            {
+                DumpDatagram("Received", buffer[..length]);
+            }
+            return length;
+        }
+#endif
+
         public virtual void Send(byte[] buf, int off, int len)
         {
+//#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+#if NET6_0_OR_GREATER
+            Send(buf.AsSpan(off, len));
+#else
             DumpDatagram("Sending", buf, off, len);
             m_transport.Send(buf, off, len);
+#endif
         }
 
+//#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+#if NET6_0_OR_GREATER
+        public virtual void Send(ReadOnlySpan<byte> buffer)
+        {
+            DumpDatagram("Sending", buffer);
+            m_transport.Send(buffer);
+        }
+#endif
+
         public virtual void Close()
         {
             m_transport.Close();
         }
 
+//#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+#if NET6_0_OR_GREATER
+        private void DumpDatagram(string verb, ReadOnlySpan<byte> buffer)
+        {
+            int len = buffer.Length;
+            long timestamp = DateTimeUtilities.CurrentUnixMs() - m_launchTimestamp;
+            StringBuilder sb = new StringBuilder("(+" + timestamp + "ms) " + verb + " " + len + " byte datagram:");
+            for (int pos = 0; pos < len; ++pos)
+            {
+                if (pos % 16 == 0)
+                {
+                    sb.Append(Environment.NewLine);
+                    sb.Append("    ");
+                }
+                else if (pos % 16 == 8)
+                {
+                    sb.Append('-');
+                }
+                else
+                {
+                    sb.Append(' ');
+                }
+                int val = buffer[pos] & 0xFF;
+                sb.Append(HEX_CHARS[val >> 4]);
+                sb.Append(HEX_CHARS[val & 0xF]);
+            }
+            Dump(sb.ToString());
+        }
+#else
         private void DumpDatagram(string verb, byte[] buf, int off, int len)
         {
             long timestamp = DateTimeUtilities.CurrentUnixMs() - m_launchTimestamp;
@@ -78,6 +139,7 @@ namespace Org.BouncyCastle.Tls.Tests
             }
             Dump(sb.ToString());
         }
+#endif
 
         private void Dump(string s)
         {
diff --git a/crypto/test/src/tls/test/MockDatagramAssociation.cs b/crypto/test/src/tls/test/MockDatagramAssociation.cs
index ef317c7b6..3612bec40 100644
--- a/crypto/test/src/tls/test/MockDatagramAssociation.cs
+++ b/crypto/test/src/tls/test/MockDatagramAssociation.cs
@@ -58,6 +58,10 @@ namespace Org.BouncyCastle.Tls.Tests
 
             public virtual int Receive(byte[] buf, int off, int len, int waitMillis)
             {
+//#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+#if NET6_0_OR_GREATER
+                return Receive(buf.AsSpan(off, len), waitMillis);
+#else
                 lock (m_receiveQueue)
                 {
                     if (m_receiveQueue.Count < 1)
@@ -81,10 +85,45 @@ namespace Org.BouncyCastle.Tls.Tests
                     Array.Copy(packet, 0, buf, off, copyLength);
                     return copyLength;
                 }
+#endif
             }
 
+//#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+#if NET6_0_OR_GREATER
+            public virtual int Receive(Span<byte> buffer, int waitMillis)
+            {
+                lock (m_receiveQueue)
+                {
+                    if (m_receiveQueue.Count < 1)
+                    {
+                        try
+                        {
+                            Monitor.Wait(m_receiveQueue, waitMillis);
+                        }
+                        catch (ThreadInterruptedException)
+                        {
+                            // TODO Keep waiting until full wait expired?
+                        }
+
+                        if (m_receiveQueue.Count < 1)
+                            return -1;
+                    }
+
+                    byte[] packet = m_receiveQueue[0];
+                    m_receiveQueue.RemoveAt(0);
+                    int copyLength = System.Math.Min(buffer.Length, packet.Length);
+                    packet.AsSpan(0, copyLength).CopyTo(buffer);
+                    return copyLength;
+                }
+            }
+#endif
+
             public virtual void Send(byte[] buf, int off, int len)
             {
+//#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+#if NET6_0_OR_GREATER
+                Send(buf.AsSpan(off, len));
+#else
                 if (len > m_outer.m_mtu)
                 {
                     // TODO Simulate rejection?
@@ -97,7 +136,27 @@ namespace Org.BouncyCastle.Tls.Tests
                     m_sendQueue.Add(packet);
                     Monitor.PulseAll(m_sendQueue);
                 }
+#endif
+            }
+
+//#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+#if NET6_0_OR_GREATER
+            public virtual void Send(ReadOnlySpan<byte> buffer)
+            {
+                if (buffer.Length > m_outer.m_mtu)
+                {
+                    // TODO Simulate rejection?
+                }
+
+                byte[] packet = buffer.ToArray();
+
+                lock (m_sendQueue)
+                {
+                    m_sendQueue.Add(packet);
+                    Monitor.PulseAll(m_sendQueue);
+                }
             }
+#endif
 
             public virtual void Close()
             {
diff --git a/crypto/test/src/tls/test/UnreliableDatagramTransport.cs b/crypto/test/src/tls/test/UnreliableDatagramTransport.cs
index bdbfd6e67..7769db9d1 100644
--- a/crypto/test/src/tls/test/UnreliableDatagramTransport.cs
+++ b/crypto/test/src/tls/test/UnreliableDatagramTransport.cs
@@ -37,6 +37,10 @@ namespace Org.BouncyCastle.Tls.Tests
 
         public virtual int Receive(byte[] buf, int off, int len, int waitMillis)
         {
+//#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+#if NET6_0_OR_GREATER
+            return Receive(buf.AsSpan(off, len), waitMillis);
+#else
             long endMillis = DateTimeUtilities.CurrentUnixMs() + waitMillis;
             for (;;)
             {
@@ -52,10 +56,37 @@ namespace Org.BouncyCastle.Tls.Tests
 
                 waitMillis = (int)(endMillis - now);
             }
+#endif
         }
 
+//#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+#if NET6_0_OR_GREATER
+        public virtual int Receive(Span<byte> buffer, int waitMillis)
+        {
+            long endMillis = DateTimeUtilities.CurrentUnixMs() + waitMillis;
+            for (;;)
+            {
+                int length = m_transport.Receive(buffer, waitMillis);
+                if (length < 0 || !LostPacket(m_percentPacketLossReceiving))
+                    return length;
+
+                Console.WriteLine("PACKET LOSS (" + length + " byte packet not received)");
+
+                long now = DateTimeUtilities.CurrentUnixMs();
+                if (now >= endMillis)
+                    return -1;
+
+                waitMillis = (int)(endMillis - now);
+            }
+        }
+#endif
+
         public virtual void Send(byte[] buf, int off, int len)
         {
+//#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+#if NET6_0_OR_GREATER
+            Send(buf.AsSpan(off, len));
+#else
             if (LostPacket(m_percentPacketLossSending))
             {
                 Console.WriteLine("PACKET LOSS (" + len + " byte packet not sent)");
@@ -64,7 +95,23 @@ namespace Org.BouncyCastle.Tls.Tests
             {
                 m_transport.Send(buf, off, len);
             }
+#endif
+        }
+
+//#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+#if NET6_0_OR_GREATER
+        public virtual void Send(ReadOnlySpan<byte> buffer)
+        {
+            if (LostPacket(m_percentPacketLossSending))
+            {
+                Console.WriteLine("PACKET LOSS (" + buffer.Length + " byte packet not sent)");
+            }
+            else
+            {
+                m_transport.Send(buffer);
+            }
         }
+#endif
 
         public virtual void Close()
         {