Esempio n. 1
0
        /// <summary>
        /// The unit tests for the SmartCardXMLSerializer class.
        /// </summary>
        public static void Test()
        {
            try
            {
                SmartCardXMLSerializer serial;
                SmartCard card, newCard;
                Cylinder  cylinder;
                string    cardXML, properXML;

                serial   = new SmartCardXMLSerializer();
                card     = new SmartCard();
                cylinder = new Cylinder("1002-3440", "AIRLE");

                // Fill out the cylinder properties.
                cylinder.FactoryId      = "18-6789-23";
                cylinder.ExpirationDate = DateTime.Parse("10/23/1969");

                // Add the cylinder and its serializer
                card.Add(cylinder, new CylinderXMLSerializer());

                // Set the card's properties.
                card.PartNumber  = "1002-3440";
                card.ProgramDate = DateTime.Parse("10/23/1969");
                // Console.WriteLine( "@ Set properties on the SmartCard." );

                cardXML = serial.Serialize(card);
                // Console.WriteLine( "@ Serialized the SmartCard." );
                // Console.WriteLine( "@ SmartCard XML: '" + cardXML + "'." );

                properXML = "<smartCard partNumber=\"1002-3440\" programDate=\"1969-10-23 0:0:0\"><item><cylinder partNumber=\"1002-3440\" expirationDate=\"1969-10-23 0:0:0\" factoryID=\"18-6789-23\" /></item></smartCard>";
                Debug.Assert(cardXML.CompareTo(properXML) == 0,
                             "SmartCard XML Serialization failed.");
                // Console.WriteLine( "@ XML Comparison succeeded." );

                newCard = ( SmartCard )serial.Deserialize(cardXML);
                // Console.WriteLine( "@ Built the SmartCard." );

                Debug.Assert(card.PartNumber.CompareTo(newCard.PartNumber) == 0,
                             "SmartCard XML deserializing did not extract PartNumber properly.");
                Debug.Assert(card.ProgramDate.CompareTo(newCard.ProgramDate) == 0,
                             "SmartCard XML deserializing did not extract ProgramDate properly.");

                Cylinder newcylinder = (Cylinder )newCard.GetContent(0);
                Debug.Assert(cylinder.FactoryId.CompareTo(newcylinder.FactoryId) == 0,
                             "Cylinder XML building did not extract FactoryID properly.");
                Debug.Assert(cylinder.PartNumber.CompareTo(newcylinder.PartNumber) == 0,
                             "Cylinder XML building did not extract PartNumber properly.");
                Debug.Assert(cylinder.ExpirationDate.CompareTo(newcylinder.ExpirationDate) == 0,
                             "Cylinder XML building did not extract ExpirationDate properly.");
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception in SmartCardSerializerXMLTester.Test(): {0}",
                                  e.ToString());
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Deserializes the supplied SmartCard from the _readerWriter.
        /// </summary>
        /// <param name="numberOfBytes">
        /// The number of bytes to return to the calling application.
        /// Specify 0 to return the maximum number of bytes stored on the device.
        /// </param>
        /// <returns>The SmartCard read from the device.</returns>
        /// <exception cref="DeviceNotConnectedException">
        ///		When the device is not connected.
        /// </exception>
        /// <exception cref="CardNotPresentException">
        ///		When the smart card is not inserted.
        /// </exception>
        /// <exception cref="CardReadException">
        ///		When there is a failure to read the data.
        /// </exception>
        public SmartCard Read(int numberOfBytes)
        {
            byte[]      theData;
            ISerializer cardSerial;
            SmartCard   card;

            theData = _readerWriter.Read(numberOfBytes);

            // Deserialize the data.
            cardSerial = new SmartCardXMLSerializer();

            card = ( SmartCard )cardSerial.Deserialize(Encoding.ASCII.GetString(theData, 0, theData.Length));

            return(card);
        }
Esempio n. 3
0
        /// <summary>
        /// Persists the serialized SmartCard to the _readerWriter;
        /// </summary>
        /// <param name="card">
        /// The SmartCard to persist to the device.
        /// </param>
        /// <exception cref="DeviceNotConnectedException">
        ///		When the device is not connected.
        /// </exception>
        /// <exception cref="CardNotPresentException">
        ///		When the smart card is not inserted.
        /// </exception>
        /// <exception cref="CardWriteException">
        ///		When there is a failure to write the data.
        /// </exception>
        public void Write(SmartCard card)
        {
            ISerializer cardSerial;
            string      cardXML;

            byte[] theData;

            cardSerial = new SmartCardXMLSerializer();
            cardXML    = cardSerial.Serialize(card);

            // Transform the data into a byte array.
            theData = new byte[cardXML.Length];

            for (int n = 0; n < theData.Length; n++)
            {
                // Or just extract the low bits.
                theData[n] = Convert.ToByte(cardXML[n]);
            }

            _readerWriter.Write(theData);
        }