-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathauth_requestor_plymouth_test.go
More file actions
603 lines (519 loc) · 21.9 KB
/
Copy pathauth_requestor_plymouth_test.go
File metadata and controls
603 lines (519 loc) · 21.9 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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2026 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package secboot_test
import (
"context"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/snapcore/secboot/internal/testutil"
snapd_testutil "github.com/snapcore/snapd/testutil"
. "gopkg.in/check.v1"
. "github.com/snapcore/secboot"
)
type authRequestorPlymouthTestMixin struct {
passwordFile string
stopPlymouthdFile string
displayMessageFailFile string
mockPlymouth *snapd_testutil.MockCmd
}
func (m *authRequestorPlymouthTestMixin) setUpTest(c *C) (restore func()) {
dir := c.MkDir()
m.passwordFile = filepath.Join(dir, "password") // password to be returned by the mock plymouth
m.stopPlymouthdFile = filepath.Join(dir, "plymouthd-stopped")
m.displayMessageFailFile = filepath.Join(dir, "display-message-fail")
plymouthBottom := `if [ "$1" == "--ping" ] && [ -e %[1]s ]; then
exit 1
elif [ "$1" == "ask-for-password" ]; then
cat %[2]s
elif [ "$1" == "display-message" ] && [ -e %[3]s ]; then
exit 1
fi`
m.mockPlymouth = snapd_testutil.MockCommand(c, "plymouth", fmt.Sprintf(plymouthBottom, m.stopPlymouthdFile, m.passwordFile, m.displayMessageFailFile))
return m.mockPlymouth.Restore
}
func (m *authRequestorPlymouthTestMixin) setPassphrase(c *C, passphrase string) {
c.Assert(ioutil.WriteFile(m.passwordFile, []byte(passphrase), 0600), IsNil)
}
func (m *authRequestorPlymouthTestMixin) stopPlymouthd(c *C) {
f, err := os.Create(m.stopPlymouthdFile)
c.Assert(err, IsNil)
f.Close()
}
func (m *authRequestorPlymouthTestMixin) makePlymouthDisplayMessageFail(c *C) {
f, err := os.Create(m.displayMessageFailFile)
c.Assert(err, IsNil)
f.Close()
}
type authRequestorPlymouthSuite struct {
snapd_testutil.BaseTest
authRequestorPlymouthTestMixin
}
func (s *authRequestorPlymouthSuite) SetUpTest(c *C) {
s.AddCleanup(s.authRequestorPlymouthTestMixin.setUpTest(c))
}
var _ = Suite(&authRequestorPlymouthSuite{})
type mockPlymouthAuthRequestorStringer struct {
err error
}
func (s *mockPlymouthAuthRequestorStringer) RequestUserCredentialString(name, path string, authType UserAuthType) (string, error) {
if s.err != nil {
return "", s.err
}
var fmtString string
switch authType {
case UserAuthTypePassphrase:
fmtString = "Enter passphrase for %s (%s):"
case UserAuthTypePIN:
fmtString = "Enter PIN for %s (%s):"
case UserAuthTypeRecoveryKey:
fmtString = "Enter recovery key for %s (%s):"
case UserAuthTypePassphrase | UserAuthTypePIN:
fmtString = "Enter passphrase or PIN for %s (%s):"
case UserAuthTypePassphrase | UserAuthTypeRecoveryKey:
fmtString = "Enter passphrase or recovery key for %s (%s):"
case UserAuthTypePIN | UserAuthTypeRecoveryKey:
fmtString = "Enter PIN or recovery key for %s (%s):"
case UserAuthTypePassphrase | UserAuthTypePIN | UserAuthTypeRecoveryKey:
fmtString = "Enter passphrase, PIN or recovery key for %s (%s):"
default:
return "", errors.New("unexpected UserAuthType")
}
return fmt.Sprintf(fmtString, name, path), nil
}
func (s *mockPlymouthAuthRequestorStringer) NotifyUserAuthResultString(name, path string, result UserAuthResult, authTypes, unavailableAuthTypes UserAuthType) (string, error) {
if s.err != nil {
return "", s.err
}
switch result {
case UserAuthResultSuccess:
var fmtString string
switch authTypes {
case UserAuthTypePassphrase:
fmtString = "Unlocked %s (%s) successfully with passphrase"
case UserAuthTypePIN:
fmtString = "Unlocked %s (%s) successfully with PIN"
case UserAuthTypeRecoveryKey:
fmtString = "Unlocked %s (%s) successfully with recovery key"
default:
return "", errors.New("unexpected UserAuthType")
}
return fmt.Sprintf(fmtString, name, path), nil
case UserAuthResultFailed:
var b strings.Builder
switch authTypes {
case UserAuthTypePassphrase:
io.WriteString(&b, "Incorrect passphrase")
case UserAuthTypePIN:
io.WriteString(&b, "Incorrect PIN")
case UserAuthTypeRecoveryKey:
io.WriteString(&b, "Incorrect recovery key")
case UserAuthTypePassphrase | UserAuthTypePIN:
io.WriteString(&b, "Incorrect passphrase or PIN")
case UserAuthTypePassphrase | UserAuthTypeRecoveryKey:
io.WriteString(&b, "Incorrect passphrase or recovery key")
case UserAuthTypePIN | UserAuthTypeRecoveryKey:
io.WriteString(&b, "Incorrect PIN or recovery key")
case UserAuthTypePassphrase | UserAuthTypePIN | UserAuthTypeRecoveryKey:
io.WriteString(&b, "Incorrect passphrase, PIN or recovery key")
default:
return "", errors.New("unexpected UserAuthType")
}
switch unavailableAuthTypes {
case UserAuthType(0):
case UserAuthTypePassphrase:
io.WriteString(&b, ". No more passphrase tries remaining")
case UserAuthTypePIN:
io.WriteString(&b, ". No more PIN tries remaining")
case UserAuthTypeRecoveryKey:
io.WriteString(&b, ". No more recovery key tries remaining")
case UserAuthTypePassphrase | UserAuthTypePIN:
io.WriteString(&b, ". No more passphrase or PIN tries remaining")
case UserAuthTypePassphrase | UserAuthTypeRecoveryKey:
io.WriteString(&b, ". No more passphrase or recovery key tries remaining")
case UserAuthTypePIN | UserAuthTypeRecoveryKey:
io.WriteString(&b, ". No more PIN or recovery key tries remaining")
case UserAuthTypePassphrase | UserAuthTypePIN | UserAuthTypeRecoveryKey:
io.WriteString(&b, ". No more passphrase, PIN or recovery key tries remaining")
default:
return "", errors.New("unexpected UserAuthType")
}
return b.String(), nil
case UserAuthResultInvalidFormat:
switch authTypes {
case UserAuthTypePIN:
return "Invalid PIN", nil
case UserAuthTypeRecoveryKey:
return "Invalid recovery key", nil
case UserAuthTypePIN | UserAuthTypeRecoveryKey:
return "Invalid PIN or recovery key", nil
default:
return "", errors.New("unexpected UserAuthType")
}
default:
return "", errors.New("unexpected UserAuthResult")
}
}
type testPlymouthRequestUserCredentialsParams struct {
passphrase string
ctx context.Context
name string
path string
authTypes UserAuthType
expectedMsg string
}
func (s *authRequestorPlymouthSuite) testRequestUserCredential(c *C, params *testPlymouthRequestUserCredentialsParams) {
s.setPassphrase(c, params.passphrase)
requestor, err := NewPlymouthAuthRequestor(new(mockPlymouthAuthRequestorStringer))
c.Assert(err, IsNil)
passphrase, passphraseType, err := requestor.RequestUserCredential(params.ctx, params.name, params.path, params.authTypes)
c.Check(err, IsNil)
c.Check(passphrase, Equals, params.passphrase)
c.Check(passphraseType, Equals, params.authTypes)
c.Check(s.mockPlymouth.Calls(), DeepEquals, [][]string{
{"plymouth", "--ping"},
{"plymouth", "ask-for-password", "--prompt", params.expectedMsg},
})
c.Assert(requestor, testutil.ConvertibleTo, &PlymouthAuthRequestor{})
c.Check(requestor.(*PlymouthAuthRequestor).LastRequestUserCredentialCtx(), DeepEquals, PlymouthRequestUserCredentialContext{
Name: params.name,
Path: params.path,
})
}
func (s *authRequestorPlymouthSuite) TestRequestUserCredentialPassphrase(c *C) {
s.testRequestUserCredential(c, &testPlymouthRequestUserCredentialsParams{
passphrase: "password",
ctx: context.Background(),
name: "data",
path: "/dev/sda1",
authTypes: UserAuthTypePassphrase,
expectedMsg: "Enter passphrase for data (/dev/sda1):",
})
}
func (s *authRequestorPlymouthSuite) TestRequestUserCredentialPassphraseDifferentPassphrase(c *C) {
s.testRequestUserCredential(c, &testPlymouthRequestUserCredentialsParams{
passphrase: "1234",
ctx: context.Background(),
name: "data",
path: "/dev/sda1",
authTypes: UserAuthTypePassphrase,
expectedMsg: "Enter passphrase for data (/dev/sda1):",
})
}
func (s *authRequestorPlymouthSuite) TestRequestUserCredentialDifferentPath(c *C) {
s.testRequestUserCredential(c, &testPlymouthRequestUserCredentialsParams{
passphrase: "password",
ctx: context.Background(),
name: "data",
path: "/dev/nvme0n1p1",
authTypes: UserAuthTypePassphrase,
expectedMsg: "Enter passphrase for data (/dev/nvme0n1p1):",
})
}
func (s *authRequestorPlymouthSuite) TestRequestUserCredentialPassphraseDifferentName(c *C) {
s.testRequestUserCredential(c, &testPlymouthRequestUserCredentialsParams{
passphrase: "password",
ctx: context.Background(),
name: "foo",
path: "/dev/sda1",
authTypes: UserAuthTypePassphrase,
expectedMsg: "Enter passphrase for foo (/dev/sda1):",
})
}
func (s *authRequestorPlymouthSuite) TestRequestUserCredentialPIN(c *C) {
s.testRequestUserCredential(c, &testPlymouthRequestUserCredentialsParams{
passphrase: "1234",
ctx: context.Background(),
name: "data",
path: "/dev/sda1",
authTypes: UserAuthTypePIN,
expectedMsg: "Enter PIN for data (/dev/sda1):",
})
}
func (s *authRequestorPlymouthSuite) TestRequestUserCredentialRecoveryKey(c *C) {
s.testRequestUserCredential(c, &testPlymouthRequestUserCredentialsParams{
passphrase: "00000-11111-22222-33333-44444-55555-00000-11111",
ctx: context.Background(),
name: "data",
path: "/dev/sda1",
authTypes: UserAuthTypeRecoveryKey,
expectedMsg: "Enter recovery key for data (/dev/sda1):",
})
}
func (s *authRequestorPlymouthSuite) TestRequestUserCredentialPassphraseOrPIN(c *C) {
s.testRequestUserCredential(c, &testPlymouthRequestUserCredentialsParams{
passphrase: "1234",
ctx: context.Background(),
name: "data",
path: "/dev/sda1",
authTypes: UserAuthTypePassphrase | UserAuthTypePIN,
expectedMsg: "Enter passphrase or PIN for data (/dev/sda1):",
})
}
func (s *authRequestorPlymouthSuite) TestRequestUserCredentialPassphraseOrRecoveryKey(c *C) {
s.testRequestUserCredential(c, &testPlymouthRequestUserCredentialsParams{
passphrase: "password",
ctx: context.Background(),
name: "data",
path: "/dev/sda1",
authTypes: UserAuthTypePassphrase | UserAuthTypeRecoveryKey,
expectedMsg: "Enter passphrase or recovery key for data (/dev/sda1):",
})
}
func (s *authRequestorPlymouthSuite) TestRequestUserCredentialPINOrRecoveryKey(c *C) {
s.testRequestUserCredential(c, &testPlymouthRequestUserCredentialsParams{
passphrase: "00000-11111-22222-33333-44444-55555-00000-11111",
ctx: context.Background(),
name: "data",
path: "/dev/sda1",
authTypes: UserAuthTypePIN | UserAuthTypeRecoveryKey,
expectedMsg: "Enter PIN or recovery key for data (/dev/sda1):",
})
}
func (s *authRequestorPlymouthSuite) TestRequestUserCredentialPassphraseOrPINOrRecoveryKey(c *C) {
s.testRequestUserCredential(c, &testPlymouthRequestUserCredentialsParams{
passphrase: "password",
ctx: context.Background(),
name: "data",
path: "/dev/sda1",
authTypes: UserAuthTypePassphrase | UserAuthTypePIN | UserAuthTypeRecoveryKey,
expectedMsg: "Enter passphrase, PIN or recovery key for data (/dev/sda1):",
})
}
func (s *authRequestorPlymouthSuite) TestNewRequestorNotAvailable(c *C) {
old := os.Getenv("PATH")
dir := c.MkDir()
os.Setenv("PATH", dir)
defer func() { os.Setenv("PATH", old) }()
_, err := NewPlymouthAuthRequestor(nil)
c.Check(err, ErrorMatches, `the auth requestor is not available`)
c.Check(errors.Is(err, ErrAuthRequestorNotAvailable), testutil.IsTrue)
}
func (s *authRequestorPlymouthSuite) TestNewRequestorNoStringer(c *C) {
_, err := NewPlymouthAuthRequestor(nil)
c.Check(err, ErrorMatches, `must supply an implementation of AuthRequestorStringer`)
}
func (s *authRequestorPlymouthSuite) TestRequestUserCredentialObtainMessageError(c *C) {
requestor, err := NewPlymouthAuthRequestor(&mockPlymouthAuthRequestorStringer{
err: errors.New("some error"),
})
c.Assert(err, IsNil)
_, _, err = requestor.RequestUserCredential(context.Background(), "data", "/dev/sda1", UserAuthTypePassphrase)
c.Check(err, ErrorMatches, `cannot request message string: some error`)
c.Assert(requestor, testutil.ConvertibleTo, &PlymouthAuthRequestor{})
c.Check(requestor.(*PlymouthAuthRequestor).LastRequestUserCredentialCtx(), DeepEquals, PlymouthRequestUserCredentialContext{})
}
func (s *authRequestorPlymouthSuite) TestRequestUserCredentialFailure(c *C) {
requestor, err := NewPlymouthAuthRequestor(new(mockPlymouthAuthRequestorStringer))
c.Assert(err, IsNil)
_, _, err = requestor.RequestUserCredential(context.Background(), "data", "/dev/sda1", UserAuthTypePassphrase)
c.Check(err, ErrorMatches, "cannot execute plymouth ask-for-password: exit status 1")
c.Assert(requestor, testutil.ConvertibleTo, &PlymouthAuthRequestor{})
c.Check(requestor.(*PlymouthAuthRequestor).LastRequestUserCredentialCtx(), DeepEquals, PlymouthRequestUserCredentialContext{})
}
func (s *authRequestorPlymouthSuite) TestRequestUserCredentialCanceledContext(c *C) {
s.setPassphrase(c, "foo")
requestor, err := NewPlymouthAuthRequestor(new(mockPlymouthAuthRequestorStringer))
c.Assert(err, IsNil)
ctx, cancel := context.WithCancel(context.Background())
cancel()
_, _, err = requestor.RequestUserCredential(ctx, "data", "/dev/sda1", UserAuthTypePassphrase)
c.Check(err, ErrorMatches, "cannot execute plymouth --ping: context canceled")
c.Check(errors.Is(err, context.Canceled), testutil.IsTrue)
c.Assert(requestor, testutil.ConvertibleTo, &PlymouthAuthRequestor{})
c.Check(requestor.(*PlymouthAuthRequestor).LastRequestUserCredentialCtx(), DeepEquals, PlymouthRequestUserCredentialContext{})
}
func (s *authRequestorPlymouthSuite) TestRequestUserCredentialNotAvailable(c *C) {
requestor, err := NewPlymouthAuthRequestor(new(mockPlymouthAuthRequestorStringer))
c.Assert(err, IsNil)
s.stopPlymouthd(c)
_, _, err = requestor.RequestUserCredential(context.Background(), "data", "/dev/sda1", UserAuthTypePassphrase)
c.Check(err, ErrorMatches, "the auth requestor is not available")
c.Check(errors.Is(err, ErrAuthRequestorNotAvailable), testutil.IsTrue)
c.Assert(requestor, testutil.ConvertibleTo, &PlymouthAuthRequestor{})
c.Check(requestor.(*PlymouthAuthRequestor).LastRequestUserCredentialCtx(), DeepEquals, PlymouthRequestUserCredentialContext{})
}
type testPlymouthNotifyUserAuthResultParams struct {
name string
path string
result UserAuthResult
authTypes UserAuthType
unavailableAuthTypes UserAuthType
expectedMsg string
}
func (s *authRequestorPlymouthSuite) testNotifyUserAuthResult(c *C, params *testPlymouthNotifyUserAuthResultParams) {
requestor := NewPlymouthAuthRequestorForTesting(new(mockPlymouthAuthRequestorStringer), &PlymouthRequestUserCredentialContext{Name: params.name, Path: params.path})
c.Check(requestor.NotifyUserAuthResult(context.Background(), params.result, params.authTypes, params.unavailableAuthTypes), IsNil)
c.Check(s.mockPlymouth.Calls(), DeepEquals, [][]string{
{"plymouth", "--ping"},
{"plymouth", "display-message", "--text", params.expectedMsg},
})
}
func (s *authRequestorPlymouthSuite) TestNotifyUserAuthResultSuccessPassphrase(c *C) {
s.testNotifyUserAuthResult(c, &testPlymouthNotifyUserAuthResultParams{
name: "data",
path: "/dev/sda1",
result: UserAuthResultSuccess,
authTypes: UserAuthTypePassphrase,
expectedMsg: "Unlocked data (/dev/sda1) successfully with passphrase",
})
}
func (s *authRequestorPlymouthSuite) TestNotifyUserAuthResultSuccessDifferentName(c *C) {
s.testNotifyUserAuthResult(c, &testPlymouthNotifyUserAuthResultParams{
name: "foo",
path: "/dev/sda1",
result: UserAuthResultSuccess,
authTypes: UserAuthTypePassphrase,
expectedMsg: "Unlocked foo (/dev/sda1) successfully with passphrase",
})
}
func (s *authRequestorPlymouthSuite) TestNotifyUserAuthResultSuccessDifferentPath(c *C) {
s.testNotifyUserAuthResult(c, &testPlymouthNotifyUserAuthResultParams{
name: "data",
path: "/dev/nvme0n1p3",
result: UserAuthResultSuccess,
authTypes: UserAuthTypePassphrase,
expectedMsg: "Unlocked data (/dev/nvme0n1p3) successfully with passphrase",
})
}
func (s *authRequestorPlymouthSuite) TestNotifyUserAuthResultSuccessPIN(c *C) {
s.testNotifyUserAuthResult(c, &testPlymouthNotifyUserAuthResultParams{
name: "data",
path: "/dev/sda1",
result: UserAuthResultSuccess,
authTypes: UserAuthTypePIN,
expectedMsg: "Unlocked data (/dev/sda1) successfully with PIN",
})
}
func (s *authRequestorPlymouthSuite) TestNotifyUserAuthResultSuccessRecoveryKey(c *C) {
s.testNotifyUserAuthResult(c, &testPlymouthNotifyUserAuthResultParams{
name: "data",
path: "/dev/sda1",
result: UserAuthResultSuccess,
authTypes: UserAuthTypeRecoveryKey,
expectedMsg: "Unlocked data (/dev/sda1) successfully with recovery key",
})
}
func (s *authRequestorPlymouthSuite) TestNotifyUserAuthResultFailurePassphrase(c *C) {
s.testNotifyUserAuthResult(c, &testPlymouthNotifyUserAuthResultParams{
name: "data",
path: "/dev/sda1",
result: UserAuthResultFailed,
authTypes: UserAuthTypePassphrase,
expectedMsg: "Incorrect passphrase",
})
}
func (s *authRequestorPlymouthSuite) TestNotifyUserAuthResultFailurePIN(c *C) {
s.testNotifyUserAuthResult(c, &testPlymouthNotifyUserAuthResultParams{
name: "data",
path: "/dev/sda1",
result: UserAuthResultFailed,
authTypes: UserAuthTypePIN,
expectedMsg: "Incorrect PIN",
})
}
func (s *authRequestorPlymouthSuite) TestNotifyUserAuthResultFailurePassphraseOrRecoveryKey(c *C) {
s.testNotifyUserAuthResult(c, &testPlymouthNotifyUserAuthResultParams{
name: "data",
path: "/dev/sda1",
result: UserAuthResultFailed,
authTypes: UserAuthTypePassphrase | UserAuthTypeRecoveryKey,
expectedMsg: "Incorrect passphrase or recovery key",
})
}
func (s *authRequestorPlymouthSuite) TestNotifyUserAuthResultFailurePassphraseNoMoreTriesLeft(c *C) {
s.testNotifyUserAuthResult(c, &testPlymouthNotifyUserAuthResultParams{
name: "data",
path: "/dev/sda1",
result: UserAuthResultFailed,
authTypes: UserAuthTypePassphrase,
unavailableAuthTypes: UserAuthTypePassphrase,
expectedMsg: "Incorrect passphrase. No more passphrase tries remaining",
})
}
func (s *authRequestorPlymouthSuite) TestNotifyUserAuthResultFailureRecoveryKeyNoMoreTriesLeft(c *C) {
s.testNotifyUserAuthResult(c, &testPlymouthNotifyUserAuthResultParams{
name: "data",
path: "/dev/sda1",
result: UserAuthResultFailed,
authTypes: UserAuthTypeRecoveryKey,
unavailableAuthTypes: UserAuthTypeRecoveryKey,
expectedMsg: "Incorrect recovery key. No more recovery key tries remaining",
})
}
func (s *authRequestorPlymouthSuite) TestNotifyUserAuthResultInvalidPIN(c *C) {
s.testNotifyUserAuthResult(c, &testPlymouthNotifyUserAuthResultParams{
name: "data",
path: "/dev/sda1",
result: UserAuthResultInvalidFormat,
authTypes: UserAuthTypePIN,
expectedMsg: "Invalid PIN",
})
}
func (s *authRequestorPlymouthSuite) TestNotifyUserAuthResultInvalidRecoveryKey(c *C) {
s.testNotifyUserAuthResult(c, &testPlymouthNotifyUserAuthResultParams{
name: "data",
path: "/dev/sda1",
result: UserAuthResultInvalidFormat,
authTypes: UserAuthTypeRecoveryKey,
expectedMsg: "Invalid recovery key",
})
}
func (s *authRequestorPlymouthSuite) TestNotifyUserAuthResultInvalidPINOrRecoveryKey(c *C) {
s.testNotifyUserAuthResult(c, &testPlymouthNotifyUserAuthResultParams{
name: "data",
path: "/dev/sda1",
result: UserAuthResultInvalidFormat,
authTypes: UserAuthTypePIN | UserAuthTypeRecoveryKey,
expectedMsg: "Invalid PIN or recovery key",
})
}
func (s *authRequestorPlymouthSuite) TestNotifyUserAuthResultNotAvailable(c *C) {
requestor, err := NewPlymouthAuthRequestor(new(mockPlymouthAuthRequestorStringer))
c.Assert(err, IsNil)
s.stopPlymouthd(c)
err = requestor.NotifyUserAuthResult(context.Background(), UserAuthResultSuccess, UserAuthTypePassphrase, 0)
c.Check(err, ErrorMatches, "the auth requestor is not available")
c.Check(errors.Is(err, ErrAuthRequestorNotAvailable), testutil.IsTrue)
}
func (s *authRequestorPlymouthSuite) TestNotifyUserAuthResultObtainMessageError(c *C) {
requestor, err := NewPlymouthAuthRequestor(&mockPlymouthAuthRequestorStringer{
err: errors.New("some error"),
})
c.Assert(err, IsNil)
err = requestor.NotifyUserAuthResult(context.Background(), UserAuthResultSuccess, UserAuthTypePassphrase, 0)
c.Check(err, ErrorMatches, `cannot request message string: some error`)
}
func (s *authRequestorPlymouthSuite) TestNotifyUserAuthResultCanceledContext(c *C) {
requestor := NewPlymouthAuthRequestorForTesting(new(mockPlymouthAuthRequestorStringer), &PlymouthRequestUserCredentialContext{Name: "data", Path: "/dev/sda1"})
ctx, cancel := context.WithCancel(context.Background())
cancel()
err := requestor.NotifyUserAuthResult(ctx, UserAuthResultSuccess, UserAuthTypePassphrase, 0)
c.Check(err, ErrorMatches, "cannot execute plymouth --ping: context canceled")
c.Check(errors.Is(err, context.Canceled), testutil.IsTrue)
}
func (s *authRequestorPlymouthSuite) TestNotifyUserAuthResultFailure(c *C) {
requestor := NewPlymouthAuthRequestorForTesting(new(mockPlymouthAuthRequestorStringer), &PlymouthRequestUserCredentialContext{Name: "data", Path: "/dev/sda1"})
s.makePlymouthDisplayMessageFail(c)
err := requestor.NotifyUserAuthResult(context.Background(), UserAuthResultSuccess, UserAuthTypePassphrase, 0)
c.Check(err, ErrorMatches, "cannot execute plymouth display-message: exit status 1")
}