-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcodesign_test.go
393 lines (377 loc) · 11.7 KB
/
codesign_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
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
package codesign
import (
"errors"
"os"
"path/filepath"
"reflect"
"testing"
"time"
"github.com/bitrise-io/go-steputils/v2/stepconf"
"github.com/bitrise-io/go-utils/v2/log"
"github.com/bitrise-io/go-xcode/certificateutil"
"github.com/bitrise-io/go-xcode/v2/autocodesign"
"github.com/bitrise-io/go-xcode/v2/codesign/mocks"
"github.com/bitrise-io/go-xcode/v2/devportalservice"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
func Test_manager_selectCodeSigningStrategy(t *testing.T) {
tests := []struct {
name string
project DetailsProvider
credentials devportalservice.Credentials
XcodeMajorVersion int
minDaysProfileValidity int
want codeSigningStrategy
wantErr bool
}{
{
name: "Apple ID",
credentials: devportalservice.Credentials{
AppleID: &devportalservice.AppleID{},
},
project: newMockProject(false, nil),
want: codeSigningBitriseAppleID,
},
{
name: "API Key, Xcode 12",
credentials: devportalservice.Credentials{
APIKey: &devportalservice.APIKeyConnection{},
},
XcodeMajorVersion: 12,
project: newMockProject(false, nil),
want: codeSigningBitriseAPIKey,
},
{
name: "API Key, Xcode 13, Manual signing",
credentials: devportalservice.Credentials{
APIKey: &devportalservice.APIKeyConnection{},
},
XcodeMajorVersion: 13,
project: newMockProject(false, nil),
want: codeSigningBitriseAPIKey,
},
{
name: "API Key, Xcode 13, Xcode managed signing, custom features",
credentials: devportalservice.Credentials{
APIKey: &devportalservice.APIKeyConnection{},
},
XcodeMajorVersion: 13,
project: newMockProject(true, nil),
want: codeSigningXcode,
},
{
name: "API Key, Xcode 13, Xcode managed signing, no custom features",
credentials: devportalservice.Credentials{
APIKey: &devportalservice.APIKeyConnection{},
},
XcodeMajorVersion: 13,
minDaysProfileValidity: 5,
project: newMockProject(true, nil),
want: codeSigningBitriseAPIKey,
},
{
name: "API Key, Xcode 13, can not determine if project automtic",
credentials: devportalservice.Credentials{
APIKey: &devportalservice.APIKeyConnection{},
},
XcodeMajorVersion: 13,
project: newMockProject(true, errors.New("")),
want: codeSigningBitriseAPIKey,
wantErr: true,
},
{
name: "Enterprise API Key",
credentials: devportalservice.Credentials{
APIKey: &devportalservice.APIKeyConnection{
EnterpriseAccount: true,
},
},
XcodeMajorVersion: 16,
project: newMockProject(true, nil),
want: codeSigningBitriseAPIKey,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := &Manager{
detailsProvider: tt.project,
opts: Opts{
XcodeMajorVersion: tt.XcodeMajorVersion,
ShouldConsiderXcodeSigning: true,
MinDaysProfileValidity: tt.minDaysProfileValidity,
},
}
got, _, err := m.selectCodeSigningStrategy(tt.credentials)
if (err != nil) != tt.wantErr {
t.Errorf("manager.selectCodeSigningStrategy() error = %v, wantErr %v", err, tt.wantErr)
return
}
require.Equal(t, tt.want, got)
})
}
}
func newMockProject(isAutoSign bool, mockErr error) DetailsProvider {
mockProjectHelper := new(mocks.DetailsProvider)
mockProjectHelper.On("IsSigningManagedAutomatically", mock.Anything).Return(isAutoSign, mockErr)
return mockProjectHelper
}
func TestManager_checkXcodeManagedCertificates(t *testing.T) {
devCert := generateCert(t, "Apple Development: test")
distCert := generateCert(t, "Apple Distribution: test")
tests := []struct {
name string
distributionMethod autocodesign.DistributionType
certificates []certificateutil.CertificateInfoModel
wantErr bool
}{
{
name: "no certs uploaded, development",
distributionMethod: autocodesign.Development,
certificates: []certificateutil.CertificateInfoModel{},
wantErr: true,
},
{
name: "development, no matching cert",
distributionMethod: autocodesign.Development,
certificates: []certificateutil.CertificateInfoModel{
distCert,
},
wantErr: true,
},
{
name: "no certs uploaded, distribution",
distributionMethod: autocodesign.AppStore,
certificates: []certificateutil.CertificateInfoModel{},
},
{
name: "1 certs uploaded, development",
distributionMethod: autocodesign.Development,
certificates: []certificateutil.CertificateInfoModel{
devCert,
},
},
{
name: "1 certs uploaded, distribution",
distributionMethod: autocodesign.AdHoc,
certificates: []certificateutil.CertificateInfoModel{
distCert,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := &Manager{
opts: Opts{
ExportMethod: tt.distributionMethod,
},
logger: log.NewLogger(),
}
if err := m.validateCertificatesForXcodeManagedSigning(tt.certificates); (err != nil) != tt.wantErr {
t.Errorf("Manager.downloadAndInstallCertificates() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func generateCert(t *testing.T, commonName string) certificateutil.CertificateInfoModel {
const (
teamID = "MYTEAMID"
teamName = "BITFALL FEJLESZTO KORLATOLT FELELOSSEGU TARSASAG"
)
expiry := time.Now().AddDate(1, 0, 0)
cert, privateKey, err := certificateutil.GenerateTestCertificate(int64(1), teamID, teamName, commonName, expiry)
if err != nil {
t.Fatalf("init: failed to generate certificate: %s", err)
}
return certificateutil.NewCertificateInfo(*cert, privateKey)
}
func TestSelectConnectionCredentials(t *testing.T) {
testAPIKeyConnection := devportalservice.APIKeyConnection{
KeyID: "TestKeyID",
IssuerID: "TestIssuerID",
PrivateKey: "test private key contents",
}
testAppleIDConnection := devportalservice.AppleIDConnection{
AppleID: "test@bitrise.io",
Password: "testpw",
AppSpecificPassword: "testapppw",
SessionExpiryDate: nil,
SessionCookies: nil,
}
localKeyPath := filepath.Join(t.TempDir(), "key.p8")
err := os.WriteFile(localKeyPath, []byte("private key contents"), 0700)
if err != nil {
t.Fatal(err.Error())
}
testInputs := ConnectionOverrideInputs{
APIKeyPath: stepconf.Secret(localKeyPath),
APIKeyID: "TestKeyIDFromInput",
APIKeyIssuerID: "TestKeyIssuerIDFromInput",
}
testNoInputs := ConnectionOverrideInputs{}
tests := []struct {
name string
authType AuthType
bitriseConnection *devportalservice.AppleDeveloperConnection
inputs ConnectionOverrideInputs
want devportalservice.Credentials
wantErr bool
}{
{
name: "API key auth with nil Bitrise connection",
authType: APIKeyAuth,
bitriseConnection: nil,
inputs: testNoInputs,
wantErr: true,
},
{
name: "API key auth type with valid Bitrise connection",
authType: APIKeyAuth,
bitriseConnection: &devportalservice.AppleDeveloperConnection{
AppleIDConnection: nil,
APIKeyConnection: &testAPIKeyConnection,
TestDevices: []devportalservice.TestDevice{},
DuplicatedTestDevices: []devportalservice.TestDevice{},
},
inputs: testNoInputs,
want: devportalservice.Credentials{
AppleID: nil,
APIKey: &testAPIKeyConnection,
},
},
{
name: "API key auth type without valid Bitrise connection",
authType: APIKeyAuth,
bitriseConnection: &devportalservice.AppleDeveloperConnection{
AppleIDConnection: nil,
APIKeyConnection: nil,
TestDevices: nil,
DuplicatedTestDevices: nil,
},
inputs: testNoInputs,
wantErr: true,
},
{
name: "API key auth type without valid Bitrise connection but input overrides",
authType: APIKeyAuth,
bitriseConnection: &devportalservice.AppleDeveloperConnection{
AppleIDConnection: nil,
APIKeyConnection: nil,
TestDevices: nil,
DuplicatedTestDevices: nil,
},
inputs: testInputs,
want: devportalservice.Credentials{
AppleID: nil,
APIKey: &devportalservice.APIKeyConnection{
KeyID: "TestKeyIDFromInput",
IssuerID: "TestKeyIssuerIDFromInput",
PrivateKey: "private key contents",
},
},
},
{
name: "API key auth type with valid Bitrise connection and input overrides",
authType: APIKeyAuth,
bitriseConnection: &devportalservice.AppleDeveloperConnection{
AppleIDConnection: nil,
APIKeyConnection: &testAPIKeyConnection,
TestDevices: []devportalservice.TestDevice{},
DuplicatedTestDevices: []devportalservice.TestDevice{},
},
inputs: testInputs,
want: devportalservice.Credentials{
AppleID: nil,
APIKey: &devportalservice.APIKeyConnection{
KeyID: "TestKeyIDFromInput",
IssuerID: "TestKeyIssuerIDFromInput",
PrivateKey: "private key contents",
},
},
},
{
name: "Apple ID auth type with nil Bitrise connection",
authType: AppleIDAuth,
bitriseConnection: nil,
inputs: testNoInputs,
wantErr: true,
},
{
name: "Apple ID auth type without valid Bitrise connection and input overrides for API key params",
authType: AppleIDAuth,
bitriseConnection: &devportalservice.AppleDeveloperConnection{
AppleIDConnection: nil,
APIKeyConnection: nil,
TestDevices: nil,
DuplicatedTestDevices: nil,
},
inputs: testInputs,
wantErr: true,
},
{
name: "Apple ID auth type with valid Bitrise connection and input overrides for API key params",
authType: AppleIDAuth,
bitriseConnection: &devportalservice.AppleDeveloperConnection{
AppleIDConnection: &testAppleIDConnection,
APIKeyConnection: nil,
TestDevices: nil,
DuplicatedTestDevices: nil,
},
inputs: testInputs,
want: devportalservice.Credentials{
AppleID: &devportalservice.AppleID{
Username: "test@bitrise.io",
Password: "testpw",
Session: "",
AppSpecificPassword: "testapppw",
},
APIKey: nil,
},
},
{
name: "Apple ID auth type with valid Bitrise connection",
authType: AppleIDAuth,
bitriseConnection: &devportalservice.AppleDeveloperConnection{
AppleIDConnection: &testAppleIDConnection,
APIKeyConnection: &testAPIKeyConnection,
TestDevices: nil,
DuplicatedTestDevices: nil,
},
inputs: testNoInputs,
want: devportalservice.Credentials{
AppleID: &devportalservice.AppleID{
Username: "test@bitrise.io",
Password: "testpw",
Session: "",
AppSpecificPassword: "testapppw",
},
APIKey: nil,
},
},
{
name: "Apple ID auth type without valid Bitrise connection",
authType: AppleIDAuth,
bitriseConnection: &devportalservice.AppleDeveloperConnection{
AppleIDConnection: nil,
APIKeyConnection: nil,
TestDevices: nil,
DuplicatedTestDevices: nil,
},
inputs: testNoInputs,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := SelectConnectionCredentials(tt.authType, tt.bitriseConnection, tt.inputs, log.NewLogger())
if (err != nil) != tt.wantErr {
t.Errorf("SelectConnectionCredentials() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("SelectConnectionCredentials() got = %v, want %v", got, tt.want)
}
})
}
}