package stats import ( "testing" "time" ) func TestParseResponse(t *testing.T) { loc := time.FixedZone("test", 3600) for _, tc := range []struct { line string ok bool client, view, name, typ, rc string }{ // Real lines from named 9.20.24 (with and without views). {`07-Oct-2026 19:46:22.184 client @0x71358b8ff000 127.0.0.1#53937 (www.example.test): view internal: response: www.example.test IN A NOERROR 1 0 1 +E(0)K (127.0.0.1)`, true, "127.0.0.1", "internal", "www.example.test", "A", "NOERROR"}, {`07-Oct-2026 19:46:22.195 client @0x71358b8ff000 10.10.0.37#51709 (nope.example.test): response: Nope.Example.Test IN AAAA NXDOMAIN 0 1 1 +E(0)K (127.0.0.1)`, true, "10.10.0.37", "", "nope.example.test", "AAAA", "NXDOMAIN"}, {`07-Oct-2026 19:46:22.217 client @0x71358b8ff000 2001:db8::7#55006 (other.zone): view external net: response: other.zone. IN MX REFUSED 0 0 1 +E(0)K (2001:db8::1)`, true, "2001:db8::7", "external net", "other.zone", "MX", "REFUSED"}, {`07-Oct-2026 19:46:22.217 client @0x71358b8ff000 ::ffff:10.0.0.1#55006 (.): response: . IN NS NOERROR 13 0 27 + (10.0.0.2)`, true, "10.0.0.1", "", ".", "NS", "NOERROR"}, // Query lines and other messages are not responses. {`07-Oct-2026 19:46:22.184 client @0x71358b8ff000 127.0.0.1#53937 (www.example.test): view internal: query: www.example.test IN A +E(0)K (127.0.0.1)`, false, "", "", "", "", ""}, {`07-Oct-2026 19:46:20.000 logging channel 'jarl_responses' file '/var/log/named/jarl-responses.log': rolled`, false, "", "", "", "", ""}, {``, false, "", "", "", "", ""}, } { ev, ok := ParseResponse(tc.line, loc) if ok != tc.ok { t.Errorf("%q: ok = %v", tc.line, ok) continue } if !ok { continue } if ev.Client.String() != tc.client || ev.View != tc.view || ev.Name != tc.name || ev.Type != tc.typ || ev.RCode != tc.rc { t.Errorf("%q: got %+v", tc.line, ev) } if want := time.Date(2026, 10, 7, 19, 46, 22, 0, loc); ev.Time.Truncate(time.Second) != want { t.Errorf("%q: time %v, want %v", tc.line, ev.Time, want) } } }