ajp13_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package ajp13
  2. import (
  3. "encoding/base64"
  4. "fmt"
  5. "io/ioutil"
  6. "log"
  7. "os"
  8. "testing"
  9. "github.com/google/gopacket"
  10. "github.com/google/gopacket/pcap"
  11. )
  12. func TestParser(t *testing.T) {
  13. dataEncoded := "1MOyoQIABAAAAAAAAAAAAP//AAABAAAAEap4WQGTAwBJAgAASQIAAAAAAAAAAAAAAAAAAIbdYAAAAAITBkAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAZUYH0lJ90Z8dtQzgYAYAgACGwAAAQEICj+Ftv4/hbb+EjQB7wICAAhIVFRQLzEuMQAAHi95cC93d3cvaGVpenVuZy1zYW5pdGFlci9tYXllbgAADTk1LjkxLjIyMC4xNTMA//8AEnd3dy5nZWxiZXNlaXRlbi5kZQABuwEACKAGAAprZWVwLWFsaXZlAKABAD90ZXh0L2h0bWwsYXBwbGljYXRpb24veGh0bWwreG1sLGFwcGxpY2F0aW9uL3htbDtxPTAuOSwqLyo7cT0wLjgAoA4Ah01vemlsbGEvNS4wIChpUGhvbmU7IENQVSBpUGhvbmUgT1MgMTBfM18yIGxpa2UgTWFjIE9TIFgpIEFwcGxlV2ViS2l0LzYwMy4yLjQgKEtIVE1MLCBsaWtlIEdlY2tvKSBWZXJzaW9uLzEwLjAgTW9iaWxlLzE0Rjg5IFNhZmFyaS82MDIuMQCgBAAFZGUtZGUAoA0AFmh0dHBzOi8vd3d3Lmdvb2dsZS5kZS8AoAMADWd6aXAsIGRlZmxhdGUAABBYLUZvcndhcmRlZC1Ib3N0AAASd3d3LmdlbGJlc2VpdGVuLmRlAKALABRnc2Ntcy5nZWxiZXNlaXRlbi5kZQAIABtFQ0RIRS1SU0EtQUVTMjU2LUdDTS1TSEEzODQACwEACgAPQUpQX1JFTU9URV9QT1JUAAAFMjU0MDQA/w=="
  14. decoded, err := base64.StdEncoding.DecodeString(dataEncoded)
  15. if err != nil {
  16. t.Errorf("Failed to decode base64 packet data: %s\n", err)
  17. }
  18. tmpFile, err := ioutil.TempFile("", "ajp13")
  19. if err != nil {
  20. fmt.Printf("Failed to create temp file: %s\n", err)
  21. os.Exit(1)
  22. }
  23. defer os.Remove(tmpFile.Name())
  24. if _, err := tmpFile.Write(decoded); err != nil {
  25. fmt.Printf("Failed to write to temp file: %s\n", err)
  26. os.Exit(2)
  27. }
  28. if err := tmpFile.Close(); err != nil {
  29. fmt.Printf("Failed to close temp file: %s\n", err)
  30. os.Exit(4)
  31. }
  32. handle, err := pcap.OpenOffline(tmpFile.Name())
  33. if err != nil {
  34. fmt.Printf("Failed to open %s: %s\n", tmpFile.Name(), err)
  35. os.Exit(3)
  36. }
  37. err = handle.SetBPFFilter("tcp")
  38. if err != nil {
  39. log.Fatalf("Failed to set BPF Filter: %s\n", err)
  40. }
  41. packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
  42. for packet := range packetSource.Packets() {
  43. app := packet.ApplicationLayer()
  44. if app == nil {
  45. continue
  46. }
  47. data := app.Payload()
  48. a, err := Parse(data)
  49. if err != nil {
  50. t.Errorf("Failed to parse data: %s\n", err)
  51. }
  52. if a.Length() != 495 {
  53. t.Errorf("The AJP13 packet should announce a length of 495 bytes (%d)\n", a.len)
  54. }
  55. if a.Type != ForwardRequest {
  56. t.Errorf("The request type should be 2 (forward request): %d\n", a.Type)
  57. }
  58. if a.Method() != "GET" {
  59. t.Errorf("The method should be GET (2) but is %d\n", a.method)
  60. }
  61. if a.Version != "HTTP/1.1" {
  62. t.Errorf("The version should be HTTP/1.1 but is %s\n", a.Version)
  63. }
  64. if a.URI != "/yp/www/heizung-sanitaer/mayen" {
  65. t.Errorf("The URI should be /yp/www/heizung-sanitaer/mayen but is %s\n", a.URI)
  66. }
  67. if a.RemoteAddr.String() != "95.91.220.153" {
  68. t.Errorf("RemoteAddr should be 95.91.220.153 but is %s\n", a.RemoteAddr)
  69. }
  70. if a.RemoteHost != "" {
  71. t.Errorf("RemoteHost should be '' but is '%s'\n", a.RemoteHost)
  72. }
  73. if a.Server != "www.gelbeseiten.de" {
  74. t.Errorf("Server should be www.gelbeseiten.de but is %s", a.Server)
  75. }
  76. if a.Port != 443 {
  77. t.Errorf("Port should be 443 but is %d\n", a.Port)
  78. }
  79. if a.SSL != true {
  80. t.Errorf("SSL should be true but is false\n")
  81. }
  82. if val, _ := a.Header("X-Forwarded-Host"); val != "www.gelbeseiten.de" {
  83. t.Errorf("Header field X-Forwarded-Host should be 'www.gelbeseiten.de' but is '%s'\n", val)
  84. }
  85. 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" {
  86. 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)
  87. }
  88. break
  89. }
  90. }