Jan Tattermusch | f6edf87 | 2015-08-19 12:54:44 -0700 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | // Copyright 2015 gRPC authors. |
Jan Tattermusch | f6edf87 | 2015-08-19 12:54:44 -0700 | [diff] [blame] | 4 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
Jan Tattermusch | f6edf87 | 2015-08-19 12:54:44 -0700 | [diff] [blame] | 8 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
Jan Tattermusch | f6edf87 | 2015-08-19 12:54:44 -0700 | [diff] [blame] | 10 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
Jan Tattermusch | f6edf87 | 2015-08-19 12:54:44 -0700 | [diff] [blame] | 16 | |
| 17 | #endregion |
| 18 | |
| 19 | using System; |
| 20 | using System.Diagnostics; |
| 21 | using System.Runtime.InteropServices; |
| 22 | using System.Threading; |
| 23 | using System.Threading.Tasks; |
| 24 | using Grpc.Core; |
| 25 | using Grpc.Core.Internal; |
| 26 | using Grpc.Core.Utils; |
| 27 | using NUnit.Framework; |
| 28 | |
| 29 | namespace Grpc.Core.Tests |
| 30 | { |
| 31 | public class MetadataTest |
| 32 | { |
| 33 | [Test] |
| 34 | public void AsciiEntry() |
| 35 | { |
| 36 | var entry = new Metadata.Entry("ABC", "XYZ"); |
Jan Tattermusch | 249fc80 | 2015-08-19 14:16:16 -0700 | [diff] [blame] | 37 | Assert.IsFalse(entry.IsBinary); |
Jan Tattermusch | f6edf87 | 2015-08-19 12:54:44 -0700 | [diff] [blame] | 38 | Assert.AreEqual("abc", entry.Key); // key is in lowercase. |
| 39 | Assert.AreEqual("XYZ", entry.Value); |
| 40 | CollectionAssert.AreEqual(new[] { (byte)'X', (byte)'Y', (byte)'Z' }, entry.ValueBytes); |
| 41 | |
| 42 | Assert.Throws(typeof(ArgumentException), () => new Metadata.Entry("abc-bin", "xyz")); |
Jan Tattermusch | 249fc80 | 2015-08-19 14:16:16 -0700 | [diff] [blame] | 43 | |
| 44 | Assert.AreEqual("[Entry: key=abc, value=XYZ]", entry.ToString()); |
Jan Tattermusch | f6edf87 | 2015-08-19 12:54:44 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | [Test] |
| 48 | public void BinaryEntry() |
| 49 | { |
| 50 | var bytes = new byte[] { 1, 2, 3 }; |
| 51 | var entry = new Metadata.Entry("ABC-BIN", bytes); |
Jan Tattermusch | 249fc80 | 2015-08-19 14:16:16 -0700 | [diff] [blame] | 52 | Assert.IsTrue(entry.IsBinary); |
Jan Tattermusch | f6edf87 | 2015-08-19 12:54:44 -0700 | [diff] [blame] | 53 | Assert.AreEqual("abc-bin", entry.Key); // key is in lowercase. |
| 54 | Assert.Throws(typeof(InvalidOperationException), () => { var v = entry.Value; }); |
| 55 | CollectionAssert.AreEqual(bytes, entry.ValueBytes); |
| 56 | |
| 57 | Assert.Throws(typeof(ArgumentException), () => new Metadata.Entry("abc", bytes)); |
Jan Tattermusch | 249fc80 | 2015-08-19 14:16:16 -0700 | [diff] [blame] | 58 | |
| 59 | Assert.AreEqual("[Entry: key=abc-bin, valueBytes=System.Byte[]]", entry.ToString()); |
Jan Tattermusch | f6edf87 | 2015-08-19 12:54:44 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | [Test] |
Jan Tattermusch | 7279567 | 2015-08-28 08:54:26 -0700 | [diff] [blame] | 63 | public void AsciiEntry_KeyValidity() |
| 64 | { |
| 65 | new Metadata.Entry("ABC", "XYZ"); |
| 66 | new Metadata.Entry("0123456789abc", "XYZ"); |
| 67 | new Metadata.Entry("-abc", "XYZ"); |
| 68 | new Metadata.Entry("a_bc_", "XYZ"); |
| 69 | Assert.Throws(typeof(ArgumentException), () => new Metadata.Entry("abc[", "xyz")); |
| 70 | Assert.Throws(typeof(ArgumentException), () => new Metadata.Entry("abc/", "xyz")); |
| 71 | } |
| 72 | |
| 73 | [Test] |
Jan Tattermusch | f6edf87 | 2015-08-19 12:54:44 -0700 | [diff] [blame] | 74 | public void Entry_ConstructionPreconditions() |
| 75 | { |
| 76 | Assert.Throws(typeof(ArgumentNullException), () => new Metadata.Entry(null, "xyz")); |
| 77 | Assert.Throws(typeof(ArgumentNullException), () => new Metadata.Entry("abc", (string)null)); |
| 78 | Assert.Throws(typeof(ArgumentNullException), () => new Metadata.Entry("abc-bin", (byte[])null)); |
| 79 | } |
| 80 | |
| 81 | [Test] |
| 82 | public void Entry_Immutable() |
| 83 | { |
| 84 | var origBytes = new byte[] { 1, 2, 3 }; |
| 85 | var bytes = new byte[] { 1, 2, 3 }; |
| 86 | var entry = new Metadata.Entry("ABC-BIN", bytes); |
| 87 | bytes[0] = 255; // changing the array passed to constructor should have any effect. |
| 88 | CollectionAssert.AreEqual(origBytes, entry.ValueBytes); |
| 89 | |
| 90 | entry.ValueBytes[0] = 255; |
| 91 | CollectionAssert.AreEqual(origBytes, entry.ValueBytes); |
| 92 | } |
| 93 | |
| 94 | [Test] |
| 95 | public void Entry_CreateUnsafe_Ascii() |
| 96 | { |
| 97 | var bytes = new byte[] { (byte)'X', (byte)'y' }; |
| 98 | var entry = Metadata.Entry.CreateUnsafe("abc", bytes); |
Jan Tattermusch | 249fc80 | 2015-08-19 14:16:16 -0700 | [diff] [blame] | 99 | Assert.IsFalse(entry.IsBinary); |
Jan Tattermusch | f6edf87 | 2015-08-19 12:54:44 -0700 | [diff] [blame] | 100 | Assert.AreEqual("abc", entry.Key); |
| 101 | Assert.AreEqual("Xy", entry.Value); |
| 102 | CollectionAssert.AreEqual(bytes, entry.ValueBytes); |
| 103 | } |
| 104 | |
| 105 | [Test] |
| 106 | public void Entry_CreateUnsafe_Binary() |
| 107 | { |
| 108 | var bytes = new byte[] { 1, 2, 3 }; |
| 109 | var entry = Metadata.Entry.CreateUnsafe("abc-bin", bytes); |
Jan Tattermusch | 249fc80 | 2015-08-19 14:16:16 -0700 | [diff] [blame] | 110 | Assert.IsTrue(entry.IsBinary); |
Jan Tattermusch | f6edf87 | 2015-08-19 12:54:44 -0700 | [diff] [blame] | 111 | Assert.AreEqual("abc-bin", entry.Key); |
| 112 | Assert.Throws(typeof(InvalidOperationException), () => { var v = entry.Value; }); |
| 113 | CollectionAssert.AreEqual(bytes, entry.ValueBytes); |
| 114 | } |
Jan Tattermusch | 1d344f1 | 2015-12-11 08:51:21 -0800 | [diff] [blame] | 115 | |
| 116 | [Test] |
| 117 | public void IndexOf() |
| 118 | { |
| 119 | var metadata = CreateMetadata(); |
| 120 | Assert.AreEqual(0, metadata.IndexOf(metadata[0])); |
| 121 | Assert.AreEqual(1, metadata.IndexOf(metadata[1])); |
| 122 | } |
| 123 | |
| 124 | [Test] |
| 125 | public void Insert() |
| 126 | { |
| 127 | var metadata = CreateMetadata(); |
| 128 | metadata.Insert(0, new Metadata.Entry("new-key", "new-value")); |
| 129 | Assert.AreEqual(3, metadata.Count); |
| 130 | Assert.AreEqual("new-key", metadata[0].Key); |
| 131 | Assert.AreEqual("abc", metadata[1].Key); |
| 132 | } |
| 133 | |
| 134 | [Test] |
| 135 | public void RemoveAt() |
| 136 | { |
| 137 | var metadata = CreateMetadata(); |
| 138 | metadata.RemoveAt(0); |
| 139 | Assert.AreEqual(1, metadata.Count); |
| 140 | Assert.AreEqual("xyz", metadata[0].Key); |
| 141 | } |
| 142 | |
| 143 | [Test] |
| 144 | public void Remove() |
| 145 | { |
| 146 | var metadata = CreateMetadata(); |
| 147 | metadata.Remove(metadata[0]); |
| 148 | Assert.AreEqual(1, metadata.Count); |
| 149 | Assert.AreEqual("xyz", metadata[0].Key); |
| 150 | } |
| 151 | |
| 152 | [Test] |
| 153 | public void Indexer_Set() |
| 154 | { |
| 155 | var metadata = CreateMetadata(); |
| 156 | var entry = new Metadata.Entry("new-key", "new-value"); |
| 157 | |
| 158 | metadata[1] = entry; |
| 159 | Assert.AreEqual(entry, metadata[1]); |
| 160 | } |
| 161 | |
| 162 | [Test] |
| 163 | public void Clear() |
| 164 | { |
| 165 | var metadata = CreateMetadata(); |
| 166 | metadata.Clear(); |
| 167 | Assert.AreEqual(0, metadata.Count); |
| 168 | } |
| 169 | |
| 170 | [Test] |
| 171 | public void Contains() |
| 172 | { |
| 173 | var metadata = CreateMetadata(); |
| 174 | Assert.IsTrue(metadata.Contains(metadata[0])); |
| 175 | Assert.IsFalse(metadata.Contains(new Metadata.Entry("new-key", "new-value"))); |
| 176 | } |
| 177 | |
| 178 | [Test] |
| 179 | public void CopyTo() |
| 180 | { |
| 181 | var metadata = CreateMetadata(); |
| 182 | var array = new Metadata.Entry[metadata.Count + 1]; |
| 183 | |
| 184 | metadata.CopyTo(array, 1); |
| 185 | Assert.AreEqual(default(Metadata.Entry), array[0]); |
| 186 | Assert.AreEqual(metadata[0], array[1]); |
| 187 | } |
| 188 | |
| 189 | [Test] |
| 190 | public void IEnumerableGetEnumerator() |
| 191 | { |
| 192 | var metadata = CreateMetadata(); |
| 193 | var enumerator = (metadata as System.Collections.IEnumerable).GetEnumerator(); |
| 194 | |
| 195 | int i = 0; |
| 196 | while (enumerator.MoveNext()) |
| 197 | { |
| 198 | Assert.AreEqual(metadata[i], enumerator.Current); |
| 199 | i++; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | [Test] |
| 204 | public void FreezeMakesReadOnly() |
| 205 | { |
| 206 | var entry = new Metadata.Entry("new-key", "new-value"); |
| 207 | var metadata = CreateMetadata().Freeze(); |
| 208 | |
| 209 | Assert.IsTrue(metadata.IsReadOnly); |
| 210 | Assert.Throws<InvalidOperationException>(() => metadata.Insert(0, entry)); |
| 211 | Assert.Throws<InvalidOperationException>(() => metadata.RemoveAt(0)); |
| 212 | Assert.Throws<InvalidOperationException>(() => metadata[0] = entry); |
| 213 | Assert.Throws<InvalidOperationException>(() => metadata.Add(entry)); |
| 214 | Assert.Throws<InvalidOperationException>(() => metadata.Add("new-key", "new-value")); |
| 215 | Assert.Throws<InvalidOperationException>(() => metadata.Add("new-key-bin", new byte[] { 0xaa })); |
| 216 | Assert.Throws<InvalidOperationException>(() => metadata.Clear()); |
| 217 | Assert.Throws<InvalidOperationException>(() => metadata.Remove(metadata[0])); |
| 218 | } |
| 219 | |
| 220 | private Metadata CreateMetadata() |
| 221 | { |
| 222 | return new Metadata |
| 223 | { |
| 224 | { "abc", "abc-value" }, |
| 225 | { "xyz", "xyz-value" }, |
| 226 | }; |
| 227 | } |
Jan Tattermusch | f6edf87 | 2015-08-19 12:54:44 -0700 | [diff] [blame] | 228 | } |
| 229 | } |