OLD | NEW |
(Empty) | |
| 1 #region Copyright notice and license |
| 2 |
| 3 // Copyright 2015, Google Inc. |
| 4 // All rights reserved. |
| 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 |
| 32 #endregion |
| 33 |
| 34 using System; |
| 35 using System.Diagnostics; |
| 36 using System.Runtime.InteropServices; |
| 37 using System.Threading; |
| 38 using System.Threading.Tasks; |
| 39 using Grpc.Core; |
| 40 using Grpc.Core.Internal; |
| 41 using Grpc.Core.Utils; |
| 42 using NUnit.Framework; |
| 43 |
| 44 namespace Grpc.Core.Tests |
| 45 { |
| 46 public class MetadataTest |
| 47 { |
| 48 [Test] |
| 49 public void AsciiEntry() |
| 50 { |
| 51 var entry = new Metadata.Entry("ABC", "XYZ"); |
| 52 Assert.IsFalse(entry.IsBinary); |
| 53 Assert.AreEqual("abc", entry.Key); // key is in lowercase. |
| 54 Assert.AreEqual("XYZ", entry.Value); |
| 55 CollectionAssert.AreEqual(new[] { (byte)'X', (byte)'Y', (byte)'Z' },
entry.ValueBytes); |
| 56 |
| 57 Assert.Throws(typeof(ArgumentException), () => new Metadata.Entry("a
bc-bin", "xyz")); |
| 58 |
| 59 Assert.AreEqual("[Entry: key=abc, value=XYZ]", entry.ToString()); |
| 60 } |
| 61 |
| 62 [Test] |
| 63 public void BinaryEntry() |
| 64 { |
| 65 var bytes = new byte[] { 1, 2, 3 }; |
| 66 var entry = new Metadata.Entry("ABC-BIN", bytes); |
| 67 Assert.IsTrue(entry.IsBinary); |
| 68 Assert.AreEqual("abc-bin", entry.Key); // key is in lowercase. |
| 69 Assert.Throws(typeof(InvalidOperationException), () => { var v = ent
ry.Value; }); |
| 70 CollectionAssert.AreEqual(bytes, entry.ValueBytes); |
| 71 |
| 72 Assert.Throws(typeof(ArgumentException), () => new Metadata.Entry("a
bc", bytes)); |
| 73 |
| 74 Assert.AreEqual("[Entry: key=abc-bin, valueBytes=System.Byte[]]", en
try.ToString()); |
| 75 } |
| 76 |
| 77 [Test] |
| 78 public void AsciiEntry_KeyValidity() |
| 79 { |
| 80 new Metadata.Entry("ABC", "XYZ"); |
| 81 new Metadata.Entry("0123456789abc", "XYZ"); |
| 82 new Metadata.Entry("-abc", "XYZ"); |
| 83 new Metadata.Entry("a_bc_", "XYZ"); |
| 84 Assert.Throws(typeof(ArgumentException), () => new Metadata.Entry("a
bc[", "xyz")); |
| 85 Assert.Throws(typeof(ArgumentException), () => new Metadata.Entry("a
bc/", "xyz")); |
| 86 } |
| 87 |
| 88 [Test] |
| 89 public void Entry_ConstructionPreconditions() |
| 90 { |
| 91 Assert.Throws(typeof(ArgumentNullException), () => new Metadata.Entr
y(null, "xyz")); |
| 92 Assert.Throws(typeof(ArgumentNullException), () => new Metadata.Entr
y("abc", (string)null)); |
| 93 Assert.Throws(typeof(ArgumentNullException), () => new Metadata.Entr
y("abc-bin", (byte[])null)); |
| 94 } |
| 95 |
| 96 [Test] |
| 97 public void Entry_Immutable() |
| 98 { |
| 99 var origBytes = new byte[] { 1, 2, 3 }; |
| 100 var bytes = new byte[] { 1, 2, 3 }; |
| 101 var entry = new Metadata.Entry("ABC-BIN", bytes); |
| 102 bytes[0] = 255; // changing the array passed to constructor should
have any effect. |
| 103 CollectionAssert.AreEqual(origBytes, entry.ValueBytes); |
| 104 |
| 105 entry.ValueBytes[0] = 255; |
| 106 CollectionAssert.AreEqual(origBytes, entry.ValueBytes); |
| 107 } |
| 108 |
| 109 [Test] |
| 110 public void Entry_CreateUnsafe_Ascii() |
| 111 { |
| 112 var bytes = new byte[] { (byte)'X', (byte)'y' }; |
| 113 var entry = Metadata.Entry.CreateUnsafe("abc", bytes); |
| 114 Assert.IsFalse(entry.IsBinary); |
| 115 Assert.AreEqual("abc", entry.Key); |
| 116 Assert.AreEqual("Xy", entry.Value); |
| 117 CollectionAssert.AreEqual(bytes, entry.ValueBytes); |
| 118 } |
| 119 |
| 120 [Test] |
| 121 public void Entry_CreateUnsafe_Binary() |
| 122 { |
| 123 var bytes = new byte[] { 1, 2, 3 }; |
| 124 var entry = Metadata.Entry.CreateUnsafe("abc-bin", bytes); |
| 125 Assert.IsTrue(entry.IsBinary); |
| 126 Assert.AreEqual("abc-bin", entry.Key); |
| 127 Assert.Throws(typeof(InvalidOperationException), () => { var v = ent
ry.Value; }); |
| 128 CollectionAssert.AreEqual(bytes, entry.ValueBytes); |
| 129 } |
| 130 |
| 131 [Test] |
| 132 public void IndexOf() |
| 133 { |
| 134 var metadata = CreateMetadata(); |
| 135 Assert.AreEqual(0, metadata.IndexOf(metadata[0])); |
| 136 Assert.AreEqual(1, metadata.IndexOf(metadata[1])); |
| 137 } |
| 138 |
| 139 [Test] |
| 140 public void Insert() |
| 141 { |
| 142 var metadata = CreateMetadata(); |
| 143 metadata.Insert(0, new Metadata.Entry("new-key", "new-value")); |
| 144 Assert.AreEqual(3, metadata.Count); |
| 145 Assert.AreEqual("new-key", metadata[0].Key); |
| 146 Assert.AreEqual("abc", metadata[1].Key); |
| 147 } |
| 148 |
| 149 [Test] |
| 150 public void RemoveAt() |
| 151 { |
| 152 var metadata = CreateMetadata(); |
| 153 metadata.RemoveAt(0); |
| 154 Assert.AreEqual(1, metadata.Count); |
| 155 Assert.AreEqual("xyz", metadata[0].Key); |
| 156 } |
| 157 |
| 158 [Test] |
| 159 public void Remove() |
| 160 { |
| 161 var metadata = CreateMetadata(); |
| 162 metadata.Remove(metadata[0]); |
| 163 Assert.AreEqual(1, metadata.Count); |
| 164 Assert.AreEqual("xyz", metadata[0].Key); |
| 165 } |
| 166 |
| 167 [Test] |
| 168 public void Indexer_Set() |
| 169 { |
| 170 var metadata = CreateMetadata(); |
| 171 var entry = new Metadata.Entry("new-key", "new-value"); |
| 172 |
| 173 metadata[1] = entry; |
| 174 Assert.AreEqual(entry, metadata[1]); |
| 175 } |
| 176 |
| 177 [Test] |
| 178 public void Clear() |
| 179 { |
| 180 var metadata = CreateMetadata(); |
| 181 metadata.Clear(); |
| 182 Assert.AreEqual(0, metadata.Count); |
| 183 } |
| 184 |
| 185 [Test] |
| 186 public void Contains() |
| 187 { |
| 188 var metadata = CreateMetadata(); |
| 189 Assert.IsTrue(metadata.Contains(metadata[0])); |
| 190 Assert.IsFalse(metadata.Contains(new Metadata.Entry("new-key", "new-
value"))); |
| 191 } |
| 192 |
| 193 [Test] |
| 194 public void CopyTo() |
| 195 { |
| 196 var metadata = CreateMetadata(); |
| 197 var array = new Metadata.Entry[metadata.Count + 1]; |
| 198 |
| 199 metadata.CopyTo(array, 1); |
| 200 Assert.AreEqual(default(Metadata.Entry), array[0]); |
| 201 Assert.AreEqual(metadata[0], array[1]); |
| 202 } |
| 203 |
| 204 [Test] |
| 205 public void IEnumerableGetEnumerator() |
| 206 { |
| 207 var metadata = CreateMetadata(); |
| 208 var enumerator = (metadata as System.Collections.IEnumerable).GetEnu
merator(); |
| 209 |
| 210 int i = 0; |
| 211 while (enumerator.MoveNext()) |
| 212 { |
| 213 Assert.AreEqual(metadata[i], enumerator.Current); |
| 214 i++; |
| 215 } |
| 216 } |
| 217 |
| 218 [Test] |
| 219 public void FreezeMakesReadOnly() |
| 220 { |
| 221 var entry = new Metadata.Entry("new-key", "new-value"); |
| 222 var metadata = CreateMetadata().Freeze(); |
| 223 |
| 224 Assert.IsTrue(metadata.IsReadOnly); |
| 225 Assert.Throws<InvalidOperationException>(() => metadata.Insert(0, en
try)); |
| 226 Assert.Throws<InvalidOperationException>(() => metadata.RemoveAt(0))
; |
| 227 Assert.Throws<InvalidOperationException>(() => metadata[0] = entry); |
| 228 Assert.Throws<InvalidOperationException>(() => metadata.Add(entry)); |
| 229 Assert.Throws<InvalidOperationException>(() => metadata.Add("new-key
", "new-value")); |
| 230 Assert.Throws<InvalidOperationException>(() => metadata.Add("new-key
-bin", new byte[] { 0xaa })); |
| 231 Assert.Throws<InvalidOperationException>(() => metadata.Clear()); |
| 232 Assert.Throws<InvalidOperationException>(() => metadata.Remove(metad
ata[0])); |
| 233 } |
| 234 |
| 235 private Metadata CreateMetadata() |
| 236 { |
| 237 return new Metadata |
| 238 { |
| 239 { "abc", "abc-value" }, |
| 240 { "xyz", "xyz-value" }, |
| 241 }; |
| 242 } |
| 243 } |
| 244 } |
OLD | NEW |