arblant
firefox-vpn-client / core / internal / accounts / damaged_test.go
damaged_test.go53 lines · 1.6 KB
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
package accounts

import (
	"os"
	"path/filepath"
	"strings"
	"testing"
	"time"

	"github.com/armanlife/firefox-vpn-client/core/internal/fxa"
)

// A folder under accounts/ that holds no readable account (a copy made by
// hand, or an account file left empty by a crash) is skipped with a
// warning; the other accounts still load, and the folder stays.
func TestListSkipsDamagedFolders(t *testing.T) {
	dir := t.TempDir()
	store, err := OpenStore(dir)
	if err != nil {
		t.Fatal(err)
	}
	good, _, err := store.Add("a@example.com", &fxa.SavedTokens{AccessToken: "access", RefreshToken: "refresh"}, time.Now())
	if err != nil {
		t.Fatal(err)
	}
	accountsDir := filepath.Join(dir, "accounts")
	for _, name := range []string{"backup.old", "abcdef12"} {
		if err := os.MkdirAll(filepath.Join(accountsDir, name), 0o700); err != nil {
			t.Fatal(err)
		}
	}
	// An empty account file, as a power cut can leave.
	if err := os.WriteFile(filepath.Join(accountsDir, "abcdef12", accountFileName), nil, 0o600); err != nil {
		t.Fatal(err)
	}
	var warnings []string
	defer func(old func(string)) { Warn = old }(Warn)
	Warn = func(s string) { warnings = append(warnings, s) }

	list, err := store.List()
	if err != nil {
		t.Fatalf("a damaged folder must not stop the listing: %v", err)
	}
	if len(list) != 1 || list[0].ID != good.ID {
		t.Fatalf("want only the good account, got %+v", list)
	}
	if len(warnings) != 2 || !strings.Contains(strings.Join(warnings, "\n"), "backup.old") {
		t.Fatalf("want a warning per skipped folder, got %q", warnings)
	}
	if _, err := os.Stat(filepath.Join(accountsDir, "abcdef12")); err != nil {
		t.Fatal("a skipped folder must stay for the user")
	}
}