コード例 #1
0
        public static void SerializeResourceInCoral(IResource resource, CoralBody coral,
                                                    Dictionary <string, CBORObject> dictionary,
                                                    ResourceAttributes otherAttributes = null, Uri uriRelative = null,
                                                    bool isEndPoint = false)
        {
            CBORObject obj = CBORObject.NewArray();
            CBORObject href;

            if (uriRelative == null)
            {
                href = Ciri.ToCbor(resource.Path + resource.Name);
            }
            else
            {
                href = Ciri.ToCbor(new Uri(uriRelative, resource.Path + resource.Name));
            }

            CoralBody body = new CoralBody();

            SerializeAttributesInCoral(resource.Attributes, body, dictionary, uriRelative);
            if (otherAttributes != null)
            {
                SerializeAttributesInCoral(otherAttributes, body, dictionary, uriRelative);
            }

            if (body.Length == 0)
            {
                body = null;
            }

            CoralItem item = new CoralLink(isEndPoint ? "http://coreapps.org/ref#rd-unit" : "http://coreapps.org/reef#rd-item", href, body);

            coral.Add(item);
        }
コード例 #2
0
        public void Constructors()
        {
            CoralLink link;

            link = new CoralLink("http://test.augustcellars.com/relation", CBORObject.FromObject(1));
            Assert.AreEqual(link.RelationTypeText, "http://test.augustcellars.com/relation");
            Assert.AreEqual(1, link.Value.AsInt32());

            link = new CoralLink("http://test.augustcellars.com/relation", "value");
            Assert.AreEqual("http://test.augustcellars.com/relation", link.RelationTypeText);
            Assert.AreEqual(CBORType.TextString, link.Value.Type);
            Assert.AreEqual("value", link.Value.AsString());

            link = new CoralLink("http://test.augustcellars.com/relation", new Cori("coap://host:5/root/path"));
            Assert.AreEqual("http://test.augustcellars.com/relation", link.RelationTypeText);
            Assert.IsNull(link.Value);
            Assert.AreEqual("coap://host:5/root/path", link.Target.ToString());


            Assert.ThrowsException <ArgumentException>(() => link = new CoralLink("relation", CBORObject.FromObject(DateTime.UtcNow)));
            Assert.ThrowsException <ArgumentException>(() => link = new CoralLink("http://test.augustcellars.com/relation", CBORObject.FromObject(DateTime.UtcNow)));
            Assert.ThrowsException <ArgumentException>(() => link = new CoralLink("http://test.augustcellars.com/relation", CBORObject.NewArray()));
            Assert.ThrowsException <ArgumentException>(() => link = new CoralLink("http://test.augustcellars.com/relation", CBORObject.NewMap()));
            Assert.ThrowsException <ArgumentException>(() => link = new CoralLink("http://test.augustcellars.com/relation", CBORObject.FromSimpleValue(99)));
            // Assert.ThrowsException<ArgumentException>(() => link = new CoralLink("relation", new Cori(CBORObject.DecodeFromBytes(Hex.Decode("8102")))));
        }
コード例 #3
0
        public void TestConstructors()
        {
            Cori cori = new Cori("coap://host:99");

            Assert.ThrowsException <ArgumentException>(() => new CoralBody(CBORObject.DecodeFromBytes(Hex.Decode("01")), cori, null));
            Assert.ThrowsException <ArgumentException>(() => new CoralBody(CBORObject.DecodeFromBytes(Hex.Decode("830202820500")), cori, null));

            CoralBody body = new CoralBody(CBORObject.DecodeFromBytes(Hex.Decode("81830202820500")), cori, _testDictionary);

            Assert.AreEqual(1, body.Length);
            Assert.AreEqual("http://www.iana.org/assignments/relation/collection", ((CoralLink)body[0]).RelationTypeText);

            body = new CoralBody(CBORObject.DecodeFromBytes(Hex.Decode("828302028205008302006377766F")), cori, _testDictionary);
            Assert.AreEqual(2, body.Length);
            Assert.AreEqual("http://www.iana.org/assignments/relation/collection", ((CoralLink)body[0]).RelationTypeText);
            Assert.AreEqual("http://www.w3.org/1999/02/22-rdf-syntax-ns#type", ((CoralLink)body[1]).RelationTypeText);

            // [
            // [2
            // [0, "http", 1, "apps.augustcellars.com", 5, "rel1"],
            // [5, "target1"],
            // [
            // [2,
            // [0, "http", 1, "apps.augustcellars.com", 5, "rel2"],
            // [4, 1, 5, "target2"]], [1, [0, "coap", 1, "host3", 5, "link1"]], [2, [0, "http", 1, "apps.augustcellars.com", 5, "rel2"], [4, 1, 5, "target2"]]]], [2, [0, "http", 1, "apps.augustcellars.com", 5, "rel3"], [5, "target2"]], [1, [0, "http", 1, "host", 5, "link2", 5, "link3"]], [2, [0, "http", 1, "apps.augustcellars.com", 5, "rel4"], [4, 1, 5, "target2"]], [3, [0, "http", 1, "apps.augustcellars.com", 5, "op-type"], [4, 1, 5, "form"]]]

            body = new CoralBody(CBORObject.DecodeFromBytes(Hex.Decode("858402860064687474700176617070732E61756775737463656C6C6172732E636F6D056472656C3182056774617267657431838302860064687474700176617070732E61756775737463656C6C6172732E636F6D056472656C328404010567746172676574328201860064636F61700165686F73743305656C696E6B318302860064687474700176617070732E61756775737463656C6C6172732E636F6D056472656C328404010567746172676574328302860064687474700176617070732E61756775737463656C6C6172732E636F6D056472656C33820567746172676574328201880064687474700164686F737405656C696E6B3205656C696E6B338302860064687474700176617070732E61756775737463656C6C6172732E636F6D056472656C348404010567746172676574328303860064687474700176617070732E61756775737463656C6C6172732E636F6D05676F702D747970658404010564666F726D")), cori, _testDictionary);
            Assert.AreEqual(5, body.Length);

            Assert.IsTrue(body[0] is CoralLink);
            Assert.IsTrue(body[1] is CoralLink);
            Assert.IsTrue(body[2] is CoralBaseDirective);
            Assert.IsTrue(body[3] is CoralLink);
            Assert.IsTrue(body[4] is CoralForm);

            CoralLink link = (CoralLink)body[0];

            Assert.AreEqual("coap://host:99/target1", link.Target.ToString());
            Assert.AreEqual(3, link.Body.Length);

            Assert.IsTrue(link.Body[0] is CoralLink);
            Assert.IsTrue(link.Body[1] is CoralBaseDirective);
            Assert.IsTrue(link.Body[2] is CoralLink);

            CoralLink link2 = (CoralLink)link.Body[0];

            Assert.AreEqual("coap://host:99/target1/target2", link2.Target.ToString());
            link2 = (CoralLink)link.Body[2];
            Assert.AreEqual("coap://host3/link1/target2", link2.Target.ToString());

            link = (CoralLink)body[1];
            Assert.AreEqual("coap://host:99/target2", link.Target.ToString());

            link = (CoralLink)body[3];
            Assert.AreEqual("http://host/link2/link3/target2", link.Target.ToString());

            Assert.AreEqual("http://host/link2/link3/form", ((CoralForm)body[4]).Target.ToString());
        }
コード例 #4
0
        private static void SerializeAttributeInCoral(string name, List <string> values, CoralBody coral,
                                                      Dictionary <string, CBORObject> dictionary)
        {
            bool       useSpace = SpaceSeparatedValueAttributes.Contains(name);
            CBORObject result;

            string nameX = CoralsKeys[name];

            if (useSpace && values.Count > 1)
            {
                StringBuilder sb = new StringBuilder();

                foreach (string value in values)
                {
                    sb.Append(value);
                    sb.Append(" ");
                }

                sb.Length = sb.Length - 1;

                result = CBORObject.FromObject(sb.ToString());
            }
            else if (values.Count == 1)
            {
                string value = values.First();
                result = string.IsNullOrEmpty(value) ? CBORObject.True : CBORObject.FromObject(values.First());
            }
            else
            {
                result = CBORObject.NewArray();
                foreach (string value in values)
                {
                    if (string.IsNullOrEmpty(value))
                    {
                        result.Add(CBORObject.True);
                    }
                    else
                    {
                        result.Add(value);
                    }
                }
            }

            CoralLink link = new CoralLink(nameX, result);

            coral.Add(link);
        }
コード例 #5
0
        public void TestEncode()
        {
            CoralLink  link = new CoralLink("http://test.augustcellars.com/relation", "value");
            CBORObject cbor = link.EncodeToCBORObject(null, testDictionary);

            Assert.AreEqual(CBORType.Array, cbor.Type);
            Assert.AreEqual(3, cbor.Values.Count);
            Assert.AreEqual(2, cbor[0].AsInt32());
            Assert.AreEqual(CBORType.Array, cbor[1].Type);
            Assert.AreEqual(CBORType.TextString, cbor[2].Type);
            Assert.AreEqual("[2, [0, \"http\", 1, \"test.augustcellars.com\", 5, \"relation\"], \"value\"]", cbor.ToString());

            link = new CoralLink("http://www.w3.org/1999/02/22-rdf-syntax-ns#type", new Cori("http://www.iana.org/assignments/relation/collection"));
            cbor = link.EncodeToCBORObject(null, testDictionary);
            Assert.AreEqual(CBORType.Array, cbor.Type);
            Assert.AreEqual(3, cbor.Values.Count);
            Assert.AreEqual(2, cbor[0].AsInt32());
            Assert.AreEqual(CBORType.Integer, cbor[1].Type);
            Assert.AreEqual(0, cbor[1].AsInt32());
            Assert.AreEqual(CBORType.Integer, cbor[2].Type);
            Assert.IsTrue(cbor[2].IsTagged);
            Assert.IsTrue(cbor[2].HasOneTag(CoralDictionary.DictionaryTag));
            Assert.AreEqual(2, cbor[2].Untag().AsInt32());
            Assert.AreEqual("[2, 0, 99999(2)]", cbor.ToString());

            link = new CoralLink("http://test.augustcellars.com/relation", CBORObject.FromObject(15));
            cbor = link.EncodeToCBORObject(null, testDictionary);
            Assert.AreEqual(CBORType.Array, cbor.Type);
            Assert.AreEqual(3, cbor.Values.Count);
            Assert.IsFalse(cbor[2].IsTagged);
            Assert.AreEqual(15, cbor[2].AsInt32());
            Assert.AreEqual("[2, [0, \"http\", 1, \"test.augustcellars.com\", 5, \"relation\"], 15]", cbor.ToString());

            link = new CoralLink("http://www.w3.org/1999/02/22-rdf-syntax-ns#type", new Cori(CBORObject.DecodeFromBytes(Hex.Decode("820501"))));
            cbor = link.EncodeToCBORObject(null, testDictionary);
            Assert.AreEqual("[2, 0, [5, 1]]", cbor.ToString());

            link = new CoralLink("http://test.augustcellars.com/relation", CBORObject.False);
            cbor = link.EncodeToCBORObject(null, testDictionary);
            Assert.IsTrue(cbor[2].IsFalse);
            Assert.AreEqual("[2, [0, \"http\", 1, \"test.augustcellars.com\", 5, \"relation\"], false]", cbor.ToString());
        }
コード例 #6
0
        public void OneLink()
        {
            CoralLink link = new CoralLink("http://coreapps.org/reef#rd-unit", "/sensors");

            StringBuilder sb = new StringBuilder();

            link.BuildString(sb, "", null, null);

            Assert.AreEqual("<http://coreapps.org/reef#rd-unit> \"/sensors\"\n", sb.ToString());

            CoralDictionary dictionary = new CoralDictionary();

            CBORObject obj = link.EncodeToCBORObject(null, dictionary);

            Assert.AreEqual("[2, [0, \"http\", 1, \"coreapps.org\", 5, \"reef\", 7, \"rd-unit\"], \"/sensors\"]", obj.ToString());

            dictionary.Add(99, new Cori("http://coreapps.org/reef#rd-unit"));
            obj = link.EncodeToCBORObject(null, dictionary);
            Assert.AreEqual("[2, 99, \"/sensors\"]", obj.ToString());

            dictionary.Add(98, "/sensors");
            obj = link.EncodeToCBORObject(null, dictionary);
            Assert.AreEqual("[2, 99, 99999(98)]", obj.ToString());
        }
コード例 #7
0
        public void ParseFromCBOR()
        {
            Cori cori = new Cori("coap://host:99");

            // [2, "relation", "value"]
            CoralLink link = new CoralLink(CBORObject.DecodeFromBytes(Hex.Decode("830288006468747470016C636F7265617070732E6F7267056472656566076772642D756E69746576616C7565")), cori, testDictionary);

            Assert.AreEqual("http://coreapps.org/reef#rd-unit", link.RelationTypeText);
            Assert.AreEqual(null, link.RelationTypeInt);
            Assert.AreEqual("value", link.Value.AsString());
            Assert.AreEqual(null, link.Body);

            // [2, 2, 4]
            link = new CoralLink(CBORObject.DecodeFromBytes(Hex.Decode("830202DA0001869F04")), cori, testDictionary);
            Assert.AreEqual("http://www.iana.org/assignments/relation/collection", link.RelationTypeText);
            Assert.AreEqual(2, link.RelationTypeInt);
            Assert.AreEqual("http://coreapps.org/base#update", link.Target.ToString());
            Assert.AreEqual(4, link.TargetInt);
            Assert.AreEqual(null, link.Body);

            // [2, 99, false]
            link = new CoralLink(CBORObject.DecodeFromBytes(Hex.Decode("83021863F4")), cori, testDictionary);
            Assert.AreEqual(null, link.RelationType);
            Assert.AreEqual(99, link.RelationTypeInt);
            Assert.AreEqual(null, link.Body);

            // [2, 2, 1(999)]   Date
            link = new CoralLink(CBORObject.DecodeFromBytes(Hex.Decode("830202C11903E7")), cori, testDictionary);
            Assert.IsTrue(link.Value.IsTagged);
            Assert.AreEqual(1, link.Value.TagCount);
            Assert.AreEqual(1, link.Value.MostInnerTag);
            Assert.AreEqual(999, link.Value.Untag().AsInt32());
            Assert.AreEqual(null, link.Body);

            // [1, 2, 4]
            Assert.ThrowsException <ArgumentException>(() => { link = new CoralLink(CBORObject.DecodeFromBytes(Hex.Decode("83010204")), cori, testDictionary); }, "Not an encoded CoRAL link");

            // [2, true, false]
            Assert.ThrowsException <ArgumentException>(() => { link = new CoralLink(CBORObject.DecodeFromBytes(Hex.Decode("8302F5F4")), cori, testDictionary); }, "Invalid relation in CoRAL link");

            // [2, 2, false, [2, 3, true]]
            link = new CoralLink(CBORObject.DecodeFromBytes(Hex.Decode("840202F481830203F5")), cori, testDictionary);
            Assert.IsNotNull(link.Body);
            Assert.AreEqual(1, link.Body.Length);

            // [2, 2, 5]
            link = new CoralLink(CBORObject.DecodeFromBytes(Hex.Decode("83020205")), cori, testDictionary);
            Assert.IsNotNull(link.Value.Type);
            Assert.AreEqual(CBORType.Integer, link.Value.Type);
            Assert.AreEqual(5, link.Value.AsInt32());

            // [ 2, 2, [5, 0]]
            link = new CoralLink(CBORObject.DecodeFromBytes(Hex.Decode("830202820400")), cori, testDictionary);
            Assert.IsNotNull(link.Target);
            Assert.AreEqual(cori.Data.ToString(), link.Target.Data.ToString());

            // [ 2, 2, [5, 0, 6, "path2"], [[ 2, 2, [5, 0, 6, "path3"], [5, 2, 6, "path4"]]]]
            link = new CoralLink(CBORObject.DecodeFromBytes(Hex.Decode("84020284040005657061746832828302028404000565706174683383020284040105657061746834")), cori, testDictionary);
            Assert.IsNotNull(link.Target);
            Assert.AreEqual("[0, \"coap\", 1, \"host\", 3, 99, 5, \"path2\"]", link.Target.Data.ToString());
            CoralLink l2 = (CoralLink)link.Body[0];

            Assert.AreEqual("[0, \"coap\", 1, \"host\", 3, 99, 5, \"path3\"]", l2.Target.Data.ToString());
            l2 = (CoralLink)link.Body[1];
            Assert.AreEqual("[0, \"coap\", 1, \"host\", 3, 99, 5, \"path2\", 5, \"path4\"]", l2.Target.Data.ToString());
        }