Exemple #1
0
        private static CBORObject ValidateObject(CBORObject obj)
        {
            if (obj.Type != CBORType.TextString)
            {
                throw new CBORException("URI/IRI must be a text string");
            }
            bool isiri    = obj.HasMostOuterTag(266);
            bool isiriref = obj.HasMostOuterTag(267);

            if (
                isiriref && !URIUtility.IsValidIRI(
                    obj.AsString(),
                    URIUtility.ParseMode.IRIStrict))
            {
                throw new CBORException("String is not a valid IRI Reference");
            }
            if (
                isiri && (!URIUtility.IsValidIRI(
                              obj.AsString(),
                              URIUtility.ParseMode.IRIStrict) ||
                          !URIUtility.HasScheme(obj.AsString())))
            {
                throw new CBORException("String is not a valid IRI");
            }
            if (!URIUtility.IsValidIRI(
                    obj.AsString(),
                    URIUtility.ParseMode.URIStrict) ||
                !URIUtility.HasScheme(obj.AsString()))
            {
                throw new CBORException("String is not a valid URI");
            }
            return(obj);
        }
Exemple #2
0
 private static void TestEmptyPathOne(string uri)
 {
     int[] iriPositions = URIUtility.SplitIRI(uri);
     if (iriPositions == null)
     {
         Assert.Fail();
     }
     Assert.IsTrue(iriPositions[4] >= 0);
     Assert.AreEqual(iriPositions[4], iriPositions[5]);
 }
Exemple #3
0
 public void TestPercentDecode()
 {
     Assert.AreEqual(null, URIUtility.PercentDecode(null));
     TestPercentDecodeOne("test", "test");
     TestPercentDecodeOne("te%23t", "te\u0023t");
     TestPercentDecodeOne("te%7ft", "te\u007ft");
     TestPercentDecodeOne("te%04t", "te\u0004t");
     TestPercentDecodeOne("te%c2%80t", "te\u0080t");
     TestPercentDecodeOneFail("te%c2%40t", "te\ufffd\u0040t");
     TestPercentDecodeOneFail("te%c2%c3t", "te\ufffd\ufffdt");
 }
Exemple #4
0
 public CBORObject ValidateObject(CBORObject obj)
 {
     if (obj.Type != CBORType.TextString)
     {
         throw new CBORException("URI must be a text string");
     }
     if (!URIUtility.isValidIRI(obj.AsString()))
     {
         throw new CBORException("String is not a valid URI/IRI");
     }
     return(obj);
 }
Exemple #5
0
        private static void AssertResolve(String src, String baseuri, String dest)
        {
            AssertIdempotency(src);
            AssertIdempotency(baseuri);
            AssertIdempotency(dest);
            var res = (string)URIUtility.RelativeResolve(
                src,
                baseuri);

            AssertIdempotency(res);
            Assert.AreEqual(dest, res);
        }
Exemple #6
0
 private static void TestPercentDecodeOne(string str, string exp)
 {
     Assert.AreEqual(exp, URIUtility.PercentDecode(str));
     Assert.AreEqual(exp, URIUtility.PercentDecode(str, true));
     Assert.AreEqual(exp, URIUtility.PercentDecode(str, false));
     Assert.AreEqual(exp, URIUtility.PercentDecode(str, 0, str.Length, true));
     Assert.AreEqual(exp, URIUtility.PercentDecode(str, 0, str.Length, false));
     Assert.AreEqual(
         exp,
         URIUtility.PercentDecode("??" + str + "??", 2, 2 + str.Length, true));
     Assert.AreEqual(
         exp,
         URIUtility.PercentDecode("??" + str + "??", 2, 2 + str.Length, false));
 }
Exemple #7
0
        private static void AssertIdempotencyNeg(
            string s)
        {
            if ((bool)URIUtility.IsValidIRI(s))
            {
                Assert.Fail(s);
            }

            {
                var stringTemp = (string)URIUtility.EscapeURI(
                    s,
                    0);
                var stringTemp2 = (string)URIUtility.EscapeURI(
                    (string)URIUtility.EscapeURI(s, 0),
                    0);
                Assert.AreEqual(stringTemp, stringTemp2);
            }
            {
                var stringTemp = (string)URIUtility.EscapeURI(
                    s,
                    1);
                var stringTemp2 = (string)URIUtility.EscapeURI(
                    (string)URIUtility.EscapeURI(s, 1),
                    1);
                Assert.AreEqual(stringTemp, stringTemp2);
            }
            {
                var stringTemp = (string)URIUtility.EscapeURI(
                    s,
                    2);
                var stringTemp2 = (string)URIUtility.EscapeURI(
                    (string)URIUtility.EscapeURI(s, 2),
                    2);
                Assert.AreEqual(stringTemp, stringTemp2);
            }
            {
                var stringTemp = (string)URIUtility.EscapeURI(
                    s,
                    3);
                var stringTemp2 = (string)URIUtility.EscapeURI(
                    (string)URIUtility.EscapeURI(s, 3),
                    3);
                Assert.AreEqual(stringTemp, stringTemp2);
            }
        }
Exemple #8
0
        private static void AssertIdempotency(string s)
        {
            bool cond = URIUtility.IsValidIRI(s);

            Assert.IsTrue(cond, s);
            {
                var stringTemp = (string)URIUtility.EscapeURI(
                    s,
                    0);
                var stringTemp2 = (string)URIUtility.EscapeURI(
                    (string)URIUtility.EscapeURI(s, 0),
                    0);
                Assert.AreEqual(stringTemp, stringTemp2);
            }
            {
                var stringTemp = (string)URIUtility.EscapeURI(
                    s,
                    1);
                var stringTemp2 = (string)URIUtility.EscapeURI(
                    (string)URIUtility.EscapeURI(s, 1),
                    1);
                Assert.AreEqual(stringTemp, stringTemp2);
            }
            {
                var stringTemp = (string)URIUtility.EscapeURI(
                    s,
                    2);
                var stringTemp2 = (string)URIUtility.EscapeURI(
                    (string)URIUtility.EscapeURI(s, 2),
                    2);
                Assert.AreEqual(stringTemp, stringTemp2);
            }
            {
                var stringTemp = (string)URIUtility.EscapeURI(
                    s,
                    3);
                var stringTemp2 = (string)URIUtility.EscapeURI(
                    (string)URIUtility.EscapeURI(s, 3),
                    3);
                Assert.AreEqual(stringTemp, stringTemp2);
            }
        }
Exemple #9
0
        public CBORObject ToCBORObject(Uri uri)
        {
            if (uri == null)
            {
                throw new ArgumentNullException(nameof(uri));
            }
            string uriString = uri.ToString();
            var    nonascii  = false;

            for (var i = 0; i < uriString.Length; ++i)
            {
                nonascii |= uriString[i] >= 0x80;
            }
            int tag = nonascii ? 266 : 32;

            if (!URIUtility.HasScheme(uriString))
            {
                tag = 267;
            }
            return(CBORObject.FromObjectAndTag(uriString, (int)tag));
        }
Exemple #10
0
 private static void TestPercentDecodeOneFail(string str, string exp)
 {
     Assert.AreEqual(exp, URIUtility.PercentDecode(str));
     Assert.AreEqual(exp, URIUtility.PercentDecode(str, true));
     Assert.AreEqual(null, URIUtility.PercentDecode(str, false));
     Assert.AreEqual(exp, URIUtility.PercentDecode(str, 0, str.Length, true));
     {
         object objectTemp  = null;
         object objectTemp2 = URIUtility.PercentDecode(
             str,
             0,
             str.Length,
             false);
         Assert.AreEqual(objectTemp, objectTemp2);
     }
     Assert.AreEqual(
         exp,
         URIUtility.PercentDecode("??" + str + "??", 2, 2 + str.Length, true));
     Assert.AreEqual(
         null,
         URIUtility.PercentDecode("??" + str + "??", 2, 2 + str.Length, false));
 }
Exemple #11
0
 public static bool SplitIRIFails(string iri, bool expectedNonNull)
 {
     return(expectedNonNull ? URIUtility.SplitIRI(iri) == null :
            URIUtility.SplitIRI(iri) != null);
 }