PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/MongoDB.Bson.Tests/IO/CStringUtf8EncodingTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 107 lines | 77 code | 16 blank | 14 comment | 2 complexity | e8800ac69edeb5b8158468d40188cef0 MD5 | raw file
Possible License(s): Apache-2.0
  1. /* Copyright 2016-present MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.Linq;
  17. using FluentAssertions;
  18. using MongoDB.Bson.IO;
  19. using MongoDB.Bson.TestHelpers;
  20. using MongoDB.Bson.TestHelpers.XunitExtensions;
  21. using Xunit;
  22. namespace MongoDB.Bson.Tests.IO
  23. {
  24. public class CStringUtf8EncodingTests
  25. {
  26. [Theory]
  27. [ParameterAttributeData]
  28. public void GetMaxByteCount_should_return_expected_result(
  29. [Values(0, 1, 2)] int charCount)
  30. {
  31. var expectedResult = charCount * 3;
  32. var result = CStringUtf8Encoding.GetMaxByteCount(charCount);
  33. result.Should().Be(expectedResult);
  34. }
  35. [Theory]
  36. [InlineData("abc", 0, 3, new byte[] { 97, 98, 99 })]
  37. [InlineData("abc", 1, 3, new byte[] { 97, 98, 99 })]
  38. [InlineData("\\u0080", 0, 2, new byte[] { 0xc2, 0x80 })]
  39. [InlineData("\\u0080", 1, 2, new byte[] { 0xc2, 0x80 })]
  40. [InlineData("\\u07ff", 0, 2, new byte[] { 0xdf, 0xbf })]
  41. [InlineData("\\u07ff", 1, 2, new byte[] { 0xdf, 0xbf })]
  42. [InlineData("\\u0800", 0, 3, new byte[] { 0xe0, 0xa0, 0x80 })]
  43. [InlineData("\\u0800", 1, 3, new byte[] { 0xe0, 0xa0, 0x80 })]
  44. [InlineData("\\ud7ff", 0, 3, new byte[] { 0xed, 0x9f, 0xbf })]
  45. [InlineData("\\ud7ff", 1, 3, new byte[] { 0xed, 0x9f, 0xbf })]
  46. [InlineData("\\ue000", 0, 3, new byte[] { 0xee, 0x80, 0x80 })]
  47. [InlineData("\\ue000", 1, 3, new byte[] { 0xee, 0x80, 0x80 })]
  48. [InlineData("\\uffff", 0, 3, new byte[] { 0xef, 0xbf, 0xbf })]
  49. [InlineData("\\uffff", 1, 3, new byte[] { 0xef, 0xbf, 0xbf })]
  50. [InlineData("\\ud800\\udc00", 0, 4, new byte[] { 0xf0, 0x90, 0x80, 0x80 })] // surrogate pair
  51. public void GetBytes_should_return_expected_result(
  52. string value,
  53. int byteIndex,
  54. int expectedResult,
  55. byte[] expectedBytes)
  56. {
  57. value = UnicodeHelper.Unescape(value);
  58. var bytes = new byte[CStringUtf8Encoding.GetMaxByteCount(value.Length) + byteIndex];
  59. var result = CStringUtf8Encoding.GetBytes(value, bytes, byteIndex, Utf8Encodings.Strict);
  60. result.Should().Be(expectedResult);
  61. bytes.Take(byteIndex).All(b => b == 0).Should().BeTrue();
  62. bytes.Skip(byteIndex).Take(result).Should().Equal(expectedBytes);
  63. bytes.Skip(byteIndex + result).All(b => b == 0).Should().BeTrue();
  64. }
  65. [Theory]
  66. [ParameterAttributeData]
  67. public void GetBytes_should_throw_when_value_contains_null(
  68. [Values("\0", "a\0", "ab\0")] string value)
  69. {
  70. var bytes = new byte[CStringUtf8Encoding.GetMaxByteCount(value.Length)];
  71. Action action = () => CStringUtf8Encoding.GetBytes(value, bytes, 0, Utf8Encodings.Strict);
  72. action.ShouldThrow<ArgumentException>().And.ParamName.Should().Be("value");
  73. }
  74. [Fact]
  75. public void GetBytes_should_throw_when_value_contains_null_and_fallback_encoding_is_used()
  76. {
  77. var value = "\ud800\udc00\u0000";
  78. var bytes = new byte[CStringUtf8Encoding.GetMaxByteCount(value.Length)];
  79. Action action = () => CStringUtf8Encoding.GetBytes(value, bytes, 0, Utf8Encodings.Strict);
  80. action.ShouldThrow<ArgumentException>().And.ParamName.Should().Be("value");
  81. }
  82. [Fact]
  83. public void GetBytes_should_throw_when_value_contains_unmatched_surrogate_pair()
  84. {
  85. var value = "\ud800";
  86. var bytes = new byte[CStringUtf8Encoding.GetMaxByteCount(value.Length)];
  87. Action action = () => CStringUtf8Encoding.GetBytes(value, bytes, 0, Utf8Encodings.Strict);
  88. action.ShouldThrow<ArgumentException>();
  89. }
  90. }
  91. }