Files
HackBrowserData/internal/browser/firefox/extract_storage_test.go
T
Roger 3e9150249c refactor: rename core types and spell out every name (#677)
Def, Request, DirID, and Source said too little, and short variables and doc comments that skip their name hid intent.
varnamelen and staticcheck ST1020-22 now keep both from coming back.
2026-11-02 11:06:05 +08:00

74 lines
1.8 KiB
Go

package firefox
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/moond4rk/hackbrowserdata/internal/record"
)
func setupWebappsDB(t *testing.T) string {
t.Helper()
return createTestDB(t, "webappsstore.sqlite", []string{webappsstore2Schema},
insertWebappsstore("moc.buhtig.:https:443", "theme", "dark"),
insertWebappsstore("moc.buhtig.:https:443", "lang", "en"),
insertWebappsstore("moc.elpmaxe.:http:8080", "token", "abc123"),
)
}
func TestExtractLocalStorage(t *testing.T) {
path := setupWebappsDB(t)
var out testCollector
err := extractLocalStorage(path, &out)
got := collected[record.LocalStorage](t, &out)
require.NoError(t, err)
require.Len(t, got, 3)
byKey := map[string]string{}
for _, entry := range got {
byKey[entry.URL+"/"+entry.Key] = entry.Value
}
assert.Equal(t, "dark", byKey["https://github.com:443/theme"])
assert.Equal(t, "en", byKey["https://github.com:443/lang"])
assert.Equal(t, "abc123", byKey["http://example.com:8080/token"])
}
func TestParseOriginKey(t *testing.T) {
tests := []struct {
name string
originKey string
want string
}{
{
name: "https with port",
originKey: "moc.buhtig.:https:443",
want: "https://github.com:443",
},
{
name: "http with non-standard port",
originKey: "moc.elpmaxe.:http:8080",
want: "http://example.com:8080",
},
{
name: "no port",
originKey: "moc.elpmaxe.:https",
want: "https://example.com",
},
{
name: "invalid format",
originKey: "something",
want: "something",
},
}
for _, testCase := range tests {
t.Run(testCase.name, func(t *testing.T) {
got := parseOriginKey(testCase.originKey)
assert.Equal(t, testCase.want, got)
})
}
}