public void MaliciousRecursion() { ByteString data64 = MakeRecursiveMessage(64).ToByteString(); ByteString data65 = MakeRecursiveMessage(65).ToByteString(); AssertMessageDepth(TestRecursiveMessage.ParseFrom(data64), 64); try { TestRecursiveMessage.ParseFrom(data65); Assert.Fail("Should have thrown an exception!"); } catch (InvalidProtocolBufferException) { // success. } CodedInputStream input = data64.CreateCodedInput(); input.SetRecursionLimit(8); try { TestRecursiveMessage.ParseFrom(input); Assert.Fail("Should have thrown an exception!"); } catch (InvalidProtocolBufferException) { // success. } }
private static TestRecursiveMessage MakeRecursiveMessage(int depth) { if (depth == 0) { return(TestRecursiveMessage.CreateBuilder().SetI(5).Build()); } else { return(TestRecursiveMessage.CreateBuilder() .SetA(MakeRecursiveMessage(depth - 1)).Build()); } }
internal static void AssertMessageDepth(TestRecursiveMessage message, int depth) { if (depth == 0) { Assert.IsNull(message.A); Assert.AreEqual(5, message.I); } else { Assert.IsNotNull(message.A); AssertMessageDepth(message.A, depth - 1); } }
private static void AssertMessageDepth(TestRecursiveMessage message, int depth) { if (depth == 0) { Assert.IsFalse(message.HasA); Assert.AreEqual(5, message.I); } else { Assert.IsTrue(message.HasA); AssertMessageDepth(message.A, depth - 1); } }
public void MaliciousRecursion() { ByteString data64 = MakeRecursiveMessage(64).ToByteString(); ByteString data65 = MakeRecursiveMessage(65).ToByteString(); AssertMessageDepth(TestRecursiveMessage.ParseFrom(data64), 64); Assert.Throws <InvalidProtocolBufferException>(() => TestRecursiveMessage.ParseFrom(data65)); CodedInputStream input = data64.CreateCodedInput(); input.SetRecursionLimit(8); Assert.Throws <InvalidProtocolBufferException>(() => TestRecursiveMessage.ParseFrom(input)); }