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.Linq; 13 14 namespace Google.Protobuf.Test; 15 16 internal class ParsingPrimitivesTest 17 { 18 // Note: test cases use integers rather than bytes as they're easier 19 // to specify in attributes. 20 21 [Test] 22 [TestCase("\ufffd", 255)] 23 [TestCase("A\ufffd", 65, 255)] 24 [TestCase("A\ufffd\ufffdB", 65, 255, 255, 66)] 25 // Overlong form of "space" 26 [TestCase("\ufffd\ufffd", 0xc0, 0xa0)] ReadRawString_NonUtf8(string expectedText, params int[] bytes)27 public void ReadRawString_NonUtf8(string expectedText, params int[] bytes) 28 { 29 var context = CreateContext(bytes); 30 string text = ParsingPrimitives.ReadRawString(ref context.buffer, ref context.state, bytes.Length); 31 Assert.AreEqual(expectedText, text); 32 } 33 CreateContext(int[] bytes)34 private static ParseContext CreateContext(int[] bytes) 35 { 36 byte[] actualBytes = bytes.Select(b => (byte) b).ToArray(); 37 ParseContext.Initialize(actualBytes.AsSpan(), out var context); 38 return context; 39 } 40 } 41