-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathscard_test.go
160 lines (136 loc) · 2.71 KB
/
scard_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package scard
import (
"runtime"
"testing"
)
// FIXME: all tests assume pcscd is running, and there is a single reader with a
// card present
type testCard struct {
ctx *Context
card *Card
}
func setup(t *testing.T) *testCard {
var ctx *Context
var card *Card
ctx, err := EstablishContext()
if err != nil {
t.Skipf("EstablishContext: %s", err)
t.SkipNow()
}
defer func() {
if card == nil {
ctx.Release()
}
}()
readers, err := ctx.ListReaders()
if err != nil {
t.Error(err)
t.FailNow()
}
if len(readers) == 0 {
t.Skip("no smartcard reader found")
t.SkipNow()
}
card, err = ctx.Connect(readers[0], ShareExclusive, ProtocolAny)
if err != nil {
t.Skip("no smartcard found")
t.SkipNow()
}
return &testCard{ctx: ctx, card: card}
}
func teardown(c *testCard) {
if c.card != nil {
c.card.Disconnect(LeaveCard)
}
if c.ctx != nil {
c.ctx.Release()
}
}
func TestListReaders(t *testing.T) {
ctx, err := EstablishContext()
if err != nil {
t.Skipf("EstablishContext: %s", err)
t.SkipNow()
}
defer ctx.Release()
readers, err := ctx.ListReaders()
if err != nil {
t.Fatal(err)
}
for _, reader := range readers {
t.Log(reader)
}
}
func TestListReaderGroups(t *testing.T) {
ctx, err := EstablishContext()
if err != nil {
t.Skipf("EstablishContext: %s", err)
t.SkipNow()
}
defer ctx.Release()
groups, err := ctx.ListReaderGroups()
if err != nil {
t.Fatal(err)
}
for _, group := range groups {
t.Log(group)
}
}
func TestTransmit(t *testing.T) {
c := setup(t)
defer teardown(c)
var cmd = []byte{0x00, 0xa4, 0x00, 0x0c, 0x02, 0x3f, 0x00} // SELECT MF
t.Logf("cmd: % x\n", cmd)
rsp, err := c.card.Transmit(cmd)
if err != nil {
t.Fatal(err)
}
t.Logf("rsp: % x\n", rsp)
}
func TestControl(t *testing.T) {
c := setup(t)
defer teardown(c)
// euh...
ioctl := uint32(0x42000000) + 3400 // CM_IOCTL_GET_FEATURE_REQUEST
if runtime.GOOS == "windows" {
ioctl = 0x00310000 + 3400<<2
}
rsp, err := c.card.Control(ioctl, nil)
if err != nil {
// skip on unsupported control code errors
if runtime.GOOS == "windows" {
if err, ok := err.(Error); ok {
if uintptr(err) == 1 {
t.Skip()
}
}
}
t.Fatal(err)
}
t.Logf("rsp: % x\n", rsp)
}
func TestStatus(t *testing.T) {
c := setup(t)
defer teardown(c)
status, err := c.card.Status()
if err != nil {
t.Fatal(err)
}
t.Logf("%+#v\n", status)
}
func TestGetAttrib(t *testing.T) {
c := setup(t)
defer teardown(c)
atr, err := c.card.GetAttrib(AttrAtrString)
if err != nil {
t.Error(err)
} else {
t.Logf("ATTR_ATR_STRING: % x\n", atr)
}
vendor, err := c.card.GetAttrib(AttrVendorName)
if err != nil {
t.Error(err)
} else {
t.Logf("ATTR_VENDOR_NAME: %s [% x]\n", string(vendor), vendor)
}
}