Ejemplo n.º 1
0
        public void ExampleTest()
        {
            CoralDocument document = new CoralDocument();

            CoralBody body = new CoralBody();

            body.Add(new CoralLink("coap://jimsch.example.com/coreapp/reef#content-type", CBORObject.FromObject(99599)));
            body.Add(new CoralLink("coap://jimsch.example.com/coreapp/reef#security", "OSCORE"));
            body.Add(new CoralLink("coap://jimsch.example.com/coreapp/reef#authority-type", "ACE"));
            body.Add(new CoralLink("coap://jimsch.example.com/coreapp/reef#authority", new Cori("coap://ace.example.org/token")));
            document.Add(new CoralLink("coap://jimsch.example.com/coreapp/reef#rd-register", new Cori("coap://jimsch.example.org/rd/endpoints"), body));

            document.Add(new CoralLink("coap://jimsch.example.com/coreapp/reef#rd-endpointSearch", new Cori("coap://jimsch.example.org/rd/endpoints")));
            document.Add(new CoralLink("coap://jimsch.example.com/coreapp/reef#rd-resourceSearch", new Cori("coap://jimsch.example.org/rd/resources")));

            document.Add(new CoralBaseDirective(new Cori("coaps://jimsch.example.org/rd")));

            body = new CoralBody();
            body.Add(new CoralLink("coap://jimsch.example.com/coreapp/reef#content-type", CBORObject.FromObject(99599)));
            body.Add(new CoralLink("coap://jimsch.example.com/coreapp/reef#security", "OSCORE"));
            body.Add(new CoralLink("coap://jimsch.example.com/coreapp/reef#authority-type", "ACE"));
            body.Add(new CoralLink("coap://jimsch.example.com/coreapp/reef#authority", new Cori("coaps://ace.example.org/token")));
            document.Add(new CoralLink("coap://jimsch.example.com/coreapp/reef#rd-register", new Cori("coaps://jimsch.example.org/rd/endpoints"), body));

            document.Add(new CoralLink("coap://jimsch.example.com/coreapp/reef#rd-endpointSearch", new Cori("coaps://jimsch.example.org/rd/endpoints")));
            document.Add(new CoralLink("coap://jimsch.example.com/coreapp/reef#rd-resourceSearch", new Cori("coaps://jimsch.example.org/rd/resources")));

            CBORObject result = document.EncodeToCBORObject(new Cori("coap://jimsch.example.org/rd"), _Dictionary);

            Assert.AreEqual("[[2, 5, [4, 2, 5, \"endpoints\"], [[2, 1, 99599], [2, 2, \"OSCORE\"], [2, 3, \"ACE\"], [2, 4, [1, \"ace.example.org\", 5, \"token\"]]]], [2, 6, [4, 2, 5, \"endpoints\"]], [2, 7, [4, 2, 5, \"resources\"]], [1, [0, \"coaps\", 1, \"jimsch.example.org\", 5, \"rd\"]], [2, 5, [4, 2, 5, \"endpoints\"], [[2, 1, 99599], [2, 2, \"OSCORE\"], [2, 3, \"ACE\"], [2, 4, [1, \"ace.example.org\", 5, \"token\"]]]], [2, 6, [4, 2, 5, \"endpoints\"]], [2, 7, [4, 2, 5, \"resources\"]]]", result.ToString());

            CoralDocument document2 = CoralDocument.DecodeFromBytes(document.EncodeToBytes(new Cori("coap://jimsch.example.org/rd"), _Dictionary), new Cori("coap://jimsch.example.org/rd"), _Dictionary);

            //Assert.AreEqual(document, document2);

            CoralUsing dict = new CoralUsing()
            {
                { "reef", "coap://jimsch.example.com/coreapp/reef#" }
            };

            string resultOut =
                "reef:rd-register <./endpoints> [\n  reef:content-type 99599\n  reef:security \"OSCORE\"\n  reef:authority-type \"ACE\"\n  reef:authority <//ace.example.org/token>\n]\nreef:rd-endpointSearch <./endpoints>\nreef:rd-resourceSearch <./resources>\n#base <coaps://jimsch.example.org/rd>\nreef:rd-register <./endpoints> [\n  reef:content-type 99599\n  reef:security \"OSCORE\"\n  reef:authority-type \"ACE\"\n  reef:authority <//ace.example.org/token>\n]\nreef:rd-endpointSearch <./endpoints>\nreef:rd-resourceSearch <./resources>\n";

            Assert.AreEqual(resultOut, document.EncodeToString(new Cori("coap://jimsch.example.org/rd"), dict));

            resultOut = "<coap://jimsch.example.com/coreapp/reef#rd-register> <coap://jimsch.example.org/rd/endpoints> [\n  <coap://jimsch.example.com/coreapp/reef#content-type> 99599\n  <coap://jimsch.example.com/coreapp/reef#security> \"OSCORE\"\n  <coap://jimsch.example.com/coreapp/reef#authority-type> \"ACE\"\n  <coap://jimsch.example.com/coreapp/reef#authority> <coap://ace.example.org/token>\n]\n<coap://jimsch.example.com/coreapp/reef#rd-endpointSearch> <coap://jimsch.example.org/rd/endpoints>\n<coap://jimsch.example.com/coreapp/reef#rd-resourceSearch> <coap://jimsch.example.org/rd/resources>\n#base <coaps://jimsch.example.org/rd>\n<coap://jimsch.example.com/coreapp/reef#rd-register> <./endpoints> [\n  <coap://jimsch.example.com/coreapp/reef#content-type> 99599\n  <coap://jimsch.example.com/coreapp/reef#security> \"OSCORE\"\n  <coap://jimsch.example.com/coreapp/reef#authority-type> \"ACE\"\n  <coap://jimsch.example.com/coreapp/reef#authority> <//ace.example.org/token>\n]\n<coap://jimsch.example.com/coreapp/reef#rd-endpointSearch> <./endpoints>\n<coap://jimsch.example.com/coreapp/reef#rd-resourceSearch> <./resources>\n";
            Assert.AreEqual(resultOut, document.ToString());
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
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);
        }