-
Notifications
You must be signed in to change notification settings - Fork 0
/
pin_test.go
60 lines (45 loc) · 1.4 KB
/
pin_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
// SPDX-FileCopyrightText: 2023 Steffen Vogel <[email protected]>
// SPDX-License-Identifier: Apache-2.0
package ykoath_test
import (
"testing"
"github.com/stretchr/testify/require"
"cunicu.li/go-ykoath/v2"
)
func TestPIN(t *testing.T) {
withCard(t, nil, func(t *testing.T, card *ykoath.Card) {
require := require.New(t)
// Validate should fail if not PIN is set
err := card.Validate([]byte("1234"))
require.ErrorIs(err, ykoath.ErrNoSuchObject)
// Select applet again
sel, err := card.Select()
require.NoError(err)
require.Nil(sel.Challenge)
require.Nil(sel.Algorithm)
// Set PIN
err = card.SetCode([]byte("1338"), ykoath.HmacSha256)
require.NoError(err)
// Reset card to clear authenticated state
// err = test.ResetCard(card.Card)
// require.NoError(err)
// Select applet again
sel, err = card.Select()
require.NoError(err)
require.NotNil(sel.Challenge)
require.Len(sel.Algorithm, 1)
require.Equal(ykoath.Algorithm(sel.Algorithm[0]), ykoath.HmacSha256)
// RemoveCode should fail as we are not authenticated yet
// err = card.RemoveCode()
// require.ErrorIs(err, ykoath.ErrAuthRequired)
// Test invalid PIN
err = card.Validate([]byte("1337"))
require.ErrorIs(err, ykoath.ErrWrongSyntax)
// Test valid PIN
err = card.Validate([]byte("1338"))
require.NoError(err)
// RemoveCode should succeed now
err = card.RemoveCode()
require.NoError(err)
})
}