Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions tests/IceRpc.Ice.Codec.Tests/DictionaryDecodingTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) ZeroC, Inc.

using NUnit.Framework;
using System.Runtime.CompilerServices;
using ZeroC.Tests.Common;

namespace IceRpc.Ice.Codec.Tests;
Expand Down Expand Up @@ -34,4 +35,62 @@ public void Decode_dictionary()
Assert.That(decoded, Is.EqualTo(expected));
Assert.That(decoder.Consumed, Is.EqualTo(buffer.WrittenMemory.Length));
}

[TestCase(10)]
[TestCase(50)]
[TestCase(100)]
public void Decode_dictionary_exceeds_max_collection_allocation(int count)
{
// Arrange
var buffer = new MemoryBufferWriter(new byte[count * (Unsafe.SizeOf<int>() + Unsafe.SizeOf<long>()) + 256]);
var encoder = new IceEncoder(buffer);
var dict = Enumerable.Range(0, count).ToDictionary(k => k, v => (long)v);
encoder.EncodeDictionary(
dict,
(ref IceEncoder encoder, int key) => encoder.EncodeInt(key),
(ref IceEncoder encoder, long value) => encoder.EncodeLong(value));

int allocationLimit = (count - 1) * (Unsafe.SizeOf<int>() + Unsafe.SizeOf<long>());

// Act/Assert
Assert.That(
() =>
{
var sut = new IceDecoder(buffer.WrittenMemory, maxCollectionAllocation: allocationLimit);
_ = sut.DecodeDictionary(
count => new Dictionary<int, long>(count),
(ref IceDecoder decoder) => decoder.DecodeInt(),
(ref IceDecoder decoder) => decoder.DecodeLong());
},
Throws.InstanceOf<InvalidDataException>());
}

[TestCase(10)]
[TestCase(50)]
[TestCase(100)]
public void Decode_dictionary_within_max_collection_allocation(int count)
{
// Arrange
var buffer = new MemoryBufferWriter(new byte[count * (Unsafe.SizeOf<int>() + Unsafe.SizeOf<long>()) + 256]);
var encoder = new IceEncoder(buffer);
var dict = Enumerable.Range(0, count).ToDictionary(k => k, v => (long)v);
encoder.EncodeDictionary(
dict,
(ref IceEncoder encoder, int key) => encoder.EncodeInt(key),
(ref IceEncoder encoder, long value) => encoder.EncodeLong(value));

int allocationLimit = count * (Unsafe.SizeOf<int>() + Unsafe.SizeOf<long>());

// Act/Assert
Assert.That(
() =>
{
var sut = new IceDecoder(buffer.WrittenMemory, maxCollectionAllocation: allocationLimit);
_ = sut.DecodeDictionary(
count => new Dictionary<int, long>(count),
(ref IceDecoder decoder) => decoder.DecodeInt(),
(ref IceDecoder decoder) => decoder.DecodeLong());
},
Throws.Nothing);
}
}
49 changes: 49 additions & 0 deletions tests/IceRpc.Ice.Codec.Tests/SequenceDecodingTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) ZeroC, Inc.

using NUnit.Framework;
using System.Runtime.CompilerServices;
using ZeroC.Tests.Common;

namespace IceRpc.Ice.Codec.Tests;
Expand Down Expand Up @@ -79,4 +80,52 @@ public void Decode_sequence_with_element_action()
Assert.That(decoded, Is.EqualTo(expected));
Assert.That(checkedValues, Is.EqualTo(expected));
}

[TestCase(10)]
[TestCase(50)]
[TestCase(100)]
public void Decode_sequence_exceeds_max_collection_allocation(int count)
{
// Arrange
var buffer = new MemoryBufferWriter(new byte[count * Unsafe.SizeOf<int>() + 256]);
var encoder = new IceEncoder(buffer);
encoder.EncodeSequence(
Enumerable.Range(0, count),
(ref IceEncoder encoder, int value) => encoder.EncodeInt(value));

int allocationLimit = (count - 1) * Unsafe.SizeOf<int>();

// Act/Assert
Assert.That(
() =>
{
var sut = new IceDecoder(buffer.WrittenMemory, maxCollectionAllocation: allocationLimit);
_ = sut.DecodeSequence((ref IceDecoder decoder) => decoder.DecodeInt());
},
Throws.InstanceOf<InvalidDataException>());
}

[TestCase(10)]
[TestCase(50)]
[TestCase(100)]
public void Decode_sequence_within_max_collection_allocation(int count)
{
// Arrange
var buffer = new MemoryBufferWriter(new byte[count * Unsafe.SizeOf<int>() + 256]);
var encoder = new IceEncoder(buffer);
encoder.EncodeSequence(
Enumerable.Range(0, count),
(ref IceEncoder encoder, int value) => encoder.EncodeInt(value));

int allocationLimit = count * Unsafe.SizeOf<int>();

// Act/Assert
Assert.That(
() =>
{
var sut = new IceDecoder(buffer.WrittenMemory, maxCollectionAllocation: allocationLimit);
_ = sut.DecodeSequence((ref IceDecoder decoder) => decoder.DecodeInt());
},
Throws.Nothing);
}
}
47 changes: 47 additions & 0 deletions tests/IceRpc.Ice.Codec.Tests/StringDecodingTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) ZeroC, Inc.

using NUnit.Framework;
using System.Runtime.CompilerServices;
using ZeroC.Tests.Common;

namespace IceRpc.Ice.Codec.Tests;
Expand Down Expand Up @@ -42,4 +43,50 @@ public void Decode_non_utf8_string_fails()
_ = sut.DecodeString();
}, Throws.InstanceOf<InvalidDataException>());
}

[TestCase(10)]
[TestCase(50)]
[TestCase(100)]
public void Decode_string_exceeds_max_collection_allocation(int length)
{
// Arrange
string testString = new('a', length);
var buffer = new MemoryBufferWriter(new byte[length + 256]);
var encoder = new IceEncoder(buffer);
encoder.EncodeString(testString);

int allocationLimit = (length - 1) * Unsafe.SizeOf<char>();

// Act/Assert
Assert.That(
() =>
{
var sut = new IceDecoder(buffer.WrittenMemory, maxCollectionAllocation: allocationLimit);
_ = sut.DecodeString();
},
Throws.InstanceOf<InvalidDataException>());
}

[TestCase(10)]
[TestCase(50)]
[TestCase(100)]
public void Decode_string_within_max_collection_allocation(int length)
{
// Arrange
string testString = new('a', length);
var buffer = new MemoryBufferWriter(new byte[length + 256]);
var encoder = new IceEncoder(buffer);
encoder.EncodeString(testString);

int allocationLimit = length * Unsafe.SizeOf<char>();

// Act/Assert
Assert.That(
() =>
{
var sut = new IceDecoder(buffer.WrittenMemory, maxCollectionAllocation: allocationLimit);
_ = sut.DecodeString();
},
Throws.Nothing);
}
}
Loading