package store import ( "strings" "testing" ) func TestParseLocation(t *testing.T) { l, err := ParseLocation("-8.05, -34.9, Recife, PE") if err != nil || l.Lat != -8.05 || l.Lon != -34.9 || l.Label != "Recife, PE" { t.Fatal(l, err) } if l, err := ParseLocation(""); l != nil || err != nil { t.Fatal("empty is unset") } for _, bad := range []string{"1", "a,b", "91,0", "0,181", "NaN,0", "0,0,x\x01y", "0,0," + strings.Repeat("a", 65)} { if _, err := ParseLocation(bad); err == nil { t.Errorf("%q must be refused", bad) } } } func TestPeerLocationAndLiveSanitize(t *testing.T) { e := PeerExport{V: ExportVersion, Summary: Summary{Hosts: []HostInfo{{Name: "x", Location: &Location{Lat: 95, Lon: 0}}}}} if e.Sanitize("24h"); e.Summary.Hosts[0].Location != nil { t.Fatal("invalid location must be dropped") } e = PeerExport{V: ExportVersion, Summary: Summary{Hosts: []HostInfo{{Name: "x", Location: &Location{Lat: 1, Lon: 2, Label: "a\x00" + strings.Repeat("b", 100)}}}}} e.Sanitize("24h") if l := e.Summary.Hosts[0].Location; l == nil || len(l.Label) != 64 || strings.ContainsRune(l.Label, 0) { t.Fatalf("%+v", l) } le := LiveEvent{FeedEvent: FeedEvent{IP: "::ffff:1.2.3.4", Cat: "scanner", T: 1}} if !le.Sanitize(1_800_000_000) || le.IP != "1.2.3.4" || le.T != 1_800_000_000 { t.Fatalf("%+v", le) } }