Ejemplo n.º 1
0
        public void Constructors()
        {
            var a = new Counter64();

            Assert.Equal(SnmpConstants.SmiCounter64, a.Type);
            Assert.Equal(0U, a.Value);

            var b = new Counter64(10);

            Assert.Equal(10U, b.Value);

            var c = new Counter64(10);

            Assert.Equal(10U, c.Value);
            b.Value = 12;
            Assert.Equal(10U, c.Value);

            var d = new Counter64("15");

            Assert.Equal(15U, d.Value);

            var e = (Counter64)d.Clone();

            d.Value = 20;
            Assert.Equal(15U, e.Value);
        }
Ejemplo n.º 2
0
        public void TestEqual()
        {
            var left  = new Counter64(673737665);
            var right = new Counter64(673737665);

            Assert.AreEqual(left, right);
        }
Ejemplo n.º 3
0
        private static long RunBenchmark(Action<int, int> action, int threads, int time)
        {
            Counter64 cnt = new Counter64();
            Task[] workers = new Task[threads];
            Stopwatch sw = Stopwatch.StartNew();
            ManualResetEventSlim e = new ManualResetEventSlim();
            long stop_time = 0;

            Action body = () =>
            {
                int iteration = 0;
                int threadBias = RandomizeBits(Environment.CurrentManagedThreadId);
                e.Wait();
                while (sw.ElapsedMilliseconds < stop_time)
                {
                    const int batch = 10000;
                    for (int i = 0; i < batch; i++)
                    {
                        action(iteration++, threadBias);
                    }
                    cnt.Add(batch);
                }
            };

            for (int i = 0; i < workers.Length; i++)
            {
                workers[i] = Task.Factory.StartNew(body, TaskCreationOptions.LongRunning);
            }

            stop_time = sw.ElapsedMilliseconds + time;
            e.Set();

            Task.WaitAll(workers);
            return cnt.Value;
        }
Ejemplo n.º 4
0
        public void TestParseExample1()
        {
            var i      = new Counter64();
            var result = i.Decode(CounterExample1, 0);

            Assert.Equal(3, result);
            Assert.Equal(3U, i.Value);
        }
Ejemplo n.º 5
0
        private static void Counter64Perf()
        {
            var benchmarkName = "======== Counter64 1M Ops/sec:";

            Counter64         cnt = new Counter64();
            Action <int, int> act = (_, __) => { cnt.Increment(); };

            RunBench(benchmarkName, act);
        }
Ejemplo n.º 6
0
        public void TestEqual()
        {
            var left = new Counter64(673737665);
            var right = new Counter64(673737665);
            Assert.AreEqual(left, right);
// ReSharper disable EqualExpressionComparison
            Assert.IsTrue(left == left);
// ReSharper restore EqualExpressionComparison
            Assert.IsTrue(left != null);
            Assert.IsTrue(null != right);
            Assert.IsTrue(left.Equals(right));
        }
Ejemplo n.º 7
0
        public void TestEqual()
        {
            var left  = new Counter64(673737665);
            var right = new Counter64(673737665);

            Assert.AreEqual(left, right);
// ReSharper disable EqualExpressionComparison
            Assert.IsTrue(left == left);
// ReSharper restore EqualExpressionComparison
            Assert.IsTrue(left != null);
            Assert.IsTrue(null != right);
            Assert.IsTrue(left.Equals(right));
        }
Ejemplo n.º 8
0
        public void Diff()
        {
            var a = new Counter64(15);

            Assert.Equal(15U, a.Value);

            var b = new Counter64(10);

            Assert.Equal(10U, b.Value);

            // TODO : Is this correct?
            Assert.Equal(4U, Counter64.Diff(b, a));

            // TODO : Is this correct?
            Assert.Equal(18446744073709551611U, Counter64.Diff(a, b));
        }
Ejemplo n.º 9
0
        public void Generate()
        {
            var i      = new Counter64(300);
            var buffer = new MutableByte();

            i.Encode(buffer);

            var expected = new byte[]
            {
                SnmpConstants.SmiCounter64, // ASN.1 Type
                0x02,                       // Length
                0x01, 0x2C,                 // 300 in big endian
            };

            Assert.Equal(expected, buffer);
        }
Ejemplo n.º 10
0
        public void TestContructor()
        {
            var counter64 = new Counter64(new byte[] {0x00, 0xC9, 0xAC, 0xC1, 0x87, 0x4B, 0xB1, 0xE1, 0xC9});
            Assert.AreEqual(14532202884452442569, counter64.ToUInt64());
            Assert.AreEqual(14532202884452442569.GetHashCode(), counter64.GetHashCode());
            Assert.AreEqual("14532202884452442569", counter64.ToString());

            Assert.Throws<ArgumentNullException>(() => new Counter64(new Tuple<int, byte[]>(0, new byte[] { 0 }), null));
            Assert.Throws<ArgumentException>(() => new Counter64(new Tuple<int, byte[]>(-1, new[] { (byte)255 }), new MemoryStream()));
            Assert.Throws<ArgumentException>(() => new Counter64(new Tuple<int, byte[]>(10, new byte[]{10}), new MemoryStream()));
            Assert.Throws<ArgumentException>(
                () => new Counter64(new byte[] {0x05, 0xC9, 0xAC, 0xC1, 0x87, 0x4B, 0xB1, 0xE1, 0xC9}));

            var small = new Counter64(new byte[] { 0x00, 0xC9, 0xAC, 0xC1, 0x87 });
            Assert.AreEqual(3383542151, small.ToUInt64());

            Assert.Throws<ArgumentNullException>(() => new Counter64(0).AppendBytesTo(null));
        }
Ejemplo n.º 11
0
        public void TestContructor()
        {
            var counter64 = new Counter64(new byte[] { 0x00, 0xC9, 0xAC, 0xC1, 0x87, 0x4B, 0xB1, 0xE1, 0xC9 });

            Assert.AreEqual(14532202884452442569, counter64.ToUInt64());
            Assert.AreEqual(14532202884452442569.GetHashCode(), counter64.GetHashCode());
            Assert.AreEqual("14532202884452442569", counter64.ToString());

            Assert.Throws <ArgumentNullException>(() => new Counter64(0, null));
            Assert.Throws <ArgumentException>(() => new Counter64(-1, new MemoryStream()));
            Assert.Throws <ArgumentException>(() => new Counter64(10, new MemoryStream()));
            Assert.Throws <ArgumentException>(
                () => new Counter64(new byte[] { 0x05, 0xC9, 0xAC, 0xC1, 0x87, 0x4B, 0xB1, 0xE1, 0xC9 }));

            var small = new Counter64(new byte[] { 0x00, 0xC9, 0xAC, 0xC1, 0x87 });

            Assert.AreEqual(3383542151, small.ToUInt64());

            Assert.Throws <ArgumentNullException>(() => new Counter64(0).AppendBytesTo(null));
        }
Ejemplo n.º 12
0
        /// <summary>Used to create correct variable type object for the specified encoded type</summary>
        /// <param name="asnType">ASN.1 type code</param>
        /// <returns>A new object matching type supplied or null if type was not recognized.</returns>
        public static AsnType GetSyntaxObject(SMIDataTypeCode asnType)
        {
            AsnType obj = null;
            if (asnType == SMIDataTypeCode.Integer)
                obj = new Integer32 ();
            else if (asnType == SMIDataTypeCode.Counter32)
                obj = new Counter32 ();
            else if (asnType == SMIDataTypeCode.Gauge32)
                obj = new Gauge32 ();
            else if (asnType == SMIDataTypeCode.Counter64)
                obj = new Counter64 ();
            else if (asnType == SMIDataTypeCode.TimeTicks)
                obj = new TimeTicks ();
            else if (asnType == SMIDataTypeCode.OctetString)
                obj = new OctetString ();
            else if (asnType == SMIDataTypeCode.Opaque)
                obj = new Opaque ();
            else if (asnType == SMIDataTypeCode.IPAddress)
                obj = new IpAddress ();
            else if (asnType == SMIDataTypeCode.ObjectId)
                obj = new Oid ();
            else if (asnType == SMIDataTypeCode.PartyClock)
                obj = new V2PartyClock ();
            else if (asnType == SMIDataTypeCode.NoSuchInstance)
                obj = new NoSuchInstance ();
            else if (asnType == SMIDataTypeCode.NoSuchObject)
                obj = new NoSuchObject ();
            else if (asnType == SMIDataTypeCode.EndOfMibView)
                obj = new EndOfMibView ();
            else if (asnType == SMIDataTypeCode.Null)
            {
                obj = new Null ();
            }

            return obj;
        }
Ejemplo n.º 13
0
 public void TestEqual()
 {
     var left = new Counter64(673737665);
     var right = new Counter64(673737665);
     Assert.AreEqual(left, right);
 }
Ejemplo n.º 14
0
        /// <summary>
        /// Return SNMP type object of the type specified by name. Supported variable types are:
        /// * <see cref="Integer32"/>
        /// * <see cref="Counter32"/>
        /// * <see cref="Gauge32"/>
        /// * <see cref="Counter64"/>
        /// * <see cref="TimeTicks"/>
        /// * <see cref="OctetString"/>
        /// * <see cref="IpAddress"/>
        /// * <see cref="Oid"/>
        /// * <see cref="Null"/>
        /// </summary>
        /// <param name="name">Name of the object type</param>
        /// <returns>New <see cref="AsnType"/> object.</returns>
        public static AsnType GetSyntaxObject(string name)
        {
            AsnType obj = null;
            if (name == "Integer32")
                obj = new Integer32();
            else if (name == "Counter32")
                obj = new Counter32();
            else if (name == "Gauge32")
                obj = new Gauge32();
            else if (name == "Counter64")
                obj = new Counter64();
            else if (name == "TimeTicks")
                obj = new TimeTicks();
            else if (name == "OctetString")
                obj = new OctetString();
            else if (name == "IpAddress")
                obj = new IpAddress();
            else if (name == "Oid")
                obj = new Oid();
            else if (name == "Null")
                obj = new Null();
            else
                throw new ArgumentException("Invalid value type name");

            return obj;
        }
Ejemplo n.º 15
0
        /// <summary>Used to create correct variable type object for the specified encoded type</summary>
        /// <param name="asnType">ASN.1 type code</param>
        /// <returns>A new object matching type supplied or null if type was not recognized.</returns>
        public static AsnType GetSyntaxObject(byte asnType)
        {
            AsnType obj = null;
            if (asnType == SnmpConstants.SMI_INTEGER)
                obj = new Integer32();
            else if (asnType == SnmpConstants.SMI_COUNTER32)
                obj = new Counter32();
            else if (asnType == SnmpConstants.SMI_GAUGE32)
                obj = new Gauge32();
            else if (asnType == SnmpConstants.SMI_COUNTER64)
                obj = new Counter64();
            else if (asnType == SnmpConstants.SMI_TIMETICKS)
                obj = new TimeTicks();
            else if (asnType == SnmpConstants.SMI_STRING)
                obj = new OctetString();
            else if (asnType == SnmpConstants.SMI_OPAQUE)
                obj = new Opaque();
            else if (asnType == SnmpConstants.SMI_IPADDRESS)
                obj = new IpAddress();
            else if (asnType == SnmpConstants.SMI_OBJECTID)
                obj = new Oid();
            else if (asnType == SnmpConstants.SMI_PARTY_CLOCK)
                obj = new V2PartyClock();
            else if (asnType == SnmpConstants.SMI_NOSUCHINSTANCE)
                obj = new NoSuchInstance();
            else if (asnType == SnmpConstants.SMI_NOSUCHOBJECT)
                obj = new NoSuchObject();
            else if (asnType == SnmpConstants.SMI_ENDOFMIBVIEW)
                obj = new EndOfMibView();
            else if (asnType == SnmpConstants.SMI_NULL)
            {
                obj = new Null();
            }

            return obj;
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Return SNMP type object of the type specified by name. Supported variable types are:
        /// <see cref="Integer32"/>, <see cref="Counter32"/>, <see cref="Gauge32"/>, <see cref="Counter64"/>,
        /// <see cref="TimeTicks"/>, <see cref="OctetString"/>, <see cref="IpAddress"/>, <see cref="Oid"/>, and
        /// <see cref="Null"/>.
        /// 
        /// Type names are the same as support class names compared without case sensitivity (e.g. Integer == INTEGER).
        /// </summary>
        /// <param name="name">Name of the object type (not case sensitive)</param>
        /// <returns>New <see cref="AsnType"/> object.</returns>
        public static AsnType GetSyntaxObject(string name)
        {
            AsnType obj = null;
            if (name.ToUpper ().Equals ("INTEGER32") || name.ToUpper ().Equals ("INTEGER"))
                obj = new Integer32 ();
            else if (name.ToUpper ().Equals ("COUNTER32"))
                obj = new Counter32 ();
            else if (name.ToUpper ().Equals ("GAUGE32"))
                obj = new Gauge32 ();
            else if (name.ToUpper ().Equals ("COUNTER64"))
                obj = new Counter64 ();
            else if (name.ToUpper ().Equals ("TIMETICKS"))
                obj = new TimeTicks ();
            else if (name.ToUpper ().Equals ("OCTETSTRING"))
                obj = new OctetString ();
            else if (name.ToUpper ().Equals ("IPADDRESS"))
                obj = new IpAddress ();
            else if (name.ToUpper ().Equals ("OID"))
                obj = new Oid ();
            else if (name.ToUpper ().Equals ("NULL"))
                obj = new Null ();
            else
                throw new ArgumentException ("Invalid value type name");

            return obj;
        }
Ejemplo n.º 17
0
        public void ConvertToString()
        {
            var i1 = new Counter64(10);

            Assert.Equal("10", i1.ToString());
        }
Ejemplo n.º 18
0
 public Gauge64(Counter64 src)
 {
     today              = src.GetToday();
     partialstart       = src.GetPartialStart();
     _time_hack_enabled = false;
 }