-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathpinentry_test.go
169 lines (134 loc) · 4.58 KB
/
pinentry_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
161
162
163
164
165
166
167
168
169
// Copyright (c) 2021 Jorge Luis Betancourt. All rights reserved.
// Use of this source code is governed by the Apache License, Version 2.0
// that can be found in the LICENSE file.
package main
import (
"io/ioutil"
"log"
"testing"
"github.com/foxcpp/go-assuan/pinentry"
"github.com/keybase/go-keychain"
)
const (
emptyPassword = ""
testPassword = "toomanysecrets2"
keyDesc = `Please enter the passphrase to unlock the OpenPGP secret key:
"Firstname Lastname <test@email.com>"
2048-bit RSA key, ID 61AF059BD632F971,
created 2021-01-01 (main key ID 70D56DF4CA30DE16).
`
keyInfo = "n/8043823CBC5C5A0C66866520F333076D"
)
var (
failedAuthFn = func(reason string) (bool, error) { return false, nil }
successfulAuthFn = func(reason string) (bool, error) { return true, nil }
dummyPrompt = func(s pinentry.Settings) ([]byte, error) { return []byte{}, nil }
)
func TestStoreEntryInKeychain(t *testing.T) {
err := storePasswordInKeychain("sampleLabel", "keyInfo", []byte(testPassword))
if err != nil {
t.Fatalf("storing entry in the Keychain should succeed: %s", err)
}
}
func TestGetPasswordFromKeychain(t *testing.T) {
defer func() {
err := cleanKeychain("sampleLabel")
if err != nil {
t.Fatalf("failed to clear entry from Keychain: %s", err)
}
}()
pass, err := passwordFromKeychain("sampleLabel")
if err != nil {
t.Fatalf("fetch entry from Keychain should succeed: %s", err)
}
if pass != testPassword {
t.Fatalf("password mismatch got: %s want: %s", pass, testPassword)
}
}
func TestGetPINSuccessfulAuthentication(t *testing.T) {
keychainLabel := `Firstname Lastname <test@email.com> (61AF059BD632F971)`
defer func() { _ = cleanKeychain(keychainLabel) }()
params := pinentry.Settings{
Desc: keyDesc,
KeyInfo: keyInfo,
}
err := storePasswordInKeychain(keychainLabel, keyInfo, []byte(testPassword))
if err != nil {
t.Fatalf("failed precreating entry in the Keychain: %s", err)
}
logger := &log.Logger{}
logger.SetOutput(ioutil.Discard)
fn := GetPIN(successfulAuthFn, dummyPrompt, logger)
pass, pinErr := fn(params)
if pinErr != nil {
t.Fatalf("call to GetPIN should succeed: %s", err)
}
if pass != testPassword {
t.Fatalf("password mismatch got: %s want: %s", pass, testPassword)
}
}
func TestGetPINUnsuccessfulAuthentication(t *testing.T) {
keychainLabel := `Firstname Lastname <test@email.com> (61AF059BD632F971)`
defer func() { _ = cleanKeychain(keychainLabel) }()
logger := log.New(ioutil.Discard, "", 0)
params := pinentry.Settings{
Desc: keyDesc,
KeyInfo: keyInfo,
}
err := storePasswordInKeychain(keychainLabel, keyInfo, []byte(testPassword))
if err != nil {
t.Fatalf("failed precreating entry in the Keychain: %s", err)
}
fn := GetPIN(failedAuthFn, dummyPrompt, logger)
pass, pinErr := fn(params)
if pinErr != nil {
t.Fatalf("call to GetPIN should succeed: %s", pinErr)
}
if pass != emptyPassword {
t.Fatalf("password mismatch got: %s want: %s", pass, testPassword)
}
}
func TestEntryNotInKeychain(t *testing.T) {
keychainLabel := `Firstname Lastname <test@email.com> (61AF059BD632F971)`
defer func() { _ = cleanKeychain(keychainLabel) }()
logger := log.New(ioutil.Discard, "", 0)
params := pinentry.Settings{
Desc: keyDesc,
KeyInfo: keyInfo,
}
// initially the entry for the test key is not in the keychain
if pass, err := passwordFromKeychain(keychainLabel); err == nil || pass != "" {
t.Fatalf("unexpected entry found in the keychain: %s", keychainLabel)
}
fallBack := false
validPinFn := func(s pinentry.Settings) ([]byte, error) {
fallBack = true
return []byte(testPassword), nil
}
fn := GetPIN(successfulAuthFn, validPinFn, logger)
pass, pinErr := fn(params)
if pinErr != nil {
t.Fatalf("call to GetPIN should succeed: %s", pinErr)
}
if !fallBack {
t.Fatalf("the fallback password prompt should have been called")
}
if pass != testPassword {
t.Fatalf("password mismatch got: %s want: %s", pass, testPassword)
}
// after the successful run of GetPIN the entry should be present in the keychain
if pass, err := passwordFromKeychain(keychainLabel); err != nil || pass == "" {
t.Fatalf("missing entry from the keychain: %s", keychainLabel)
}
}
// Removes a matching entry from the "main" keychain.
// Since the item gets added in the same process, i.e temporal build while executing the test, it
// shouldn't request the password from the user.
func cleanKeychain(label string) error {
query := keychain.NewItem()
query.SetSecClass(keychain.SecClassGenericPassword)
query.SetLabel(label)
query.SetMatchLimit(keychain.MatchLimitOne)
query.SetReturnData(true)
return keychain.DeleteItem(query)
}