File: creds_test.go

package info (click to toggle)
git-lfs 3.3.0-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,676 kB
  • sloc: sh: 19,133; makefile: 487; ruby: 228
file content (278 lines) | stat: -rw-r--r-- 7,999 bytes parent folder | download
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
package creds

import (
	"errors"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestCredsBufferProtect(t *testing.T) {
	creds := make(Creds)

	// Always disallow LF characters
	creds["protocol"] = "https"
	creds["host"] = "one.example.com\nhost=two.example.com"

	buf, err := bufferCreds(creds)
	assert.Error(t, err)
	assert.Nil(t, buf)
}

type testCredHelper struct {
	fillErr    error
	approveErr error
	rejectErr  error
	fill       []Creds
	approve    []Creds
	reject     []Creds
}

func newTestCredHelper() *testCredHelper {
	return &testCredHelper{
		fill:    make([]Creds, 0),
		approve: make([]Creds, 0),
		reject:  make([]Creds, 0),
	}
}

func (h *testCredHelper) Fill(input Creds) (Creds, error) {
	h.fill = append(h.fill, input)
	return input, h.fillErr
}

func (h *testCredHelper) Approve(creds Creds) error {
	h.approve = append(h.approve, creds)
	return h.approveErr
}

func (h *testCredHelper) Reject(creds Creds) error {
	h.reject = append(h.reject, creds)
	return h.rejectErr
}

func TestCredHelperSetNoErrors(t *testing.T) {
	cache := NewCredentialCacher()
	helper1 := newTestCredHelper()
	helper2 := newTestCredHelper()
	helpers := NewCredentialHelpers([]CredentialHelper{cache, helper1, helper2})
	creds := Creds{"protocol": "https", "host": "example.com"}

	out, err := helpers.Fill(creds)
	assert.Nil(t, err)
	assert.Equal(t, creds, out)
	assert.Equal(t, 1, len(helper1.fill))
	assert.Equal(t, 0, len(helper2.fill))

	// calling Fill() with empty cache
	out, err = helpers.Fill(creds)
	assert.Nil(t, err)
	assert.Equal(t, creds, out)
	assert.Equal(t, 2, len(helper1.fill))
	assert.Equal(t, 0, len(helper2.fill))

	credsWithPass := Creds{"protocol": "https", "host": "example.com", "username": "foo", "password": "bar"}
	assert.Nil(t, helpers.Approve(credsWithPass))
	assert.Equal(t, 1, len(helper1.approve))
	assert.Equal(t, 0, len(helper2.approve))

	// calling Approve() again is cached
	assert.Nil(t, helpers.Approve(credsWithPass))
	assert.Equal(t, 1, len(helper1.approve))
	assert.Equal(t, 0, len(helper2.approve))

	// access cache
	for i := 0; i < 3; i++ {
		out, err = helpers.Fill(creds)
		assert.Nil(t, err)
		assert.Equal(t, credsWithPass, out)
		assert.Equal(t, 2, len(helper1.fill))
		assert.Equal(t, 0, len(helper2.fill))
	}

	assert.Nil(t, helpers.Reject(creds))
	assert.Equal(t, 1, len(helper1.reject))
	assert.Equal(t, 0, len(helper2.reject))

	// Reject() is never cached
	assert.Nil(t, helpers.Reject(creds))
	assert.Equal(t, 2, len(helper1.reject))
	assert.Equal(t, 0, len(helper2.reject))

	// calling Fill() with empty cache
	out, err = helpers.Fill(creds)
	assert.Nil(t, err)
	assert.Equal(t, creds, out)
	assert.Equal(t, 3, len(helper1.fill))
	assert.Equal(t, 0, len(helper2.fill))
}

func TestCredHelperSetFillError(t *testing.T) {
	cache := NewCredentialCacher()
	helper1 := newTestCredHelper()
	helper2 := newTestCredHelper()
	helpers := NewCredentialHelpers([]CredentialHelper{cache, helper1, helper2})
	creds := Creds{"protocol": "https", "host": "example.com"}

	helper1.fillErr = errors.New("boom")
	out, err := helpers.Fill(creds)
	assert.Nil(t, err)
	assert.Equal(t, creds, out)
	assert.Equal(t, 1, len(helper1.fill))
	assert.Equal(t, 1, len(helper2.fill))

	assert.Nil(t, helpers.Approve(creds))
	assert.Equal(t, 0, len(helper1.approve))
	assert.Equal(t, 1, len(helper2.approve))

	// Fill() with cache
	for i := 0; i < 3; i++ {
		out, err = helpers.Fill(creds)
		assert.Nil(t, err)
		assert.Equal(t, creds, out)
		assert.Equal(t, 1, len(helper1.fill))
		assert.Equal(t, 1, len(helper2.fill))
	}

	assert.Nil(t, helpers.Reject(creds))
	assert.Equal(t, 0, len(helper1.reject))
	assert.Equal(t, 1, len(helper2.reject))

	// Fill() with empty cache
	out, err = helpers.Fill(creds)
	assert.Nil(t, err)
	assert.Equal(t, creds, out)
	assert.Equal(t, 1, len(helper1.fill)) // still skipped
	assert.Equal(t, 2, len(helper2.fill))
}

func TestCredHelperSetApproveError(t *testing.T) {
	cache := NewCredentialCacher()
	helper1 := newTestCredHelper()
	helper2 := newTestCredHelper()
	helpers := NewCredentialHelpers([]CredentialHelper{cache, helper1, helper2})
	creds := Creds{"protocol": "https", "host": "example.com"}

	approveErr := errors.New("boom")
	helper1.approveErr = approveErr
	out, err := helpers.Fill(creds)
	assert.Nil(t, err)
	assert.Equal(t, creds, out)
	assert.Equal(t, 1, len(helper1.fill))
	assert.Equal(t, 0, len(helper2.fill))

	assert.Equal(t, approveErr, helpers.Approve(creds))
	assert.Equal(t, 1, len(helper1.approve))
	assert.Equal(t, 0, len(helper2.approve))

	// cache is never set
	out, err = helpers.Fill(creds)
	assert.Nil(t, err)
	assert.Equal(t, creds, out)
	assert.Equal(t, 2, len(helper1.fill))
	assert.Equal(t, 0, len(helper2.fill))

	assert.Nil(t, helpers.Reject(creds))
	assert.Equal(t, 1, len(helper1.reject))
	assert.Equal(t, 0, len(helper2.reject))
}

func TestCredHelperSetFillAndApproveError(t *testing.T) {
	cache := NewCredentialCacher()
	helper1 := newTestCredHelper()
	helper2 := newTestCredHelper()
	helpers := NewCredentialHelpers([]CredentialHelper{cache, helper1, helper2})
	creds := Creds{"protocol": "https", "host": "example.com"}

	credErr := errors.New("boom")
	helper1.fillErr = credErr
	helper2.approveErr = credErr

	out, err := helpers.Fill(creds)
	assert.Nil(t, err)
	assert.Equal(t, creds, out)
	assert.Equal(t, 1, len(helper1.fill))
	assert.Equal(t, 1, len(helper2.fill))

	assert.Equal(t, credErr, helpers.Approve(creds))
	assert.Equal(t, 0, len(helper1.approve)) // skipped
	assert.Equal(t, 0, len(helper1.reject))  // skipped
	assert.Equal(t, 1, len(helper2.approve))

	// never approved, so cache is empty
	out, err = helpers.Fill(creds)
	assert.Nil(t, err)
	assert.Equal(t, creds, out)
	assert.Equal(t, 1, len(helper1.fill)) // still skipped
	assert.Equal(t, 2, len(helper2.fill))
}

func TestCredHelperSetRejectError(t *testing.T) {
	cache := NewCredentialCacher()
	helper1 := newTestCredHelper()
	helper2 := newTestCredHelper()
	helpers := NewCredentialHelpers([]CredentialHelper{cache, helper1, helper2})
	creds := Creds{"protocol": "https", "host": "example.com"}

	rejectErr := errors.New("boom")
	helper1.rejectErr = rejectErr
	out, err := helpers.Fill(creds)
	assert.Nil(t, err)
	assert.Equal(t, creds, out)
	assert.Equal(t, 1, len(helper1.fill))
	assert.Equal(t, 0, len(helper2.fill))

	assert.Nil(t, helpers.Approve(creds))
	assert.Equal(t, 1, len(helper1.approve))
	assert.Equal(t, 0, len(helper2.approve))

	// Fill() with cache
	out, err = helpers.Fill(creds)
	assert.Nil(t, err)
	assert.Equal(t, creds, out)
	assert.Equal(t, 1, len(helper1.fill))
	assert.Equal(t, 0, len(helper2.fill))

	assert.Equal(t, rejectErr, helpers.Reject(creds))
	assert.Equal(t, 1, len(helper1.reject))
	assert.Equal(t, 0, len(helper2.reject))

	// failed Reject() still clears cache
	out, err = helpers.Fill(creds)
	assert.Nil(t, err)
	assert.Equal(t, creds, out)
	assert.Equal(t, 2, len(helper1.fill))
	assert.Equal(t, 0, len(helper2.fill))
}

func TestCredHelperSetAllFillErrors(t *testing.T) {
	cache := NewCredentialCacher()
	helper1 := newTestCredHelper()
	helper2 := newTestCredHelper()
	helpers := NewCredentialHelpers([]CredentialHelper{cache, helper1, helper2})
	creds := Creds{"protocol": "https", "host": "example.com"}

	helper1.fillErr = errors.New("boom 1")
	helper2.fillErr = errors.New("boom 2")
	out, err := helpers.Fill(creds)
	if assert.NotNil(t, err) {
		assert.Equal(t, "credential fill errors:\nboom 1\nboom 2", err.Error())
	}
	assert.Nil(t, out)
	assert.Equal(t, 1, len(helper1.fill))
	assert.Equal(t, 1, len(helper2.fill))

	err = helpers.Approve(creds)
	if assert.NotNil(t, err) {
		assert.Equal(t, "no valid credential helpers to approve", err.Error())
	}
	assert.Equal(t, 0, len(helper1.approve))
	assert.Equal(t, 0, len(helper2.approve))

	err = helpers.Reject(creds)
	if assert.NotNil(t, err) {
		assert.Equal(t, "no valid credential helpers to reject", err.Error())
	}
	assert.Equal(t, 0, len(helper1.reject))
	assert.Equal(t, 0, len(helper2.reject))
}