WellKnownStringsTest.cs 2.2 KB

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