package obs
import (
"net/http"
"testing"
)
// A range is the block an address is routed in; anything else is none.
func TestPrefixOfIsTheRoutedBlock(t *testing.T) {
for in, want := range map[string]string{
"203.0.113.77": "203.0.113.0/24",
"::ffff:203.0.113.77": "203.0.113.0/24",
"2a01:4f8:1c19:d052::1": "2a01:4f8:1c19::/48",
"": "",
"not an address": "",
} {
if got := PrefixOf(in); got != want {
t.Errorf("FAIL: PrefixOf(%q) = %q, want %q", in, got, want)
}
}
}
// A browser's page load and a script borrowing its name present
// differently, and the edge's own headers do not count.
func TestAFingerprintSeparatesABrowserFromAScript(t *testing.T) {
browser := http.Header{}
for k, v := range map[string]string{
"User-Agent": "Mozilla/5.0", "Sec-Ch-Ua": `"Chromium";v="153"`, "Sec-Fetch-Mode": "navigate",
"Sec-Fetch-Site": "none", "Accept-Language": "en-GB,en;q=0.9", "Accept-Encoding": "gzip, br", "Cookie": "a=b",
} {
browser.Set(k, v)
}
hints, lang, set := Fingerprint(browser)
if hints != "ch fs fm=navigate al ae ck" || lang != "en-GB" || set == "" {
t.Errorf("FAIL: browser: %q %q %q", hints, lang, set)
}
script := http.Header{"User-Agent": {"Mozilla/5.0"}, "Accept-Encoding": {"gzip"}}
sh, sl, ss := Fingerprint(script)
if sh != "ae" || sl != "" || ss == set {
t.Errorf("FAIL: script: %q %q %q", sh, sl, ss)
}
behind := script.Clone()
behind.Set("Cf-Ray", "x")
behind.Set("X-Forwarded-For", "1.2.3.4")
if _, _, bs := Fingerprint(behind); bs != ss {
t.Errorf("FAIL: the edge's headers changed the set")
}
}