Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ToReader method for streaming out XML results #37

Merged
merged 1 commit into from
Oct 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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