package ajp13 import ( "encoding/base64" "fmt" "io/ioutil" "log" "os" "testing" "github.com/google/gopacket" "github.com/google/gopacket/pcap" ) func TestParser(t *testing.T) { dataEncoded := "1MOyoQIABAAAAAAAAAAAAP//AAABAAAAEap4WQGTAwBJAgAASQIAAAAAAAAAAAAAAAAAAIbdYAAAAAITBkAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAZUYH0lJ90Z8dtQzgYAYAgACGwAAAQEICj+Ftv4/hbb+EjQB7wICAAhIVFRQLzEuMQAAHi95cC93d3cvaGVpenVuZy1zYW5pdGFlci9tYXllbgAADTk1LjkxLjIyMC4xNTMA//8AEnd3dy5nZWxiZXNlaXRlbi5kZQABuwEACKAGAAprZWVwLWFsaXZlAKABAD90ZXh0L2h0bWwsYXBwbGljYXRpb24veGh0bWwreG1sLGFwcGxpY2F0aW9uL3htbDtxPTAuOSwqLyo7cT0wLjgAoA4Ah01vemlsbGEvNS4wIChpUGhvbmU7IENQVSBpUGhvbmUgT1MgMTBfM18yIGxpa2UgTWFjIE9TIFgpIEFwcGxlV2ViS2l0LzYwMy4yLjQgKEtIVE1MLCBsaWtlIEdlY2tvKSBWZXJzaW9uLzEwLjAgTW9iaWxlLzE0Rjg5IFNhZmFyaS82MDIuMQCgBAAFZGUtZGUAoA0AFmh0dHBzOi8vd3d3Lmdvb2dsZS5kZS8AoAMADWd6aXAsIGRlZmxhdGUAABBYLUZvcndhcmRlZC1Ib3N0AAASd3d3LmdlbGJlc2VpdGVuLmRlAKALABRnc2Ntcy5nZWxiZXNlaXRlbi5kZQAIABtFQ0RIRS1SU0EtQUVTMjU2LUdDTS1TSEEzODQACwEACgAPQUpQX1JFTU9URV9QT1JUAAAFMjU0MDQA/w==" decoded, err := base64.StdEncoding.DecodeString(dataEncoded) if err != nil { t.Errorf("Failed to decode base64 packet data: %s\n", err) } tmpFile, err := ioutil.TempFile("", "ajp13") if err != nil { fmt.Printf("Failed to create temp file: %s\n", err) os.Exit(1) } defer os.Remove(tmpFile.Name()) if _, err := tmpFile.Write(decoded); err != nil { fmt.Printf("Failed to write to temp file: %s\n", err) os.Exit(2) } if err := tmpFile.Close(); err != nil { fmt.Printf("Failed to close temp file: %s\n", err) os.Exit(4) } handle, err := pcap.OpenOffline(tmpFile.Name()) if err != nil { fmt.Printf("Failed to open %s: %s\n", tmpFile.Name(), err) os.Exit(3) } err = handle.SetBPFFilter("tcp") if err != nil { log.Fatalf("Failed to set BPF Filter: %s\n", err) } packetSource := gopacket.NewPacketSource(handle, handle.LinkType()) for packet := range packetSource.Packets() { app := packet.ApplicationLayer() if app == nil { continue } data := app.Payload() a, err := Parse(data) if err != nil { t.Errorf("Failed to parse data: %s\n", err) } if a.Length() != 495 { t.Errorf("The AJP13 packet should announce a length of 495 bytes (%d)\n", a.len) } if a.Type != ForwardRequest { t.Errorf("The request type should be 2 (forward request): %d\n", a.Type) } if a.Method() != "GET" { t.Errorf("The method should be GET (2) but is %d\n", a.method) } if a.Version != "HTTP/1.1" { t.Errorf("The version should be HTTP/1.1 but is %s\n", a.Version) } if a.URI != "/yp/www/heizung-sanitaer/mayen" { t.Errorf("The URI should be /yp/www/heizung-sanitaer/mayen but is %s\n", a.URI) } if a.RemoteAddr.String() != "95.91.220.153" { t.Errorf("RemoteAddr should be 95.91.220.153 but is %s\n", a.RemoteAddr) } if a.RemoteHost != "" { t.Errorf("RemoteHost should be '' but is '%s'\n", a.RemoteHost) } if a.Server != "www.gelbeseiten.de" { t.Errorf("Server should be www.gelbeseiten.de but is %s", a.Server) } if a.Port != 443 { t.Errorf("Port should be 443 but is %d\n", a.Port) } if a.SSL != true { t.Errorf("SSL should be true but is false\n") } if val, _ := a.Header("X-Forwarded-Host"); val != "www.gelbeseiten.de" { t.Errorf("Header field X-Forwarded-Host should be 'www.gelbeseiten.de' but is '%s'\n", val) } if val, _ := a.Header("User-Agent"); val != "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_2 like Mac OS X) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.0 Mobile/14F89 Safari/602.1" { t.Errorf("Header field User-Agent should be 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_2 like Mac OS X) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.0 Mobile/14F89 Safari/602.1' but is '%s'", val) } break } }