public static MessageType TryParseMessage(byte[] messageBytes, out BDictionary message) { if (BObject.TryParse(messageBytes, out BObject bObject)) { if (bObject is BDictionary dictionary) { message = dictionary; if (message.TryGetValue("y", out BString type)) { if (!message.ContainsKey <BString>("t")) { return(MessageType.Invalid); } switch (type.Text) { case "q": return(MessageType.Query); case "r": return(MessageType.Response); case "e": return(MessageType.Error); default: return(MessageType.Unknown); } } } } message = null; return(MessageType.Invalid); }
public void BDictionary_EnforcedSorting_Unsorted() { // { "b", 1 }, { "a", 2 } byte[] encoded = Encoding.UTF8.GetBytes("d1:bi1e1:ai2ee"); BDictionary.EnforceDictionarySorting = true; Assert.False(BObject.TryParse(encoded, out BObject bObject)); BDictionary.EnforceDictionarySorting = false; }
private void TestBDictionary(Dictionary <BString, BObject> testDictionary) { BDictionary bDictionary = new BDictionary(testDictionary); byte[] encoded = bDictionary.Encode(); Assert.True(BObject.TryParse(encoded, out BDictionary bObject)); Assert.True(bDictionary.Equals(bObject)); using (MemoryStream ms = new MemoryStream()) { bDictionary.Encode(ms); encoded = ms.ToArray(); } Assert.True(BObject.TryParse(encoded, out bObject)); Assert.True(bDictionary.Equals(bObject)); Assert.Equal(encoded.Length, bDictionary.GetEncodedSize()); }
private void TestBList(List <BObject> testList) { BList bList = new BList(testList); byte[] encoded = bList.Encode(); Assert.True(BObject.TryParse(encoded, out BList bObject)); Assert.True(bList.Equals(bObject)); using (MemoryStream ms = new MemoryStream()) { bList.Encode(ms); encoded = ms.ToArray(); } Assert.True(BObject.TryParse(encoded, out bObject)); Assert.True(bList.Equals(bObject)); Assert.Equal(encoded.Length, bList.GetEncodedSize()); }
private void TestBInteger(long testInteger) { BInteger bInteger = new BInteger(testInteger); byte[] encoded = bInteger.Encode(); Assert.True(BObject.TryParse(encoded, out BInteger bObject)); Assert.Equal(testInteger, bObject.Value); using (MemoryStream ms = new MemoryStream()) { bInteger.Encode(ms); encoded = ms.ToArray(); } Assert.True(BObject.TryParse(encoded, out bObject)); Assert.Equal(testInteger, bObject.Value); Assert.Equal(encoded.Length, bInteger.EncodedSize); }
public void BDictionary_MissingTerminator() { byte[] encoded = Encoding.UTF8.GetBytes("d4:testi1e"); Assert.False(BObject.TryParse(encoded, out BObject bObject)); }
public void BList_MissingTerminator() { byte[] encoded = Encoding.UTF8.GetBytes("li1e"); Assert.False(BObject.TryParse(encoded, out BObject bObject)); }
public void BInteger_MisplacedNegative() { byte[] encoded = Encoding.UTF8.GetBytes("i123-4e"); Assert.False(BObject.TryParse(encoded, out BObject bObject)); }
public void BInteger_MultipleNegative() { byte[] encoded = Encoding.UTF8.GetBytes("i--1e"); Assert.False(BObject.TryParse(encoded, out BObject bObject)); }
public void BInteger_NegativeZero() { byte[] encoded = Encoding.UTF8.GetBytes("i-0e"); Assert.False(BObject.TryParse(encoded, out BObject bObject)); }
public void BInteger_EmptyIsValidAndZero() { byte[] encoded = Encoding.UTF8.GetBytes("ie"); Assert.True(BObject.TryParse(encoded, out BInteger bObject)); Assert.Equal(0L, bObject.Value); }