Added test cases for expected templating file outputs

attachment-template-support
Glenn Wilkinson 2020-12-10 18:17:01 +01:00
parent da5a82ef4b
commit 1cb085ad72
8 changed files with 75 additions and 20 deletions

View File

@ -34,38 +34,49 @@ func (s *ModelsSuite) TestAttachment(c *check.C) {
log.Fatalf("Failed to open attachment folder 'testdata': %v\n", err)
}
for _, ff := range files {
if !ff.IsDir() {
if !ff.IsDir() && !strings.Contains(ff.Name(), "templated") {
fname := ff.Name()
fmt.Printf("Checking attachment file -> %s\n", fname)
f, err := os.Open("testdata/" + fname)
if err != nil {
log.Fatalf("Failed to open attachment test file '%s': %v\n", fname, err)
}
reader := bufio.NewReader(f)
content, err := ioutil.ReadAll(reader)
if err != nil {
log.Fatalf("Failed to read attachment test file '%s': %v\n", fname, err)
}
data := ""
data := readFile("testdata/" + fname)
if filepath.Ext(fname) == ".b64" {
data = string(content)
fname = fname[:len(fname)-4]
} else {
data = base64.StdEncoding.EncodeToString(content)
}
a := Attachment{
Content: data,
Name: fname,
}
_, err = a.ApplyTemplate(ptx)
t, err := a.ApplyTemplate(ptx)
c.Assert(err, check.Equals, nil)
c.Assert(a.vanillaFile, check.Equals, strings.Contains(fname, "without-vars"))
c.Assert(a.vanillaFile, check.Not(check.Equals), strings.Contains(fname, "with-vars"))
// Verfify template was applied as expected
tt, err := ioutil.ReadAll(t)
if err != nil {
log.Fatalf("Failed to parse templated file '%s': %v\n", fname, err)
}
templatedFile := base64.StdEncoding.EncodeToString(tt)
expectedOutput := readFile("testdata/" + strings.TrimSuffix(ff.Name(), filepath.Ext(ff.Name())) + ".templated" + filepath.Ext(ff.Name())) // e.g text-file-with-vars.templated.txt
c.Assert(templatedFile, check.Equals, expectedOutput)
}
}
}
func readFile(fname string) string {
f, err := os.Open(fname)
if err != nil {
log.Fatalf("Failed to open file '%s': %v\n", fname, err)
}
reader := bufio.NewReader(f)
content, err := ioutil.ReadAll(reader)
if err != nil {
log.Fatalf("Failed to read file '%s': %v\n", fname, err)
}
data := ""
if filepath.Ext(fname) == ".b64" {
data = string(content)
} else {
data = base64.StdEncoding.EncodeToString(content)
}
return data
}

View File

@ -0,0 +1,13 @@
<html>
<head><title>Page for Foo</title></head>
<body>
Hello Foo Bar <p>
Click <a href="http://testurl.com/?rid=1234567">here</a> for a legit link.
</body>
<img alt='' style='display: none' src='http://testurl.local/track?rid=1234567'/>
</html>

View File

@ -0,0 +1,9 @@
<html>
<head><title>There are no variables here.</title></head>
<body>
There are no vars in this file.
</body>
</html>

View File

@ -0,0 +1,19 @@
The target's unique ID: 1234567
The target's first name: Foo
The target's last name: Bar
The target's position: Space Janitor
The target's email address: foo@bar.com
The spoofed sender: From Address
The URL to the tracking handler: http://testurl.local/track?rid=1234567
An alias for tracker image: <img alt='' style='display: none' src='http://testurl.local/track?rid=1234567'/>
The phishing URL: http://testurl.com/?rid=1234567
The base URL with the path and rid parameter stripped. Useful for making links to static files: http://testurl.com

View File

@ -0,0 +1 @@
There are no variables in this file.

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long