Jan Tattermusch | 638a081 | 2020-04-14 18:04:47 +0200 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | // Protocol Buffers - Google's data interchange format |
| 3 | // Copyright 2015 Google Inc. All rights reserved. |
| 4 | // https://developers.google.com/protocol-buffers/ |
| 5 | // |
| 6 | // Redistribution and use in source and binary forms, with or without |
| 7 | // modification, are permitted provided that the following conditions are |
| 8 | // met: |
| 9 | // |
| 10 | // * Redistributions of source code must retain the above copyright |
| 11 | // notice, this list of conditions and the following disclaimer. |
| 12 | // * Redistributions in binary form must reproduce the above |
| 13 | // copyright notice, this list of conditions and the following disclaimer |
| 14 | // in the documentation and/or other materials provided with the |
| 15 | // distribution. |
| 16 | // * Neither the name of Google Inc. nor the names of its |
| 17 | // contributors may be used to endorse or promote products derived from |
| 18 | // this software without specific prior written permission. |
| 19 | // |
| 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | #endregion |
| 32 | |
| 33 | using NUnit.Framework; |
| 34 | using System; |
Jan Tattermusch | 638a081 | 2020-04-14 18:04:47 +0200 | [diff] [blame] | 35 | using System.Buffers; |
Jan Tattermusch | 2bce090 | 2020-06-03 14:36:39 +0200 | [diff] [blame] | 36 | using Google.Protobuf.Buffers; |
Jan Tattermusch | 638a081 | 2020-04-14 18:04:47 +0200 | [diff] [blame] | 37 | |
| 38 | namespace Google.Protobuf |
| 39 | { |
| 40 | public static class MessageParsingHelpers |
| 41 | { |
| 42 | public static void AssertReadingMessage<T>(MessageParser<T> parser, byte[] bytes, Action<T> assert) where T : IMessage<T> |
| 43 | { |
Jan Tattermusch | 2f0e269 | 2021-05-04 11:39:15 +0200 | [diff] [blame] | 44 | var parsedMsg = parser.ParseFrom(bytes); |
| 45 | assert(parsedMsg); |
Jan Tattermusch | 638a081 | 2020-04-14 18:04:47 +0200 | [diff] [blame] | 46 | |
| 47 | // Load content as single segment |
Jan Tattermusch | 2f0e269 | 2021-05-04 11:39:15 +0200 | [diff] [blame] | 48 | parsedMsg = parser.ParseFrom(new ReadOnlySequence<byte>(bytes)); |
| 49 | assert(parsedMsg); |
Jan Tattermusch | 638a081 | 2020-04-14 18:04:47 +0200 | [diff] [blame] | 50 | |
| 51 | // Load content as multiple segments |
Jan Tattermusch | 2f0e269 | 2021-05-04 11:39:15 +0200 | [diff] [blame] | 52 | parsedMsg = parser.ParseFrom(ReadOnlySequenceFactory.CreateWithContent(bytes)); |
| 53 | assert(parsedMsg); |
Jan Tattermusch | 638a081 | 2020-04-14 18:04:47 +0200 | [diff] [blame] | 54 | |
Jensaarai | 5095b68 | 2021-05-03 12:03:22 -0700 | [diff] [blame] | 55 | // Load content as ReadOnlySpan |
Jan Tattermusch | 2f0e269 | 2021-05-04 11:39:15 +0200 | [diff] [blame] | 56 | parsedMsg = parser.ParseFrom(new ReadOnlySpan<byte>(bytes)); |
| 57 | assert(parsedMsg); |
Jan Tattermusch | 638a081 | 2020-04-14 18:04:47 +0200 | [diff] [blame] | 58 | } |
| 59 | |
Jan Tattermusch | 238fd35 | 2020-04-15 10:08:19 +0200 | [diff] [blame] | 60 | public static void AssertReadingMessage(MessageParser parser, byte[] bytes, Action<IMessage> assert) |
| 61 | { |
Jan Tattermusch | 2f0e269 | 2021-05-04 11:39:15 +0200 | [diff] [blame] | 62 | var parsedMsg = parser.ParseFrom(bytes); |
| 63 | assert(parsedMsg); |
Jan Tattermusch | 238fd35 | 2020-04-15 10:08:19 +0200 | [diff] [blame] | 64 | |
| 65 | // Load content as single segment |
Jan Tattermusch | 2f0e269 | 2021-05-04 11:39:15 +0200 | [diff] [blame] | 66 | parsedMsg = parser.ParseFrom(new ReadOnlySequence<byte>(bytes)); |
| 67 | assert(parsedMsg); |
Jan Tattermusch | 238fd35 | 2020-04-15 10:08:19 +0200 | [diff] [blame] | 68 | |
| 69 | // Load content as multiple segments |
Jan Tattermusch | 2f0e269 | 2021-05-04 11:39:15 +0200 | [diff] [blame] | 70 | parsedMsg = parser.ParseFrom(ReadOnlySequenceFactory.CreateWithContent(bytes)); |
| 71 | assert(parsedMsg); |
Jan Tattermusch | f1d12ac | 2020-04-15 11:26:58 +0200 | [diff] [blame] | 72 | |
Jensaarai | 5095b68 | 2021-05-03 12:03:22 -0700 | [diff] [blame] | 73 | // Load content as ReadOnlySpan |
Jan Tattermusch | 2f0e269 | 2021-05-04 11:39:15 +0200 | [diff] [blame] | 74 | parsedMsg = parser.ParseFrom(new ReadOnlySpan<byte>(bytes)); |
| 75 | assert(parsedMsg); |
Jan Tattermusch | 238fd35 | 2020-04-15 10:08:19 +0200 | [diff] [blame] | 76 | } |
| 77 | |
Jan Tattermusch | 638a081 | 2020-04-14 18:04:47 +0200 | [diff] [blame] | 78 | public static void AssertReadingMessageThrows<TMessage, TException>(MessageParser<TMessage> parser, byte[] bytes) |
| 79 | where TMessage : IMessage<TMessage> |
| 80 | where TException : Exception |
| 81 | { |
| 82 | Assert.Throws<TException>(() => parser.ParseFrom(bytes)); |
| 83 | |
| 84 | Assert.Throws<TException>(() => parser.ParseFrom(new ReadOnlySequence<byte>(bytes))); |
Jensaarai | 5095b68 | 2021-05-03 12:03:22 -0700 | [diff] [blame] | 85 | |
Jan Tattermusch | 89244fe | 2021-05-04 11:24:55 +0200 | [diff] [blame] | 86 | Assert.Throws<TException>(() => parser.ParseFrom(new ReadOnlySpan<byte>(bytes))); |
Jan Tattermusch | 638a081 | 2020-04-14 18:04:47 +0200 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | public static void AssertRoundtrip<T>(MessageParser<T> parser, T message, Action<T> additionalAssert = null) where T : IMessage<T> |
| 90 | { |
| 91 | var bytes = message.ToByteArray(); |
| 92 | |
Jan Tattermusch | 2bce090 | 2020-06-03 14:36:39 +0200 | [diff] [blame] | 93 | // also serialize using IBufferWriter and check it leads to the same data |
James Newton-King | 8932446 | 2020-12-16 12:56:14 +1300 | [diff] [blame] | 94 | var bufferWriter = new TestArrayBufferWriter<byte>(); |
Jan Tattermusch | 2bce090 | 2020-06-03 14:36:39 +0200 | [diff] [blame] | 95 | message.WriteTo(bufferWriter); |
| 96 | Assert.AreEqual(bytes, bufferWriter.WrittenSpan.ToArray(), "Both serialization approaches need to result in the same data."); |
| 97 | |
Jan Tattermusch | 2f0e269 | 2021-05-04 11:39:15 +0200 | [diff] [blame] | 98 | var parsedMsg = parser.ParseFrom(bytes); |
| 99 | Assert.AreEqual(message, parsedMsg); |
| 100 | additionalAssert?.Invoke(parsedMsg); |
Jensaarai | 5095b68 | 2021-05-03 12:03:22 -0700 | [diff] [blame] | 101 | |
Jan Tattermusch | 638a081 | 2020-04-14 18:04:47 +0200 | [diff] [blame] | 102 | // Load content as single segment |
Jan Tattermusch | 2f0e269 | 2021-05-04 11:39:15 +0200 | [diff] [blame] | 103 | parsedMsg = parser.ParseFrom(new ReadOnlySequence<byte>(bytes)); |
| 104 | Assert.AreEqual(message, parsedMsg); |
| 105 | additionalAssert?.Invoke(parsedMsg); |
Jan Tattermusch | 638a081 | 2020-04-14 18:04:47 +0200 | [diff] [blame] | 106 | |
| 107 | // Load content as multiple segments |
Jan Tattermusch | 2f0e269 | 2021-05-04 11:39:15 +0200 | [diff] [blame] | 108 | parsedMsg = parser.ParseFrom(ReadOnlySequenceFactory.CreateWithContent(bytes)); |
| 109 | Assert.AreEqual(message, parsedMsg); |
| 110 | additionalAssert?.Invoke(parsedMsg); |
Jan Tattermusch | 638a081 | 2020-04-14 18:04:47 +0200 | [diff] [blame] | 111 | |
Jensaarai | 5095b68 | 2021-05-03 12:03:22 -0700 | [diff] [blame] | 112 | // Load content as ReadOnlySpan |
Jan Tattermusch | 2f0e269 | 2021-05-04 11:39:15 +0200 | [diff] [blame] | 113 | parsedMsg = parser.ParseFrom(new ReadOnlySpan<byte>(bytes)); |
| 114 | Assert.AreEqual(message, parsedMsg); |
| 115 | additionalAssert?.Invoke(parsedMsg); |
Jan Tattermusch | 638a081 | 2020-04-14 18:04:47 +0200 | [diff] [blame] | 116 | } |
Jan Tattermusch | 2bce090 | 2020-06-03 14:36:39 +0200 | [diff] [blame] | 117 | |
| 118 | public static void AssertWritingMessage(IMessage message) |
| 119 | { |
| 120 | // serialize using CodedOutputStream |
| 121 | var bytes = message.ToByteArray(); |
| 122 | |
Jan Tattermusch | b3cdba1 | 2020-06-03 15:50:07 +0200 | [diff] [blame] | 123 | int messageSize = message.CalculateSize(); |
| 124 | Assert.AreEqual(message.CalculateSize(), bytes.Length); |
| 125 | |
| 126 | // serialize using IBufferWriter and check it leads to the same output |
James Newton-King | 8932446 | 2020-12-16 12:56:14 +1300 | [diff] [blame] | 127 | var bufferWriter = new TestArrayBufferWriter<byte>(); |
Jan Tattermusch | 2bce090 | 2020-06-03 14:36:39 +0200 | [diff] [blame] | 128 | message.WriteTo(bufferWriter); |
Jan Tattermusch | b3cdba1 | 2020-06-03 15:50:07 +0200 | [diff] [blame] | 129 | Assert.AreEqual(bytes, bufferWriter.WrittenSpan.ToArray()); |
Jan Tattermusch | 2bce090 | 2020-06-03 14:36:39 +0200 | [diff] [blame] | 130 | |
Jan Tattermusch | b3cdba1 | 2020-06-03 15:50:07 +0200 | [diff] [blame] | 131 | // serialize into a single span and check it leads to the same output |
| 132 | var singleSpan = new Span<byte>(new byte[messageSize]); |
| 133 | message.WriteTo(singleSpan); |
| 134 | Assert.AreEqual(bytes, singleSpan.ToArray()); |
Jan Tattermusch | 8dbf707 | 2020-06-03 14:47:55 +0200 | [diff] [blame] | 135 | |
Jan Tattermusch | e14a5c8 | 2020-06-08 16:23:33 +0200 | [diff] [blame] | 136 | // test for different IBufferWriter.GetSpan() segment sizes |
| 137 | for (int blockSize = 1; blockSize < 256; blockSize *= 2) |
| 138 | { |
James Newton-King | 8932446 | 2020-12-16 12:56:14 +1300 | [diff] [blame] | 139 | var segmentedBufferWriter = new TestArrayBufferWriter<byte>(); |
Jan Tattermusch | e14a5c8 | 2020-06-08 16:23:33 +0200 | [diff] [blame] | 140 | segmentedBufferWriter.MaxGrowBy = blockSize; |
| 141 | message.WriteTo(segmentedBufferWriter); |
Jan Tattermusch | 53708e2 | 2020-06-08 17:03:03 +0200 | [diff] [blame] | 142 | Assert.AreEqual(bytes, segmentedBufferWriter.WrittenSpan.ToArray()); |
| 143 | } |
| 144 | |
| 145 | // if the full message is small enough, try serializing directly into stack-allocated buffer |
| 146 | if (bytes.Length <= 256) |
| 147 | { |
| 148 | Span<byte> stackAllocBuffer = stackalloc byte[bytes.Length]; |
| 149 | message.WriteTo(stackAllocBuffer); |
| 150 | Assert.AreEqual(bytes, stackAllocBuffer.ToArray()); |
Jan Tattermusch | e14a5c8 | 2020-06-08 16:23:33 +0200 | [diff] [blame] | 151 | } |
Jan Tattermusch | 2bce090 | 2020-06-03 14:36:39 +0200 | [diff] [blame] | 152 | } |
Jan Tattermusch | 638a081 | 2020-04-14 18:04:47 +0200 | [diff] [blame] | 153 | } |
| 154 | } |