|
| 1 | +package inputformats |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestBurpFormat_Name(t *testing.T) { |
| 9 | + b := NewBurpFormat() |
| 10 | + if b.Name() != "burp" { |
| 11 | + t.Errorf("Expected name 'burp', got '%s'", b.Name()) |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +func TestBurpFormat_Parse(t *testing.T) { |
| 16 | + burpXML := `<?xml version="1.0"?> |
| 17 | +<items burpVersion="2023.10.1.2" exportTime="Sat Sep 30 20:11:44 IST 2023"> |
| 18 | + <item> |
| 19 | + <time>Sat Sep 30 20:11:32 IST 2023</time> |
| 20 | + <url><![CDATA[http://example.com/path1]]></url> |
| 21 | + <host ip="127.0.0.1">example.com</host> |
| 22 | + <port>80</port> |
| 23 | + <protocol>http</protocol> |
| 24 | + <method><![CDATA[GET]]></method> |
| 25 | + <path><![CDATA[/path1]]></path> |
| 26 | + <extension>null</extension> |
| 27 | + <request base64="true"><![CDATA[R0VUIC8gSFRUUC8xLjE=]]></request> |
| 28 | + <status>200</status> |
| 29 | + <responselength>100</responselength> |
| 30 | + <mimetype>HTML</mimetype> |
| 31 | + <response base64="true"><![CDATA[SFRUUC8xLjEgMjAwIE9L]]></response> |
| 32 | + <comment></comment> |
| 33 | + </item> |
| 34 | + <item> |
| 35 | + <time>Sat Sep 30 20:08:54 IST 2023</time> |
| 36 | + <url><![CDATA[https://example.com/path2]]></url> |
| 37 | + <host ip="127.0.0.1">example.com</host> |
| 38 | + <port>443</port> |
| 39 | + <protocol>https</protocol> |
| 40 | + <method><![CDATA[POST]]></method> |
| 41 | + <path><![CDATA[/path2]]></path> |
| 42 | + <extension>null</extension> |
| 43 | + <request base64="true"><![CDATA[UE9TVCAvIEhUVFAvMS4x]]></request> |
| 44 | + <status>200</status> |
| 45 | + <responselength>100</responselength> |
| 46 | + <mimetype>JSON</mimetype> |
| 47 | + <response base64="true"><![CDATA[SFRUUC8xLjEgMjAwIE9L]]></response> |
| 48 | + <comment></comment> |
| 49 | + </item> |
| 50 | +</items>` |
| 51 | + |
| 52 | + b := NewBurpFormat() |
| 53 | + var urls []string |
| 54 | + |
| 55 | + err := b.Parse(strings.NewReader(burpXML), func(url string) bool { |
| 56 | + urls = append(urls, url) |
| 57 | + return true |
| 58 | + }) |
| 59 | + |
| 60 | + if err != nil { |
| 61 | + t.Fatalf("Parse returned error: %v", err) |
| 62 | + } |
| 63 | + |
| 64 | + if len(urls) != 2 { |
| 65 | + t.Errorf("Expected 2 URLs, got %d", len(urls)) |
| 66 | + } |
| 67 | + |
| 68 | + expectedURLs := []string{"http://example.com/path1", "https://example.com/path2"} |
| 69 | + if len(urls) != len(expectedURLs) { |
| 70 | + t.Fatalf("Expected %d URLs, got %d: %v", len(expectedURLs), len(urls), urls) |
| 71 | + } |
| 72 | + for i, expected := range expectedURLs { |
| 73 | + if urls[i] != expected { |
| 74 | + t.Errorf("Expected URL %d to be '%s', got '%s'", i, expected, urls[i]) |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +func TestBurpFormat_ParseEmpty(t *testing.T) { |
| 80 | + burpXML := `<?xml version="1.0"?> |
| 81 | +<items burpVersion="2023.10.1.2" exportTime="Sat Sep 30 20:11:44 IST 2023"> |
| 82 | +</items>` |
| 83 | + |
| 84 | + b := NewBurpFormat() |
| 85 | + var urls []string |
| 86 | + |
| 87 | + err := b.Parse(strings.NewReader(burpXML), func(url string) bool { |
| 88 | + urls = append(urls, url) |
| 89 | + return true |
| 90 | + }) |
| 91 | + |
| 92 | + if err != nil { |
| 93 | + t.Fatalf("Parse returned error: %v", err) |
| 94 | + } |
| 95 | + |
| 96 | + if len(urls) != 0 { |
| 97 | + t.Errorf("Expected 0 URLs, got %d", len(urls)) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func TestBurpFormat_ParseStopEarly(t *testing.T) { |
| 102 | + burpXML := `<?xml version="1.0"?> |
| 103 | +<items burpVersion="2023.10.1.2" exportTime="Sat Sep 30 20:11:44 IST 2023"> |
| 104 | + <item> |
| 105 | + <url><![CDATA[http://example.com/1]]></url> |
| 106 | + <host>example.com</host> |
| 107 | + <port>80</port> |
| 108 | + <protocol>http</protocol> |
| 109 | + <method><![CDATA[GET]]></method> |
| 110 | + <path><![CDATA[/1]]></path> |
| 111 | + <extension>null</extension> |
| 112 | + <request base64="true"><![CDATA[R0VUIC8=]]></request> |
| 113 | + <status>200</status> |
| 114 | + <responselength>100</responselength> |
| 115 | + <mimetype>HTML</mimetype> |
| 116 | + <response base64="true"><![CDATA[T0s=]]></response> |
| 117 | + <comment></comment> |
| 118 | + </item> |
| 119 | + <item> |
| 120 | + <url><![CDATA[http://example.com/2]]></url> |
| 121 | + <host>example.com</host> |
| 122 | + <port>80</port> |
| 123 | + <protocol>http</protocol> |
| 124 | + <method><![CDATA[GET]]></method> |
| 125 | + <path><![CDATA[/2]]></path> |
| 126 | + <extension>null</extension> |
| 127 | + <request base64="true"><![CDATA[R0VUIC8=]]></request> |
| 128 | + <status>200</status> |
| 129 | + <responselength>100</responselength> |
| 130 | + <mimetype>HTML</mimetype> |
| 131 | + <response base64="true"><![CDATA[T0s=]]></response> |
| 132 | + <comment></comment> |
| 133 | + </item> |
| 134 | +</items>` |
| 135 | + |
| 136 | + b := NewBurpFormat() |
| 137 | + var urls []string |
| 138 | + |
| 139 | + err := b.Parse(strings.NewReader(burpXML), func(url string) bool { |
| 140 | + urls = append(urls, url) |
| 141 | + return false // stop after first |
| 142 | + }) |
| 143 | + |
| 144 | + if err != nil { |
| 145 | + t.Fatalf("Parse returned error: %v", err) |
| 146 | + } |
| 147 | + |
| 148 | + if len(urls) != 1 { |
| 149 | + t.Errorf("Expected 1 URL (stopped early), got %d", len(urls)) |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +func TestBurpFormat_ParseMalformed(t *testing.T) { |
| 154 | + malformedXML := `<?xml version="1.0"?> |
| 155 | +<items burpVersion="2023.10.1.2"> |
| 156 | + <item> |
| 157 | + <url><![CDATA[http://example.com/path1]]></url> |
| 158 | + <!-- missing closing tags --> |
| 159 | +</items>` |
| 160 | + |
| 161 | + b := NewBurpFormat() |
| 162 | + err := b.Parse(strings.NewReader(malformedXML), func(url string) bool { |
| 163 | + return true |
| 164 | + }) |
| 165 | + |
| 166 | + if err == nil { |
| 167 | + t.Error("Expected error for malformed XML, got nil") |
| 168 | + } |
| 169 | +} |
0 commit comments