Beispiel #1
0
        public void TestFingerprintConstruction1()
        {
            var fingerprint = Fingerprint.Parse("01 28 12345612 800000123456789A 800000ABCDEF0123");

            Assert.AreEqual(0x12345612U, fingerprint.Timecode);
            Assert.AreEqual("12:34:56:12", fingerprint.ToString());
            Assert.AreEqual(40, fingerprint.AudioSize);
            Assert.AreEqual(Analyser.CHANNELS, fingerprint.AudioFingerprints.Length);
            Assert.AreEqual(0x800000123456789AUL, fingerprint.AudioFingerprints[0]);
            Assert.AreEqual(0x800000ABCDEF0123UL, fingerprint.AudioFingerprints[1]);
            Assert.IsTrue(fingerprint.AudioFingerprints.Skip(2).All(f => f == 0));
        }
Beispiel #2
0
        public void TestEmptyMatch()
        {
            bool presentA, presentB;
            var  fingerprints = audioFingerprints.Select(a => Fingerprint.Parse($"01 28 12345612 800000{a}")).ToList();
            var  empty        = audioFingerprints.Select(a => Fingerprint.Parse($"01 28 12345612 0000000000000000")).ToList();

            var corr1 = AudioCorrelator.Correlate(0, empty.Skip(5), 15, fingerprints, 25, out presentA, out presentB);

            CollectionAssert.AreEqual(null, corr1);
            Assert.IsFalse(presentA);
            Assert.IsTrue(presentB);

            var corr2 = AudioCorrelator.Correlate(0, fingerprints.Skip(5), 15, empty, 25, out presentA, out presentB);

            CollectionAssert.AreEqual(null, corr2);
            Assert.IsTrue(presentA);
            Assert.IsFalse(presentB);
        }
Beispiel #3
0
        internal static IEnumerable <Attribute> ParseAttributes(
            IEnumerable <byte> bytes,
            byte[] transactionId = null
            )
        {
            while (bytes.Any())
            {
                var    type    = (Attribute.AttributeType)bytes.Take(2).ToUShort();
                ushort length  = bytes.Skip(2).Take(2).ToUShort();
                byte[] payload = bytes.Skip(4).Take(length).ToArray();

                switch (type)
                {
                case Attribute.AttributeType.ErrorCode:
                    yield return(ErrorCode.Parse(payload));

                    break;

                case Attribute.AttributeType.Realm:
                    yield return(Realm.Parse(payload));

                    break;

                case Attribute.AttributeType.Nonce:
                    yield return(Stun.Attributes.Nonce.Parse(payload));

                    break;

                case Attribute.AttributeType.Software:
                    yield return(Software.Parse(payload));

                    break;

                case Attribute.AttributeType.Fingerprint:
                    yield return(Fingerprint.Parse(payload));

                    break;

                case Attribute.AttributeType.XorMappedAddress:
                    yield return(XorMappedAddress.Parse(
                                     payload, transactionId));

                    break;

                case Attribute.AttributeType.XorRelayedAddress:
                    yield return(XorRelayedAddress.Parse(
                                     payload, transactionId));

                    break;

                case Attribute.AttributeType.ConnectionId:
                    yield return(new ConnectionId(payload));

                    break;

                case Attribute.AttributeType.Lifetime:
                    yield return(new Lifetime((int)payload.ToUInt()));

                    break;
                }

                // Detect padding
                var padBytes = (ushort)((4 + length) % 4);
                if (padBytes > 0)
                {
                    length += padBytes;
                }

                bytes = bytes.Skip(4 + length);
            }
        }
Beispiel #4
0
        public void TestExactMatch()
        {
            var expected = new[]
            { -52, -46, -47, -46, -65, -62, -74, -66, -64, -63,
              -57, -55, -62, -65, -70, -60, -68, -64, -63, -58,
              -59, -54, -74, -45, -58, -63, -63, -44, -75, -72,
              -62, -56, -61, -53, -78, -67, -62, -67, -57, -59,
              -74, -66, -65, -58, -101, -72, -66, -56, -62, -77,
              -71, -69, -52, -63, -84, -64, -58, -58, -59, -66,
              -83, -86, -86, -73, -76, -75, -79, -74, -73, -62,
              -86, -70, -63, -67, -56, -73, -60, -79, -81, -49,
              -87, -63, -68, -51, -64, -47, -81, -71, -63, -72,
              -70, -74, -53, -66, -75, -67, -97, -71, -58, -73,
              -76, -65, -81, -82, -83, -84, -84, -77, -68, -63,
              -67, -55, -96, -82, -63, -60, -67, -58, -56, -62,
              -55, -55, -64, -69, -70, -71, -41, -67, -63, -60,
              -56, -66, -71, -94, -79, -65, -67, -59, -48, -91,
              -68, -77, -57, -80, -61, -82, -76, -61, -64, -71,
              -65, -57, -74, -62, -45, -46, -49, -50, -60, -74,
              -40, -74, -51, -62, -61, -70, -72, -42, -54, -53,
              -85, -55, -88, -47, -48, -78, -66, -78, -71, -86,
              -59, -78, -74, -73, -38, -85, -79, -66, -89, -72,
              -68, -76, -65, -51, -58, -77, -86, -83, -73, -67,
              256, -42, -51, -64, -63, -48, -34, -24, -40, -47,            // Correlation spike at start of line
              -45, -47, -70, -45, -58, -58, -10, -52, -53, -60,
              -39, -68, -48, -53, -46, -57, -29, -30, -71, -36,
              -64, -36, -27, -17, -48, -51, -42, -39, -37, -57,
              -39, -63, -52, -49, -44, -41, -53, -69, -65, -54,
              -60, -70, -63, -60, -79, -87, -55, -75, -52, -67,
              -62, -85, -55, -56, -57, -66, -70, -81, -66, -61,
              -55, -59, -58, -66, -37, -54, -63, -66, -70, -48,
              -62, -60, -47, -62, -65, -54, -56, -86, -108, -57,
              -57, -63, -70, -71, -82, -64, -90, -72, -77, -70,
              -75, -64, -58, -63, -88, -63, -75, -68, -59, -68,
              -68, -76, -65, -77, -78, -53, -70, -59, -81, -59,
              -80, -56, -77, -88, -57, -64, -46, -58, -66, -73,
              -75, -67, -80, -69, -64, -52, -62, -68, -79, -82,
              -77, -56, -46, -49, -48, -61, -73, -60, -47, -66,
              -62, -60, -61, -49, -52, -65, -88, -49, -67, -61,
              -67, -47, -62, -71, -68, -73, -77, -49, -69, -64,
              -56, -72, -89, -56, -71, -67, -65, -41, -74, -67,
              -74, -63, -79, -56, -51, -50, -68, -69, -76, -55,
              -71, -67, -66, -64, -71, -76, -63, -64, -48, -74 };

            bool presentA, presentB;
            // Fingerprints for second channel have data (A5A5A5) in the unused bits of the audio fingerprint
            var fingerprints =
                audioFingerprints.Select(a => Fingerprint.Parse($"01 28 12345612 800000{a} A5A5A5{a}")).ToList();

            var corr1 = AudioCorrelator.Correlate(0, fingerprints.Skip(5), 15, fingerprints, 25, out presentA, out presentB);

            CollectionAssert.AreEqual(expected, corr1);
            Assert.IsTrue(presentA);
            Assert.IsTrue(presentB);

            // Check that unused fingerprint bits are ignored
            var corr2 = AudioCorrelator.Correlate(1, fingerprints.Skip(5), 15, fingerprints, 25, out presentA, out presentB);

            CollectionAssert.AreEqual(expected, corr2);
            Assert.IsTrue(presentA);
            Assert.IsTrue(presentB);
        }