blob: 89167317573bd9a8a07552186c6a01b49902b3fb [file] [log] [blame]
Jan Tattermuschf6edf872015-08-19 12:54:44 -07001#region Copyright notice and license
2
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003// Copyright 2015 gRPC authors.
Jan Tattermuschf6edf872015-08-19 12:54:44 -07004//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005// 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 Tattermuschf6edf872015-08-19 12:54:44 -07008//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009// http://www.apache.org/licenses/LICENSE-2.0
Jan Tattermuschf6edf872015-08-19 12:54:44 -070010//
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011// 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 Tattermuschf6edf872015-08-19 12:54:44 -070016
17#endregion
18
19using System;
20using System.Diagnostics;
21using System.Runtime.InteropServices;
22using System.Threading;
23using System.Threading.Tasks;
24using Grpc.Core;
25using Grpc.Core.Internal;
26using Grpc.Core.Utils;
27using NUnit.Framework;
28
29namespace 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 Tattermusch249fc802015-08-19 14:16:16 -070037 Assert.IsFalse(entry.IsBinary);
Jan Tattermuschf6edf872015-08-19 12:54:44 -070038 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 Tattermusch249fc802015-08-19 14:16:16 -070043
44 Assert.AreEqual("[Entry: key=abc, value=XYZ]", entry.ToString());
Jan Tattermuschf6edf872015-08-19 12:54:44 -070045 }
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 Tattermusch249fc802015-08-19 14:16:16 -070052 Assert.IsTrue(entry.IsBinary);
Jan Tattermuschf6edf872015-08-19 12:54:44 -070053 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 Tattermusch249fc802015-08-19 14:16:16 -070058
59 Assert.AreEqual("[Entry: key=abc-bin, valueBytes=System.Byte[]]", entry.ToString());
Jan Tattermuschf6edf872015-08-19 12:54:44 -070060 }
61
62 [Test]
Jan Tattermusch72795672015-08-28 08:54:26 -070063 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 Tattermuschf6edf872015-08-19 12:54:44 -070074 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 Tattermusch249fc802015-08-19 14:16:16 -070099 Assert.IsFalse(entry.IsBinary);
Jan Tattermuschf6edf872015-08-19 12:54:44 -0700100 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 Tattermusch249fc802015-08-19 14:16:16 -0700110 Assert.IsTrue(entry.IsBinary);
Jan Tattermuschf6edf872015-08-19 12:54:44 -0700111 Assert.AreEqual("abc-bin", entry.Key);
112 Assert.Throws(typeof(InvalidOperationException), () => { var v = entry.Value; });
113 CollectionAssert.AreEqual(bytes, entry.ValueBytes);
114 }
Jan Tattermusch1d344f12015-12-11 08:51:21 -0800115
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 Tattermuschf6edf872015-08-19 12:54:44 -0700228 }
229}