Пример #1
0
        protected static byte[] CreateBuffer(int length)
        {
            var buffer = new byte[length];

            StaticRandom.NextBytes(buffer);
            return(buffer);
        }
Пример #2
0
        static uint GetRandomNonce()
        {
            Span <byte> span = stackalloc byte[4];

            StaticRandom.NextBytes(span);
            return(BinaryPrimitives.ReadUInt32LittleEndian(span));
        }
Пример #3
0
 public TcpTestConnectionContext(ControlConnectionContext control, NetworkStream testStream, TcpClient testClient, int testDurationSeconds)
 {
     this.control             = control;
     this.testStream          = testStream;
     this.testClient          = testClient;
     this.testDurationSeconds = testDurationSeconds;
     this.bufferToWrite       = StaticRandom.NextBytes(500000);
     this.bufferToRead        = new byte[500000];
 }
Пример #4
0
        public void TestWriteSynchronous()
        {
            var testBuffer = new byte[125];

            StaticRandom.NextBytes(testBuffer);

            _stream.Write(testBuffer, 5, 80);

            Assert.Equal(testBuffer, _delgatedBuffer.Array);
            Assert.Equal(5, _delgatedBuffer.Offset);
            Assert.Equal(80, _delgatedBuffer.Count);
        }
        public async Task TestAwaitWorks()
        {
            var testData = new byte[12];

            StaticRandom.NextBytes(testData);

            var x = new ResultCallback();
            await Task.Delay(TimeSpan.FromMilliseconds(50))
            .ContinueWith(task => x.ReceivedResult(CallTransmissionResponseType.MethodExecuted, testData, 4));

            await x.Wait(TimeSpan.FromSeconds(1));

            Assert.Equal(CallTransmissionResponseType.MethodExecuted, x.ResponseType);
            Assert.Equal(testData, x.Data);
            Assert.Equal(4, x.Offset);
        }
        public static void TestThreadSafety()
        {
            int NUMBER_OF_THREADS = 5;
            int NUMBER_OF_OPS     = 100000;

            List <Thread> threads = new List <Thread>();

            ThreadStart threadDelegate = (ThreadStart) delegate()
            {
                Thread.Sleep(0);
                byte[] buffer = new byte[1];
                for (int i = 0; i < NUMBER_OF_OPS; i++)
                {
                    StaticRandom.Next();
                    StaticRandom.Next(100);
                    StaticRandom.Next(10, 100);
                    StaticRandom.NextDouble();
                    StaticRandom.NextBytes(buffer);
                    StaticRandom.NextString(1, true, true);
                }
            };

            for (int i = 0; i < NUMBER_OF_THREADS; i++)
            {
                threads.Add(new Thread(threadDelegate));
            }

            foreach (Thread t in threads)
            {
                t.Start();
            }

            foreach (Thread t in threads)
            {
                t.Join();
            }
        }
Пример #7
0
 public TestAutomaticResponseCompression()
 {
     _testData = new byte[PackageSize * 2];
     StaticRandom.NextBytes(_testData);
 }
Пример #8
0
        public override void handleGETRequest(HttpProcessor p)
        {
            string pageLower = p.requestedPage.ToLower();

            if (p.requestedPage == "randomdata")
            {
                p.writeSuccess("application/x-binary");
                p.outputStream.Flush();

                int testSec = p.GetIntParam("testsec", 5);
                testSec = BPMath.Clamp(testSec, 1, 30);

                long   endTime    = sw.ElapsedMilliseconds + (long)TimeSpan.FromSeconds(testSec).TotalMilliseconds;
                byte[] randomData = StaticRandom.NextBytes(p.tcpClient.SendBufferSize);
                while (sw.ElapsedMilliseconds < endTime)
                {
                    p.tcpStream.Write(randomData, 0, randomData.Length);
                }
            }
            else if (p.requestedPage == "nstws")
            {
                wss.AcceptIncomingConnection(p.tcpClient);
            }
            else if (p.requestedPage == "HEADERS")
            {
                p.writeSuccess("text/plain");
                p.outputStream.Write(string.Join(Environment.NewLine, p.httpHeadersRaw.Select(h => h.Key + ": " + h.Value)));
            }
            else if (p.requestedPage == "IP")
            {
                p.writeSuccess("text/plain");
                p.outputStream.Write(p.RemoteIPAddressStr);
            }
            else
            {
                if (p.requestedPage == "")
                {
                    p.requestedPage = "default.html";
                }

                string wwwPath = Globals.ApplicationDirectoryBase + "www/";
#if DEBUG
                if (System.Diagnostics.Debugger.IsAttached)
                {
                    wwwPath = Globals.ApplicationDirectoryBase + "../../www/";
                }
#endif
                DirectoryInfo WWWDirectory     = new DirectoryInfo(wwwPath);
                string        wwwDirectoryBase = WWWDirectory.FullName.Replace('\\', '/').TrimEnd('/') + '/';
                FileInfo      fi             = new FileInfo(wwwDirectoryBase + p.requestedPage);
                string        targetFilePath = fi.FullName.Replace('\\', '/');
                if (!targetFilePath.StartsWith(wwwDirectoryBase) || targetFilePath.Contains("../"))
                {
                    p.writeFailure("400 Bad Request");
                    return;
                }
                if (!fi.Exists)
                {
                    return;
                }
                if ((fi.Extension == ".html" || fi.Extension == ".htm") && fi.Length < 256000)
                {
                    string html = File.ReadAllText(fi.FullName);
                    html = html.Replace("%%VERSION%%", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString());
                    html = html.Replace("%%RND%%", rnd.ToString());

                    byte[] data = Encoding.UTF8.GetBytes(html);
                    p.writeSuccess(Mime.GetMimeType(fi.Extension), data.Length);
                    p.outputStream.Flush();
                    p.tcpStream.Write(data, 0, data.Length);
                    p.tcpStream.Flush();
                }
                else
                {
                    string mime = Mime.GetMimeType(fi.Extension);
                    if (pageLower.StartsWith(".well-known/acme-challenge/"))
                    {
                        mime = "text/plain";
                    }
                    if (fi.LastWriteTimeUtc.ToString("R") == p.GetHeaderValue("if-modified-since"))
                    {
                        p.writeSuccess(mime, -1, "304 Not Modified");
                        return;
                    }
                    p.writeSuccess(mime, fi.Length, additionalHeaders: GetCacheLastModifiedHeaders(TimeSpan.FromHours(1), fi.LastWriteTimeUtc));
                    p.outputStream.Flush();
                    using (FileStream fs = fi.OpenRead())
                    {
                        fs.CopyTo(p.tcpStream);
                    }
                    p.tcpStream.Flush();
                }
            }
        }