github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/kbpagesconfig/upgrade_test.go (about) 1 // Copyright 2018 Keybase Inc. All rights reserved. 2 // Use of this source code is governed by a BSD 3 // license that can be found in the LICENSE file. 4 5 package main 6 7 import ( 8 "context" 9 "os" 10 "path/filepath" 11 "testing" 12 13 "github.com/keybase/client/go/kbfs/libpages/config" 14 "github.com/stretchr/testify/require" 15 "golang.org/x/crypto/bcrypt" 16 ) 17 18 func TestUpgrade(t *testing.T) { 19 configDir, err := os.MkdirTemp(".", "kbpagesconfig-editor-test-") 20 require.NoError(t, err) 21 defer os.RemoveAll(configDir) 22 kbpConfigPath := filepath.Join(configDir, config.DefaultConfigFilename) 23 24 t.Logf("creating config with bcrypt pass at %s", kbpConfigPath) 25 26 v1 := config.DefaultV1() 27 28 bcryptHash, err := bcrypt.GenerateFromPassword([]byte("ecila"), bcrypt.MinCost) 29 require.NoError(t, err) 30 v1.Users = make(map[string]string) 31 v1.Users["alice"] = string(bcryptHash) 32 33 f1, err := os.Create(kbpConfigPath) 34 require.NoError(t, err) 35 defer f1.Close() 36 err = v1.Encode(f1, true) 37 require.NoError(t, err) 38 39 t.Logf("testing upgrade") 40 41 nextResponse := make(chan string, 5) 42 prompter := &fakePrompterForTest{ 43 nextResponse: nextResponse, 44 } 45 46 nextResponse <- "y" // "You are about to migrate ..." 47 nextResponse <- "123" // answer with a wrong password 48 nextResponse <- "n" // don't use a random generated one 49 nextResponse <- "ecila" // give correct password 50 nextResponse <- "y" // confirm write 51 52 err = upgradeToSHA256WithPrompter(configDir, prompter) 53 require.NoError(t, err) 54 55 t.Logf("testing new config is upgraded and still works") 56 f2, err := os.Open(kbpConfigPath) 57 require.NoError(t, err) 58 defer f2.Close() 59 60 cfg, err := config.ParseConfig(f2) 61 require.NoError(t, err) 62 require.Equal(t, config.Version1, cfg.Version()) 63 v1 = cfg.(*config.V1) 64 65 needsUpgrade, err := v1.HasBcryptPasswords() 66 require.NoError(t, err) 67 require.False(t, needsUpgrade) 68 authed := v1.Authenticate(context.Background(), "alice", "ecila") 69 require.True(t, authed) 70 }