Example #1
0
        public void Asn1BigVarBindEncodingTest()
        {
            var varBinds = new VarBind[] {
                new VarBind(new ObjectIdentifier("1.3.6.1.2.1.1.3.0")),
                new VarBind(new ObjectIdentifier("1.3.6.1.2.1.1.3.1"), 1, new Asn1TagInfo(Asn1Tag.Integer)),
                new VarBind(new ObjectIdentifier("1.3.6.1.2.1.1.3.1"), 13, new Asn1TagInfo(Asn1Tag.Integer)),
                new VarBind(new ObjectIdentifier("1.3.6.1.2.1.1.3.1"), 143, new Asn1TagInfo(Asn1Tag.Integer)),
                new VarBind(new ObjectIdentifier("1.3.6.1.2.1.1.3.1"), 122, new Asn1TagInfo(Asn1Tag.Integer)),
                new VarBind(new ObjectIdentifier("1.3.6.1.2.1.1.3.1"), 13456, new Asn1TagInfo(Asn1Tag.Integer)),
                new VarBind(new ObjectIdentifier("1.3.6.1.2.1.1.3.1"), 1334, new Asn1TagInfo(Asn1Tag.Integer)),
                new VarBind(new ObjectIdentifier("1.3.6.1.2.1.1.3.1"), 1444, new Asn1TagInfo(Asn1Tag.Integer)),
                new VarBind(new ObjectIdentifier("1.3.6.1.2.1.1.3.1"), 13, new Asn1TagInfo(Asn1Tag.Integer)),
                new VarBind(new ObjectIdentifier("1.3.6.1.2.1.1.3.1"), 10, new Asn1TagInfo(Asn1Tag.Integer)),
            };

            var packet = new SnmpDatagram(
                PduType.SNMPv2Trap,
                SnmpVersion.V2C,
                "Community",
                509000,
                SnmpErrorStatus.TooBig,
                10000,
                varBinds);

            var encoded  = packet.ToSnmpEncodedByteArray();
            var snmpPack = encoded.ToSnmpDatagram();

            Assert.IsNull(snmpPack.PduV2c.VarBinds[0].Value);
            Assert.AreEqual(1, (long)snmpPack.PduV2c.VarBinds[1].Value);
        }
Example #2
0
        public void TestInitialize()
        {
            var integerVarBind = new VarBind(
                new ObjectIdentifier("1.3.6.1.4.1.1.1.1"),
                5L,
                new Asn1TagInfo(Asn1Tag.Integer, ConstructType.Primitive, Asn1Class.Universal));

            var sysUpTime = new VarBind(new ObjectIdentifier("1.3.6.1.2.1.1.3.0"),
                                        506009u,
                                        new Asn1TagInfo(Asn1SnmpTag.TimeTicks));

            var trapVb = new VarBind(new ObjectIdentifier("1.3.6.1.6.3.1.1.4.1.0"),
                                     new ObjectIdentifier("1.3.6.1.4.1.500.12"),
                                     new Asn1TagInfo(Asn1Tag.ObjectIdentifier, ConstructType.Primitive, Asn1Class.Universal));

            var extraneousVb = new VarBind(new ObjectIdentifier("1.3.6.1.6.3.1.1.42.42.42.0"),
                                           8938ul,
                                           new Asn1TagInfo(Asn1SnmpTag.Counter64));

            var packet = new SnmpDatagram(
                PduType.SNMPv2Trap,
                SnmpVersion.V2C,
                "Community",
                50000,
                SnmpErrorStatus.NoError,
                0,
                new[] { sysUpTime, trapVb, integerVarBind, extraneousVb });

            var encoded = packet.ToSnmpEncodedByteArray();

            var ipPacket = new IpPacket(
                NetworkInterfaceComponent.IPv4,
                Byte.MaxValue,
                Byte.MaxValue,
                Byte.MaxValue,
                UInt16.MaxValue,
                UInt16.MaxValue,
                Byte.MaxValue,
                UInt16.MaxValue,
                Byte.MaxValue,
                ProtocolType.Udp,
                IPAddress.Parse("1.1.1.1"),
                IPAddress.Parse("2.2.2.2"),
                new byte[0],
                new byte[0]);

            this.fakeTrapUdp = new UdpDatagram(ipPacket, 10, 10, (ushort)(encoded.Length + 8), encoded);
        }
Example #3
0
        public ObjectIdentifier GetInputKey(IEnvelope envelope)
        {
            this.snmpDatagram = envelope.PayloadInstance as SnmpDatagram;

            if (this.snmpDatagram == null && string.Equals(envelope.Protocol, Protocol.SnmpTrap, StringComparison.OrdinalIgnoreCase))
            {
                this.snmpDatagram = envelope.Payload.AsByteArraySegment()
                                    .ToSnmpDatagram(envelope.ReceivedTime, "0.0.0.0");
            }

            if (this.snmpDatagram == null)
            {
                return(default(ObjectIdentifier));
            }

            return(this.trapTypeMap.GetInputKey(this.snmpDatagram));
        }
Example #4
0
        public void SnmpEncodeDecodedDataWithoutError()
        {
            try
            {
                foreach (var device in testDeviceResponseCollection)
                {
                    SnmpDatagram snmpPacket  = device.ToSnmpDatagram();
                    byte[]       encodedData = snmpPacket.ToSnmpEncodedByteArray();

                    //Check if encoding is correct by decoding that back
                    encodedData.ToSnmpDatagram();
                }
            }
            catch (Exception ex)
            {
                Assert.Fail("Encoding/Decoding Failed " + ex);
            }
        }
Example #5
0
        public void Asn1NullEncodingTest()
        {
            var sysUpTime = new VarBind(new ObjectIdentifier("1.3.6.1.2.1.1.3.0"));

            var packet = new SnmpDatagram(
                PduType.SNMPv2Trap,
                SnmpVersion.V2C,
                "Community",
                50000,
                SnmpErrorStatus.NoError,
                0,
                new[] { sysUpTime, });

            var encoded  = packet.ToSnmpEncodedByteArray();
            var snmpPack = encoded.ToSnmpDatagram();

            Assert.IsNull(snmpPack.PduV2c.VarBinds[0].Value);
        }
Example #6
0
        public void Asn1Counter64EncodingTest()
        {
            var sysUpTime = new VarBind(new ObjectIdentifier("1.3.6.1.2.1.1.3.0"),
                                        (ulong)50600900, new Asn1TagInfo(Asn1SnmpTag.Counter64));

            var packet = new SnmpDatagram(
                PduType.SNMPv2Trap,
                SnmpVersion.V2C,
                "Community",
                50000,
                SnmpErrorStatus.NoError,
                0,
                new[] { sysUpTime, });

            var encoded  = packet.ToSnmpEncodedByteArray();
            var snmpPack = encoded.ToSnmpDatagram();

            Assert.AreEqual((ulong)50600900, (ulong)snmpPack.PduV2c.VarBinds[0].Value);
        }
Example #7
0
        public void Asn1StringEncodingTest()
        {
            var sysUpTime = new VarBind(new ObjectIdentifier("1.3.6.1.2.1.1.3.0"),
                                        "TestString", new Asn1TagInfo(Asn1Tag.OctetString));

            var packet = new SnmpDatagram(
                PduType.SNMPv2Trap,
                SnmpVersion.V2C,
                "Community",
                50000,
                SnmpErrorStatus.NoError,
                0,
                new[] { sysUpTime, });

            var encoded  = packet.ToSnmpEncodedByteArray();
            var snmpPack = encoded.ToSnmpDatagram();

            Assert.AreEqual("TestString", snmpPack.PduV2c.VarBinds[0].Value.ToString());
        }
Example #8
0
        public void TestTimeTicksTest()
        {
            var sysUpTime = new VarBind(new ObjectIdentifier("1.3.6.1.2.1.1.3.0"),
                                        506009u, new Asn1TagInfo(Asn1SnmpTag.TimeTicks));

            var packet = new SnmpDatagram(
                PduType.SNMPv2Trap,
                SnmpVersion.V2C,
                "Community",
                50000,
                SnmpErrorStatus.NoError,
                0,
                new[] { sysUpTime, });

            var encoded  = packet.ToSnmpEncodedByteArray();
            var snmpPack = encoded.ToSnmpDatagram();

            Assert.AreEqual(506009u, (uint)snmpPack.PDU.VarBinds[0].Value);
        }
Example #9
0
        public void Asn1IpAdressEncodingTest()
        {
            var sysUpTime = new VarBind(new ObjectIdentifier("1.3.6.1.2.1.1.3.0"),
                                        new System.Net.IPAddress(123453), new Asn1TagInfo(Asn1SnmpTag.IpAddress));

            var packet = new SnmpDatagram(
                PduType.SNMPv2Trap,
                SnmpVersion.V2C,
                "Community",
                50000,
                SnmpErrorStatus.NoError,
                0,
                new[] { sysUpTime, });

            var encoded  = packet.ToSnmpEncodedByteArray();
            var snmpPack = encoded.ToSnmpDatagram();

            Assert.AreEqual("61.226.1.0", snmpPack.PDU.VarBinds[0].Value.ToString());
        }
Example #10
0
        public void Asn1NegativeIntegerEncodingTest()
        {
            var sysUpTime = new VarBind(new ObjectIdentifier("1.3.6.1.2.1.1.3.0"),
                                        -506009979999L, new Asn1TagInfo(Asn1Tag.Integer));

            var packet = new SnmpDatagram(
                PduType.SNMPv2Trap,
                SnmpVersion.V2C,
                "Community",
                50000,
                SnmpErrorStatus.NoError,
                0,
                new[] { sysUpTime, });

            var encoded  = packet.ToSnmpEncodedByteArray();
            var snmpPack = encoded.ToSnmpDatagram();

            Assert.AreEqual(-506009979999L, (long)snmpPack.PDU.VarBinds[0].Value);
        }
Example #11
0
        public void Asn1NullAndNotNullEncodingTest()
        {
            var oidWithNull    = new VarBind(new ObjectIdentifier("1.3.6.1.2.1.1.3.0"));
            var oidWithoutNull = new VarBind(new ObjectIdentifier("1.3.6.1.2.1.1.3.1"),
                                             1, new Asn1TagInfo(Asn1Tag.Integer));

            var packet = new SnmpDatagram(
                PduType.SNMPv2Trap,
                SnmpVersion.V2C,
                "Community",
                50000,
                SnmpErrorStatus.NoError,
                0,
                new[] { oidWithNull, oidWithoutNull });

            var encoded  = packet.ToSnmpEncodedByteArray();
            var snmpPack = encoded.ToSnmpDatagram();

            Assert.IsNull(snmpPack.PduV2c.VarBinds[0].Value);
            Assert.AreEqual(1, (long)snmpPack.PduV2c.VarBinds[1].Value);
        }
Example #12
0
        public void Test_OctetStringAsByteArray_2()
        {
            var typeMap = new TrapTypeMap();

            var transform = typeMap.GetTransform(typeof(FakeTrap2));

            Assert.IsNotNull(transform);

            var payload = new byte[] { 0x07, 0xE0, 0x06, 0x0E, 0x0E, 0x1E, 0x0E, 0x00 };

            var octetStringVarBind = new VarBind(
                new ObjectIdentifier("1.3.6.1.4.1.562.29.6.2.2"),
                payload.ReadOctetString(0, payload.Length),
                new Asn1TagInfo(Asn1Tag.OctetString, ConstructType.Primitive, Asn1Class.Universal));

            var sysUpTime = new VarBind(new ObjectIdentifier("1.3.6.1.2.1.1.3.0"),
                                        506009u,
                                        new Asn1TagInfo(Asn1SnmpTag.TimeTicks));

            var trapVb = new VarBind(new ObjectIdentifier("1.3.6.1.6.3.1.1.4.1.0"),
                                     new ObjectIdentifier("1.3.6.1.4.1.500.12"),
                                     new Asn1TagInfo(Asn1Tag.ObjectIdentifier, ConstructType.Primitive, Asn1Class.Universal));

            var packet = new SnmpDatagram(
                DateTimeOffset.MinValue,
                "1.2.3.4",
                new SnmpHeader(SnmpVersion.V2C, "Community"),
                new[] { sysUpTime, trapVb, octetStringVarBind });

            var transformedOutput = transform(packet) as FakeTrap2;

            Assert.IsNotNull(transformedOutput);
            Assert.IsNotNull(transformedOutput.Property);

            Assert.AreEqual(payload.Length, transformedOutput.Property.Length);
            Assert.IsTrue(payload.Zip(transformedOutput.Property, (b, b1) => b == b1).All(i => i));
        }
        public void Test_OctetStringAsByteArray_2()
        {
            var typeMap = new TrapTypeMap();

            var transform = typeMap.GetTransform(typeof(FakeTrap2));

            Assert.IsNotNull(transform);

            var payload = new byte[] { 0x07, 0xE0, 0x06, 0x0E, 0x0E, 0x1E, 0x0E, 0x00 };

            var octetStringVarBind = new VarBind(
                new ObjectIdentifier("1.3.6.1.4.1.562.29.6.2.2"),
                payload.ReadOctetString(0, payload.Length),
                new Asn1TagInfo(Asn1Tag.OctetString, ConstructType.Primitive, Asn1Class.Universal));

            var sysUpTime = new VarBind(new ObjectIdentifier("1.3.6.1.2.1.1.3.0"),
                506009u,
                new Asn1TagInfo(Asn1SnmpTag.TimeTicks));

            var trapVb = new VarBind(new ObjectIdentifier("1.3.6.1.6.3.1.1.4.1.0"),
                new ObjectIdentifier("1.3.6.1.4.1.500.12"),
                new Asn1TagInfo(Asn1Tag.ObjectIdentifier, ConstructType.Primitive, Asn1Class.Universal));

            var packet = new SnmpDatagram(
                DateTimeOffset.MinValue,
                "1.2.3.4",
                new SnmpHeader(SnmpVersion.V2C, "Community"),
                new[] { sysUpTime, trapVb, octetStringVarBind });

            var transformedOutput = transform(packet) as FakeTrap2;

            Assert.IsNotNull(transformedOutput);
            Assert.IsNotNull(transformedOutput.Property);

            Assert.AreEqual(payload.Length, transformedOutput.Property.Length);
            Assert.IsTrue(payload.Zip(transformedOutput.Property, (b, b1) => b == b1).All(i => i));
        }
Example #14
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SnmpDatagram"/> class.
 /// Copy construtor
 /// </summary>
 /// <param name="snmpPacket">The SNMP packet.</param>
 public SnmpDatagram(SnmpDatagram snmpPacket)
 {
     Header = snmpPacket.Header;
     PDU    = snmpPacket.PDU;
 }