This repository has been archived by the owner on Jan 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
updater_test.go
798 lines (651 loc) · 25.8 KB
/
updater_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
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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package updater
import (
"fmt"
"io"
"net/http"
"time"
"net/http/httptest"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/keybase/go-logging"
"github.com/keybase/go-updater/saltpack"
"github.com/keybase/go-updater/util"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var testLog = &logging.Logger{Module: "test"}
var testZipPath string
var testAppStatePath = filepath.Join(os.TempDir(), "KBTest_app_state.json")
const (
// shasum -a 256 test/test.zip
validDigest = "54970995e4d02da631e0634162ef66e2663e0eee7d018e816ac48ed6f7811c84"
// keybase sign -d -i test.zip
validSignature = `BEGIN KEYBASE SALTPACK DETACHED SIGNATURE.
kXR7VktZdyH7rvq v5weRa8moXPeKBe e2YLT0PnyHzCrVi RbC1J5uJtYgYyLW eGg4qzsWqkb7hcX
GTVc0vsEUVwBCly qhPdOL0mE19kfxg A4fMqpNGNTY0jtO iMpjwwuIyLBxkCC jHzMiJFskzluz2S
otWUI0nTu2vG2Fx Mgeyqm20Ug8j7Bi N. END KEYBASE SALTPACK DETACHED SIGNATURE.`
invalidDigest = "74970995e4d02da631e0634162ef66e2663e0eee7d018e816ac48ed6f7811c84"
invalidSignature = `BEGIN KEYBASE SALTPACK DETACHED SIGNATURE.
QXR7VktZdyH7rvq v5wcIkPOwDJ1n11 M8RnkLKQGO2f3Bb fzCeMYz4S6oxLAy
Cco4N255JFzv2PX E6WWdobANV4guJI iEE8XJb6uudCX4x QWZfnamVAaZpXuW
vdz65rE7oZsLSdW oxMsbBgG9NVpSJy x3CD6LaC9GlZ4IS ofzkHe401mHjr7M M. END
KEYBASE SALTPACK DETACHED SIGNATURE.`
)
func makeKeybaseUpdateTempDir(t *testing.T, updater *Updater, testAsset *Asset) (tmpDir string) {
// This creates a real KebyaseUpdater.[ID] directory in os.TempDir
// Then we download the test zip to this directory from testServer
tmpDir, err := util.MakeTempDir("KeybaseUpdater.", 0700)
require.NoError(t, err)
err = updater.downloadAsset(testAsset, tmpDir, UpdateOptions{})
require.NoError(t, err)
return tmpDir
}
func init() {
_, filename, _, _ := runtime.Caller(0)
testZipPath = filepath.Join(filepath.Dir(filename), "test/test.zip")
}
func newTestUpdater(t *testing.T) (*Updater, error) {
return newTestUpdaterWithServer(t, nil, nil, &testConfig{})
}
func newTestUpdaterWithServer(t *testing.T, testServer *httptest.Server, update *Update, config Config) (*Updater, error) {
return NewUpdater(testUpdateSource{testServer: testServer, config: config, update: update}, config, testLog), nil
}
func newTestContext(options UpdateOptions, cfg Config, response *UpdatePromptResponse) *testUpdateUI {
return &testUpdateUI{options: options, cfg: cfg, response: response}
}
type testUpdateUI struct {
options UpdateOptions
cfg Config
response *UpdatePromptResponse
promptErr error
verifyErr error
beforeApplyErr error
afterApplyErr error
errReported error
actionReported UpdateAction
autoUpdateReported bool
updateReported *Update
successReported bool
isCheckCommand bool
}
func (u testUpdateUI) BeforeUpdatePrompt(_ Update, _ UpdateOptions) error {
return nil
}
func (u testUpdateUI) UpdatePrompt(_ Update, _ UpdateOptions, _ UpdatePromptOptions) (*UpdatePromptResponse, error) {
if u.promptErr != nil {
return nil, u.promptErr
}
return u.response, nil
}
func (u testUpdateUI) BeforeApply(update Update) error {
return u.beforeApplyErr
}
func (u testUpdateUI) Apply(update Update, options UpdateOptions, tmpDir string) error {
return nil
}
func (u testUpdateUI) AfterApply(update Update) error {
return u.afterApplyErr
}
func (u testUpdateUI) GetUpdateUI() UpdateUI {
return u
}
func (u testUpdateUI) Verify(update Update) error {
if u.verifyErr != nil {
return u.verifyErr
}
var validCodeSigningKIDs = map[string]bool{
"0120d7539e27e83a9c8caf8701199c6985c0a96801ff7cb69456e9b3a8a8446c66080a": true, // joshblum (saltine)
}
return saltpack.VerifyDetachedFileAtPath(update.Asset.LocalPath, update.Asset.Signature, validCodeSigningKIDs, testLog)
}
func (u *testUpdateUI) ReportError(err error, update *Update, options UpdateOptions) {
u.errReported = err
}
func (u *testUpdateUI) ReportAction(actionResponse UpdatePromptResponse, update *Update, options UpdateOptions) {
u.actionReported = actionResponse.Action
autoUpdate, _ := u.cfg.GetUpdateAuto()
u.autoUpdateReported = autoUpdate
u.updateReported = update
}
func (u *testUpdateUI) ReportSuccess(update *Update, options UpdateOptions) {
u.successReported = true
u.updateReported = update
}
func (u *testUpdateUI) AfterUpdateCheck(update *Update) {}
func (u testUpdateUI) UpdateOptions() UpdateOptions {
return u.options
}
func (u testUpdateUI) GetAppStatePath() string {
return testAppStatePath
}
func (u testUpdateUI) IsCheckCommand() bool {
return u.isCheckCommand
}
func (u testUpdateUI) DeepClean() {}
type testUpdateSource struct {
testServer *httptest.Server
config Config
update *Update
findErr error
}
func (u testUpdateSource) Description() string {
return "Test"
}
func testUpdate(uri string) *Update {
return newTestUpdate(uri, true)
}
func newTestUpdate(uri string, needUpdate bool) *Update {
update := &Update{
Version: "1.0.1",
Name: "Test",
Description: "Bug fixes",
InstallID: "deadbeef",
RequestID: "cafedead",
NeedUpdate: needUpdate,
}
if uri != "" {
update.Asset = &Asset{
Name: "test.zip",
URL: uri,
Digest: validDigest,
Signature: validSignature,
}
}
return update
}
func (u testUpdateSource) FindUpdate(options UpdateOptions) (*Update, error) {
return u.update, u.findErr
}
type testConfig struct {
auto bool
autoSet bool
autoOverride bool
installID string
err error
}
func (c testConfig) GetUpdateAuto() (bool, bool) {
return c.auto, c.autoSet
}
func (c *testConfig) SetUpdateAuto(b bool) error {
c.auto = b
c.autoSet = true
return c.err
}
func (c *testConfig) IsLastUpdateCheckTimeRecent(d time.Duration) bool {
return true
}
func (c *testConfig) SetLastUpdateCheckTime() {
}
// For overriding the current Auto setting
func (c testConfig) GetUpdateAutoOverride() bool {
return c.autoOverride
}
func (c *testConfig) SetUpdateAutoOverride(auto bool) error {
c.autoOverride = auto
return nil
}
func (c testConfig) GetInstallID() string {
return c.installID
}
func (c *testConfig) SetInstallID(s string) error {
c.installID = s
return c.err
}
func (c testConfig) GetLastAppliedVersion() string {
return ""
}
func (c *testConfig) SetLastAppliedVersion(version string) error {
return nil
}
func newDefaultTestUpdateOptions() UpdateOptions {
return UpdateOptions{
Version: "1.0.0",
Platform: runtime.GOOS,
DestinationPath: filepath.Join(os.TempDir(), "Test"),
}
}
func testServerForUpdateFile(t *testing.T, path string) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
f, err := os.Open(path)
require.NoError(t, err)
w.Header().Set("Content-Type", "application/zip")
_, err = io.Copy(w, f)
require.NoError(t, err)
}))
}
func testServerForError(t *testing.T, err error) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), 500)
}))
}
func testServerNotFound(t *testing.T) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Not Found", 404)
}))
}
func TestUpdaterApply(t *testing.T) {
testServer := testServerForUpdateFile(t, testZipPath)
defer testServer.Close()
upr, err := newTestUpdaterWithServer(t, testServer, testUpdate(testServer.URL), &testConfig{})
assert.NoError(t, err)
ctx := newTestContext(newDefaultTestUpdateOptions(), upr.config, &UpdatePromptResponse{Action: UpdateActionApply, AutoUpdate: true})
update, err := upr.Update(ctx)
require.NoError(t, err)
require.NotNil(t, update)
t.Logf("Update: %#v\n", *update)
require.NotNil(t, update.Asset)
t.Logf("Asset: %#v\n", *update.Asset)
auto, autoSet := upr.config.GetUpdateAuto()
assert.True(t, auto)
assert.True(t, autoSet)
assert.Equal(t, "deadbeef", upr.config.GetInstallID())
assert.Nil(t, ctx.errReported)
assert.Equal(t, ctx.actionReported, UpdateActionApply)
assert.True(t, ctx.autoUpdateReported)
require.NotNil(t, ctx.updateReported)
assert.Equal(t, "deadbeef", ctx.updateReported.InstallID)
assert.Equal(t, "cafedead", ctx.updateReported.RequestID)
assert.True(t, ctx.successReported)
assert.Equal(t, "apply", UpdateActionApply.String())
}
func TestUpdaterDownloadError(t *testing.T) {
testServer := testServerForError(t, fmt.Errorf("bad response"))
defer testServer.Close()
upr, err := newTestUpdaterWithServer(t, testServer, testUpdate(testServer.URL), &testConfig{})
assert.NoError(t, err)
ctx := newTestContext(newDefaultTestUpdateOptions(), upr.config, &UpdatePromptResponse{Action: UpdateActionApply, AutoUpdate: true})
_, err = upr.Update(ctx)
assert.EqualError(t, err, "Update Error (download): Responded with 500 Internal Server Error")
require.NotNil(t, ctx.errReported)
assert.Equal(t, ctx.errReported.(Error).errorType, DownloadError)
assert.False(t, ctx.successReported)
}
func TestUpdaterCancel(t *testing.T) {
testServer := testServerForUpdateFile(t, testZipPath)
defer testServer.Close()
upr, err := newTestUpdaterWithServer(t, testServer, testUpdate(testServer.URL), &testConfig{})
assert.NoError(t, err)
ctx := newTestContext(newDefaultTestUpdateOptions(), upr.config, &UpdatePromptResponse{Action: UpdateActionCancel, AutoUpdate: true})
_, err = upr.Update(ctx)
assert.EqualError(t, err, "Update Error (cancel): Canceled")
// Don't report error on user cancel
assert.NoError(t, ctx.errReported)
}
func TestUpdaterSnooze(t *testing.T) {
testServer := testServerForUpdateFile(t, testZipPath)
defer testServer.Close()
upr, err := newTestUpdaterWithServer(t, testServer, testUpdate(testServer.URL), &testConfig{})
assert.NoError(t, err)
ctx := newTestContext(newDefaultTestUpdateOptions(), upr.config, &UpdatePromptResponse{Action: UpdateActionSnooze, AutoUpdate: true})
_, err = upr.Update(ctx)
assert.EqualError(t, err, "Update Error (cancel): Snoozed update")
// Don't report error on user snooze
assert.NoError(t, ctx.errReported)
}
func TestUpdaterContinue(t *testing.T) {
testServer := testServerForUpdateFile(t, testZipPath)
defer testServer.Close()
upr, err := newTestUpdaterWithServer(t, testServer, testUpdate(testServer.URL), &testConfig{})
assert.NoError(t, err)
ctx := newTestContext(newDefaultTestUpdateOptions(), upr.config, &UpdatePromptResponse{Action: UpdateActionContinue})
update, err := upr.Update(ctx)
require.NoError(t, err)
require.NotNil(t, update)
require.NotNil(t, update.Asset)
auto, autoSet := upr.config.GetUpdateAuto()
assert.False(t, auto)
assert.False(t, autoSet)
assert.Equal(t, "deadbeef", upr.config.GetInstallID())
assert.Nil(t, ctx.errReported)
assert.Empty(t, string(ctx.actionReported))
assert.False(t, ctx.autoUpdateReported)
require.NotNil(t, ctx.updateReported)
assert.Equal(t, "deadbeef", ctx.updateReported.InstallID)
assert.Equal(t, "cafedead", ctx.updateReported.RequestID)
assert.True(t, ctx.successReported)
}
func TestUpdateNoResponse(t *testing.T) {
testServer := testServerForUpdateFile(t, testZipPath)
defer testServer.Close()
upr, err := newTestUpdaterWithServer(t, testServer, testUpdate(testServer.URL), &testConfig{})
assert.NoError(t, err)
ctx := newTestContext(newDefaultTestUpdateOptions(), upr.config, nil)
_, err = upr.Update(ctx)
assert.EqualError(t, err, "Update Error (prompt): No response")
require.NotNil(t, ctx.errReported)
assert.Equal(t, ctx.errReported.(Error).errorType, PromptError)
assert.False(t, ctx.successReported)
}
func TestUpdateNoAsset(t *testing.T) {
testServer := testServerNotFound(t)
defer testServer.Close()
upr, err := newTestUpdaterWithServer(t, testServer, testUpdate(""), &testConfig{})
assert.NoError(t, err)
ctx := newTestContext(newDefaultTestUpdateOptions(), upr.config, &UpdatePromptResponse{Action: UpdateActionApply, AutoUpdate: true})
update, err := upr.Update(ctx)
assert.NoError(t, err)
assert.Nil(t, update.Asset)
}
func testUpdaterError(t *testing.T, errorType ErrorType) {
testServer := testServerForUpdateFile(t, testZipPath)
defer testServer.Close()
upr, _ := newTestUpdaterWithServer(t, testServer, testUpdate(testServer.URL), &testConfig{})
ctx := newTestContext(newDefaultTestUpdateOptions(), upr.config, &UpdatePromptResponse{Action: UpdateActionApply, AutoUpdate: true})
testErr := fmt.Errorf("Test error")
switch errorType {
case PromptError:
ctx.promptErr = testErr
case VerifyError:
ctx.verifyErr = testErr
}
_, err := upr.Update(ctx)
assert.EqualError(t, err, fmt.Sprintf("Update Error (%s): Test error", errorType.String()))
require.NotNil(t, ctx.errReported)
assert.Equal(t, ctx.errReported.(Error).errorType, errorType)
}
func TestUpdaterErrors(t *testing.T) {
testUpdaterError(t, PromptError)
testUpdaterError(t, VerifyError)
}
func TestUpdaterConfigError(t *testing.T) {
testServer := testServerForUpdateFile(t, testZipPath)
defer testServer.Close()
upr, _ := newTestUpdaterWithServer(t, testServer, testUpdate(testServer.URL), &testConfig{err: fmt.Errorf("Test config error")})
ctx := newTestContext(newDefaultTestUpdateOptions(), upr.config, &UpdatePromptResponse{Action: UpdateActionApply, AutoUpdate: true})
_, err := upr.Update(ctx)
assert.NoError(t, err)
require.NotNil(t, ctx.errReported)
assert.Equal(t, ConfigError, ctx.errReported.(Error).errorType)
}
func TestUpdaterAuto(t *testing.T) {
testServer := testServerForUpdateFile(t, testZipPath)
defer testServer.Close()
upr, _ := newTestUpdaterWithServer(t, testServer, testUpdate(testServer.URL), &testConfig{auto: true, autoSet: true})
ctx := newTestContext(newDefaultTestUpdateOptions(), upr.config, &UpdatePromptResponse{Action: UpdateActionApply, AutoUpdate: true})
_, err := upr.Update(ctx)
assert.NoError(t, err)
assert.Equal(t, UpdateActionAuto, ctx.actionReported)
}
func TestUpdaterDownloadNil(t *testing.T) {
upr, err := newTestUpdater(t)
require.NoError(t, err)
tmpDir, err := util.MakeTempDir("TestUpdaterDownloadNil", 0700)
defer util.RemoveFileAtPath(tmpDir)
require.NoError(t, err)
err = upr.downloadAsset(nil, tmpDir, UpdateOptions{})
assert.EqualError(t, err, "No asset to download")
}
func TestUpdaterApplyError(t *testing.T) {
testServer := testServerForUpdateFile(t, testZipPath)
defer testServer.Close()
upr, _ := newTestUpdaterWithServer(t, testServer, testUpdate(testServer.URL), &testConfig{auto: true, autoSet: true})
ctx := newTestContext(newDefaultTestUpdateOptions(), upr.config, &UpdatePromptResponse{Action: UpdateActionApply, AutoUpdate: true})
ctx.beforeApplyErr = fmt.Errorf("Test before error")
_, err := upr.Update(ctx)
assert.EqualError(t, err, "Update Error (apply): Test before error")
ctx.beforeApplyErr = nil
ctx.afterApplyErr = fmt.Errorf("Test after error")
_, err = upr.Update(ctx)
assert.EqualError(t, err, "Update Error (apply): Test after error")
}
func TestUpdaterNotNeeded(t *testing.T) {
testServer := testServerForUpdateFile(t, testZipPath)
defer testServer.Close()
upr, err := newTestUpdaterWithServer(t, testServer, newTestUpdate(testServer.URL, false), &testConfig{})
assert.NoError(t, err)
ctx := newTestContext(newDefaultTestUpdateOptions(), upr.config, &UpdatePromptResponse{Action: UpdateActionSnooze, AutoUpdate: true})
update, err := upr.Update(ctx)
assert.NoError(t, err)
assert.Nil(t, update)
assert.False(t, ctx.successReported)
assert.Equal(t, "deadbeef", upr.config.GetInstallID())
}
func TestUpdaterCheckAndUpdate(t *testing.T) {
testServer := testServerForUpdateFile(t, testZipPath)
defer testServer.Close()
testUpdate := newTestUpdate(testServer.URL, false)
upr, err := newTestUpdaterWithServer(t, testServer, testUpdate, &testConfig{})
assert.NoError(t, err)
defer func() {
err = upr.CleanupPreviousUpdates()
assert.NoError(t, err)
}()
ctx := newTestContext(newDefaultTestUpdateOptions(), upr.config, &UpdatePromptResponse{Action: UpdateActionSnooze, AutoUpdate: true})
// 1.No update from the server
// Need update = false
// FindDownloadedAsset = false
// return updateAvailable = false, updateWasDownloaded = false
updateAvailable, updateWasDownloaded, err := upr.CheckAndDownload(ctx)
assert.NoError(t, err)
assert.False(t, updateAvailable)
assert.False(t, updateWasDownloaded)
assert.False(t, ctx.successReported)
assert.Equal(t, "deadbeef", upr.config.GetInstallID())
// 2. Download asset from URL
// Need update = true
testUpdate.NeedUpdate = true
updateAvailable, updateWasDownloaded, err = upr.CheckAndDownload(ctx)
assert.NoError(t, err)
assert.True(t, updateAvailable)
assert.True(t, updateWasDownloaded)
assert.False(t, ctx.successReported)
assert.Equal(t, "deadbeef", upr.config.GetInstallID())
// 3.Find existing downloaded asset
// Need update = true
// FindDownloadedAsset = true
// return updateAvailable = true, updateWasDownloaded = true
tmpDir := makeKeybaseUpdateTempDir(t, upr, testUpdate.Asset)
updateAvailable, updateWasDownloaded, err = upr.CheckAndDownload(ctx)
assert.NoError(t, err)
assert.True(t, updateAvailable)
assert.False(t, updateWasDownloaded)
assert.False(t, ctx.successReported)
assert.Equal(t, "deadbeef", upr.config.GetInstallID())
// Run it again to ensure we don't accidentally download again
updateAvailable, updateWasDownloaded, err = upr.CheckAndDownload(ctx)
assert.NoError(t, err)
assert.True(t, updateAvailable)
assert.False(t, updateWasDownloaded)
assert.False(t, ctx.successReported)
assert.Equal(t, "deadbeef", upr.config.GetInstallID())
util.RemoveFileAtPath(tmpDir)
// 4.Verify fails b.c. bit flip
// Need update = true
// FindDownloadedAsset = true
// return updateAvailable = false, updateWasDownloaded = false
tmpDir = makeKeybaseUpdateTempDir(t, upr, testUpdate.Asset)
testUpdate.Asset.Signature = invalidSignature
updateAvailable, updateWasDownloaded, err = upr.CheckAndDownload(ctx)
assert.EqualError(t, err, "Update Error (verify): error verifying signature: failed to read header bytes")
assert.False(t, updateAvailable)
assert.False(t, updateWasDownloaded)
assert.False(t, ctx.successReported)
assert.Equal(t, "deadbeef", upr.config.GetInstallID())
util.RemoveFileAtPath(tmpDir)
testUpdate.Asset.Signature = validSignature
// 5.Digest fails b.c. bit flip
// Need update = true
// FindDownloadedAsset = true
// return updateAvailable = false, updateWasDownloaded = false
tmpDir = makeKeybaseUpdateTempDir(t, upr, testUpdate.Asset)
testUpdate.Asset.Digest = invalidDigest
updateAvailable, updateWasDownloaded, err = upr.CheckAndDownload(ctx)
assert.EqualError(t, err, fmt.Sprintf("Update Error (verify): Invalid digest: 54970995e4d02da631e0634162ef66e2663e0eee7d018e816ac48ed6f7811c84 != 74970995e4d02da631e0634162ef66e2663e0eee7d018e816ac48ed6f7811c84 (%s)", filepath.Join(tmpDir, testUpdate.Asset.Name)))
assert.False(t, updateAvailable)
assert.False(t, updateWasDownloaded)
assert.False(t, ctx.successReported)
assert.Equal(t, "deadbeef", upr.config.GetInstallID())
util.RemoveFileAtPath(tmpDir)
testUpdate.Asset.Digest = validDigest
}
func TestApplyDownloaded(t *testing.T) {
testServer := testServerForUpdateFile(t, testZipPath)
defer testServer.Close()
testUpdate := newTestUpdate(testServer.URL, false)
testAsset := *testUpdate.Asset
upr, err := newTestUpdaterWithServer(t, testServer, testUpdate, &testConfig{})
assert.NoError(t, err)
defer func() {
err = upr.CleanupPreviousUpdates()
assert.NoError(t, err)
}()
ctx := newTestContext(newDefaultTestUpdateOptions(), upr.config, &UpdatePromptResponse{Action: UpdateActionSnooze, AutoUpdate: true})
resetCtxErr := func() {
ctx.promptErr = nil
ctx.verifyErr = nil
ctx.beforeApplyErr = nil
ctx.afterApplyErr = nil
ctx.errReported = nil
}
// 1. NeedUpdate = false -> return nil
applied, err := upr.ApplyDownloaded(ctx)
assert.EqualError(t, err, "No previously downloaded update to apply since client is update to date")
assert.False(t, applied)
assert.NotNil(t, ctx.errReported)
assert.Nil(t, ctx.updateReported)
assert.False(t, ctx.successReported)
resetCtxErr()
// 2. Update missing asset
testUpdate.NeedUpdate = true
testUpdate.Asset = nil
applied, err = upr.ApplyDownloaded(ctx)
assert.EqualError(t, err, "Update contained no asset to apply. Update version: 1.0.1")
assert.False(t, applied)
assert.NotNil(t, ctx.errReported)
assert.Nil(t, ctx.updateReported)
assert.False(t, ctx.successReported)
resetCtxErr()
testUpdate.Asset = &testAsset
tempURL := testUpdate.Asset.URL
testUpdate.Asset.URL = ""
applied, err = upr.ApplyDownloaded(ctx)
assert.EqualError(t, err, "Update contained no asset to apply. Update version: 1.0.1")
assert.False(t, applied)
assert.NotNil(t, ctx.errReported)
assert.Nil(t, ctx.updateReported)
assert.False(t, ctx.successReported)
resetCtxErr()
testUpdate.Asset.URL = tempURL
// 3. FindDownloadedAsset = false -> return nil
applied, err = upr.ApplyDownloaded(ctx)
assert.EqualError(t, err, "No downloaded asset found for version: 1.0.1")
assert.False(t, applied)
assert.NotNil(t, ctx.errReported)
assert.Nil(t, ctx.updateReported)
assert.False(t, ctx.successReported)
resetCtxErr()
// 4. FindDownloadedAsset = true -> digest fails
tmpDir := makeKeybaseUpdateTempDir(t, upr, testUpdate.Asset)
testUpdate.Asset.Digest = invalidDigest
applied, err = upr.ApplyDownloaded(ctx)
assert.EqualError(t, err, fmt.Sprintf("Update Error (verify): Invalid digest: 54970995e4d02da631e0634162ef66e2663e0eee7d018e816ac48ed6f7811c84 != 74970995e4d02da631e0634162ef66e2663e0eee7d018e816ac48ed6f7811c84 (%s)", filepath.Join(tmpDir, testUpdate.Asset.Name)))
assert.False(t, applied)
assert.NotNil(t, ctx.errReported)
assert.Nil(t, ctx.updateReported)
assert.False(t, ctx.successReported)
resetCtxErr()
testUpdate.Asset.Digest = validDigest
util.RemoveFileAtPath(tmpDir)
// 5. FindDownloadedAsset = true -> verify fails
tmpDir = makeKeybaseUpdateTempDir(t, upr, testUpdate.Asset)
testUpdate.Asset.Signature = invalidSignature
applied, err = upr.ApplyDownloaded(ctx)
assert.EqualError(t, err, "Update Error (verify): error verifying signature: failed to read header bytes")
assert.False(t, applied)
assert.NotNil(t, ctx.errReported)
assert.Nil(t, ctx.updateReported)
assert.False(t, ctx.successReported)
resetCtxErr()
testUpdate.Asset.Signature = validSignature
util.RemoveFileAtPath(tmpDir)
// 6. FindDownloadedAsset = true -> no error success
tmpDir = makeKeybaseUpdateTempDir(t, upr, testUpdate.Asset)
applied, err = upr.ApplyDownloaded(ctx)
assert.NoError(t, err)
assert.True(t, applied)
assert.Nil(t, ctx.errReported)
assert.NotNil(t, ctx.updateReported)
assert.True(t, ctx.successReported)
resetCtxErr()
util.RemoveFileAtPath(tmpDir)
}
func TestFindDownloadedAsset(t *testing.T) {
upr, err := newTestUpdater(t)
assert.NoError(t, err)
defer func() {
err = upr.CleanupPreviousUpdates()
assert.NoError(t, err)
}()
// 1. empty asset
matchingAssetPath, err := upr.FindDownloadedAsset("")
assert.EqualError(t, err, "No asset name provided")
assert.Equal(t, "", matchingAssetPath)
// 2. assset given -> did not create KeybaseUpdate.
matchingAssetPath, err = upr.FindDownloadedAsset("temp")
assert.NoError(t, err)
assert.Equal(t, "", matchingAssetPath)
// 3. asset given -> created KeybaseUpdate. -> directory empty
tmpDir, err := util.MakeTempDir("KeybaseUpdater.", 0700)
assert.NoError(t, err)
require.NoError(t, err)
matchingAssetPath, err = upr.FindDownloadedAsset("temp")
assert.NoError(t, err)
assert.Equal(t, "", matchingAssetPath)
util.RemoveFileAtPath(tmpDir)
// 4. asset given -> created KeybaseUpdate. -> file exists but no match
tmpDir, err = util.MakeTempDir("KeybaseUpdater.", 0700)
assert.NoError(t, err)
tmpFile := filepath.Join(tmpDir, "nottemp")
err = os.WriteFile(tmpFile, []byte("Contents of temp file"), 0700)
require.NoError(t, err)
matchingAssetPath, err = upr.FindDownloadedAsset("temp")
assert.NoError(t, err)
assert.Equal(t, "", matchingAssetPath)
util.RemoveFileAtPath(tmpDir)
// 5. asset given -> created KeybaseUpdate. -> file exixst and matches
tmpDir, err = util.MakeTempDir("KeybaseUpdater.", 0700)
tmpFile = filepath.Join(tmpDir, "temp")
err = os.WriteFile(tmpFile, []byte("Contents of temp file"), 0700)
require.NoError(t, err)
matchingAssetPath, err = upr.FindDownloadedAsset("temp")
assert.NoError(t, err)
assert.Equal(t, tmpFile, matchingAssetPath)
util.RemoveFileAtPath(tmpDir)
}
func TestUpdaterGuiBusy(t *testing.T) {
testServer := testServerForUpdateFile(t, testZipPath)
defer testServer.Close()
upr, err := newTestUpdaterWithServer(t, testServer, testUpdate(testServer.URL), &testConfig{auto: true, autoSet: true})
assert.NoError(t, err)
ctx := newTestContext(newDefaultTestUpdateOptions(), upr.config, &UpdatePromptResponse{Action: UpdateActionApply, AutoUpdate: true})
// Expect no error when the app state config is not found, allowing auto update to continue
_, err = upr.Update(ctx)
assert.NoError(t, err)
// Now put the config file there and make sure the right error is returned
now := time.Now().Unix() * 1000
err = os.WriteFile(testAppStatePath, []byte(fmt.Sprintf(`{"isUserActive":true, "changedAtMs":%d}`, now)), 0644)
assert.NoError(t, err)
defer util.RemoveFileAtPath(testAppStatePath)
_, err = upr.Update(ctx)
assert.EqualError(t, err, "Update Error (guiBusy): User active, retrying later")
// If the user was recently active, they are still considered busy.
err = os.WriteFile(testAppStatePath, []byte(fmt.Sprintf(`{"isUserActive":false, "changedAtMs":%d}`, now)), 0644)
assert.NoError(t, err)
_, err = upr.Update(ctx)
assert.EqualError(t, err, "Update Error (guiBusy): User active, retrying later")
// Make sure check command doesn't skip update on active UI
ctx.isCheckCommand = true
_, err = upr.Update(ctx)
assert.NoError(t, err)
// If the user wasn't recently active, they are not considered busy
ctx.isCheckCommand = false
later := time.Now().Add(-5*time.Minute).Unix() * 1000
err = os.WriteFile(testAppStatePath, []byte(fmt.Sprintf(`{"isUserActive":false, "changedAtMs":%d}`, later)), 0644)
assert.NoError(t, err)
_, err = upr.Update(ctx)
assert.NoError(t, err)
}