github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/libkb/secret_store_mem_test.go (about)

     1  package libkb
     2  
     3  import (
     4  	"bytes"
     5  	"sort"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestSecretStoreMem(t *testing.T) {
    12  	cases := map[string]struct {
    13  		username NormalizedUsername
    14  		secret   []byte
    15  	}{
    16  		"alice":   {"alice", []byte("alice_first_sec_first_sec_first_")},
    17  		"charlie": {"charlie", []byte("charliecharliecharliecharliechar")},
    18  		"replace": {"alice", []byte("alice_next_secret_alice_next_sec")},
    19  	}
    20  
    21  	s := NewSecretStoreMem()
    22  	m := newNilMetaContext()
    23  	for _, test := range cases {
    24  		fs, err := newLKSecFullSecretFromBytes(test.secret)
    25  		require.NoError(t, err)
    26  
    27  		err = s.StoreSecret(m, test.username, fs)
    28  		require.NoError(t, err)
    29  
    30  		secret, err := s.RetrieveSecret(m, test.username)
    31  		require.NoError(t, err)
    32  		require.True(t, bytes.Equal(secret.Bytes(), test.secret))
    33  	}
    34  
    35  	_, err := s.RetrieveSecret(m, "nobody")
    36  	require.IsType(t, SecretStoreError{}, err)
    37  
    38  	users, err := s.GetUsersWithStoredSecrets(m)
    39  	require.NoError(t, err)
    40  	require.Len(t, users, 2)
    41  	sort.Strings(users)
    42  	require.Equal(t, users[0], "alice")
    43  	require.Equal(t, users[1], "charlie")
    44  
    45  	err = s.ClearSecret(m, "alice")
    46  	require.NoError(t, err)
    47  
    48  	secret, err := s.RetrieveSecret(m, "alice")
    49  	require.IsType(t, SecretStoreError{}, err)
    50  	require.True(t, secret.IsNil())
    51  }