1 #region Copyright notice and license 2 // Protocol Buffers - Google's data interchange format 3 // Copyright 2022 Google Inc. All rights reserved. 4 // 5 // Use of this source code is governed by a BSD-style 6 // license that can be found in the LICENSE file or at 7 // https://developers.google.com/open-source/licenses/bsd 8 #endregion 9 10 using NUnit.Framework; 11 using System; 12 using System.Collections.Generic; 13 using System.Linq; 14 using System.Text; 15 using System.Threading.Tasks; 16 17 namespace Google.Protobuf.Test; 18 19 internal class WritingPrimitivesTest 20 { 21 [Test] WriteRawString_IllFormedUnicodeString()22 public void WriteRawString_IllFormedUnicodeString() 23 { 24 // See https://codeblog.jonskeet.uk/2014/11/07/when-is-a-string-not-a-string/ 25 char c1 = '\u0058'; 26 char c2 = '\ud800'; 27 char c3 = '\u0059'; 28 string text = new string(new[] { c1, c2, c3 }); 29 Span<byte> buffer = new byte[10]; 30 WriteContext.Initialize(ref buffer, out var context); 31 WritingPrimitives.WriteString(ref context.buffer, ref context.state, text); 32 33 // The high surrogate is written out in a "raw" form, surrounded by the ASCII 34 // characters. 35 byte[] expectedBytes = { 0x5, 0x58, 0xef, 0xbf, 0xbd, 0x59 }; 36 Assert.AreEqual(expectedBytes, buffer.Slice(0, context.state.position).ToArray()); 37 } 38 } 39