public void TestNewlineNormalizerStream8bitWriting()
        {
            var newlinePermutations = new byte[][] { "\n".ToUtf8(), "\r".ToUtf8(), "\r\n".ToUtf8() }.Permutations();
            foreach (var permutation in newlinePermutations)
            {
                var permutationArr = permutation.ToArray();
                var bytes = "Line 1".ToUtf8().Concat(permutationArr[0])
                    .Concat("Line 2".ToUtf8()).Concat(permutationArr[1])
                    .Concat("Line 3".ToUtf8()).Concat(permutationArr[2])
                    .Concat("Line 4".ToUtf8()).ToArray();

                for (int chunkSize1 = 1; chunkSize1 <= bytes.Length; chunkSize1++)
                {
                    for (int chunkSize2 = 1; chunkSize2 < chunkSize1; chunkSize2++)
                    {
                        using (var str = new MemoryStream())
                        {
                            using (var slowMaker1 = new SlowStream(str, chunkSize1))
                            using (var normaliser = new NewlineNormalizerStream8bit(slowMaker1, "<NL>".ToUtf8()))
                            using (var slowMaker2 = new SlowStream(normaliser, chunkSize2))
                                slowMaker2.Write(bytes, 0, bytes.Length);
                            Assert.AreEqual("Line 1<NL>Line 2<NL>Line 3<NL>Line 4", str.ToArray().FromUtf8());
                        }
                    }
                }
            }
        }
示例#2
0
        public void TestNewlineNormalizerStream16bitLittleEndianRead()
        {
            var newlinePermutations = new byte[][] { "\n".ToUtf16(), "\r".ToUtf16(), "\r\n".ToUtf16() }.Permutations();

            foreach (var permutation in newlinePermutations)
            {
                var permutationArr = permutation.ToArray();
                var bytes          = "Line 1".ToUtf16().Concat(permutationArr[0])
                                     .Concat("Line 2".ToUtf16()).Concat(permutationArr[1])
                                     .Concat("Line 3".ToUtf16()).Concat(permutationArr[2])
                                     .Concat("Line 4".ToUtf16()).ToArray();

                for (int chunkSize1 = 1; chunkSize1 <= bytes.Length; chunkSize1++)
                {
                    for (int chunkSize2 = 1; chunkSize2 < chunkSize1; chunkSize2++)
                    {
                        using (var str = new MemoryStream(bytes))
                            using (var slowMaker1 = new SlowStream(str, chunkSize1))
                                using (var normaliser = new NewlineNormalizerStream16bit(slowMaker1, "<NL>".ToUtf16()))
                                    using (var slowMaker2 = new SlowStream(normaliser, chunkSize2))
                                    {
                                        var text = slowMaker2.ReadAllText(Encoding.Unicode);
                                        Assert.AreEqual("Line 1<NL>Line 2<NL>Line 3<NL>Line 4", text);
                                    }
                    }
                }
            }
        }
示例#3
0
        public void TestNewlineNormalizerStream8bitWriting()
        {
            var newlinePermutations = new byte[][] { "\n".ToUtf8(), "\r".ToUtf8(), "\r\n".ToUtf8() }.Permutations();

            foreach (var permutation in newlinePermutations)
            {
                var permutationArr = permutation.ToArray();
                var bytes          = "Line 1".ToUtf8().Concat(permutationArr[0])
                                     .Concat("Line 2".ToUtf8()).Concat(permutationArr[1])
                                     .Concat("Line 3".ToUtf8()).Concat(permutationArr[2])
                                     .Concat("Line 4".ToUtf8()).ToArray();

                for (int chunkSize1 = 1; chunkSize1 <= bytes.Length; chunkSize1++)
                {
                    for (int chunkSize2 = 1; chunkSize2 < chunkSize1; chunkSize2++)
                    {
                        using (var str = new MemoryStream())
                        {
                            using (var slowMaker1 = new SlowStream(str, chunkSize1))
                                using (var normaliser = new NewlineNormalizerStream8bit(slowMaker1, "<NL>".ToUtf8()))
                                    using (var slowMaker2 = new SlowStream(normaliser, chunkSize2))
                                        slowMaker2.Write(bytes, 0, bytes.Length);
                            Assert.AreEqual("Line 1<NL>Line 2<NL>Line 3<NL>Line 4", str.ToArray().FromUtf8());
                        }
                    }
                }
            }
        }
        public void TestControlCodedStream()
        {
            byte[] buffer;
            using (var memoryStream = new MemoryStream())
            using (var peekable = new PeekableStream(memoryStream))
            using (var ccs = new ControlCodedStream(peekable))
            using (var binary = new BinaryStream(ccs))
            {
                binary.WriteString("← ↔ →");
                ccs.WriteControlCode(47);
                binary.WriteChar((char) 255);
                ccs.WriteControlCode(48);
                binary.WriteVarInt(0x1FF);
                ccs.WriteControlCode(49);
                binary.WriteFloat(float.NaN);
                binary.WriteFloat(float.PositiveInfinity);
                binary.WriteFloat(float.NegativeInfinity);
                ccs.WriteControlCode(50);

                Assert.Throws<ArgumentOutOfRangeException>(() => ccs.WriteControlCode(255));

                binary.Close();
                ccs.Close();
                peekable.Close();
                memoryStream.Close();
                buffer = memoryStream.ToArray();
            }

            for (int i = 1; i < buffer.Length; i++)
            {
                using (var memoryStream = new MemoryStream(buffer))
                using (var slowStream = new SlowStream(memoryStream, i))
                using (var peekable = new PeekableStream(slowStream))
                using (var ccs = new ControlCodedStream(peekable))
                using (var binary = new BinaryStream(ccs))
                {
                    Assert.AreEqual("← ↔ →", binary.ReadString());
                    Assert.Throws<InvalidOperationException>(() => ccs.Read(new byte[1], 0, 1));
                    Assert.AreEqual(47, ccs.ReadControlCode());
                    Assert.AreEqual((char) 255, binary.ReadChar());
                    Assert.AreEqual(48, ccs.ReadControlCode());
                    Assert.AreEqual(0x1FF, binary.ReadVarInt());
                    Assert.AreEqual(49, ccs.ReadControlCode());
                    Assert.IsNaN(binary.ReadFloat());
                    Assert.AreEqual(-1, ccs.ReadControlCode());
                    Assert.AreEqual(float.PositiveInfinity, binary.ReadFloat());
                    Assert.AreEqual(float.NegativeInfinity, binary.ReadFloat());
                    Assert.AreEqual(50, ccs.ReadControlCode());
                    Assert.AreEqual(-1, ccs.ReadControlCode());
                    Assert.AreEqual(0, ccs.Read(new byte[1], 0, 1));
                }
            }
        }
 public void TestPeekableStreamRead()
 {
     var inputStr = "← ↔ →";
     var buffer = inputStr.ToUtf8();
     for (int i = 1; i <= buffer.Length; i++)
     {
         using (var mem = new MemoryStream(buffer))
         using (var slow = new SlowStream(mem, i))
         using (var peekable = new PeekableStream(slow))
         {
             using (var peek = peekable.GetPeekStream())
             {
                 var buf = peek.Read(11);
                 Assert.AreEqual(inputStr, buf.FromUtf8());
                 Assert.AreEqual(0, peek.Read(buf, 0, 1));
             }
             var buf2 = peekable.Read(11);
             Assert.AreEqual(inputStr, buf2.FromUtf8());
             Assert.AreEqual(0, peekable.Read(buf2, 0, 1));
             using (var peek = peekable.GetPeekStream())
                 Assert.AreEqual(0, peek.Read(buf2, 0, 1));
         }
     }
 }
        public void TestNewlineNormalizerStream16bitLittleEndianRead()
        {
            var newlinePermutations = new byte[][] { "\n".ToUtf16(), "\r".ToUtf16(), "\r\n".ToUtf16() }.Permutations();
            foreach (var permutation in newlinePermutations)
            {
                var permutationArr = permutation.ToArray();
                var bytes = "Line 1".ToUtf16().Concat(permutationArr[0])
                    .Concat("Line 2".ToUtf16()).Concat(permutationArr[1])
                    .Concat("Line 3".ToUtf16()).Concat(permutationArr[2])
                    .Concat("Line 4".ToUtf16()).ToArray();

                for (int chunkSize1 = 1; chunkSize1 <= bytes.Length; chunkSize1++)
                {
                    for (int chunkSize2 = 1; chunkSize2 < chunkSize1; chunkSize2++)
                    {
                        using (var str = new MemoryStream(bytes))
                        using (var slowMaker1 = new SlowStream(str, chunkSize1))
                        using (var normaliser = new NewlineNormalizerStream16bit(slowMaker1, "<NL>".ToUtf16()))
                        using (var slowMaker2 = new SlowStream(normaliser, chunkSize2))
                        {
                            var text = slowMaker2.ReadAllText(Encoding.Unicode);
                            Assert.AreEqual("Line 1<NL>Line 2<NL>Line 3<NL>Line 4", text);
                        }
                    }
                }
            }
        }
        public void TestParsePost()
        {
            string inputStr = @"-----------------------------265001916915724
Content-Disposition: form-data; name=""y""

This is what should be found in ""y"" at the end of the test.
-----------------------------265001916915724
Content-Disposition: form-data; name=""What a wonderful day it is today; so wonderful in fact, that I'm inclined to go out and meet friends""


<CRLF>(this)<CRLF>

-----------------------------265001916915724
Content-Disposition: form-data; name=""documentfile""; filename=""temp.htm""
Content-Type: text/html

<html>
    <head>
    </head>
    <body>
        <form action='http://*****:*****@"C:\serverstests";
            int i = 1;
            while (Directory.Exists(directoryNotToBeCreated))
            {
                i++;
                directoryNotToBeCreated = @"C:\serverstests_" + i;
            }

            for (int cs = 1; cs < testCase.Length; cs++)
            {
                HttpRequest r = new HttpRequest
                {
                    Url = new HttpUrl("example.com", "/"),
                    Headers = new HttpRequestHeaders
                    {
                        ContentLength = inputStr.Length,
                        ContentMultipartBoundary = "---------------------------265001916915724",
                        ContentType = HttpPostContentType.MultipartFormData
                    },
                    Method = HttpMethod.Post
                };

                using (Stream f = new SlowStream(new MemoryStream(testCase), cs))
                {
                    r.ParsePostBody(f, directoryNotToBeCreated);
                    var gets = r.Url.Query.ToList();
                    var posts = r.Post;
                    var files = r.FileUploads;

                    Assert.IsTrue(files.ContainsKey("documentfile"));
                    Assert.AreEqual("temp.htm", files["documentfile"].Filename);
                    Assert.AreEqual("text/html", files["documentfile"].ContentType);

                    using (var stream = files["documentfile"].GetStream())
                    {
                        string fileContent = Encoding.UTF8.GetString(stream.ReadAllBytes());
                        Assert.AreEqual(@"<html>
    <head>
    </head>
    <body>
        <form action='http://*****:*****@"This is what should be found in ""y"" at the end of the test.", posts["y"].Value);
                    Assert.IsTrue(posts.ContainsKey("What a wonderful day it is today; so wonderful in fact, that I'm inclined to go out and meet friends"));
                    Assert.AreEqual("\r\n<CRLF>(this)<CRLF>\r\n",
                        posts["What a wonderful day it is today; so wonderful in fact, that I'm inclined to go out and meet friends"].Value);
                }
            }

            Assert.IsFalse(Directory.Exists(directoryNotToBeCreated));
        }