• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #region Copyright notice and license
2 // Copyright 2019 The 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 
17 using System.Text;
18 using Grpc.Core.Internal;
19 using NUnit.Framework;
20 
21 namespace Grpc.Core.Internal.Tests
22 {
23     public class WellKnownStringsTest
24     {
25         [Test]
26         [TestCase("", true)]
27         [TestCase("u", false)]
28         [TestCase("us", false)]
29         [TestCase("use", false)]
30         [TestCase("user", false)]
31         [TestCase("user-", false)]
32         [TestCase("user-a", false)]
33         [TestCase("user-ag", false)]
34         [TestCase("user-age", false)]
35         [TestCase("user-agent", true)]
36         [TestCase("user-agent ", false)]
37         [TestCase("useragent ", false)]
38         [TestCase("User-Agent", false)]
39         [TestCase("sdlkfjlskjfdlkjs;lfdksflsdfkh skjdfh sdkfhskdhf skjfhk sdhjkjh", false)]
40 
41         // test for endianness snafus (reversed in segments)
42         [TestCase("ega-resutn", false)]
TestWellKnownStrings(string input, bool expected)43         public unsafe void TestWellKnownStrings(string input, bool expected)
44         {
45             // create a copy of the data; no cheating!
46             byte[] bytes = Encoding.ASCII.GetBytes(input);
47             fixed(byte* ptr = bytes)
48             {
49                 string result = WellKnownStrings.TryIdentify(ptr, bytes.Length);
50                 if (expected) Assert.AreEqual(input, result);
51                 else Assert.IsNull(result);
52 
53                 if (expected)
54                 {
55                     // try again, and check we get the same instance
56                     string again = WellKnownStrings.TryIdentify(ptr, bytes.Length);
57                     Assert.AreSame(result, again);
58                 }
59             }
60         }
61     }
62 }
63