Exemplo n.º 1
0
        private static CBORObject readString(Stream stream, char firstChar)
        {
            var builder = new StringBuilder();

            if (firstChar < (int)'0' && firstChar > (int)'9')
            {
                throw new CBORException("Invalid integer encoding");
            }
            builder.Append(firstChar);
            while (true)
            {
                int c = stream.ReadByte();
                if (c < 0)
                {
                    throw new CBORException("Premature end of data");
                }
                if (c >= (int)'0' && c <= (int)'9')
                {
                    builder.Append((char)c);
                }
                else if (c == (int)':')
                {
                    break;
                }
                else
                {
                    throw new CBORException("Invalid integer encoding");
                }
            }
            CBORObject number = CBORDataUtilities.ParseJSONNumber(
                builder.ToString(),
                true,
                true);
            var length = 0;

            try {
                length = number.AsInt32();
            } catch (ArithmeticException ex) {
                throw new CBORException("Length too long", ex);
            }
            builder = new StringBuilder();
            switch (DataUtilities.ReadUtf8(stream, length, builder, false))
            {
            case -2:
                throw new CBORException("Premature end of data");

            case -1:
                throw new CBORException("Invalid UTF-8");
            }
            return(CBORObject.FromObject(builder.ToString()));
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
 public void TestParseJSONNumberNegativeZero()
 {
     {
         string stringTemp = CBORDataUtilities.ParseJSONNumber("-0").ToString();
         Assert.AreEqual(
             "0",
             stringTemp);
     }
     {
         string stringTemp = CBORDataUtilities.ParseJSONNumber("-0E+0").ToString();
         Assert.AreEqual(
             "0",
             stringTemp);
     }
     {
         string stringTemp = CBORDataUtilities.ParseJSONNumber("-0E-0").ToString();
         Assert.AreEqual(
             "0",
             stringTemp);
     }
     {
         string stringTemp = CBORDataUtilities.ParseJSONNumber("-0E-1").ToString();
         Assert.AreEqual(
             "0.0",
             stringTemp);
     }
     {
         string stringTemp = CBORDataUtilities.ParseJSONNumber("-0.00").ToString();
         Assert.AreEqual(
             "0.00",
             stringTemp);
     }
     {
         string stringTemp = CBORDataUtilities.ParseJSONNumber("-0.00E+0").ToString();
         Assert.AreEqual(
             "0.00",
             stringTemp);
     }
     {
         string stringTemp = CBORDataUtilities.ParseJSONNumber("-0.00E-0").ToString();
         Assert.AreEqual(
             "0.00",
             stringTemp);
     }
     {
         string stringTemp = CBORDataUtilities.ParseJSONNumber("-0.00E-1").ToString();
         Assert.AreEqual(
             "0.000",
             stringTemp);
     }
 }
Exemplo n.º 4
0
        public void TestParseJSONNumberSubstring()
        {
            string tstr =

                "-3.00931381333368754713014659613049757554804012787921371662913692598770508705049030832574634419795955864174175076186656951904296875000E-49";

            try {
                CBORDataUtilities.ParseJSONNumber(
                    "xyzxyz" + tstr,
                    6,
                    tstr.Length,
                    JSONOptions.Default);
            } catch (Exception ex) {
                Assert.Fail(ex.ToString());
                throw new InvalidOperationException(String.Empty, ex);
            }
        }
Exemplo n.º 5
0
        public void TestParseJSONNumberNegativeZero()
        {
            var strings = new string[] {
                "-0", "0", "-0E+0", "0", "-0E-0", "0", "-0E-1", "0.0",
                "-0.00", "0.00", "-0.00E+0", "0.00", "-0.00E-0", "0.00",
                "-0.00E-1", "0.000",
            };

            for (var i = 0; i < strings.Length; i += 2)
            {
                var jsonDecimal = (EDecimal)CBORDataUtilities
                                  .ParseJSONNumber(strings[i]).ToObject(typeof(EDecimal));
                Assert.AreEqual(
                    strings[i + 1],
                    jsonDecimal.ToString());
            }
        }
Exemplo n.º 6
0
 public void TestParseJSONNumberObsolete()
 {
     foreach (var str in BadJsonNumbers)
     {
         if (CBORDataUtilities.ParseJSONNumber(str, false, false, true) !=
             null)
         {
             Assert.Fail(str);
         }
     }
     foreach (var str in GoodJsonNumbers)
     {
         if (CBORDataUtilities.ParseJSONNumber(str, false, false) == null)
         {
             Assert.Fail(str);
         }
     }
 }
Exemplo n.º 7
0
        private static CBORObject readInteger(Stream stream)
        {
            var builder = new StringBuilder();
            var start   = true;

            while (true)
            {
                int c = stream.ReadByte();
                if (c < 0)
                {
                    throw new CBORException("Premature end of data");
                }
                if (c >= (int)'0' && c <= (int)'9')
                {
                    builder.Append((char)c);
                    start = false;
                }
                else if (c == (int)'e')
                {
                    break;
                }
                else if (start && c == '-')
                {
                    start = false;
                    builder.Append((char)c);
                }
                else
                {
                    throw new CBORException("Invalid integer encoding");
                }
            }
            return(CBORDataUtilities.ParseJSONNumber(
                       builder.ToString(),
                       true,
                       false));
        }
Exemplo n.º 8
0
        public void TestPreserveNegativeZero()
        {
            CBORObject cbor;

            cbor = CBORDataUtilities.ParseJSONNumber("-0", false, false, true);
            {
                string stringTemp = cbor.ToObject(typeof(EDecimal)).ToString();
                Assert.AreEqual(
                    "-0",
                    stringTemp);
            }
            cbor = CBORDataUtilities.ParseJSONNumber("-0e-1", false, false, true);
            {
                string stringTemp = cbor.ToObject(typeof(EDecimal)).ToString();
                Assert.AreEqual(
                    "-0.0",
                    stringTemp);
            }
            cbor = CBORDataUtilities.ParseJSONNumber("-0e1", false, false, true);
            {
                string stringTemp = cbor.ToObject(typeof(EDecimal)).ToString();
                Assert.AreEqual(
                    "-0E+1",
                    stringTemp);
            }
            cbor = CBORDataUtilities.ParseJSONNumber("-0.0e1", false, false, true);
            {
                string stringTemp = cbor.ToObject(typeof(EDecimal)).ToString();
                Assert.AreEqual(
                    "-0",
                    stringTemp);
            }
            cbor = CBORDataUtilities.ParseJSONNumber("-0.0", false, false, true);
            {
                string stringTemp = cbor.ToObject(typeof(EDecimal)).ToString();
                Assert.AreEqual(
                    "-0.0",
                    stringTemp);
            }
            var assertNegatives = new string[] {
                "-0",
                "-0.0",
                "-0.0000",
                "-0e0",
                "-0e+1",
                "-0e-1",
                "-0e+999999999999",
                "-0e-999999999999",
                "-0.0e0",
                "-0.0e+1",
                "-0.0e-1",
                "-0.0e+999999999999",
                "-0.0e-999999999999",
                "-0.000e0",
                "-0.000e+0",
                "-0.000e-0",
                "-0.000e1",
                "-0.000e+1",
                "-0.000e-1",
                "-0.000e+999999999999",
                "-0.000e-999999999999",
            };

            foreach (var str in assertNegatives)
            {
                cbor = CBORDataUtilities.ParseJSONNumber(str, false, false, true);
                AssertNegative(cbor);
            }
        }
Exemplo n.º 9
0
        public void TestParseJSONNumber()
        {
            if (CBORDataUtilities.ParseJSONNumber("100.", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("-100.", false, false) != null)
            {
                Assert.Fail();
            }
            if (
                CBORDataUtilities.ParseJSONNumber(
                    "100.e+20",
                    false,
                    false) != null)
            {
                Assert.Fail();
            }
            if (
                CBORDataUtilities.ParseJSONNumber(
                    "-100.e20",
                    false,
                    false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("100.e20", false, false) != null)
            {
                Assert.Fail();
            }

            if (CBORDataUtilities.ParseJSONNumber("+0.1", false, false) != null)
            {
                Assert.Fail();
            }

            if (CBORDataUtilities.ParseJSONNumber("0.", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("-0.", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0g.1", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0.e+20", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("-0.e20", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0.e20", false, false) != null)
            {
                Assert.Fail();
            }

            if (CBORDataUtilities.ParseJSONNumber(null, false, false) != null)
            {
                Assert.Fail();
            }
            if (
                CBORDataUtilities.ParseJSONNumber(
                    String.Empty,
                    false,
                    false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("xyz", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("true", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber(".1", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0..1", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0xyz", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0.1xyz", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0.xyz", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0.5exyz", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0.5q+88", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0.5ee88", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("-5e") != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("-5e-2x") != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("-5e+2x") != null)
            {
                Assert.Fail();
            }
            CBORObject cbor = CBORDataUtilities.ParseJSONNumber("2e-2147483648");

            CBORTestCommon.AssertSer(cbor, "2E-2147483648");
            if (
                CBORDataUtilities.ParseJSONNumber(
                    "0.5e+xyz",
                    false,
                    false) != null)
            {
                Assert.Fail();
            }
            if (
                CBORDataUtilities.ParseJSONNumber(
                    "0.5e+88xyz",
                    false,
                    false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0000") != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0x1") != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0xf") != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0x20") != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0x01") != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber(".2") != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber(".05") != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("-.2") != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("-.05") != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("23.") != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("23.e0") != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("23.e1") != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0.") != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("5.2", true, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("5e+1", true, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("-5.2", true, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("-5e+1", true, false) != null)
            {
                Assert.Fail();
            }

            TestCommon.CompareTestEqual(
                CBORObject.FromObject(230),
                CBORDataUtilities.ParseJSONNumber("23.0e01"));
            TestCommon.CompareTestEqual(
                CBORObject.FromObject(23),
                CBORDataUtilities.ParseJSONNumber("23.0e00"));
            cbor = CBORDataUtilities.ParseJSONNumber(
                "1e+99999999999999999999999999",
                false,
                false);
            Assert.IsTrue(cbor != null);
            Assert.IsFalse(cbor.CanFitInDouble());
            CBORTestCommon.AssertRoundTrip(cbor);
        }
Exemplo n.º 10
0
        public void TestPreserveNegativeZero()
        {
            CBORObject cbor;

            cbor = CBORDataUtilities.ParseJSONNumber("-0", false, false, true);
            {
                string stringTemp = cbor.ToString();
                Assert.AreEqual(
                    "-0",
                    stringTemp);
            }
            cbor = CBORDataUtilities.ParseJSONNumber("-0e-1", false, false, true);
            {
                string stringTemp = cbor.ToString();
                Assert.AreEqual(
                    "-0.0",
                    stringTemp);
            }
            cbor = CBORDataUtilities.ParseJSONNumber("-0e1", false, false, true);
            {
                string stringTemp = cbor.ToString();
                Assert.AreEqual(
                    "-0E+1",
                    stringTemp);
            }
            cbor = CBORDataUtilities.ParseJSONNumber("-0.0e1", false, false, true);
            {
                string stringTemp = cbor.ToString();
                Assert.AreEqual(
                    "-0",
                    stringTemp);
            }
            cbor = CBORDataUtilities.ParseJSONNumber("-0.0", false, false, true);
            {
                string stringTemp = cbor.ToString();
                Assert.AreEqual(
                    "-0.0",
                    stringTemp);
            }

            this.AssertNegative(
                CBORDataUtilities.ParseJSONNumber(
                    "-0",
                    false,
                    false,
                    true));
            this.AssertNegative(
                CBORDataUtilities.ParseJSONNumber(
                    "-0.0",
                    false,
                    false,
                    true));
            this.AssertNegative(
                CBORDataUtilities.ParseJSONNumber(
                    "-0.0000",
                    false,
                    false,
                    true));
            this.AssertNegative(
                CBORDataUtilities.ParseJSONNumber(
                    "-0e0",
                    false,
                    false,
                    true));
            this.AssertNegative(
                CBORDataUtilities.ParseJSONNumber(
                    "-0e+1",
                    false,
                    false,
                    true));
            this.AssertNegative(
                CBORDataUtilities.ParseJSONNumber(
                    "-0e-1",
                    false,
                    false,
                    true));
            this.AssertNegative(
                CBORDataUtilities.ParseJSONNumber(
                    "-0e+999999999999",
                    false,
                    false,
                    true));
            this.AssertNegative(
                CBORDataUtilities.ParseJSONNumber(
                    "-0e-999999999999",
                    false,
                    false,
                    true));
            this.AssertNegative(
                CBORDataUtilities.ParseJSONNumber(
                    "-0.0e0",
                    false,
                    false,
                    true));
            this.AssertNegative(
                CBORDataUtilities.ParseJSONNumber(
                    "-0.0e+1",
                    false,
                    false,
                    true));
            this.AssertNegative(
                CBORDataUtilities.ParseJSONNumber(
                    "-0.0e-1",
                    false,
                    false,
                    true));
            this.AssertNegative(
                CBORDataUtilities.ParseJSONNumber(
                    "-0.0e+999999999999",
                    false,
                    false,
                    true));
            this.AssertNegative(
                CBORDataUtilities.ParseJSONNumber(
                    "-0.0e-999999999999",
                    false,
                    false,
                    true));
            this.AssertNegative(
                CBORDataUtilities.ParseJSONNumber(
                    "-0.000e0",
                    false,
                    false,
                    true));
            this.AssertNegative(
                CBORDataUtilities.ParseJSONNumber(
                    "-0.000e+0",
                    false,
                    false,
                    true));
            this.AssertNegative(
                CBORDataUtilities.ParseJSONNumber(
                    "-0.000e-0",
                    false,
                    false,
                    true));
            this.AssertNegative(
                CBORDataUtilities.ParseJSONNumber(
                    "-0.000e1",
                    false,
                    false,
                    true));
            this.AssertNegative(
                CBORDataUtilities.ParseJSONNumber(
                    "-0.000e+1",
                    false,
                    false,
                    true));
            this.AssertNegative(
                CBORDataUtilities.ParseJSONNumber(
                    "-0.000e-1",
                    false,
                    false,
                    true));
            this.AssertNegative(
                CBORDataUtilities.ParseJSONNumber(
                    "-0.000e+999999999999",
                    false,
                    false,
                    true));
            this.AssertNegative(
                CBORDataUtilities.ParseJSONNumber(
                    "-0.000e-999999999999",
                    false,
                    false,
                    true));
        }