public static void DoTestLong(long value) { String b = "i" + TestCommon.LongToString(value) + "e"; CBORObject beo = EncodingFromBytes(DataUtilities.GetUtf8Bytes(b, false)); Assert.AreEqual(value, beo.AsNumber().ToInt64Checked()); String newb = DataUtilities.GetUtf8String(EncodingToBytes(beo), false); Assert.AreEqual(b, newb); }
public DateTime FromCBORObject(CBORObject obj) { CBORObject untaggedObject = obj; if (obj.HasMostOuterTag(0)) { throw new CBORException("Only numeric representation of date is permitted"); } else if (obj.HasMostOuterTag(1)) { Console.WriteLine("Section 2 of RFC8392 states that the leading 1 tag MUST be omitted, but it is present here"); untaggedObject = obj.UntagOne(); } if (!untaggedObject.IsNumber) { throw new CBORException("Expected number for representation of date"); } CBORNumber num = untaggedObject.AsNumber(); if (!num.IsFinite()) { throw new CBORException("Not a finite number"); } if (num.CompareTo(Int64.MinValue) < 0 || num.CompareTo(Int64.MaxValue) > 0) { throw new CBORException("Date can not be represented as Instant (too small or large)"); } // Section 2.4.1 of RFC7049 states: // Tag value 1 is for numerical representation of seconds relative to // 1970-01-01T00:00Z in UTC time. (For the non-negative values that the // Portable Operating System Interface (POSIX) defines, the number of // seconds is counted in the same way as for POSIX "seconds since the // epoch" [TIME_T].) The tagged item can be a positive or negative // integer (major types 0 and 1), or a floating-point number (major type // 7 with additional information 25, 26, or 27). Note that the number // can be negative (time before 1970-01-01T00:00Z) and, if a floating- // point number, indicate fractional seconds. // // We only support a positive integer ... // if (!num.IsInteger()) { throw new CBORException( String.Format("Date is represented as {0} - Only {1} is supported", num.GetType(), CBORNumber.NumberKind.Integer)); } System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc); dtDateTime = dtDateTime.AddSeconds(num.ToInt64Unchecked()).ToUniversalTime(); return(dtDateTime); }
public void TestParseJSONNumber() { foreach (var str in BadJsonNumbers) { if (CBORDataUtilities.ParseJSONNumber(str) != null) { Assert.Fail(str); } if (CBORDataUtilities.ParseJSONNumber(str, false, false, true) != null) { Assert.Fail(str); } if (CBORDataUtilities.ParseJSONNumber(str, false, false, false) != null) { Assert.Fail(str); } } CBORObject cbor = CBORDataUtilities.ParseJSONNumber("2e-2147483648"); CBORTestCommon.AssertJSONSer(cbor, "2E-2147483648"); foreach (var str in GoodJsonNumbers) { if (CBORDataUtilities.ParseJSONNumber(str) == null) { Assert.Fail(str); } if (CBORDataUtilities.ParseJSONNumber(str, false, false, true) == null) { Assert.Fail(str); } if (CBORDataUtilities.ParseJSONNumber(str, false, false, false) == null) { Assert.Fail(str); } } TestCommon.CompareTestEqual( ToObjectTest.TestToFromObjectRoundTrip(230).AsNumber(), CBORDataUtilities.ParseJSONNumber("23.0e01").AsNumber()); TestCommon.CompareTestEqual( ToObjectTest.TestToFromObjectRoundTrip(23).AsNumber(), CBORDataUtilities.ParseJSONNumber("23.0e00").AsNumber()); cbor = CBORDataUtilities.ParseJSONNumber( "1e+99999999999999999999999999"); Assert.IsTrue(cbor != null); Assert.IsFalse(cbor.AsNumber().CanFitInDouble()); CBORTestCommon.AssertRoundTrip(cbor); }
internal static void WritePlistToInternalCore( CBORObject obj, StringOutput writer, JSONOptions options, IList <CBORObject> stack) { if (obj.IsNumber) { if (obj.AsNumber().IsInteger()) { writer.WriteString("<integer>"); writer.WriteString(obj.ToJSONString()); writer.WriteString("</integer>"); } else { writer.WriteString("<real>"); writer.WriteString(obj.ToJSONString()); writer.WriteString("</real>"); } return; } if (obj.HasMostOuterTag(0) || obj.HasMostOuterTag(1)) { CBORDateConverter conv = CBORDateConverter.TaggedString; var year = new EInteger[1]; var lesserFields = new int[7]; if (!conv.TryGetDateTimeFields(obj, year, lesserFields)) { throw new InvalidOperationException("Unsupported date/time"); } // Set fractional seconds and offset to 0, since // they're not needed lesserFields[5] = 0; lesserFields[6] = 0; CBORObject newobj = conv.DateTimeFieldsToCBORObject(year[0], lesserFields); writer.WriteString("<date>"); writer.WriteString(newobj.AsString()); writer.WriteString("</date>"); return; } switch (obj.Type) { case CBORType.Integer: { CBORObject untaggedObj = obj.Untag(); writer.WriteString("<integer>"); writer.WriteString(untaggedObj.ToJSONString()); writer.WriteString("</integer>"); break; } case CBORType.FloatingPoint: { CBORObject untaggedObj = obj.Untag(); writer.WriteString("<real>"); writer.WriteString(untaggedObj.ToJSONString()); writer.WriteString("</real>"); break; } case CBORType.Boolean: { if (obj.IsTrue) { writer.WriteString("<true/>"); return; } if (obj.IsFalse) { writer.WriteString("<false/>"); return; } return; } case CBORType.SimpleValue: { // Write all CBOR simple values (other than true and false) as the text string // "null". writer.WriteString("<str"); writer.WriteString("ing>"); writer.WriteString("null"); writer.WriteString("</str"); writer.WriteString("ing>"); return; } case CBORType.ByteString: { byte[] byteArray = obj.GetByteString(); if (byteArray.Length == 0) { writer.WriteString("<data></data>"); return; } if (obj.HasTag(22)) { writer.WriteString("<data>"); // Base64 with padding Base64.WriteBase64( writer, byteArray, 0, byteArray.Length, true); writer.WriteString("</data>"); } else if (obj.HasTag(23)) { writer.WriteString("<str"); writer.WriteString("ing>"); // Write as base16 for (int i = 0; i < byteArray.Length; ++i) { writer.WriteCodePoint((int)Hex16[(byteArray[i] >> 4) & 15]); writer.WriteCodePoint((int)Hex16[byteArray[i] & 15]); } writer.WriteString("</str"); writer.WriteString("ing>"); } else { writer.WriteString("<data>"); // Base64 with padding Base64.WriteBase64( writer, byteArray, 0, byteArray.Length, true); writer.WriteString("</data>"); } break; } case CBORType.TextString: { string thisString = obj.AsString(); if (thisString.Length == 0) { writer.WriteString("<str"); writer.WriteString("ing>"); writer.WriteString("</str"); writer.WriteString("ing>"); return; } writer.WriteString("<str"); writer.WriteString("ing>"); WritePlistStringUnquoted(thisString, writer, options); writer.WriteString("</str"); writer.WriteString("ing>"); break; } case CBORType.Array: { writer.WriteString("<array>"); for (var i = 0; i < obj.Count; ++i) { bool pop = CheckCircularRef(stack, obj, obj[i]); WritePlistToInternalCore(obj[i], writer, options, stack); PopRefIfNeeded(stack, pop); } writer.WriteString("</array>"); break; } case CBORType.Map: { var hasNonStringKeys = false; ICollection <KeyValuePair <CBORObject, CBORObject> > entries = obj.Entries; foreach (KeyValuePair <CBORObject, CBORObject> entry in entries) { CBORObject key = entry.Key; if (key.Type != CBORType.TextString || key.IsTagged) { // treat a non-text-string item or a tagged item // as having non-string keys hasNonStringKeys = true; break; } } if (!hasNonStringKeys) { writer.WriteString("<dict>"); foreach (KeyValuePair <CBORObject, CBORObject> entry in entries) { CBORObject key = entry.Key; CBORObject value = entry.Value; writer.WriteString("<key>"); WritePlistStringUnquoted(key.AsString(), writer, options); writer.WriteString("</key>"); bool pop = CheckCircularRef(stack, obj, value); WritePlistToInternalCore(value, writer, options, stack); PopRefIfNeeded(stack, pop); } writer.WriteString("</dict>"); } else { // This map has non-string keys IDictionary <string, CBORObject> stringMap = new Dictionary <string, CBORObject>(); // Copy to a map with String keys, since // some keys could be duplicates // when serialized to strings foreach (KeyValuePair <CBORObject, CBORObject> entry in entries) { CBORObject key = entry.Key; CBORObject value = entry.Value; string str = null; switch (key.Type) { case CBORType.TextString: str = key.AsString(); break; case CBORType.Array: case CBORType.Map: { var sb = new StringBuilder(); var sw = new StringOutput(sb); bool pop = CheckCircularRef(stack, obj, key); WritePlistToInternalCore(key, sw, options, stack); PopRefIfNeeded(stack, pop); str = sb.ToString(); break; } default: str = key.ToJSONString(options); break; } if (stringMap.ContainsKey(str)) { throw new CBORException( "Duplicate Plist string equivalents of map" + "\u0020keys"); } stringMap[str] = value; } writer.WriteString("<dict>"); foreach (KeyValuePair <string, CBORObject> entry in stringMap) { string key = entry.Key; CBORObject value = entry.Value; writer.WriteString("<key>"); WritePlistStringUnquoted((string)key, writer, options); writer.WriteString("</key>"); bool pop = CheckCircularRef(stack, obj, value); WritePlistToInternalCore(value, writer, options, stack); PopRefIfNeeded(stack, pop); } writer.WriteString("</dict>"); } break; } default: throw new InvalidOperationException("Unexpected item" + "\u0020type"); } }
public void TestAsEInteger() { try { ToObjectTest.TestToFromObjectRoundTrip( (object)null).AsNumber().ToEInteger(); Assert.Fail("Should have failed"); } catch (InvalidOperationException) { // NOTE: Intentionally empty } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { CBORObject.Null.AsNumber().ToEInteger(); Assert.Fail("Should have failed"); } catch (InvalidOperationException) { // NOTE: Intentionally empty } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { CBORObject.True.AsNumber().ToEInteger(); Assert.Fail("Should have failed"); } catch (InvalidOperationException) { // NOTE: Intentionally empty } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { CBORObject.False.AsNumber().ToEInteger(); Assert.Fail("Should have failed"); } catch (InvalidOperationException) { // NOTE: Intentionally empty } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { CBORObject.Undefined.AsNumber().ToEInteger(); Assert.Fail("Should have failed"); } catch (InvalidOperationException) { // NOTE: Intentionally empty } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { CBORObject.NewArray().AsNumber().ToEInteger(); Assert.Fail("Should have failed"); } catch (InvalidOperationException) { // NOTE: Intentionally empty } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { CBORObject.NewMap().AsNumber().ToEInteger(); Assert.Fail("Should have failed"); } catch (InvalidOperationException) { // NOTE: Intentionally empty } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } CBORObject numbers = CBORObjectTest.GetNumberData(); for (int i = 0; i < numbers.Count; ++i) { CBORObject numberinfo = numbers[i]; string numberString = numberinfo["number"].AsString(); CBORObject cbornumber = ToObjectTest.TestToFromObjectRoundTrip(EDecimal.FromString( numberString)); if (!numberinfo["integer"].Equals(CBORObject.Null)) { Assert.AreEqual( numberinfo["integer"].AsString(), cbornumber.AsNumber().ToEInteger().ToString()); } else { try { cbornumber.AsNumber().ToEInteger(); Assert.Fail("Should have failed"); } catch (OverflowException) { // NOTE: Intentionally empty } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } } } { string stringTemp = ToObjectTest.TestToFromObjectRoundTrip(0.75f).AsNumber().ToEInteger() .ToString(); Assert.AreEqual( "0", stringTemp); } { string stringTemp = ToObjectTest.TestToFromObjectRoundTrip(0.99f).AsNumber().ToEInteger() .ToString(); Assert.AreEqual( "0", stringTemp); } { string stringTemp = ToObjectTest.TestToFromObjectRoundTrip(0.0000000000000001f) .AsNumber().ToEInteger().ToString(); Assert.AreEqual( "0", stringTemp); } { string stringTemp = ToObjectTest.TestToFromObjectRoundTrip(0.5f).AsNumber().ToEInteger() .ToString(); Assert.AreEqual( "0", stringTemp); } { string stringTemp = ToObjectTest.TestToFromObjectRoundTrip(1.5f).AsNumber().ToEInteger() .ToString(); Assert.AreEqual( "1", stringTemp); } { string stringTemp = ToObjectTest.TestToFromObjectRoundTrip(2.5f).AsNumber().ToEInteger() .ToString(); Assert.AreEqual( "2", stringTemp); } { string stringTemp = ToObjectTest.TestToFromObjectRoundTrip( (float)328323f).AsNumber().ToEInteger().ToString(); Assert.AreEqual( "328323", stringTemp); } { string stringTemp = ToObjectTest.TestToFromObjectRoundTrip( (double)0.75).AsNumber().ToEInteger() .ToString(); Assert.AreEqual( "0", stringTemp); } { string stringTemp = ToObjectTest.TestToFromObjectRoundTrip( (double)0.99).AsNumber().ToEInteger().ToString(); Assert.AreEqual( "0", stringTemp); } { string stringTemp = ToObjectTest.TestToFromObjectRoundTrip((double)0.0000000000000001) .AsNumber().ToEInteger().ToString(); Assert.AreEqual( "0", stringTemp); } { string stringTemp = ToObjectTest.TestToFromObjectRoundTrip( (double)0.5).AsNumber().ToEInteger().ToString(); Assert.AreEqual( "0", stringTemp); } { string stringTemp = ToObjectTest.TestToFromObjectRoundTrip( (double)1.5).AsNumber().ToEInteger() .ToString(); Assert.AreEqual( "1", stringTemp); } { string stringTemp = ToObjectTest.TestToFromObjectRoundTrip( (double)2.5).AsNumber().ToEInteger().ToString(); Assert.AreEqual( "2", stringTemp); } { double dbl = 328323; string stringTemp = ToObjectTest.TestToFromObjectRoundTrip(dbl) .AsNumber().ToEInteger().ToString(); Assert.AreEqual( "328323", stringTemp); } try { ToObjectTest.TestToFromObjectRoundTrip(Single.PositiveInfinity) .AsNumber().ToEInteger(); Assert.Fail("Should have failed"); } catch (OverflowException) { // NOTE: Intentionally empty } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { ToObjectTest.TestToFromObjectRoundTrip(Single.NegativeInfinity) .AsNumber().ToEInteger(); Assert.Fail("Should have failed"); } catch (OverflowException) { // NOTE: Intentionally empty } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { ToObjectTest.TestToFromObjectRoundTrip( Single.NaN).AsNumber().ToEInteger(); Assert.Fail("Should have failed"); } catch (OverflowException) { // NOTE: Intentionally empty } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { ToObjectTest.TestToFromObjectRoundTrip(Double.PositiveInfinity) .AsNumber().ToEInteger(); Assert.Fail("Should have failed"); } catch (OverflowException) { // NOTE: Intentionally empty } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { ToObjectTest.TestToFromObjectRoundTrip(Double.NegativeInfinity) .AsNumber().ToEInteger(); Assert.Fail("Should have failed"); } catch (OverflowException) { // NOTE: Intentionally empty } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { ToObjectTest.TestToFromObjectRoundTrip( Double.NaN).AsNumber().ToEInteger(); Assert.Fail("Should have failed"); } catch (OverflowException) { // NOTE: Intentionally empty } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } }
private static void AssertNegative(CBORObject obj) { Assert.IsTrue(obj.AsNumber().IsNegative()); CBORTestCommon.AssertRoundTrip(obj); }