-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgenerate_test.go
158 lines (145 loc) · 3.41 KB
/
generate_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
// Copyright 2017 Josh Komoroske. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE.txt file.
package ykman
import (
"errors"
"fmt"
"os/exec"
"testing"
"github.com/stretchr/testify/assert"
)
func TestGenerateParse(t *testing.T) {
var errorGeneric = errors.New("generic error")
tests := []struct {
title string
name string
body string
error error
expectedError error
expectedCode string
}{
{
title: "empty results",
expectedError: ErrorSlotNameUnknown,
},
{
title: "single result",
name: "aws",
body: `
aws 123456
`,
expectedCode: "123456",
},
{
title: "single long result",
name: "aws",
body: `
aws 12345678
`,
expectedCode: "12345678",
},
{
title: "multiple results",
name: "aws",
body: `
aws 123456
aws 867530
`,
expectedCode: "123456",
},
{
title: "code too short",
name: "aws",
body: `
aws 1234
`,
expectedError: ErrorSlotNameUnknown,
},
{
title: "bad code format",
name: "aws",
body: `
aws DEADBEEF
`,
expectedError: ErrorSlotNameUnknown,
},
{
title: "different name returned",
name: "aws",
body: `
aws-us-gov 123456
`,
expectedError: ErrorSlotNameUnknown,
},
{
title: "error ykman executable missing",
error: &exec.Error{
Err: exec.ErrNotFound,
},
expectedError: ErrorYkmanNotFound,
},
{
title: "error no yubikey detected",
body: `
Usage: ykman [OPTIONS] COMMAND [ARGS]...
Error: Failed connecting to the YubiKey.
`,
error: errorGeneric,
expectedError: ErrorYubikeyNotDetected,
},
{
title: "error aborted",
body: `
Touch your YubiKey...
^C
Aborted!
`,
error: errorGeneric,
expectedError: ErrorYkmanInterrupted,
},
{
title: "error yubikey removed",
body: `
Touch your YubiKey...
Traceback (most recent call last):
File "/usr/local/bin/ykman", line 11, in <module>
load_entry_point('yubikey-manager==0.4.5', 'console_scripts', 'ykman')()
<...snip...>
File "/usr/local/Cellar/ykman/0.4.5/libexec/lib/python2.7/site-packages/ykman/driver_ccid.py", line 123, in send_apdu
raise CCIDError(e)
ykman.driver_ccid.CCIDError: Failed to transmit with protocol T1. Transaction failed.
`,
error: errorGeneric,
expectedError: ErrorYubikeyRemoved,
},
{
title: "error yubikey timed out",
body: `
Touch your YubiKey...
Traceback (most recent call last):
File "/usr/local/bin/ykman", line 11, in <module>
load_entry_point('yubikey-manager==0.4.5', 'console_scripts', 'ykman')()
<...snip...>
File "/usr/local/Cellar/ykman/0.4.5/libexec/lib/python2.7/site-packages/ykman/oath.py", line 203, in send_apdu
raise APDUError(resp, sw)
ykman.driver_ccid.APDUError: APDU error: SW=0x6982
`,
error: errorGeneric,
expectedError: ErrorYubikeyTimeout,
},
{
title: "generic error",
error: errorGeneric,
expectedError: errorGeneric,
},
}
for index, test := range tests {
name := fmt.Sprintf("case #%d - %s", index, test.title)
t.Run(name, func(t *testing.T) {
actualCode, actualError := parseGenerate(test.body, test.error, test.name)
assert.Equal(t, test.expectedError, actualError)
assert.Equal(t, test.expectedCode, actualCode)
})
}
}