コード例 #1
0
ファイル: BasicUnitTests.cs プロジェクト: wearhere/corefxlab
        public void ValidInputOnlyMultiByte()
        {
            Span <byte> inputSpan = new byte[1000];

            Base64TestHelper.InitalizeDecodableBytes(inputSpan);
            int         requiredLength = Base64.GetMaxDecodedFromUtf8Length(inputSpan.Length);
            Span <byte> expected       = new byte[requiredLength];

            Assert.Equal(OperationStatus.Done, Base64.DecodeFromUtf8(inputSpan, expected, out int bytesConsumed, out int bytesWritten));

            byte[][] input = new byte[10][];

            int[] split = { 100, 102, 98, 101, 2, 97, 101, 1, 2, 396 };

            int sum = 0;

            for (int i = 0; i < split.Length; i++)
            {
                int splitter = split[i];
                input[i] = inputSpan.Slice(sum, splitter).ToArray();
                sum     += splitter;
            }
            Assert.Equal(1000, sum);

            var(list, lenght) = MemoryList.Create(input);

            var output = new TestOutput();

            Base64Experimental.Utf8ToBytesDecoder.Pipe(new ReadOnlyBytes(list, lenght), output);

            var expectedArray = expected.ToArray();
            var array         = output.GetBuffer.ToArray();

            Assert.True(expected.SequenceEqual(output.GetBuffer.Slice(0, requiredLength)));
        }
コード例 #2
0
 public static ReadOnlyBytes CreateRob(params byte[][] buffers)
 {
     if (buffers.Length == 1)
     {
         return(new ReadOnlyBytes(buffers[0]));
     }
     var(first, last) = MemoryList.Create(buffers);
     return(new ReadOnlyBytes(first, last));
 }
コード例 #3
0
        public void SequencePositionOfMultiSegment()
        {
            var(list, length) = MemoryList.Create(
                new byte[] { 1, 2 },
                new byte[] { 3, 4 }
                );
            var bytes = new ReadOnlyBytes(list, length);

            Assert.Equal(4, length);
            Assert.Equal(4, bytes.Length);

            // Static method call to avoid calling instance methods
            Assert.Equal(Position.End, Sequence.PositionOf(bytes, 0));

            for (int i = 0; i < bytes.Length; i++)
            {
                var value = (byte)(i + 1);

                var listPosition = Sequence.PositionOf(list, value);
                var(node, index) = listPosition.Get <IMemoryList <byte> >();

                if (!listPosition.IsEnd)
                {
                    Assert.Equal(value, node.Memory.Span[index]);
                }

                var robPosition         = bytes.PositionOf(value);
                var robSequencePosition = Sequence.PositionOf(bytes, value);

                Assert.Equal(listPosition, robPosition);
                Assert.Equal(listPosition, robSequencePosition);

                var robSlice = bytes.Slice(1);
                robPosition         = robSlice.PositionOf(value);
                robSequencePosition = Sequence.PositionOf(robSlice, value);

                if (i > 0)
                {
                    Assert.Equal(listPosition, robPosition);
                    Assert.Equal(listPosition, robSequencePosition);
                }
                else
                {
                    Assert.Equal(Position.End, robPosition);
                    Assert.Equal(Position.End, robSequencePosition);
                }

                if (listPosition != Position.End)
                {
                    robSlice = bytes.Slice(listPosition);
                    Assert.Equal(value, robSlice.Memory.Span[0]);
                }
            }
        }
コード例 #4
0
        public void HttpParserSegmentedRob(string requestText)
        {
            var parser = new HttpParser();

            for (int pivot = 26; pivot < requestText.Length; pivot++)
            {
                var front = requestText.Substring(0, pivot);
                var back  = requestText.Substring(pivot);

                var frontBytes = Encoding.ASCII.GetBytes(front);
                var endBytes   = Encoding.ASCII.GetBytes(back);

                var(first, last) = MemoryList.Create(frontBytes, endBytes);
                ReadOnlyBytes buffer = new ReadOnlyBytes(first, last);

                var request = new Request();

                try {
                    Assert.True(parser.ParseRequestLine(ref request, buffer, out var consumed));
                    Assert.Equal(25, consumed);

                    var unconsumed = buffer.Slice(consumed);
                    Assert.True(parser.ParseHeaders(ref request, unconsumed, out consumed));
                    Assert.Equal(8, consumed);
                }
                catch {
                    throw;
                }

                // request line
                Assert.Equal(Http.Method.Get, request.Method);
                Assert.Equal(Http.Version.Http11, request.Version);
                Assert.Equal("/plaintext", request.Path);

                // headers
                Assert.Equal(1, request.Headers.Count);
                Assert.True(request.Headers.ContainsKey("N"));
                Assert.Equal("V", request.Headers["N"]);
            }
        }