ByteStringTest.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2008 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. using System;
  33. using System.Text;
  34. using NUnit.Framework;
  35. using System.IO;
  36. #if !DOTNET35
  37. using System.Threading.Tasks;
  38. #endif
  39. namespace Google.Protobuf
  40. {
  41. public class ByteStringTest
  42. {
  43. [Test]
  44. public void Equality()
  45. {
  46. ByteString b1 = ByteString.CopyFrom(1, 2, 3);
  47. ByteString b2 = ByteString.CopyFrom(1, 2, 3);
  48. ByteString b3 = ByteString.CopyFrom(1, 2, 4);
  49. ByteString b4 = ByteString.CopyFrom(1, 2, 3, 4);
  50. EqualityTester.AssertEquality(b1, b1);
  51. EqualityTester.AssertEquality(b1, b2);
  52. EqualityTester.AssertInequality(b1, b3);
  53. EqualityTester.AssertInequality(b1, b4);
  54. EqualityTester.AssertInequality(b1, null);
  55. #pragma warning disable 1718 // Deliberately calling ==(b1, b1) and !=(b1, b1)
  56. Assert.IsTrue(b1 == b1);
  57. Assert.IsTrue(b1 == b2);
  58. Assert.IsFalse(b1 == b3);
  59. Assert.IsFalse(b1 == b4);
  60. Assert.IsFalse(b1 == null);
  61. Assert.IsTrue((ByteString) null == null);
  62. Assert.IsFalse(b1 != b1);
  63. Assert.IsFalse(b1 != b2);
  64. #pragma warning disable 1718
  65. Assert.IsTrue(b1 != b3);
  66. Assert.IsTrue(b1 != b4);
  67. Assert.IsTrue(b1 != null);
  68. Assert.IsFalse((ByteString) null != null);
  69. }
  70. [Test]
  71. public void EmptyByteStringHasZeroSize()
  72. {
  73. Assert.AreEqual(0, ByteString.Empty.Length);
  74. }
  75. [Test]
  76. public void CopyFromStringWithExplicitEncoding()
  77. {
  78. ByteString bs = ByteString.CopyFrom("AB", Encoding.Unicode);
  79. Assert.AreEqual(4, bs.Length);
  80. Assert.AreEqual(65, bs[0]);
  81. Assert.AreEqual(0, bs[1]);
  82. Assert.AreEqual(66, bs[2]);
  83. Assert.AreEqual(0, bs[3]);
  84. }
  85. [Test]
  86. public void IsEmptyWhenEmpty()
  87. {
  88. Assert.IsTrue(ByteString.CopyFromUtf8("").IsEmpty);
  89. }
  90. [Test]
  91. public void IsEmptyWhenNotEmpty()
  92. {
  93. Assert.IsFalse(ByteString.CopyFromUtf8("X").IsEmpty);
  94. }
  95. [Test]
  96. public void CopyFromByteArrayCopiesContents()
  97. {
  98. byte[] data = new byte[1];
  99. data[0] = 10;
  100. ByteString bs = ByteString.CopyFrom(data);
  101. Assert.AreEqual(10, bs[0]);
  102. data[0] = 5;
  103. Assert.AreEqual(10, bs[0]);
  104. }
  105. [Test]
  106. public void ToByteArrayCopiesContents()
  107. {
  108. ByteString bs = ByteString.CopyFromUtf8("Hello");
  109. byte[] data = bs.ToByteArray();
  110. Assert.AreEqual((byte)'H', data[0]);
  111. Assert.AreEqual((byte)'H', bs[0]);
  112. data[0] = 0;
  113. Assert.AreEqual(0, data[0]);
  114. Assert.AreEqual((byte)'H', bs[0]);
  115. }
  116. [Test]
  117. public void CopyFromUtf8UsesUtf8()
  118. {
  119. ByteString bs = ByteString.CopyFromUtf8("\u20ac");
  120. Assert.AreEqual(3, bs.Length);
  121. Assert.AreEqual(0xe2, bs[0]);
  122. Assert.AreEqual(0x82, bs[1]);
  123. Assert.AreEqual(0xac, bs[2]);
  124. }
  125. [Test]
  126. public void CopyFromPortion()
  127. {
  128. byte[] data = new byte[] {0, 1, 2, 3, 4, 5, 6};
  129. ByteString bs = ByteString.CopyFrom(data, 2, 3);
  130. Assert.AreEqual(3, bs.Length);
  131. Assert.AreEqual(2, bs[0]);
  132. Assert.AreEqual(3, bs[1]);
  133. }
  134. [Test]
  135. public void ToStringUtf8()
  136. {
  137. ByteString bs = ByteString.CopyFromUtf8("\u20ac");
  138. Assert.AreEqual("\u20ac", bs.ToStringUtf8());
  139. }
  140. [Test]
  141. public void ToStringWithExplicitEncoding()
  142. {
  143. ByteString bs = ByteString.CopyFrom("\u20ac", Encoding.Unicode);
  144. Assert.AreEqual("\u20ac", bs.ToString(Encoding.Unicode));
  145. }
  146. [Test]
  147. public void FromBase64_WithText()
  148. {
  149. byte[] data = new byte[] {0, 1, 2, 3, 4, 5, 6};
  150. string base64 = Convert.ToBase64String(data);
  151. ByteString bs = ByteString.FromBase64(base64);
  152. Assert.AreEqual(data, bs.ToByteArray());
  153. }
  154. [Test]
  155. public void FromBase64_Empty()
  156. {
  157. // Optimization which also fixes issue 61.
  158. Assert.AreSame(ByteString.Empty, ByteString.FromBase64(""));
  159. }
  160. [Test]
  161. public void FromStream_Seekable()
  162. {
  163. var stream = new MemoryStream(new byte[] { 1, 2, 3, 4, 5 });
  164. // Consume the first byte, just to test that it's "from current position"
  165. stream.ReadByte();
  166. var actual = ByteString.FromStream(stream);
  167. ByteString expected = ByteString.CopyFrom(2, 3, 4, 5);
  168. Assert.AreEqual(expected, actual, $"{expected.ToBase64()} != {actual.ToBase64()}");
  169. }
  170. [Test]
  171. public void FromStream_NotSeekable()
  172. {
  173. var stream = new MemoryStream(new byte[] { 1, 2, 3, 4, 5 });
  174. // Consume the first byte, just to test that it's "from current position"
  175. stream.ReadByte();
  176. // Wrap the original stream in LimitedInputStream, which has CanSeek=false
  177. var limitedStream = new LimitedInputStream(stream, 3);
  178. var actual = ByteString.FromStream(limitedStream);
  179. ByteString expected = ByteString.CopyFrom(2, 3, 4);
  180. Assert.AreEqual(expected, actual, $"{expected.ToBase64()} != {actual.ToBase64()}");
  181. }
  182. #if !DOTNET35
  183. [Test]
  184. public async Task FromStreamAsync_Seekable()
  185. {
  186. var stream = new MemoryStream(new byte[] { 1, 2, 3, 4, 5 });
  187. // Consume the first byte, just to test that it's "from current position"
  188. stream.ReadByte();
  189. var actual = await ByteString.FromStreamAsync(stream);
  190. ByteString expected = ByteString.CopyFrom(2, 3, 4, 5);
  191. Assert.AreEqual(expected, actual, $"{expected.ToBase64()} != {actual.ToBase64()}");
  192. }
  193. [Test]
  194. public async Task FromStreamAsync_NotSeekable()
  195. {
  196. var stream = new MemoryStream(new byte[] { 1, 2, 3, 4, 5 });
  197. // Consume the first byte, just to test that it's "from current position"
  198. stream.ReadByte();
  199. // Wrap the original stream in LimitedInputStream, which has CanSeek=false
  200. var limitedStream = new LimitedInputStream(stream, 3);
  201. var actual = await ByteString.FromStreamAsync(limitedStream);
  202. ByteString expected = ByteString.CopyFrom(2, 3, 4);
  203. Assert.AreEqual(expected, actual, $"{expected.ToBase64()} != {actual.ToBase64()}");
  204. }
  205. #endif
  206. [Test]
  207. public void GetHashCode_Regression()
  208. {
  209. // We used to have an awful hash algorithm where only the last four
  210. // bytes were relevant. This is a regression test for
  211. // https://github.com/google/protobuf/issues/2511
  212. ByteString b1 = ByteString.CopyFrom(100, 1, 2, 3, 4);
  213. ByteString b2 = ByteString.CopyFrom(200, 1, 2, 3, 4);
  214. Assert.AreNotEqual(b1.GetHashCode(), b2.GetHashCode());
  215. }
  216. }
  217. }