Files
HackBrowserData/internal/browser/firefox/extract_password_test.go
T
Roger 2e7ee3c69f test: tidy misplaced, stale, and redundant tests (#691)
Tests now sit beside the code they cover; ones a golden or another test already pins are gone, and core tests gain the assertions a real regression would trip.
2026-11-09 17:25:37 +08:00

110 lines
2.9 KiB
Go

package firefox
import (
"bytes"
"encoding/base64"
"encoding/hex"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/moond4rk/hackbrowserdata/internal/record"
)
// loginPBEHex is from crypto's credentialPBETestCases: it decrypts to "Hello, World!" with globalSalt = "moond4rk" * 3.
const loginPBEHex = "303b0410f8000000000000000000000000000001301506092a864886f70d010503040830313233343536370410fe968b6565149114ea688defd6683e45"
var testGlobalSalt = bytes.Repeat([]byte("moond4rk"), 3) // 24 bytes
func fixedKey(key []byte) func() ([]byte, error) { return func() ([]byte, error) { return key, nil } }
func loginPBEBase64(t *testing.T) string {
t.Helper()
raw, err := hex.DecodeString(loginPBEHex)
require.NoError(t, err)
return base64.StdEncoding.EncodeToString(raw)
}
func TestExtractPasswords(t *testing.T) {
encB64 := loginPBEBase64(t)
json := fmt.Sprintf(`{
"logins": [
{
"hostname": "https://example.com",
"formSubmitURL": "https://example.com/login",
"encryptedUsername": "%s",
"encryptedPassword": "%s",
"timeCreated": 1700000000000
}
]
}`, encB64, encB64)
path := createTestJSON(t, "logins.json", json)
var out testCollector
err := extractPasswords(fixedKey(testGlobalSalt), path, &out)
got := collected[record.Login](t, &out)
require.NoError(t, err)
require.Len(t, got, 1)
assert.Equal(t, "Hello, World!", got[0].Username)
assert.Equal(t, "Hello, World!", got[0].Password)
assert.Equal(t, "https://example.com/login", got[0].URL)
assert.False(t, got[0].CreatedAt.IsZero())
}
func TestExtractPasswords_FormSubmitURLFallback(t *testing.T) {
encB64 := loginPBEBase64(t)
// When formSubmitURL is empty, should fall back to hostname
json := fmt.Sprintf(`{
"logins": [
{
"hostname": "https://fallback.com",
"formSubmitURL": "",
"encryptedUsername": "%s",
"encryptedPassword": "%s",
"timeCreated": 1700000000000
}
]
}`, encB64, encB64)
path := createTestJSON(t, "logins.json", json)
var out testCollector
err := extractPasswords(fixedKey(testGlobalSalt), path, &out)
got := collected[record.Login](t, &out)
require.NoError(t, err)
require.Len(t, got, 1)
assert.Equal(t, "https://fallback.com", got[0].URL)
}
func TestExtractPasswords_DecryptFailureKeepsEntry(t *testing.T) {
// Invalid base64 — decryptPBE fails, but entry is still kept with empty user/pwd
json := `{
"logins": [
{
"hostname": "https://bad.com",
"encryptedUsername": "not-valid-base64!!!",
"encryptedPassword": "also-bad",
"timeCreated": 1700000000000
}
]
}`
path := createTestJSON(t, "logins.json", json)
var out testCollector
err := extractPasswords(fixedKey(testGlobalSalt), path, &out)
got := collected[record.Login](t, &out)
require.NoError(t, err)
require.Len(t, got, 1)
assert.Equal(t, "https://bad.com", got[0].URL)
assert.Empty(t, got[0].Username)
assert.Empty(t, got[0].Password)
assert.Len(t, out.fails, 2)
}