|
| 1 | +/* |
| 2 | + * Copyright 2014 - 2026 Adaptive Financial Consulting Ltd |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +using System.Text; |
| 18 | +using Adaptive.Agrona.Concurrent; |
| 19 | +using NUnit.Framework; |
| 20 | + |
| 21 | +namespace Adaptive.Agrona.Tests |
| 22 | +{ |
| 23 | + /// <summary> |
| 24 | + /// Regression tests for a Java-to-.NET porting bug in <see cref="UnsafeBuffer"/> and |
| 25 | + /// <see cref="ExpandableArrayBuffer"/> where ASCII string helpers used |
| 26 | + /// <c>*(char*)(buf + i) = c</c> in a byte-indexed loop. Because a .NET <c>char</c> |
| 27 | + /// is 2 bytes, that: |
| 28 | + /// (1) overran the bounds-checked region by one byte in |
| 29 | + /// <c>PutStringWithoutLengthAscii</c>, and |
| 30 | + /// (2) read the same 2 bytes every iteration in |
| 31 | + /// <c>GetStringAscii(int, StringBuilder)</c> because the offset |
| 32 | + /// expression mistakenly used <c>index</c> instead of the loop cursor. |
| 33 | + /// </summary> |
| 34 | + [TestFixture] |
| 35 | + public class AsciiStringBufferCharWidthTests |
| 36 | + { |
| 37 | + // ---- UnsafeBuffer ---- |
| 38 | + |
| 39 | + [Test] |
| 40 | + public void UnsafeBuffer_PutStringWithoutLengthAscii_DoesNotOverrunBoundsByOneByte() |
| 41 | + { |
| 42 | + byte[] backing = { 0xAA, 0xAA, 0xAA, 0xAA }; |
| 43 | + var buffer = new UnsafeBuffer(backing); |
| 44 | + |
| 45 | + int written = buffer.PutStringWithoutLengthAscii(0, "abc"); |
| 46 | + |
| 47 | + Assert.AreEqual(3, written); |
| 48 | + Assert.AreEqual((byte)'a', backing[0]); |
| 49 | + Assert.AreEqual((byte)'b', backing[1]); |
| 50 | + Assert.AreEqual((byte)'c', backing[2]); |
| 51 | + // With the pre-fix code this byte was clobbered to 0x00 by the |
| 52 | + // trailing high-byte of the final 2-byte char write. |
| 53 | + Assert.AreEqual(0xAA, backing[3], "sentinel byte at offset==length must not be overwritten"); |
| 54 | + } |
| 55 | + |
| 56 | + [Test] |
| 57 | + public void UnsafeBuffer_PutStringWithoutLengthAscii_WithValueOffsetAndLength_DoesNotOverrun() |
| 58 | + { |
| 59 | + byte[] backing = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; |
| 60 | + var buffer = new UnsafeBuffer(backing); |
| 61 | + |
| 62 | + int written = buffer.PutStringWithoutLengthAscii(0, "xabcdx", 1, 4); |
| 63 | + |
| 64 | + Assert.AreEqual(4, written); |
| 65 | + Assert.AreEqual((byte)'a', backing[0]); |
| 66 | + Assert.AreEqual((byte)'b', backing[1]); |
| 67 | + Assert.AreEqual((byte)'c', backing[2]); |
| 68 | + Assert.AreEqual((byte)'d', backing[3]); |
| 69 | + Assert.AreEqual(0xAA, backing[4], "sentinel byte at offset==length must not be overwritten"); |
| 70 | + } |
| 71 | + |
| 72 | + [Test] |
| 73 | + public void UnsafeBuffer_GetStringAscii_StringBuilder_AdvancesReadOffset() |
| 74 | + { |
| 75 | + byte[] backing = new byte[16]; |
| 76 | + var buffer = new UnsafeBuffer(backing); |
| 77 | + |
| 78 | + buffer.PutStringAscii(0, "abcd"); |
| 79 | + |
| 80 | + var sb = new StringBuilder(); |
| 81 | + buffer.GetStringAscii(0, sb); |
| 82 | + |
| 83 | + // With the pre-fix code, every loop iteration read from `index` |
| 84 | + // (the length prefix), so the result was 4 copies of ''. |
| 85 | + Assert.AreEqual("abcd", sb.ToString()); |
| 86 | + } |
| 87 | + |
| 88 | + // ---- ExpandableArrayBuffer ---- |
| 89 | + |
| 90 | + [Test] |
| 91 | + public void ExpandableArrayBuffer_PutStringWithoutLengthAscii_DoesNotOverrunBoundsByOneByte() |
| 92 | + { |
| 93 | + var buffer = new ExpandableArrayBuffer(16); |
| 94 | + byte[] backing = buffer.ByteArray; |
| 95 | + for (int i = 0; i < backing.Length; i++) |
| 96 | + { |
| 97 | + backing[i] = 0xAA; |
| 98 | + } |
| 99 | + |
| 100 | + int written = buffer.PutStringWithoutLengthAscii(0, "abc"); |
| 101 | + |
| 102 | + Assert.AreEqual(3, written); |
| 103 | + Assert.AreEqual((byte)'a', backing[0]); |
| 104 | + Assert.AreEqual((byte)'b', backing[1]); |
| 105 | + Assert.AreEqual((byte)'c', backing[2]); |
| 106 | + Assert.AreEqual(0xAA, backing[3], "sentinel byte at offset==length must not be overwritten"); |
| 107 | + } |
| 108 | + |
| 109 | + [Test] |
| 110 | + public void ExpandableArrayBuffer_PutStringWithoutLengthAscii_WithValueOffsetAndLength_DoesNotOverrun() |
| 111 | + { |
| 112 | + var buffer = new ExpandableArrayBuffer(16); |
| 113 | + byte[] backing = buffer.ByteArray; |
| 114 | + for (int i = 0; i < backing.Length; i++) |
| 115 | + { |
| 116 | + backing[i] = 0xAA; |
| 117 | + } |
| 118 | + |
| 119 | + int written = buffer.PutStringWithoutLengthAscii(0, "xabcdx", 1, 4); |
| 120 | + |
| 121 | + Assert.AreEqual(4, written); |
| 122 | + Assert.AreEqual((byte)'a', backing[0]); |
| 123 | + Assert.AreEqual((byte)'b', backing[1]); |
| 124 | + Assert.AreEqual((byte)'c', backing[2]); |
| 125 | + Assert.AreEqual((byte)'d', backing[3]); |
| 126 | + Assert.AreEqual(0xAA, backing[4], "sentinel byte at offset==length must not be overwritten"); |
| 127 | + } |
| 128 | + |
| 129 | + [Test] |
| 130 | + public void ExpandableArrayBuffer_GetStringAscii_StringBuilder_AdvancesReadOffset() |
| 131 | + { |
| 132 | + var buffer = new ExpandableArrayBuffer(16); |
| 133 | + |
| 134 | + buffer.PutStringAscii(0, "abcd"); |
| 135 | + |
| 136 | + var sb = new StringBuilder(); |
| 137 | + buffer.GetStringAscii(0, sb); |
| 138 | + |
| 139 | + Assert.AreEqual("abcd", sb.ToString()); |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments