mirror of
https://github.com/moonD4rk/HackBrowserData.git
synced 2026-12-06 05:53:25 +01:00
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.
116 lines
3.3 KiB
Go
116 lines
3.3 KiB
Go
package chromium
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/moond4rk/hackbrowserdata/internal/record"
|
|
|
|
"github.com/moond4rk/hackbrowserdata/internal/masterkey"
|
|
)
|
|
|
|
func setupCookieDB(t *testing.T) string {
|
|
t.Helper()
|
|
return createTestDB(t, "Cookies", cookiesSchema,
|
|
insertCookie("session", ".old.com", "/", "", 13340000000000000, 13350000000000000, 1, 1),
|
|
insertCookie("token", ".new.com", "/api", "", 13360000000000000, 13370000000000000, 1, 0),
|
|
)
|
|
}
|
|
|
|
func TestExtractCookies(t *testing.T) {
|
|
path := setupCookieDB(t)
|
|
|
|
var out testCollector
|
|
err := extractCookies(keysOf(masterkey.Set{}), path, &out)
|
|
got := collected[record.Cookie](t, &out)
|
|
require.NoError(t, err)
|
|
require.Len(t, got, 2)
|
|
|
|
// Rows keep the database's order; the exporter sorts them.
|
|
assert.Equal(t, ".old.com", got[0].Host)
|
|
assert.Equal(t, ".new.com", got[1].Host)
|
|
|
|
assert.Equal(t, "token", got[1].Name)
|
|
assert.Equal(t, "/api", got[1].Path)
|
|
assert.True(t, got[1].IsSecure)
|
|
assert.False(t, got[1].IsHTTPOnly)
|
|
assert.True(t, got[1].HasExpire)
|
|
assert.True(t, got[1].IsPersistent)
|
|
assert.False(t, got[1].CreatedAt.IsZero())
|
|
assert.True(t, got[1].ExpireAt.After(got[1].CreatedAt))
|
|
assert.True(t, got[0].IsHTTPOnly)
|
|
assert.Empty(t, out.fails)
|
|
}
|
|
|
|
// TestExtractCookies_UndecryptableValueStillListed keeps such a cookie, or a missing key would shrink the export.
|
|
func TestExtractCookies_UndecryptableValueStillListed(t *testing.T) {
|
|
path := createTestDB(t, "Cookies", cookiesSchema,
|
|
insertCookie("SID", ".enc.com", "/", "763130deadbeef", 13350000000000000, 13360000000000000, 1, 1))
|
|
|
|
var out testCollector
|
|
require.NoError(t, extractCookies(keysOf(masterkey.Set{}), path, &out))
|
|
got := collected[record.Cookie](t, &out)
|
|
require.Len(t, got, 1)
|
|
assert.Equal(t, "SID", got[0].Name)
|
|
assert.Empty(t, got[0].Value)
|
|
assert.Len(t, out.fails, 1, "the unrecovered value is reported, which makes the extraction partial")
|
|
}
|
|
|
|
func TestStripCookieHash(t *testing.T) {
|
|
googleHash := sha256.Sum256([]byte(".google.com"))
|
|
shopifyHash := sha256.Sum256([]byte(".shopify.com"))
|
|
|
|
tests := []struct {
|
|
name string
|
|
value []byte
|
|
hostKey string
|
|
want string
|
|
}{
|
|
{
|
|
name: "Chrome 130+ strips SHA256 prefix",
|
|
value: append(googleHash[:], []byte("GA1.3.240937927.1770097858")...),
|
|
hostKey: ".google.com",
|
|
want: "GA1.3.240937927.1770097858",
|
|
},
|
|
{
|
|
name: "Chrome 130+ empty original value",
|
|
value: shopifyHash[:],
|
|
hostKey: ".shopify.com",
|
|
want: "",
|
|
},
|
|
{
|
|
name: "older Chrome no prefix",
|
|
value: []byte("plain_cookie_value"),
|
|
hostKey: ".example.com",
|
|
want: "plain_cookie_value",
|
|
},
|
|
{
|
|
name: "short value unchanged",
|
|
value: []byte("short"),
|
|
hostKey: ".example.com",
|
|
want: "short",
|
|
},
|
|
{
|
|
name: "host mismatch not stripped",
|
|
value: append(googleHash[:], []byte("value")...),
|
|
hostKey: ".other.com",
|
|
want: string(append(googleHash[:], []byte("value")...)),
|
|
},
|
|
}
|
|
|
|
for _, testCase := range tests {
|
|
t.Run(testCase.name, func(t *testing.T) {
|
|
got := stripCookieHash(testCase.value, testCase.hostKey)
|
|
assert.Equal(t, testCase.want, string(got))
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestStripCookieHash_NilValue(t *testing.T) {
|
|
got := stripCookieHash(nil, ".example.com")
|
|
assert.Nil(t, got)
|
|
}
|