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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
public void GlobalSetup() { _dynamicTable = new DynamicTable(maxSize: 4096); _dynamicTable.Insert(_headerNameBytes, _headerValueBytes); _decoder = new HPackDecoder(maxDynamicTableSize: 4096, maxHeadersLength: 65536, _dynamicTable); _testHeadersHandler = new TestHeadersHandler(); }
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)); }
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); }
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]); }
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); }
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); }
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]); }
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]); }
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); }
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); }
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); }
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)); }
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)); } }
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)); } }