forked from openpubkey/opkssh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
380 lines (358 loc) · 12 KB
/
main_test.go
File metadata and controls
380 lines (358 loc) · 12 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
// Copyright 2024 OpenPubkey
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
package main
import (
"io"
"os"
"strconv"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func TestIsOpenSSHVersion8Dot1OrGreater(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input string
wantIsGreater bool
wantErr string
}{
{
name: "Exact 8.1",
input: "OpenSSH_8.1",
wantIsGreater: true,
wantErr: "",
},
{
name: "Above 8.1 (8.4)",
input: "OpenSSH_8.4",
wantIsGreater: true,
wantErr: "",
},
{
name: "Regression test for 10.0 bug",
input: "OpenSSH_10.0",
wantIsGreater: true,
wantErr: "",
},
{
name: "Above 8.1 with patch (9.9p1)",
input: "OpenSSH_9.9p1",
wantIsGreater: true,
wantErr: "",
},
{
name: "Below 8.1 (7.9)",
input: "OpenSSH_7.9",
wantIsGreater: false,
wantErr: "",
},
{
name: "Multiple dotted version above 8.1 (8.1.2)",
input: "OpenSSH_8.1.2",
wantIsGreater: true,
wantErr: "",
},
{
name: "Multiple dotted version below 8.1 (7.10.3)",
input: "OpenSSH_7.10.3",
wantIsGreater: false,
wantErr: "",
},
{
name: "Malformed version string",
input: "OpenSSH_, something not right",
wantIsGreater: false,
wantErr: "invalid OpenSSH version",
},
{
name: "No OpenSSH prefix at all",
input: "Completely invalid input",
wantIsGreater: false,
wantErr: "invalid OpenSSH version",
},
{
name: "Includes trailing info (8.2, Raspbian-1)",
input: "OpenSSH_8.2, Raspbian-1",
wantIsGreater: true,
wantErr: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
RunOpenSSHVersionTest(t, tt.name, tt.input, tt.wantIsGreater, tt.wantErr)
})
}
}
func TestOpenSSHVersion8Dot1OrGreaterViaBruteForce(t *testing.T) {
t.Parallel()
for major := 7; major <= 15; major++ {
for minor := 0; minor <= 101; minor++ {
versionStr := "OpenSSH_" + strconv.Itoa(major) + "." + strconv.Itoa(minor)
testName := "Testing openssh version " + versionStr
expectedIsGreater := true
if major < 8 || (major == 8 && minor < 1) {
expectedIsGreater = false
}
RunOpenSSHVersionTest(t, testName, versionStr, expectedIsGreater, "")
}
}
}
func RunOpenSSHVersionTest(t *testing.T, testName string, versionOutput string, expectedIsGreater bool, expectedErr string) {
gotIsGreater, gotErr := isOpenSSHVersion8Dot1OrGreater(versionOutput)
require.Equal(t, expectedIsGreater, gotIsGreater,
"Test %q failed: isOpenSSHVersion8Dot1OrGreater(%q) got %v; want %v",
testName, versionOutput, gotIsGreater, expectedIsGreater)
if expectedErr != "" {
require.Error(t, gotErr,
"Test %q failed: isOpenSSHVersion8Dot1OrGreater(%q) expected error = %s",
testName, versionOutput, expectedErr, gotErr)
require.ErrorContains(t, gotErr, expectedErr,
"Test %q failed: isOpenSSHVersion8Dot1OrGreater(%q) expected error = %s, got %v",
testName, versionOutput, expectedErr, gotErr)
} else {
require.NoError(t, gotErr,
"Test %q failed: isOpenSSHVersion8Dot1OrGreater(%q) unexpected error = %v",
testName, versionOutput, gotErr)
}
}
func RunCliAndCaptureResult(t *testing.T, args []string) (string, int) {
// Backup and defer restore of os.Args
oldArgs := os.Args
defer func() { os.Args = oldArgs }()
os.Args = args
// Capture output
oldStdout := os.Stdout
oldStderr := os.Stderr
r, w, _ := os.Pipe()
os.Stdout = w
os.Stderr = w
// Run the opkssh cli
exitCode := run()
// Restore stdout and stderr
w.Close()
os.Stdout = oldStdout
os.Stderr = oldStderr
// Read captured output
var cmdOutput strings.Builder
_, err := io.Copy(&cmdOutput, r)
require.NoError(t, err)
return cmdOutput.String(), exitCode
}
func TestRun(t *testing.T) {
t.Parallel()
tests := []struct {
name string
args []string
wantOutput string
wantExit int
}{
{
name: "No arguments",
args: []string{"opkssh"},
wantOutput: "SSH with OpenPubkey",
wantExit: 0,
},
{
name: "Root Help flag",
args: []string{"opkssh", "--help"},
wantOutput: "opkssh [command]",
wantExit: 0,
},
{
name: "Add Help flag",
args: []string{"opkssh", "add", "--help"},
wantOutput: "Add appends a new policy entry in the auth_id policy file",
wantExit: 0,
},
{
name: "Audit Help flag",
args: []string{"opkssh", "audit", "--help"},
wantOutput: "Audit validates all entries",
wantExit: 0,
},
{
name: "Login Help flag",
args: []string{"opkssh", "login", "--help"},
wantOutput: "Login creates opkssh SSH keys",
wantExit: 0,
},
{
name: "Verify Help flag",
args: []string{"opkssh", "verify", "--help"},
wantOutput: "Verify extracts a PK token",
wantExit: 0,
},
{
name: "Version flag",
args: []string{"opkssh", "--version"},
wantOutput: "unversioned",
wantExit: 0,
},
{
name: "Unrecognized command",
args: []string{"opkssh", "unknown"},
wantOutput: "Error: unknown command \"unknown\"",
wantExit: 1,
},
{
name: "Add command with missing arguments",
args: []string{"opkssh", "add"},
wantOutput: "Error: accepts 3 arg(s), received 0",
wantExit: 1,
},
{
name: "Login command with bad arguments",
args: []string{"opkssh", "login", "-badarg"},
wantOutput: "Error: unknown shorthand flag:",
wantExit: 1,
},
{
name: "Login command with missing providers arguments",
args: []string{"opkssh", "login", "--provider"},
wantOutput: "Error: flag needs an argument: --provider",
wantExit: 1,
},
{
name: "Login command with provider bad provider value",
args: []string{"opkssh", "login", "--provider=badvalue"},
wantOutput: "error parsing provider argument: invalid provider config string. Expected format <issuer>,<client_id> or <issuer>,<client_id>,<client_secret> or <issuer>,<client_id>,<client_secret>,<scopes>",
wantExit: 1,
},
{
name: "Login command with provider bad provider issuer value",
args: []string{"opkssh", "login", "--provider=badissuer.com,client_id"},
wantOutput: "error creating provider from config: invalid provider issuer value. Expected issuer to start with 'https://' got (badissuer.com)",
wantExit: 1,
},
{
name: "Login command with provider bad provider good azure issuer but no client id value",
args: []string{"opkssh", "login", "--provider=https://login.microsoftonline.com/9188040d-6c67-4c5b-b112-36a304b66dad/v2.0,"},
wantOutput: "error parsing provider argument: invalid provider client-ID value got ()",
wantExit: 1,
},
{
name: "Login command with provider bad provider good google issuer but no client id value",
args: []string{"opkssh", "login", "--provider=https://accounts.google.com,client_id"},
wantOutput: "error parsing provider argument: invalid provider argument format. Expected format for google: <issuer>,<client_id>,<client_secret>",
wantExit: 1,
},
{
name: "Login command with provider bad provider good google issuer but no client secret value",
args: []string{"opkssh", "login", "--provider=https://accounts.google.com,client_id,"},
wantOutput: "error parsing provider argument: invalid provider argument format. Expected format for google: <issuer>,<client_id>,<client_secret>",
wantExit: 1,
},
{
name: "Login command with alias bad alias",
args: []string{"opkssh", "login", "badalias"},
wantOutput: "error getting provider config for alias badalias",
wantExit: 1,
},
{
name: "Verify command fail on bad log file path",
args: []string{"opkssh", "verify", "arg1", "arg2", "arg3"},
wantOutput: "Error opening log file:",
wantExit: 1,
},
{
name: "Verify command accepts extra arguments and fails on bad log file path",
args: []string{"opkssh", "verify", "arg1", "arg2", "arg3", "extraArg1", "extraArg2"},
wantOutput: "Error opening log file:",
wantExit: 1,
},
{
name: "Client provider list",
args: []string{"opkssh", "client", "provider", "list", "--config-path=commands/config/default-client-config.yml"},
wantOutput: "" +
"google https://accounts.google.com\n" +
"azure https://login.microsoftonline.com/9188040d-6c67-4c5b-b112-36a304b66dad/v2.0\n" +
"microsoft https://login.microsoftonline.com/9188040d-6c67-4c5b-b112-36a304b66dad/v2.0\n" +
"gitlab https://gitlab.com\n" +
"hello https://issuer.hello.coop\n",
wantExit: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmdOutput, exitCode := RunCliAndCaptureResult(t, tt.args)
require.Contains(t, cmdOutput, tt.wantOutput, "Incorrect command output")
require.Equal(t, tt.wantExit, exitCode, "Incorrect Exit code")
})
}
}
func TestWithEnvVars(t *testing.T) {
tests := []struct {
name string
envVar string
envValue string
args []string
wantOutput string
wantExit int
}{
{
name: "Set OPKSSH_DEFAULT to bad value",
envVar: "OPKSSH_DEFAULT",
envValue: "badvalue",
args: []string{"opkssh", "login", "--config-path", "/foo/bar"},
wantOutput: "error getting provider config for alias badvalue",
wantExit: 1,
},
{
name: "Set OPKSSH_PROVIDERS to bad value",
envVar: "OPKSSH_PROVIDERS",
envValue: "badvalue",
args: []string{"opkssh", "login", "--config-path", "/foo/bar"},
wantOutput: "Expected format <alias>,<issuer>,<client_id> or <alias>,<issuer>,<client_id>,<client_secret> or <alias>,<issuer>,<client_id>,<client_secret>,<scopes>",
wantExit: 1,
},
{
name: "Set OPKSSH_PROVIDERS with duplicates aliases",
envVar: "OPKSSH_PROVIDERS",
envValue: "alias1,https://accounts.google.com,client_id,client_secret,scope1;alias1,https://accounts.google.com,client_id,client_secret,scope2",
args: []string{"opkssh", "login", "--config-path", "/foo/bar"},
wantOutput: "provider in web chooser found with duplicate issuer",
wantExit: 1,
},
{
name: "Set OPKSSH_PROVIDERS with bad provider",
envVar: "OPKSSH_PROVIDERS",
envValue: "google,http://insecure.badprovider.com,client_id,client_secret,openid profile",
args: []string{"opkssh", "login", "--config-path", "/foo/bar"},
wantOutput: "error creating provider from config: invalid provider issuer value. Expected issuer to start with 'https://' got (http://insecure.badprovider.com)",
wantExit: 1,
},
{
name: "Set OPKSSH_PROVIDERS with good provider but asking for wrong alias",
envVar: "OPKSSH_PROVIDERS",
envValue: "goodprovider,https://goodprovider.com,client_id,client_secret,openid profile",
args: []string{"opkssh", "login", "badalias", "--config-path", "/foo/bar"},
wantOutput: "error getting provider config for alias badalias",
wantExit: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_ = os.Setenv(tt.envVar, tt.envValue)
defer func(key string) {
_ = os.Unsetenv(key)
}(tt.envVar)
cmdOutput, exitCode := RunCliAndCaptureResult(t, tt.args)
require.Contains(t, cmdOutput, tt.wantOutput, "Incorrect command output")
require.Equal(t, tt.wantExit, exitCode, "Incorrect Exit code")
})
}
}