示例#1
0
        public void LookupDate()
        {
            CBORObject result = _dictionary.Lookup(CBORObject.FromObjectAndTag(DateTimeOffset.UtcNow.ToUnixTimeSeconds(), 1), true);

            Assert.AreEqual(CBORType.Integer, result.Type);
            Assert.IsTrue(result.IsTagged);
            Assert.IsTrue(result.HasOneTag(1));
        }
示例#2
0
        public void SetClaim(ClaimID claim, DateTime dt)
        {
            double unixTime = (TimeZoneInfo.ConvertTimeToUtc(dt) -
                               new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc)).TotalSeconds;
            CBORObject value = CBORObject.FromObject((long)unixTime);

            value = CBORObject.FromObjectAndTag(value, 1);
            SetClaim(claim, value);
        }
示例#3
0
 public void SetupDictionary()
 {
     _dictionary = new CoralDictionary()
     {
         { 0, "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" },
         { 1, "http://www.iana.org/assignments/relation/item>" },
         { 2, "http://www.iana.org/assignments/relation/collection" },
         { 3, CBORObject.FromObject(93.56) },
         { 4, new Cori("coap://host:99/path1/path2/path3") },
         { 5, CBORObject.FromObjectAndTag(0, 1) },
         { 6, CBORObject.FromObject(new byte[] { 1, 3, 5, 7, 9, 11, 13, 15 }) },
     };
 }
示例#4
0
        // link = [2, relation-type, link-target, ?body]
        // relation-type = text
        // link-target = CoRI / literal
        // CoRI = <Defined in Section X of RFC XXXX>
        // literal = bool / int / float / time / bytes / text / null

        public override CBORObject EncodeToCBORObject(Cori coriBase, CoralDictionary dictionary)
        {
            CBORObject node = CBORObject.NewArray();

            node.Add(2);
            if (RelationType != null)
            {
                node.Add(dictionary.Lookup(RelationType, false));
            }
            else
            {
                node.Add(RelationTypeInt);
            }


            if (Target != null)
            {
                CBORObject o = dictionary.Lookup(Target, true);
                if (o.Type == CBORType.Integer)
                {
                    node.Add(o);
                }
                else
                {
                    if (coriBase != null)
                    {
                        Cori relative = Target.MakeRelative(coriBase);
                        node.Add(relative.Data);
                    }
                    else
                    {
                        node.Add(Target.Data);
                    }
                }
            }
            else if (Value != null)
            {
                node.Add(dictionary.Lookup(Value, true));
            }
            else if (TargetInt != null)
            {
                node.Add(CBORObject.FromObjectAndTag(TargetInt, CoralDictionary.DictionaryTag));
            }

            if (Body != null)
            {
                node.Add(Body.EncodeToCBORObject(coriBase, dictionary));
            }

            return(node);
        }
示例#5
0
        /// <summary>
        /// Generate a new CBOR Object based on the message.
        /// Doing this will force cryptographic operations to be created.
        /// </summary>
        /// <returns></returns>
        public CBORObject EncodeToCBORObject()
        {
            CBORObject obj  = CBORObject.NewArray();
            CBORObject obj3 = Encode();

            for (int i = 0; i < obj3.Count; i++)
            {
                obj.Add(obj3[i]);
            }

            if (m_emitTag)
            {
                return(CBORObject.FromObjectAndTag(obj, (int)m_tag));
            }
            return(obj);
        }
        public CBORObject Lookup(string value, bool isIntLegal)
        {
            foreach (KeyValuePair <int, object> o in _dictionary)
            {
                if (value.Equals(o.Value))
                {
                    if (isIntLegal)
                    {
                        return(CBORObject.FromObjectAndTag(o.Key, DictionaryTag));
                    }

                    return(CBORObject.FromObject(o.Key));
                }
            }

            return(CBORObject.FromObject(value));
        }
示例#7
0
        public CBORObject Lookup(CBORObject value)
        {
            foreach (KeyValuePair <int, string> o in _dictionary)
            {
                if (value.Equals(CBORObject.FromObject(o.Value)))
                {
                    CBORObject newValue = CBORObject.FromObject(o.Key);
                    if (value.Type == CBORType.Number)
                    {
                        newValue = CBORObject.FromObjectAndTag(newValue, DictionaryTag);
                    }

                    return(newValue);
                }
            }

            return(value);
        }
        public CBORObject Lookup(Cori value, bool isIntLegal)
        {
            if (!value.IsAbsolute())
            {
                return(value.Data);
            }

            foreach (KeyValuePair <int, object> o in _dictionary)
            {
                if (value.Equals(o.Value))
                {
                    if (isIntLegal)
                    {
                        return(CBORObject.FromObjectAndTag(o.Key, DictionaryTag));
                    }
                    return(CBORObject.FromObject(o.Key));
                }
            }

            return(value.Data);
        }
        public CBORObject Lookup(CBORObject value, bool isIntLegal)
        {
            if (value.Type == CBORType.TextString)
            {
                return(Lookup(value.AsString(), isIntLegal));
            }

            foreach (KeyValuePair <int, object> o in _dictionary)
            {
                if (value.Equals(o.Value))
                {
                    if (isIntLegal)
                    {
                        return(CBORObject.FromObjectAndTag(o.Key, DictionaryTag));
                    }

                    return(CBORObject.FromObject(o.Key));
                }
            }

            return(value);
        }
示例#10
0
        public static CBORObject RandomCBORTaggedObject(
            IRandomGenExtended rand,
            int depth)
        {
            var tag = 0;

            if (rand.GetInt32(2) == 0)
            {
                int[] tagselection =
                {
                    2,  2, 2,  3,  3,  3, 4, 4, 4, 5, 5, 5, 30, 30,
                    30, 0, 1, 25, 26, 27,
                };
                tag = tagselection[rand.GetInt32(tagselection.Length)];
            }
            else if (rand.GetInt32(100) < 90)
            {
                return(CBORObject.FromObjectAndTag(
                           RandomCBORObject(rand, depth + 1),
                           rand.GetInt32(0x100000)));
            }
            else
            {
                return(CBORObject.FromObjectAndTag(
                           RandomCBORObject(rand, depth + 1),
                           RandomEIntegerMajorType0(rand)));
            }
            if (tag == 25)
            {
                tag = 0;
            }
            if (tag == 30)
            {
                object o = RandomObjects.RandomByteString(rand);
                return(CBORObject.FromObject(o));
            }
            {
                CBORObject cbor;
                // Console.WriteLine("tag "+tag+" "+i);
                if (tag == 0 || tag == 1 || tag == 28 || tag == 29)
                {
                    tag = 999;
                }
                if (tag == 2 || tag == 3)
                {
                    object o = RandomObjects.RandomByteStringShort(rand);
                    cbor = CBORObject.FromObject(o);
                }
                else if (tag == 4 || tag == 5)
                {
                    cbor = CBORObject.NewArray();
                    object o = RandomObjects.RandomSmallIntegral(rand);
                    cbor.Add(o);
                    o = RandomObjects.RandomEInteger(rand);
                    cbor.Add(o);
                }
                else if (tag == 30)
                {
                    cbor = CBORObject.NewArray();
                    object o = RandomObjects.RandomSmallIntegral(rand);
                    cbor.Add(o);
                    o = RandomObjects.RandomEInteger(rand);
                    cbor.Add(o);
                }
                else
                {
                    cbor = RandomCBORObject(rand, depth + 1);
                }
                return(CBORObject.FromObjectAndTag(cbor, tag));
            }
        }
示例#11
0
        public static CBORObject RandomCBORTaggedObject(
            RandomGenerator rand,
            int depth)
        {
            var tag = 0;

            if (rand.UniformInt(2) == 0)
            {
                int[] tagselection = { 2,  2, 2,  3,  3, 3, 4, 4, 4, 5, 5, 5, 30, 30,
                                       30, 0, 1, 25, 26, 27 };
                tag = tagselection[rand.UniformInt(tagselection.Length)];
            }
            else
            {
                tag = rand.UniformInt(0x1000000);
            }
            if (tag == 25)
            {
                tag = 0;
            }
            if (tag == 30)
            {
                return(CBORObject.FromObject(RandomObjects.RandomByteString(rand)));
            }
            for (var i = 0; i < 15; ++i)
            {
                CBORObject o;
                // Console.WriteLine("tag "+tag+" "+i);
                if (tag == 0 || tag == 1 || tag == 28 || tag == 29)
                {
                    tag = 999;
                }
                if (tag == 2 || tag == 3)
                {
                    o = CBORObject.FromObject(RandomObjects.RandomByteStringShort(rand));
                }
                else if (tag == 4 || tag == 5)
                {
                    o = CBORObject.NewArray();
                    o.Add(CBORObject.FromObject(RandomObjects.RandomSmallIntegral(rand)));
                    o.Add(CBORObject.FromObject(RandomObjects.RandomEInteger(rand)));
                }
                else if (tag == 30)
                {
                    o = CBORObject.NewArray();
                    o.Add(CBORObject.FromObject(RandomObjects.RandomSmallIntegral(rand)));
                    o.Add(CBORObject.FromObject(RandomObjects.RandomEInteger(rand)));
                }
                else
                {
                    o = RandomCBORObject(rand, depth + 1);
                }
                try {
                    o = CBORObject.FromObjectAndTag(o, tag);
                    // Console.WriteLine("done");
                    return(o);
                } catch (Exception) {
                    continue;
                }
            }
            // Console.WriteLine("Failed "+tag);
            return(CBORObject.Null);
        }
示例#12
0
        public static CBORObject RandomCBORTaggedObject(
            RandomGenerator rand,
            int depth)
        {
            var tag = 0;

            if (rand.UniformInt(2) == 0)
            {
                int[] tagselection =
                {
                    2,  2, 2,  3,  3,  3, 4, 4, 4, 5, 5, 5, 30, 30,
                    30, 0, 1, 25, 26, 27,
                };
                tag = tagselection[rand.UniformInt(tagselection.Length)];
            }
            else
            {
                tag = rand.UniformInt(0x1000000);
            }
            if (tag == 25)
            {
                tag = 0;
            }
            if (tag == 30)
            {
                object o = RandomObjects.RandomByteString(rand);
                return(ToObjectTest.TestToFromObjectRoundTrip(o));
            }
            {
                CBORObject cbor;
                // Console.WriteLine("tag "+tag+" "+i);
                if (tag == 0 || tag == 1 || tag == 28 || tag == 29)
                {
                    tag = 999;
                }
                if (tag == 2 || tag == 3)
                {
                    object o = RandomObjects.RandomByteStringShort(rand);
                    cbor = ToObjectTest.TestToFromObjectRoundTrip(o);
                }
                else if (tag == 4 || tag == 5)
                {
                    cbor = CBORObject.NewArray();
                    object o = RandomObjects.RandomSmallIntegral(rand);
                    cbor.Add(ToObjectTest.TestToFromObjectRoundTrip(o));
                    o = RandomObjects.RandomEInteger(rand);
                    cbor.Add(ToObjectTest.TestToFromObjectRoundTrip(o));
                }
                else if (tag == 30)
                {
                    cbor = CBORObject.NewArray();
                    object o = RandomObjects.RandomSmallIntegral(rand);
                    cbor.Add(ToObjectTest.TestToFromObjectRoundTrip(o));
                    o = RandomObjects.RandomEInteger(rand);
                    cbor.Add(ToObjectTest.TestToFromObjectRoundTrip(o));
                }
                else
                {
                    cbor = RandomCBORObject(rand, depth + 1);
                }
                try {
                    cbor = CBORObject.FromObjectAndTag(cbor, tag);
                    // Console.WriteLine("done");
                    return(cbor);
                } catch (Exception) {
                    return(CBORObject.FromObjectAndTag(cbor, 999));
                }
            }
        }