public void TestFullMessage() { FormatterAssert( TestXmlMessage.CreateBuilder() .SetValid(true) .SetText("text") .AddTextlines("a") .AddTextlines("b") .AddTextlines("c") .SetNumber(0x1010101010) .AddNumbers(1) .AddNumbers(2) .AddNumbers(3) .SetChild(TestXmlChild.CreateBuilder().AddOptions(EnumOptions.ONE).SetBinary(ByteString.CopyFrom(new byte[1]))) .AddChildren(TestXmlMessage.Types.Children.CreateBuilder().AddOptions(EnumOptions.TWO).SetBinary(ByteString.CopyFrom(new byte[2]))) .AddChildren(TestXmlMessage.Types.Children.CreateBuilder().AddOptions(EnumOptions.THREE).SetBinary(ByteString.CopyFrom(new byte[3]))) .Build(), @"""text"":""text""", @"[""a"",""b"",""c""]", @"[1,2,3]", @"""child"":{", @"""children"":[{", @"AA==", @"AAA=", @"AAAA", 0x1010101010L.ToString() ); }
/// <summary> /// Performs string unescaping from C style (octal, hex, form feeds, tab etc) into a byte string. /// </summary> public static ByteString UnescapeBytes(string input) { byte[] result = new byte[input.Length]; int pos = 0; for (int i = 0; i < input.Length; i++) { char c = input[i]; if (c > 127 || c < 32) { throw new FormatException("Escaped string must only contain ASCII"); } if (c != '\\') { result[pos++] = (byte)c; continue; } if (i + 1 >= input.Length) { throw new FormatException("Invalid escape sequence: '\\' at end of string."); } i++; c = input[i]; if (c >= '0' && c <= '7') { // Octal escape. int code = ParseDigit(c); if (i + 1 < input.Length && IsOctal(input[i + 1])) { i++; code = code * 8 + ParseDigit(input[i]); } if (i + 1 < input.Length && IsOctal(input[i + 1])) { i++; code = code * 8 + ParseDigit(input[i]); } result[pos++] = (byte)code; } else { switch (c) { case 'a': result[pos++] = 0x07; break; case 'b': result[pos++] = (byte)'\b'; break; case 'f': result[pos++] = (byte)'\f'; break; case 'n': result[pos++] = (byte)'\n'; break; case 'r': result[pos++] = (byte)'\r'; break; case 't': result[pos++] = (byte)'\t'; break; case 'v': result[pos++] = 0x0b; break; case '\\': result[pos++] = (byte)'\\'; break; case '\'': result[pos++] = (byte)'\''; break; case '"': result[pos++] = (byte)'\"'; break; case 'x': // hex escape int code; if (i + 1 < input.Length && IsHex(input[i + 1])) { i++; code = ParseDigit(input[i]); } else { throw new FormatException("Invalid escape sequence: '\\x' with no digits"); } if (i + 1 < input.Length && IsHex(input[i + 1])) { ++i; code = code * 16 + ParseDigit(input[i]); } result[pos++] = (byte)code; break; default: throw new FormatException("Invalid escape sequence: '\\" + c + "'"); } } } return(ByteString.CopyFrom(result, 0, pos)); }
public void TestFullMessageWithUnknownFields() { TestXmlMessage origial = TestXmlMessage.CreateBuilder() .SetValid(true) .SetText("text") .AddTextlines("a") .AddTextlines("b") .AddTextlines("c") .SetNumber(0x1010101010) .AddNumbers(1) .AddNumbers(2) .AddNumbers(3) .SetChild(TestXmlChild.CreateBuilder().AddOptions(EnumOptions.ONE).SetBinary(ByteString.CopyFrom(new byte[1]))) .AddChildren(TestXmlMessage.Types.Children.CreateBuilder().AddOptions(EnumOptions.TWO).SetBinary(ByteString.CopyFrom(new byte[2]))) .AddChildren(TestXmlMessage.Types.Children.CreateBuilder().AddOptions(EnumOptions.THREE).SetBinary(ByteString.CopyFrom(new byte[3]))) .Build(); TestXmlNoFields message = TestXmlNoFields.CreateBuilder().MergeFrom(origial.ToByteArray()).Build(); Assert.AreEqual(0, message.AllFields.Count); StringWriter sw = new StringWriter(); XmlFormatWriter.CreateInstance(sw) .SetOptions(XmlWriterOptions.OutputNestedArrays | XmlWriterOptions.OutputEnumValues) .WriteMessage("root", message); string xml = sw.ToString(); using (XmlReader x = XmlReader.Create(new StringReader(xml))) { x.MoveToContent(); Assert.AreEqual(XmlNodeType.Element, x.NodeType); //should always be empty Assert.IsTrue(x.IsEmptyElement || (x.Read() && x.NodeType == XmlNodeType.EndElement) ); } XmlFormatReader rdr = XmlFormatReader.CreateInstance(xml); rdr.Options = XmlReaderOptions.ReadNestedArrays; TestXmlMessage copy = rdr.Merge(TestXmlMessage.CreateBuilder()).Build(); Assert.AreEqual(TestXmlMessage.DefaultInstance, copy); }
public void TestFullMessageWithRichTypes() { TestXmlMessage message = TestXmlMessage.CreateBuilder() .SetValid(true) .SetText("text") .AddTextlines("a") .AddTextlines("b") .AddTextlines("c") .SetNumber(0x1010101010) .AddNumbers(1) .AddNumbers(2) .AddNumbers(3) .SetChild(TestXmlChild.CreateBuilder().AddOptions(EnumOptions.ONE).SetBinary(ByteString.CopyFrom(new byte[1]))) .AddChildren(TestXmlMessage.Types.Children.CreateBuilder().AddOptions(EnumOptions.TWO).SetBinary(ByteString.CopyFrom(new byte[2]))) .AddChildren(TestXmlMessage.Types.Children.CreateBuilder().AddOptions(EnumOptions.THREE).SetBinary(ByteString.CopyFrom(new byte[3]))) .Build(); StringWriter sw = new StringWriter(); XmlWriter xwtr = XmlWriter.Create(sw, new XmlWriterSettings { Indent = true, IndentChars = " " }); XmlFormatWriter.CreateInstance(xwtr) .SetOptions(XmlWriterOptions.OutputNestedArrays | XmlWriterOptions.OutputEnumValues) .WriteMessage("root", message); string xml = sw.ToString(); XmlFormatReader rdr = XmlFormatReader.CreateInstance(xml); rdr.Options = XmlReaderOptions.ReadNestedArrays; TestXmlMessage copy = rdr.Merge(TestXmlMessage.CreateBuilder()).Build(); Assert.AreEqual(message, copy); }
public void ToStringWithExplicitEncoding() { ByteString bs = ByteString.CopyFrom("\u20ac", Encoding.Unicode); Assert.AreEqual("\u20ac", bs.ToString(Encoding.Unicode)); }
// ================================================================= private static ByteString Bytes(params byte[] bytes) { return(ByteString.CopyFrom(bytes)); }
/// <summary> /// Helper to construct a ByteString from a string containing only 8-bit /// characters. The characters are converted directly to bytes, *not* /// encoded using UTF-8. /// </summary> private static ByteString Bytes(string str) { return(ByteString.CopyFrom(Encoding.GetEncoding(28591).GetBytes(str))); }