MetadataTest.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #region Copyright notice and license
  2. // Copyright 2015 gRPC authors.
  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. #endregion
  16. using System;
  17. using System.Diagnostics;
  18. using System.Runtime.InteropServices;
  19. using System.Threading;
  20. using System.Threading.Tasks;
  21. using Grpc.Core;
  22. using Grpc.Core.Internal;
  23. using Grpc.Core.Utils;
  24. using NUnit.Framework;
  25. namespace Grpc.Core.Tests
  26. {
  27. public class MetadataTest
  28. {
  29. [Test]
  30. public void AsciiEntry()
  31. {
  32. var entry = new Metadata.Entry("ABC", "XYZ");
  33. Assert.IsFalse(entry.IsBinary);
  34. Assert.AreEqual("abc", entry.Key); // key is in lowercase.
  35. Assert.AreEqual("XYZ", entry.Value);
  36. CollectionAssert.AreEqual(new[] { (byte)'X', (byte)'Y', (byte)'Z' }, entry.ValueBytes);
  37. Assert.Throws(typeof(ArgumentException), () => new Metadata.Entry("abc-bin", "xyz"));
  38. Assert.AreEqual("[Entry: key=abc, value=XYZ]", entry.ToString());
  39. }
  40. [Test]
  41. public void BinaryEntry()
  42. {
  43. var bytes = new byte[] { 1, 2, 3 };
  44. var entry = new Metadata.Entry("ABC-BIN", bytes);
  45. Assert.IsTrue(entry.IsBinary);
  46. Assert.AreEqual("abc-bin", entry.Key); // key is in lowercase.
  47. Assert.Throws(typeof(InvalidOperationException), () => { var v = entry.Value; });
  48. CollectionAssert.AreEqual(bytes, entry.ValueBytes);
  49. Assert.Throws(typeof(ArgumentException), () => new Metadata.Entry("abc", bytes));
  50. Assert.AreEqual("[Entry: key=abc-bin, valueBytes=System.Byte[]]", entry.ToString());
  51. }
  52. [Test]
  53. public void AsciiEntry_KeyValidity()
  54. {
  55. new Metadata.Entry("ABC", "XYZ");
  56. new Metadata.Entry("0123456789abc", "XYZ");
  57. new Metadata.Entry("-abc", "XYZ");
  58. new Metadata.Entry("a_bc_", "XYZ");
  59. Assert.Throws(typeof(ArgumentException), () => new Metadata.Entry("abc[", "xyz"));
  60. Assert.Throws(typeof(ArgumentException), () => new Metadata.Entry("abc/", "xyz"));
  61. }
  62. [Test]
  63. public void Entry_ConstructionPreconditions()
  64. {
  65. Assert.Throws(typeof(ArgumentNullException), () => new Metadata.Entry(null, "xyz"));
  66. Assert.Throws(typeof(ArgumentNullException), () => new Metadata.Entry("abc", (string)null));
  67. Assert.Throws(typeof(ArgumentNullException), () => new Metadata.Entry("abc-bin", (byte[])null));
  68. }
  69. [Test]
  70. public void Entry_Immutable()
  71. {
  72. var origBytes = new byte[] { 1, 2, 3 };
  73. var bytes = new byte[] { 1, 2, 3 };
  74. var entry = new Metadata.Entry("ABC-BIN", bytes);
  75. bytes[0] = 255; // changing the array passed to constructor should have any effect.
  76. CollectionAssert.AreEqual(origBytes, entry.ValueBytes);
  77. entry.ValueBytes[0] = 255;
  78. CollectionAssert.AreEqual(origBytes, entry.ValueBytes);
  79. }
  80. [Test]
  81. public void Entry_CreateUnsafe_Ascii()
  82. {
  83. var bytes = new byte[] { (byte)'X', (byte)'y' };
  84. var entry = Metadata.Entry.CreateUnsafe("abc", bytes);
  85. Assert.IsFalse(entry.IsBinary);
  86. Assert.AreEqual("abc", entry.Key);
  87. Assert.AreEqual("Xy", entry.Value);
  88. CollectionAssert.AreEqual(bytes, entry.ValueBytes);
  89. }
  90. [Test]
  91. public void Entry_CreateUnsafe_Binary()
  92. {
  93. var bytes = new byte[] { 1, 2, 3 };
  94. var entry = Metadata.Entry.CreateUnsafe("abc-bin", bytes);
  95. Assert.IsTrue(entry.IsBinary);
  96. Assert.AreEqual("abc-bin", entry.Key);
  97. Assert.Throws(typeof(InvalidOperationException), () => { var v = entry.Value; });
  98. CollectionAssert.AreEqual(bytes, entry.ValueBytes);
  99. }
  100. [Test]
  101. public void IndexOf()
  102. {
  103. var metadata = CreateMetadata();
  104. Assert.AreEqual(0, metadata.IndexOf(metadata[0]));
  105. Assert.AreEqual(1, metadata.IndexOf(metadata[1]));
  106. }
  107. [Test]
  108. public void Insert()
  109. {
  110. var metadata = CreateMetadata();
  111. metadata.Insert(0, new Metadata.Entry("new-key", "new-value"));
  112. Assert.AreEqual(3, metadata.Count);
  113. Assert.AreEqual("new-key", metadata[0].Key);
  114. Assert.AreEqual("abc", metadata[1].Key);
  115. }
  116. [Test]
  117. public void RemoveAt()
  118. {
  119. var metadata = CreateMetadata();
  120. metadata.RemoveAt(0);
  121. Assert.AreEqual(1, metadata.Count);
  122. Assert.AreEqual("xyz", metadata[0].Key);
  123. }
  124. [Test]
  125. public void Remove()
  126. {
  127. var metadata = CreateMetadata();
  128. metadata.Remove(metadata[0]);
  129. Assert.AreEqual(1, metadata.Count);
  130. Assert.AreEqual("xyz", metadata[0].Key);
  131. }
  132. [Test]
  133. public void Indexer_Set()
  134. {
  135. var metadata = CreateMetadata();
  136. var entry = new Metadata.Entry("new-key", "new-value");
  137. metadata[1] = entry;
  138. Assert.AreEqual(entry, metadata[1]);
  139. }
  140. [Test]
  141. public void Clear()
  142. {
  143. var metadata = CreateMetadata();
  144. metadata.Clear();
  145. Assert.AreEqual(0, metadata.Count);
  146. }
  147. [Test]
  148. public void Contains()
  149. {
  150. var metadata = CreateMetadata();
  151. Assert.IsTrue(metadata.Contains(metadata[0]));
  152. Assert.IsFalse(metadata.Contains(new Metadata.Entry("new-key", "new-value")));
  153. }
  154. [Test]
  155. public void CopyTo()
  156. {
  157. var metadata = CreateMetadata();
  158. var array = new Metadata.Entry[metadata.Count + 1];
  159. metadata.CopyTo(array, 1);
  160. Assert.AreEqual(default(Metadata.Entry), array[0]);
  161. Assert.AreEqual(metadata[0], array[1]);
  162. }
  163. [Test]
  164. public void IEnumerableGetEnumerator()
  165. {
  166. var metadata = CreateMetadata();
  167. var enumerator = (metadata as System.Collections.IEnumerable).GetEnumerator();
  168. int i = 0;
  169. while (enumerator.MoveNext())
  170. {
  171. Assert.AreEqual(metadata[i], enumerator.Current);
  172. i++;
  173. }
  174. }
  175. [Test]
  176. public void FreezeMakesReadOnly()
  177. {
  178. var entry = new Metadata.Entry("new-key", "new-value");
  179. var metadata = CreateMetadata().Freeze();
  180. Assert.IsTrue(metadata.IsReadOnly);
  181. Assert.Throws<InvalidOperationException>(() => metadata.Insert(0, entry));
  182. Assert.Throws<InvalidOperationException>(() => metadata.RemoveAt(0));
  183. Assert.Throws<InvalidOperationException>(() => metadata[0] = entry);
  184. Assert.Throws<InvalidOperationException>(() => metadata.Add(entry));
  185. Assert.Throws<InvalidOperationException>(() => metadata.Add("new-key", "new-value"));
  186. Assert.Throws<InvalidOperationException>(() => metadata.Add("new-key-bin", new byte[] { 0xaa }));
  187. Assert.Throws<InvalidOperationException>(() => metadata.Clear());
  188. Assert.Throws<InvalidOperationException>(() => metadata.Remove(metadata[0]));
  189. }
  190. private Metadata CreateMetadata()
  191. {
  192. return new Metadata
  193. {
  194. { "abc", "abc-value" },
  195. { "xyz", "xyz-value" },
  196. };
  197. }
  198. }
  199. }