| 1 | #!/usr/bin/perl |
| 2 | |
| 3 | # test.pl: simple sample program to test the PCSC Perl wrapper |
| 4 | # Copyright (C) 2001 Lionel Victor, 2003,2006 Ludovic Rousseau |
| 5 | # |
| 6 | # This program is free software; you can redistribute it and/or modify |
| 7 | # it under the terms of the GNU General Public License as published by |
| 8 | # the Free Software Foundation; either version 2 of the License, or |
| 9 | # (at your option) any later version. |
| 10 | # |
| 11 | # This program is distributed in the hope that it will be useful, |
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | # GNU General Public License for more details. |
| 15 | # |
| 16 | # You should have received a copy of the GNU General Public License |
| 17 | # along with this program; if not, write to the Free Software |
| 18 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | |
| 20 | use Test::More; |
| 21 | use Chipcard::PCSC; |
| 22 | use Chipcard::PCSC::Card; |
| 23 | |
| 24 | plan 'no_plan'; |
| 25 | |
| 26 | use warnings; |
| 27 | use strict; |
| 28 | |
| 29 | my $hContext; |
| 30 | my @ReadersList; |
| 31 | |
| 32 | my $hCard; |
| 33 | my @StatusResult; |
| 34 | my $tmpVal; |
| 35 | my $SendData; |
| 36 | my $RecvData; |
| 37 | my $sw; |
| 38 | |
| 39 | #-------------------------------------------------------------------------- |
| 40 | diag("Getting context:"); |
| 41 | $hContext = new Chipcard::PCSC(); |
| 42 | die ("Can't create the pcsc object: $Chipcard::PCSC::errno\n") unless (defined $hContext); |
| 43 | ok(defined $hContext, "new Chipcard::PCSC()"); |
| 44 | |
| 45 | #-------------------------------------------------------------------------- |
| 46 | diag("Retrieving readers'list:"); |
| 47 | @ReadersList = $hContext->ListReaders (); |
| 48 | die ("Can't get readers' list: $Chipcard::PCSC::errno\n") unless (defined($ReadersList[0])); |
| 49 | $, = "\n "; |
| 50 | $" = "\n "; |
| 51 | ok(defined($ReadersList[0]), "\$hContext->ListReaders ()"); |
| 52 | diag(@ReadersList); |
| 53 | |
| 54 | #-------------------------------------------------------------------------- |
| 55 | diag("Getting status change:"); |
| 56 | my (@readers_states, $reader_state, $timeout, $event_state); |
| 57 | # create the list or readers to watch |
| 58 | map { push @readers_states, ({'reader_name'=>"$_"}) } @ReadersList; |
| 59 | |
| 60 | @StatusResult = $hContext->GetStatusChange(\@readers_states); |
| 61 | |
| 62 | for my $i (0..$#readers_states) |
| 63 | { |
| 64 | diag("reader: " . $readers_states[$i]{'reader_name'}); |
| 65 | diag(" ATR: " . Chipcard::PCSC::array_to_ascii($readers_states[$i]{'ATR'})) |
| 66 | if (defined $readers_states[$i]{'ATR'}); |
| 67 | diag(" state:"); |
| 68 | $event_state = $readers_states[$i]{'event_state'}; |
| 69 | diag(" state changed") |
| 70 | if ($event_state & $Chipcard::PCSC::SCARD_STATE_CHANGED); |
| 71 | diag(" card present") |
| 72 | if ($event_state & $Chipcard::PCSC::SCARD_STATE_PRESENT); |
| 73 | diag(" card absent") |
| 74 | if ($event_state & $Chipcard::PCSC::SCARD_STATE_EMPTY); |
| 75 | diag(" card mute") |
| 76 | if ($event_state & $Chipcard::PCSC::SCARD_STATE_MUTE); |
| 77 | |
| 78 | $readers_states[$i]{'current_state'} = $event_state; |
| 79 | } |
| 80 | |
| 81 | if (! ($readers_states[0]{'event_state'} & |
| 82 | $Chipcard::PCSC::SCARD_STATE_PRESENT)) |
| 83 | { |
| 84 | $timeout = 10 * 1000; # 10 seconds |
| 85 | diag("Insert a card in the first reader please (timeout in $timeout ms)"); |
| 86 | @StatusResult = $hContext->GetStatusChange(\@readers_states, $timeout); |
| 87 | } |
| 88 | |
| 89 | #-------------------------------------------------------------------------- |
| 90 | diag("Connecting to the card:"); |
| 91 | $hCard = new Chipcard::PCSC::Card ($hContext); |
| 92 | die ("Can't create the reader object: $Chipcard::PCSC::errno\n") unless (defined($hCard)); |
| 93 | |
| 94 | $tmpVal = $hCard->Connect($ReadersList[0], $Chipcard::PCSC::SCARD_SHARE_SHARED); |
| 95 | unless ($tmpVal) { |
| 96 | # Try to reconnect and reset if connect fails |
| 97 | diag("Connect failed: trying to reset the card:"); |
| 98 | $tmpVal = $hCard->Reconnect ($Chipcard::PCSC::SCARD_SHARE_SHARED, $Chipcard::PCSC::SCARD_PROTOCOL_T0, $Chipcard::PCSC::SCARD_RESET_CARD); |
| 99 | die ("Can't reconnect to the reader '$ReadersList[0]': $Chipcard::PCSC::errno\n") unless ($tmpVal); |
| 100 | } |
| 101 | ok($hCard->{dwProtocol}==$Chipcard::PCSC::SCARD_PROTOCOL_T0 || |
| 102 | $hCard->{dwProtocol}==$Chipcard::PCSC::SCARD_PROTOCOL_T1 || |
| 103 | $hCard->{dwProtocol}==$Chipcard::PCSC::SCARD_PROTOCOL_RAW, |
| 104 | "Can understand the current protocol: $hCard->{dwProtocol}"); |
| 105 | |
| 106 | #-------------------------------------------------------------------------- |
| 107 | diag("Getting status:"); |
| 108 | @StatusResult = $hCard->Status(); |
| 109 | die ("Can't get status: $Chipcard::PCSC::errno\n") unless ($StatusResult[0]); |
| 110 | diag("Reader name is $StatusResult[0]"); |
| 111 | diag("State: $StatusResult[1]"); |
| 112 | diag("Current protocol: $StatusResult[2]"); |
| 113 | diag("ATR: " . Chipcard::PCSC::array_to_ascii ($StatusResult[3])); |
| 114 | |
| 115 | #-------------------------------------------------------------------------- |
| 116 | diag("Initiating transaction:"); |
| 117 | die ("Can't initiate transaction: $Chipcard::PCSC::errno\n") unless ($hCard->BeginTransaction()); |
| 118 | diag($hCard->BeginTransaction(), "\$hCard->BeginTransaction()"); |
| 119 | |
| 120 | #-------------------------------------------------------------------------- |
| 121 | diag("Exchanging data:"); |
| 122 | $SendData = Chipcard::PCSC::ascii_to_array ("00 A4 01 00 02 01 00"); |
| 123 | $RecvData = $hCard->Transmit($SendData); |
| 124 | die ("Can't transmit data: $Chipcard::PCSC::errno") unless (defined ($RecvData)); |
| 125 | |
| 126 | diag(" Send -> " . Chipcard::PCSC::array_to_ascii ($SendData)); |
| 127 | diag(" Recv <- " . Chipcard::PCSC::array_to_ascii ($RecvData)); |
| 128 | |
| 129 | #-------------------------------------------------------------------------- |
| 130 | diag("TransmitWithCheck:"); |
| 131 | $SendData = "00 A4 00 00 02 3F 00"; # select DF 3F 00 |
| 132 | # wait for ".. .." since we the SW will depend on the inserted card |
| 133 | ($sw, $RecvData) = $hCard->TransmitWithCheck($SendData, ".. ..", 1); |
| 134 | warn "TransmitWithCheck: $Chipcard::PCSC::Card::Error" unless defined $sw; |
| 135 | ok(defined $sw, "\$hCard->TransmitWithCheck"); |
| 136 | |
| 137 | diag(" Send -> $SendData"); |
| 138 | diag(" Recv <- $RecvData (SW: $sw)"); |
| 139 | |
| 140 | #-------------------------------------------------------------------------- |
| 141 | diag("ISO7816Error:"); |
| 142 | diag("$sw: " . &Chipcard::PCSC::Card::ISO7816Error($sw)); |
| 143 | |
| 144 | #-------------------------------------------------------------------------- |
| 145 | # This test is commented since it is reader/driver specific and may do bad |
| 146 | # things for another reader. Reader your reader and driver |
| 147 | # specifications to know what data to use. |
| 148 | # |
| 149 | #diag("Control"); |
| 150 | #$SendData = Chipcard::PCSC::ascii_to_array ("02"); |
| 151 | #$RecvData = $hCard->Control(0x42000001, $SendData); |
| 152 | #die ("Can't Control data: $Chipcard::PCSC::errno") unless (defined ($RecvData)); |
| 153 | # |
| 154 | #diag(" Send -> " . Chipcard::PCSC::array_to_ascii ($SendData)); |
| 155 | #diag(" Recv <- " . Chipcard::PCSC::array_to_ascii ($RecvData)); |
| 156 | |
| 157 | #-------------------------------------------------------------------------- |
| 158 | diag("Ending transaction:"); |
| 159 | die ("Can't terminate transaction: $Chipcard::PCSC::errno\n") unless ($hCard->EndTransaction($Chipcard::PCSC::SCARD_LEAVE_CARD)); |
| 160 | ok($hCard->EndTransaction($Chipcard::PCSC::SCARD_LEAVE_CARD), |
| 161 | "\$hCard->EndTransaction($Chipcard::PCSC::SCARD_LEAVE_CARD)"); |
| 162 | |
| 163 | #-------------------------------------------------------------------------- |
| 164 | diag("Disconnecting the card:"); |
| 165 | $tmpVal = $hCard->Disconnect($Chipcard::PCSC::SCARD_LEAVE_CARD); |
| 166 | die ("Can't disconnect the Chipcard::PCSC object: $Chipcard::PCSC::errno\n") unless $tmpVal; |
| 167 | ok($tmpVal, "\$hCard->Disconnect"); |
| 168 | |
| 169 | #-------------------------------------------------------------------------- |
| 170 | diag("Closing card object:"); |
| 171 | $hCard = undef; |
| 172 | |
| 173 | #-------------------------------------------------------------------------- |
| 174 | diag("Closing context:"); |
| 175 | $hContext = undef; |
| 176 | |
| 177 | # End of File # |
| 178 |