~ubuntu-branches/ubuntu/jaunty/pcsc-lite/jaunty-security

« back to all changes in this revision

Viewing changes to src/test.c

  • Committer: Bazaar Package Importer
  • Author(s): Ludovic Rousseau
  • Date: 2004-06-13 21:45:56 UTC
  • mfrom: (1.1.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20040613214556-zio7hrzkz9wwtffx
Tags: 1.2.9-beta2-2
* debian/rules: add -lpthread to LDFLAGS so that pthread_* symbols are
  included in the library (problem only seen on mips and mipsel).
  Closes: #253629
* debian/control: make libpcsclite-dev and libpcsclite1 at Priority:
  optional so that other packages at Priority: optional can use them.
  Closes: #249374

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
/******************************************************************
3
 
 
4
 
        MUSCLE SmartCard Development ( http://www.linuxnet.com )
5
 
            Title  : test.c
6
 
            Package: pcsc lite
7
 
            Author : David Corcoran
8
 
            Date   : 7/27/99
9
 
            License: Copyright (C) 1999 David Corcoran
10
 
                     <corcoran@linuxnet.com>
11
 
            Purpose: This is a test program for pcsc-lite.
12
 
                    
13
 
********************************************************************/
14
 
 
15
 
#include <stdio.h>
16
 
#include <stdlib.h>
17
 
 
18
 
#include "config.h"
19
 
#include "pcsclite.h"
20
 
#include "winscard.h"
21
 
 
22
 
/* #define REPEAT_TEST 1 */
23
 
 
24
 
int main( int argc, char **argv ) {
25
 
  SCARDHANDLE hCard; SCARDCONTEXT hContext;
26
 
  SCARD_READERSTATE_A rgReaderStates[1];
27
 
  unsigned long dwReaderLen, dwState, dwProt, dwAtrLen;
28
 
  //unsigned long dwSendLength, dwRecvLength;
29
 
  unsigned long dwPref, dwReaders;
30
 
  char *pcReaders, *mszReaders;
31
 
  unsigned char pbAtr[MAX_ATR_SIZE];
32
 
  const char * mszGroups;
33
 
  long rv;
34
 
  int i, p, iReader;
35
 
  int iList[16];
36
 
 
37
 
  //int t = 0;
38
 
 
39
 
  printf("\nMUSCLE PC/SC Lite Test Program\n\n");
40
 
 
41
 
  printf("Testing SCardEstablishContext    : ");
42
 
  rv = SCardEstablishContext( SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext );
43
 
 
44
 
  printf("%s\n", pcsc_stringify_error(rv));
45
 
 
46
 
  if ( rv != SCARD_S_SUCCESS ) {
47
 
    return -1;
48
 
  }
49
 
  
50
 
  printf("Testing SCardGetStatusChange \n");
51
 
  printf("Please insert a working reader   : ");
52
 
  rv = SCardGetStatusChange( hContext, INFINITE, 0, 0 );
53
 
 
54
 
  printf("%s\n", pcsc_stringify_error(rv));
55
 
 
56
 
  if ( rv != SCARD_S_SUCCESS ) {
57
 
    SCardReleaseContext( hContext );
58
 
    return -1;
59
 
  }
60
 
 
61
 
  printf("Testing SCardListReaders         : ");
62
 
 
63
 
  mszGroups = 0;
64
 
  rv = SCardListReaders( hContext, mszGroups, 0, &dwReaders );
65
 
 
66
 
  printf("%s\n", pcsc_stringify_error(rv));
67
 
 
68
 
  if ( rv != SCARD_S_SUCCESS ) {
69
 
    SCardReleaseContext( hContext );
70
 
    return -1;
71
 
  }
72
 
 
73
 
  mszReaders = (char *)malloc(sizeof(char)*dwReaders);
74
 
  rv = SCardListReaders( hContext, mszGroups, mszReaders, &dwReaders );
75
 
 
76
 
  if ( rv != SCARD_S_SUCCESS ) {
77
 
    SCardReleaseContext( hContext );
78
 
    return -1;
79
 
  }
80
 
 
81
 
  /* Have to understand the multi-string here */
82
 
  p = 0;
83
 
  for ( i=0; i < dwReaders - 1; i++ ) {
84
 
    ++p;
85
 
    printf("Reader %02d: %s\n", p, &mszReaders[i]);
86
 
    iList[p] = i;
87
 
    while ( mszReaders[++i] != 0 );
88
 
  }
89
 
 
90
 
#ifdef REPEAT_TEST
91
 
  if ( t==0 ) { 
92
 
#endif
93
 
 
94
 
  do {
95
 
    printf("Enter the reader number          : " );
96
 
    scanf("%d", &iReader);
97
 
    printf("\n");
98
 
 
99
 
    if ( iReader > p || iReader <= 0 ) {
100
 
      printf("Invalid Value - try again\n");
101
 
    }
102
 
  } while ( iReader > p || iReader <= 0 );
103
 
 
104
 
#ifdef REPEAT_TEST
105
 
  t=1; 
106
 
  }    
107
 
#endif
108
 
 
109
 
  rgReaderStates[0].szReader       = &mszReaders[iList[iReader]];
110
 
  rgReaderStates[0].dwCurrentState = SCARD_STATE_EMPTY;
111
 
 
112
 
  printf("Waiting for card insertion         \n");
113
 
  rv = SCardGetStatusChange( hContext, INFINITE, rgReaderStates, 1 );
114
 
 
115
 
  printf("                                 : %s\n", pcsc_stringify_error(rv));
116
 
 
117
 
  if ( rv != SCARD_S_SUCCESS ) {
118
 
    SCardReleaseContext( hContext );
119
 
    return -1;
120
 
  }
121
 
 
122
 
  printf("Testing SCardConnect             : ");
123
 
  rv = SCardConnect(hContext, &mszReaders[iList[iReader]], 
124
 
                    SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1, 
125
 
                    &hCard, &dwPref);
126
 
 
127
 
  printf("%s\n", pcsc_stringify_error(rv));
128
 
 
129
 
  if ( rv != SCARD_S_SUCCESS ) {
130
 
    SCardReleaseContext( hContext );
131
 
    return -1;
132
 
  }
133
 
 
134
 
  printf("Testing SCardStatus              : ");
135
 
 
136
 
  dwReaderLen = 50;
137
 
  pcReaders   = (char*)malloc(sizeof(char)*50);
138
 
  
139
 
  rv = SCardStatus( hCard, pcReaders, &dwReaderLen, &dwState, &dwProt,
140
 
                    pbAtr, &dwAtrLen );
141
 
 
142
 
  printf("%s\n", pcsc_stringify_error(rv));
143
 
 
144
 
  printf("Current Reader Name              : %s\n", pcReaders);
145
 
  printf("Current Reader State             : %lx\n", dwState);
146
 
  printf("Current Reader Protocol          : %lx\n", dwProt - 1);
147
 
  printf("Current Reader ATR Size          : %lx\n", dwAtrLen);
148
 
  printf("Current Reader ATR Value         : ");
149
 
  
150
 
  for (i=0; i < dwAtrLen; i++) {
151
 
    printf("%02X ", pbAtr[i]);
152
 
  } printf("\n");
153
 
 
154
 
  if ( rv != SCARD_S_SUCCESS ) {
155
 
    SCardDisconnect( hCard, SCARD_RESET_CARD );
156
 
    SCardReleaseContext( hContext );
157
 
  }
158
 
 
159
 
  printf("Testing SCardDisconnect          : ");
160
 
  rv = SCardDisconnect( hCard, SCARD_UNPOWER_CARD );
161
 
 
162
 
  printf("%s\n", pcsc_stringify_error(rv));
163
 
 
164
 
  if ( rv != SCARD_S_SUCCESS ) {
165
 
    SCardReleaseContext( hContext );
166
 
    return -1;
167
 
  }
168
 
 
169
 
  printf("Testing SCardReleaseContext      : ");
170
 
  rv = SCardReleaseContext( hContext );
171
 
 
172
 
  printf("%s\n", pcsc_stringify_error(rv));
173
 
 
174
 
  if ( rv != SCARD_S_SUCCESS ) {
175
 
    return -1;
176
 
  }
177
 
 
178
 
  printf("\n");
179
 
  printf("PC/SC Test Completed Successfully !\n");
180
 
 
181
 
  return 0;
182
 
}