Ejemplo n.º 1
0
        public void ShouldReturnTheCorrectElementWhenIndexed()
        {
            var t = new DynamicTable(4096);

            t.Insert("a", 1, "b", 1);
            t.Insert("c", 1, "d", 1);
            t.Insert("e", 1, "f", 1);
            t.Insert("ab", 2, "cd", 2);
            Assert.Equal(new TableEntry {
                Name = "ab", NameLen = 2, Value = "cd", ValueLen = 2
            }, t.GetAt(0), ec);
            Assert.Equal(new TableEntry {
                Name = "e", NameLen = 1, Value = "f", ValueLen = 1
            }, t.GetAt(1), ec);
            Assert.Equal(new TableEntry {
                Name = "c", NameLen = 1, Value = "d", ValueLen = 1
            }, t.GetAt(2), ec);
            Assert.Equal(new TableEntry {
                Name = "a", NameLen = 1, Value = "b", ValueLen = 1
            }, t.GetAt(3), ec);

            t.Insert("xyz", 3, "fgh", 99);
            Assert.Equal(new TableEntry {
                Name = "xyz", NameLen = 3, Value = "fgh", ValueLen = 99
            }, t.GetAt(0), ec);
        }
Ejemplo n.º 2
0
        public void DynamicTable_FirstEntry_IsMostRecentEntry()
        {
            DynamicTable dynamicTable = new DynamicTable(4096);

            dynamicTable.Insert(_header1.Name, _header1.Value);
            dynamicTable.Insert(_header2.Name, _header2.Value);

            VerifyTableEntries(dynamicTable, _header2, _header1);
        }
Ejemplo n.º 3
0
        public void DynamicTable_Count_IsNumberOfEntriesInDynamicTable()
        {
            DynamicTable dynamicTable = new DynamicTable(4096);

            dynamicTable.Insert(_header1.Name, _header1.Value);
            Assert.Equal(1, dynamicTable.Count);

            dynamicTable.Insert(_header2.Name, _header2.Value);
            Assert.Equal(2, dynamicTable.Count);
        }
Ejemplo n.º 4
0
        public void DynamicTable_Size_IsCurrentDynamicTableSize()
        {
            DynamicTable dynamicTable = new DynamicTable(4096);

            Assert.Equal(0, dynamicTable.Size);

            dynamicTable.Insert(_header1.Name, _header1.Value);
            Assert.Equal(_header1.Length, dynamicTable.Size);

            dynamicTable.Insert(_header2.Name, _header2.Value);
            Assert.Equal(_header1.Length + _header2.Length, dynamicTable.Size);
        }
Ejemplo n.º 5
0
        public void DynamicTable_ResizingToZeroEvictsAllEntries()
        {
            DynamicTable dynamicTable = new DynamicTable(4096);

            dynamicTable.Insert(_header1.Name, _header1.Value);
            dynamicTable.Insert(_header2.Name, _header2.Value);

            dynamicTable.Resize(0);

            Assert.Equal(0, dynamicTable.Count);
            Assert.Equal(0, dynamicTable.Size);
        }
Ejemplo n.º 6
0
        public void DynamicTable_InsertEntryLargerThanRemainingSpace_NoOp()
        {
            DynamicTable dynamicTable = new DynamicTable(_header1.Length);

            dynamicTable.Insert(_header1.Name, _header1.Value);

            VerifyTableEntries(dynamicTable, _header1);

            dynamicTable.Insert(_header2.Name, _header2.Value);

            Assert.Equal(0, dynamicTable.Count);
            Assert.Equal(0, dynamicTable.Size);
        }
Ejemplo n.º 7
0
        public void DynamicTable_ResizingEvictsOldestEntries()
        {
            DynamicTable dynamicTable = new DynamicTable(4096);

            dynamicTable.Insert(_header1.Name, _header1.Value);
            dynamicTable.Insert(_header2.Name, _header2.Value);

            VerifyTableEntries(dynamicTable, _header2, _header1);

            dynamicTable.Resize(_header2.Length);

            VerifyTableEntries(dynamicTable, _header2);
        }
Ejemplo n.º 8
0
        public void ShouldReturnTrueIfAnItemCouldBeInserted()
        {
            var t = new DynamicTable(4096);

            var res = t.Insert("a", 1, "b", 1);

            Assert.True(res);

            res = t.Insert("a", 1, "b", 1);
            Assert.True(res);

            res = t.Insert("a", 1, "b", 4096 - 2 * (1 + 1 + 32) - 32 - 1);
            Assert.True(res);
        }
Ejemplo n.º 9
0
        public void WrapsAroundBuffer()
        {
            var header3 = new HeaderField(Encoding.ASCII.GetBytes("header-3"), Encoding.ASCII.GetBytes("value3"));
            var header4 = new HeaderField(Encoding.ASCII.GetBytes("header-4"), Encoding.ASCII.GetBytes("value4"));

            // Make the table small enough that the circular buffer kicks in.
            var dynamicTable = new DynamicTable(HeaderField.RfcOverhead * 3);

            dynamicTable.Insert(header4.Name, header4.Value);
            dynamicTable.Insert(header3.Name, header3.Value);
            dynamicTable.Insert(_header2.Name, _header2.Value);
            dynamicTable.Insert(_header1.Name, _header1.Value);

            VerifyTableEntries(dynamicTable, _header1, _header2);
        }
Ejemplo n.º 10
0
 public void GlobalSetup()
 {
     _dynamicTable = new DynamicTable(maxSize: 4096);
     _dynamicTable.Insert(_headerNameBytes, _headerValueBytes);
     _decoder            = new HPackDecoder(maxDynamicTableSize: 4096, maxHeadersLength: 65536, _dynamicTable);
     _testHeadersHandler = new TestHeadersHandler();
 }
Ejemplo n.º 11
0
        static bool InsertItemOfSize(DynamicTable table, string keyName, int size)
        {
            size -= 32;
            var aSize = size / 2;
            var bSize = size - aSize;

            return(table.Insert(keyName, aSize, "", bSize));
        }
Ejemplo n.º 12
0
    private void ProcessValue()
    {
        OnString(nextState: State.Ready);
        var headerNameSpan  = new Span <byte>(_headerName, 0, _headerNameLength);
        var headerValueSpan = new Span <byte>(_headerValueOctets, 0, _headerValueLength);

        _dynamicTable.Insert(headerNameSpan, headerValueSpan);
    }
Ejemplo n.º 13
0
        public void DecodesIndexedHeaderField_DynamicTable()
        {
            // Add the header to the dynamic table
            _dynamicTable.Insert(_headerNameBytes, _headerValueBytes);

            // Index it
            _decoder.Decode(new ReadOnlySequence <byte>(_indexedHeaderDynamic), endHeaders: true, handler: this);
            Assert.Equal(_headerValueString, _decodedHeaders[_headerNameString]);
        }
Ejemplo n.º 14
0
        public void DynamicTable_InsertEntryLargerThanMaxSize_NoOp()
        {
            DynamicTable dynamicTable = new DynamicTable(_header1.Length - 1);

            dynamicTable.Insert(_header1.Name, _header1.Value);

            Assert.Equal(0, dynamicTable.Count);
            Assert.Equal(0, dynamicTable.Size);
        }
Ejemplo n.º 15
0
        public void DynamicTable_CanBeResizedToLargerMaxSize()
        {
            DynamicTable dynamicTable = new DynamicTable(_header1.Length);

            dynamicTable.Insert(_header1.Name, _header1.Value);
            dynamicTable.Insert(_header2.Name, _header2.Value);

            // _header2 is larger than _header1, so an attempt at inserting it
            // would first clear the table then return without actually inserting it,
            // given it is larger than the current max size.
            Assert.Equal(0, dynamicTable.Count);
            Assert.Equal(0, dynamicTable.Size);

            dynamicTable.Resize(dynamicTable.MaxSize + _header2.Length);
            dynamicTable.Insert(_header2.Name, _header2.Value);

            VerifyTableEntries(dynamicTable, _header2);
        }
        public void NoOpWhenInsertingEntryLargerThanMaxSize()
        {
            var dynamicTable = new DynamicTable(_header1.Length - 1);

            dynamicTable.Insert(_header1.Name, _header1.Value);

            Assert.Equal(0, dynamicTable.Count);
            Assert.Equal(0, dynamicTable.Size);
        }
Ejemplo n.º 17
0
        public void DecodesIndexedHeaderField_DynamicTable()
        {
            // Add the header to the dynamic table
            _dynamicTable.Insert(_headerNameBytes, _headerValueBytes);

            // Index it
            _decoder.Decode(_indexedHeaderDynamic, endHeaders: true, onHeader: OnHeader, onHeaderState: null);
            Assert.Equal(_headerValueString, _decodedHeaders[_headerNameString]);
        }
Ejemplo n.º 18
0
        public void BoundsCheck_ThrowsIndexOutOfRangeException()
        {
            DynamicTable dynamicTable = new DynamicTable(4096);

            Assert.Throws <IndexOutOfRangeException>(() => dynamicTable[0]);

            dynamicTable.Insert(_header1.Name, _header1.Value);
            Assert.Throws <IndexOutOfRangeException>(() => dynamicTable[1]);
        }
Ejemplo n.º 19
0
        public void ShouldDrainTheTableIfAnElementCanNotBeInserted()
        {
            var t = new DynamicTable(4096);

            var res = t.Insert("a", 2000, "b", 2000);

            Assert.True(res);
            Assert.Equal(1, t.Length);
            Assert.Equal(4032, t.UsedSize);

            res = t.Insert("a", 0, "b", 32);
            Assert.True(res);
            Assert.Equal(4096, t.UsedSize);
            Assert.Equal(2, t.Length);

            res = t.Insert("a", 3000, "b", 3000);
            Assert.False(res);
            Assert.Equal(t.UsedSize, 0);
            Assert.Equal(0, t.Length);
        }
Ejemplo n.º 20
0
        public void ShouldReturnFalseIfAnItemCanNotBeInserted()
        {
            var t = new DynamicTable(4096);

            var res = t.Insert("a", 5000, "b", 0);

            Assert.False(res);
            Assert.Equal(0, t.Length);
            Assert.Equal(t.UsedSize, 0);

            res = t.Insert("a", 0, "b", 5000);
            Assert.False(res);
            Assert.Equal(0, t.Length);
            Assert.Equal(t.UsedSize, 0);

            res = t.Insert("a", 3000, "b", 3000);
            Assert.False(res);
            Assert.Equal(t.UsedSize, 0);
            Assert.Equal(0, t.Length);
        }
Ejemplo n.º 21
0
        public void ShouldIncreaseUsedSizeAndLengthIfItemIsInserted()
        {
            var t = new DynamicTable(4096);

            t.Insert("a", 1, "b", 1);
            var expectedSize = 1 + 1 + 32;

            Assert.Equal(expectedSize, t.UsedSize);
            Assert.Equal(1, t.Length);

            t.Insert("xyz", 3, "abcd", 4);
            expectedSize += 3 + 4 + 32;
            Assert.Equal(expectedSize, t.UsedSize);
            Assert.Equal(2, t.Length);

            // Fill the table up
            t.Insert("", 0, "a", 4096 - expectedSize - 32);
            expectedSize = 4096;
            Assert.Equal(expectedSize, t.UsedSize);
            Assert.Equal(3, t.Length);
        }
Ejemplo n.º 22
0
        public void ShouldThrowAnErrorWhenInvalidlyIndexed()
        {
            var t = new DynamicTable(4096);

            Assert.Throws <IndexOutOfRangeException>(() => t.GetAt(-1));
            Assert.Throws <IndexOutOfRangeException>(() => t.GetAt(0));
            Assert.Throws <IndexOutOfRangeException>(() => t.GetAt(1));

            t.Insert("a", 0, "b", 0);
            Assert.Throws <IndexOutOfRangeException>(() => t.GetAt(-1));
            t.GetAt(0); // should not throw
            Assert.Throws <IndexOutOfRangeException>(() => t.GetAt(1));
        }
Ejemplo n.º 23
0
        public void DynamicTable_WrapsRingBuffer_Success(int targetInsertIndex)
        {
            FieldInfo      insertIndexField = typeof(DynamicTable).GetField("_insertIndex", BindingFlags.NonPublic | BindingFlags.Instance);
            DynamicTable   table            = new DynamicTable(maxSize: 256);
            Stack <byte[]> insertedHeaders  = new Stack <byte[]>();

            // Insert into dynamic table until its insert index into its ring buffer loops back to 0.
            do
            {
                InsertOne();
            }while ((int)insertIndexField.GetValue(table) != 0);

            // Finally loop until the insert index reaches the target.
            while ((int)insertIndexField.GetValue(table) != targetInsertIndex)
            {
                InsertOne();
            }

            void InsertOne()
            {
                byte[] data = Encoding.ASCII.GetBytes($"header-{insertedHeaders.Count}");

                insertedHeaders.Push(data);
                table.Insert(data, data);
            }

            // Now check to see that we can retrieve the remaining headers.
            // Some headers will have been evacuated from the table during this process, so we don't exhaust the entire insertedHeaders stack.
            Assert.True(table.Count > 0);
            Assert.True(table.Count < insertedHeaders.Count);

            for (int i = 0; i < table.Count; ++i)
            {
                HeaderField dynamicField = table[i];
                byte[]      expectedData = insertedHeaders.Pop();

                Assert.True(expectedData.AsSpan().SequenceEqual(dynamicField.Name));
                Assert.True(expectedData.AsSpan().SequenceEqual(dynamicField.Value));
            }
        }
Ejemplo n.º 24
0
        public void DynamicTable_Resize_Success(int initialMaxSize, int finalMaxSize, int insertSize)
        {
            // This is purely to make it simple to perfectly reach our initial max size to test growing a full but non-wrapping buffer.
            Debug.Assert((insertSize % 64) == 0, $"{nameof(insertSize)} must be a multiple of 64 ({nameof(HeaderField)}.{nameof(HeaderField.RfcOverhead)} * 2)");

            DynamicTable dynamicTable = new DynamicTable(maxSize: initialMaxSize);
            int          insertedSize = 0;

            while (insertedSize != insertSize)
            {
                byte[] data = Encoding.ASCII.GetBytes($"header-{dynamicTable.Size}".PadRight(16, ' '));
                Debug.Assert(data.Length == 16);

                dynamicTable.Insert(data, data);
                insertedSize += data.Length * 2 + HeaderField.RfcOverhead;
            }

            List <HeaderField> headers = new List <HeaderField>();

            for (int i = 0; i < dynamicTable.Count; ++i)
            {
                headers.Add(dynamicTable[i]);
            }

            dynamicTable.Resize(finalMaxSize);

            int expectedCount = Math.Min(finalMaxSize / 64, headers.Count);

            Assert.Equal(expectedCount, dynamicTable.Count);

            for (int i = 0; i < dynamicTable.Count; ++i)
            {
                Assert.True(headers[i].Name.AsSpan().SequenceEqual(dynamicTable[i].Name));
                Assert.True(headers[i].Value.AsSpan().SequenceEqual(dynamicTable[i].Value));
            }
        }