github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/libkb/secret_store_test.go (about) 1 // Copyright 2015 Keybase, Inc. All rights reserved. Use of 2 // this source code is governed by the included BSD license. 3 4 package libkb 5 6 import ( 7 "fmt" 8 "sort" 9 "testing" 10 11 "github.com/stretchr/testify/require" 12 ) 13 14 func TestSecretStoreOps(t *testing.T) { 15 tc := SetupTest(t, "secret store ops", 1) 16 defer tc.Cleanup() 17 m := NewMetaContextForTest(tc) 18 19 nu := NewNormalizedUsername("username") 20 expectedSecret1 := []byte("test secret 1test secret 1test s") 21 expectedSecret2 := []byte("test secret 2test secret 2test s") 22 23 var err error 24 25 if err = tc.G.SecretStore().ClearSecret(m, nu); err != nil { 26 t.Error(err) 27 } 28 29 // TODO: Use platform-independent errors so they can be 30 // checked for. 31 var secret LKSecFullSecret 32 if secret, err = tc.G.SecretStore().RetrieveSecret(m, nu); err == nil { 33 t.Error("RetrieveSecret unexpectedly returned a nil error") 34 } 35 36 if !secret.IsNil() { 37 t.Errorf("Retrieved secret unexpectedly: %s", string(secret.Bytes())) 38 } 39 40 secret, err = newLKSecFullSecretFromBytes(expectedSecret1) 41 if err != nil { 42 t.Fatal(err) 43 } 44 45 if err = tc.G.SecretStore().StoreSecret(m, nu, secret); err != nil { 46 t.Error(err) 47 } 48 49 if secret, err = tc.G.SecretStore().RetrieveSecret(m, nu); err != nil { 50 t.Error(err) 51 } 52 53 if string(secret.Bytes()) != string(expectedSecret1) { 54 t.Errorf("Retrieved secret %s, expected %s", string(secret.Bytes()), string(expectedSecret1)) 55 } 56 57 secret, err = newLKSecFullSecretFromBytes(expectedSecret2) 58 if err != nil { 59 t.Fatal(err) 60 } 61 62 if err = tc.G.SecretStore().StoreSecret(m, nu, secret); err != nil { 63 t.Error(err) 64 } 65 66 if secret, err = tc.G.SecretStore().RetrieveSecret(m, nu); err != nil { 67 t.Error(err) 68 } 69 70 if string(secret.Bytes()) != string(expectedSecret2) { 71 t.Errorf("Retrieved secret %s, expected %s", string(secret.Bytes()), string(expectedSecret2)) 72 } 73 74 if err = tc.G.SecretStore().ClearSecret(m, nu); err != nil { 75 t.Error(err) 76 } 77 } 78 79 func TestGetUsersWithStoredSecrets(t *testing.T) { 80 81 tc := SetupTest(t, "get users with stored secrets", 1) 82 defer tc.Cleanup() 83 m := NewMetaContextForTest(tc) 84 85 usernames, err := tc.G.SecretStore().GetUsersWithStoredSecrets(m) 86 if err != nil { 87 t.Error(err) 88 } 89 if len(usernames) != 0 { 90 t.Errorf("Expected no usernames, got %d", len(usernames)) 91 } 92 93 fs, err := newLKSecFullSecretFromBytes([]byte("test secret 3test secret 3test s")) 94 if err != nil { 95 t.Fatal(err) 96 } 97 98 expectedUsernames := make([]string, 10) 99 for i := 0; i < len(expectedUsernames); i++ { 100 expectedUsernames[i] = fmt.Sprintf("account with unicode ใในใ %d", i) 101 102 if err := tc.G.SecretStore().StoreSecret(m, NewNormalizedUsername(expectedUsernames[i]), fs); err != nil { 103 t.Error(err) 104 } 105 } 106 107 usernames, err = tc.G.SecretStore().GetUsersWithStoredSecrets(m) 108 if err != nil { 109 t.Error(err) 110 } 111 112 if len(usernames) != len(expectedUsernames) { 113 t.Errorf("Expected %d usernames, got %d", len(expectedUsernames), len(usernames)) 114 } 115 116 // TODO: were these supposed to already be in order? 117 sort.Strings(usernames) 118 119 for i := 0; i < len(usernames); i++ { 120 if usernames[i] != expectedUsernames[i] { 121 t.Errorf("Expected username %s, got %s", expectedUsernames[i], usernames[i]) 122 } 123 } 124 125 for i := 0; i < len(expectedUsernames); i++ { 126 err = tc.G.SecretStore().ClearSecret(m, NewNormalizedUsername(expectedUsernames[i])) 127 if err != nil { 128 t.Error(err) 129 } 130 } 131 132 usernames, err = tc.G.SecretStore().GetUsersWithStoredSecrets(m) 133 if err != nil { 134 t.Error(err) 135 } 136 if len(usernames) != 0 { 137 t.Errorf("Expected no usernames, got %d", len(usernames)) 138 } 139 } 140 141 func TestPrimeSecretStore(t *testing.T) { 142 tc := SetupTest(t, "secret_store", 1) 143 defer tc.Cleanup() 144 tc.G.Env.Test.SecretStorePrimingDisabled = false 145 146 mctx := NewMetaContextForTest(tc) 147 err := mctx.G().SecretStore().PrimeSecretStores(mctx) 148 require.NoError(t, err) 149 }