Skip to content

Commit

Permalink
Added ToReader method for streaming out XML results (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
beckler authored and Ullaakut committed Oct 20, 2019
1 parent 401a488 commit 830f105
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
9 changes: 8 additions & 1 deletion xml.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package nmap

import (
"bytes"
"encoding/xml"
"io"
"io/ioutil"
"strconv"
"time"
Expand Down Expand Up @@ -41,6 +43,11 @@ func (r Run) ToFile(filePath string) error {
return ioutil.WriteFile(filePath, r.rawXML, 0666)
}

// ToReader writes the raw XML into an streamable buffer.
func (r Run) ToReader() io.Reader {
return bytes.NewReader(r.rawXML)
}

// ScanInfo represents the scan information.
type ScanInfo struct {
NumServices int `xml:"numservices,attr" json:"num_services"`
Expand Down Expand Up @@ -421,7 +428,7 @@ func (t *Timestamp) UnmarshalXMLAttr(attr xml.Attr) (err error) {
// Run struct.
func Parse(content []byte) (*Run, error) {
r := &Run{
rawXML: content,
rawXML: content,
}

err := xml.Unmarshal(content, r)
Expand Down
23 changes: 23 additions & 0 deletions xml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,29 @@ func TestToFile(t *testing.T) {
}
}

func TestToReader(t *testing.T) {
inputFile := "tests/xml/scan01.xml"
rawXML, err := ioutil.ReadFile(inputFile)
if err != nil {
t.Fatal(err)
}

result, err := Parse(rawXML)
if err != nil {
t.Fatal(err)
}

reader := result.ToReader()
byteOutput, err := ioutil.ReadAll(reader)
if err != nil {
t.Fatal(err)
}

if !bytes.Equal(byteOutput, result.rawXML) {
t.Error("expected ToReader method to return lexicographically identical results")
}
}

func TestTimestampJSONMarshaling(t *testing.T) {
dateTime := time.Date(2000, 0, 0, 0, 0, 0, 0, time.UTC)
dateBytes := []byte("943920000")
Expand Down

0 comments on commit 830f105

Please sign in to comment.