test: add test using fake helper

This commit is contained in:
James Houlahan
2021-01-06 13:28:39 +01:00
parent 5ad338e835
commit 67a7d556ec
2 changed files with 156 additions and 5 deletions

View File

@ -51,9 +51,6 @@ func NewKeychain(s *settings.Settings, keychainName string) (*Keychain, error) {
return nil, ErrNoKeychain
}
// hostURL uniquely identifies the app's keychain items within the system keychain.
hostURL := fmt.Sprintf("protonmail/%v/users", keychainName)
// If the preferred keychain is unsupported, fallback to the default one.
// NOTE: Maybe we want to error out here and show something in the GUI instead?
if _, ok := Helpers[s.Get(settings.PreferredKeychainKey)]; !ok {
@ -67,12 +64,17 @@ func NewKeychain(s *settings.Settings, keychainName string) (*Keychain, error) {
}
// Construct the keychain helper.
helper, err := helperConstructor(hostURL)
helper, err := helperConstructor(hostURL(keychainName))
if err != nil {
return nil, err
}
return newKeychain(helper, hostURL), nil
return newKeychain(helper, hostURL(keychainName)), nil
}
// hostURL uniquely identifies the app's keychain items within the system keychain.
func hostURL(keychainName string) string {
return fmt.Sprintf("protonmail/%v/users", keychainName)
}
func newKeychain(helper credentials.Helper, url string) *Keychain {

View File

@ -0,0 +1,149 @@
// Copyright (c) 2021 Proton Technologies AG
//
// This file is part of ProtonMail Bridge.
//
// ProtonMail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ProtonMail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
package keychain
import (
"encoding/base64"
"testing"
"github.com/docker/docker-credential-helpers/credentials"
"github.com/stretchr/testify/require"
)
var suffix = []byte("\x00avoidFix\x00\x00\x00\x00\x00\x00\x00") //nolint[gochecknoglobals]
var testData = map[string]string{ //nolint[gochecknoglobals]
"user1": base64.StdEncoding.EncodeToString(append([]byte("data1"), suffix...)),
"user2": base64.StdEncoding.EncodeToString(append([]byte("data2"), suffix...)),
}
func TestInsertReadRemove(t *testing.T) {
keychain := newKeychain(newTestHelper(), hostURL("bridge"))
for id, secret := range testData {
expectedList, _ := keychain.List()
// Add expected secrets.
expectedSecret := secret
require.NoError(t, keychain.Put(id, expectedSecret))
// Check list.
actualList, err := keychain.List()
require.NoError(t, err)
expectedList = append(expectedList, id)
require.ElementsMatch(t, expectedList, actualList)
// Get and check what was inserted.
_, actualSecret, err := keychain.Get(id)
require.NoError(t, err)
require.Equal(t, expectedSecret, actualSecret)
// Put what changed.
expectedSecret = "edited_" + id
expectedSecret = base64.StdEncoding.EncodeToString(append([]byte(expectedSecret), suffix...))
nJobs := 100
nWorkers := 3
jobs := make(chan interface{}, nJobs)
done := make(chan interface{})
for i := 0; i < nWorkers; i++ {
go func() {
for {
_, more := <-jobs
if more {
require.NoError(t, keychain.Put(id, expectedSecret))
} else {
done <- nil
return
}
}
}()
}
for i := 0; i < nJobs; i++ {
jobs <- nil
}
close(jobs)
for i := 0; i < nWorkers; i++ {
<-done
}
// Check list.
actualList, err = keychain.List()
require.NoError(t, err)
require.ElementsMatch(t, expectedList, actualList)
// Get and check what changed.
_, actualSecret, err = keychain.Get(id)
require.NoError(t, err)
require.Equal(t, expectedSecret, actualSecret)
if id != "user1" {
// Remove.
err = keychain.Delete(id)
require.NoError(t, err)
// Check removed.
actualList, err = keychain.List()
require.NoError(t, err)
expectedList = expectedList[:len(expectedList)-1]
require.ElementsMatch(t, expectedList, actualList)
}
}
// Clear first.
require.NoError(t, keychain.Delete("user1"))
actualList, err := keychain.List()
require.NoError(t, err)
for id := range testData {
require.NotContains(t, actualList, id)
}
}
type testHelper map[string]*credentials.Credentials
func newTestHelper() testHelper {
return make(testHelper)
}
func (h testHelper) Add(creds *credentials.Credentials) error {
h[creds.ServerURL] = creds
return nil
}
func (h testHelper) Delete(url string) error {
delete(h, url)
return nil
}
func (h testHelper) Get(url string) (string, string, error) {
creds := h[url]
return creds.Username, creds.Secret, nil
}
func (h testHelper) List() (map[string]string, error) {
list := make(map[string]string)
for url, creds := range h {
list[url] = creds.Username
}
return list, nil
}