示例#1
0
        public void NdefMultipleWithNoise()
        {
            // Arrange
            // Extract the data from the Mifare card dump
            var extract = ExtractAllBlocksFromCardDump(CardDumpExamples.Memory1KDump2RecordsWithNoise);
            // Get the NDEF section from the card dump
            var ndef = ExtractAllMessage(extract);
            // Act
            // Get the NDEF Message
            NdefMessage message = new NdefMessage(ndef);
            var         geo     = new GeoRecord(message.Records.First());
            // Create a new Geo Record with same data
            var geoNew = new GeoRecord(48.853231, 2.349207);

            // Set same header as for the previous one with the first message
            geoNew.Header.MessageFlag |= MessageFlag.MessageBegin;
            Span <byte> geoNewSerialized = new byte[geoNew.Length];
            Span <byte> geoSerialized    = new byte[geo.Length];

            geoNew.Serialize(geoNewSerialized);
            message.Records.First().Serialize(geoSerialized);

            // Assert
            // Check if all is ok
            Assert.Equal(2, message.Records.Count);
            Assert.True(message.Records.First().Header.IsFirstMessage);
            Assert.True(message.Records.Last().Header.IsLastMessage);
            Assert.True(GeoRecord.IsGeoRecord(message.Records.First()));
            Assert.Equal(48.853231, geo.Latitude);
            Assert.Equal(2.349207, geo.Longitude);
            Assert.Equal(geoSerialized.ToArray(), geoNewSerialized.ToArray());

            // Act
            // Create a new MEdia record which is the same
            var mediaNew = new MediaRecord("image/jpeg", Encoding.UTF8.GetBytes("Test but not an image"));

            mediaNew.Header.MessageFlag |= MessageFlag.MessageEnd;
            Span <byte> mediaNewSerialized = new byte[mediaNew.Length];
            Span <byte> mediaSerialized    = new byte[message.Records.Last().Length];

            mediaNew.Serialize(mediaNewSerialized);
            message.Records.Last().Serialize(mediaSerialized);
            // Assert
            Assert.Equal(mediaSerialized.ToArray(), mediaNewSerialized.ToArray());

            // Arrange
            // Now create a new message and add the 2 records. We will before remove the
            // message begin and begin end flags to make sure they're properly create by
            // The serialize function
            geoNew.Header.MessageFlag   &= ~MessageFlag.MessageBegin;
            mediaNew.Header.MessageFlag &= ~MessageFlag.MessageEnd;
            var messageNew = new NdefMessage();

            messageNew.Records.Add(geoNew);
            messageNew.Records.Add(mediaNew);
            Span <byte> messageNewSerialiazed = new byte[messageNew.Length];

            // Act
            messageNew.Serialize(messageNewSerialiazed);
            // Assert
            Assert.Equal(ndef, messageNewSerialiazed.ToArray());

            // Act
            // Check that the serialized buffer is the same at the raw record
            Span <byte> serializedMessage = new byte[message.Length];

            message.Serialize(serializedMessage);
            // Assert
            Assert.Equal(ndef, serializedMessage.ToArray());
        }