Skip to content

Commit

Permalink
update construction of options
Browse files Browse the repository at this point in the history
  • Loading branch information
1nv8rzim committed Oct 24, 2023
1 parent 27b595a commit 06c5454
Showing 1 changed file with 44 additions and 73 deletions.
117 changes: 44 additions & 73 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,93 +13,67 @@ import (
"github.com/jlaffaye/ftp"
)

type expectedOutputStruct struct {
type optionsStruct struct {
// Check if file exists
Exists bool `compscore:"exists"`

// Check if contents of file matches a substring
SubstringMatch string `compscore:"substring_match"`
SubstringMatch bool `compscore:"substring_match"`

// Check if contents of file matches a regex
RegexMatch string `compscore:"regex_match"`
RegexMatch bool `compscore:"regex_match"`

// Check if contents of file matches a string exactly
Match string `compscore:"match"`
Match bool `compscore:"match"`

// Sha256 hash of the expected output
Sha256 string `compscore:"sha256"`
Sha256 bool `compscore:"sha256"`

// Md5 hash of the expected output
Md5 string `compscore:"md5"`
Md5 bool `compscore:"md5"`

// Sha1 hash of the expected output
Sha1 string `compscore:"sha1"`
Sha1 bool `compscore:"sha1"`
}

func (e *expectedOutputStruct) Unmarshal(options map[string]interface{}) error {
func (e *optionsStruct) Unmarshal(options map[string]interface{}) {
_, ok := options["exists"]
if ok {
e.Exists = true
}

substringMatchInterface, ok := options["substring_match"]
_, ok = options["substring_match"]
if ok {
substringMatch, ok := substringMatchInterface.(string)
if !ok {
return fmt.Errorf("substring_match must be a string")
}
e.SubstringMatch = substringMatch
e.SubstringMatch = true
}

regexMatchInterface, ok := options["regex_match"]
_, ok = options["regex_match"]
if ok {
regexMatch, ok := regexMatchInterface.(string)
if !ok {
return fmt.Errorf("regex_match must be a string")
}
e.RegexMatch = regexMatch
e.RegexMatch = true
}

matchInterface, ok := options["match"]
_, ok = options["match"]
if ok {
match, ok := matchInterface.(string)
if !ok {
return fmt.Errorf("match must be a string")
}
e.Match = match
e.Match = true
}

sha256Interface, ok := options["sha256"]
_, ok = options["sha256"]
if ok {
sha256, ok := sha256Interface.(string)
if !ok {
return fmt.Errorf("sha256 must be a string")
}
e.Sha256 = sha256
e.Sha256 = true
}

md5Interface, ok := options["md5"]
_, ok = options["md5"]
if ok {
md5, ok := md5Interface.(string)
if !ok {
return fmt.Errorf("md5 must be a string")
}
e.Md5 = md5
e.Md5 = true
}

sha1Interface, ok := options["sha1"]
_, ok = options["sha1"]
if ok {
sha1, ok := sha1Interface.(string)
if !ok {
return fmt.Errorf("sha1 must be a string")
}
e.Sha1 = sha1
e.Sha1 = true
}

return nil
}

func (e *expectedOutputStruct) Compare(resp *ftp.Response) error {
func (e *optionsStruct) Compare(expectedOutput string, resp *ftp.Response) error {
if resp == nil {
return fmt.Errorf("file does not exist")
}
Expand All @@ -110,54 +84,57 @@ func (e *expectedOutputStruct) Compare(resp *ftp.Response) error {
}
body := string(bodyBytes)

if e.SubstringMatch != "" {
if !strings.Contains(body, e.SubstringMatch) {
return fmt.Errorf("substring match mistmatch: execpted \"%s\"", e.SubstringMatch)
if e.SubstringMatch {
if !strings.Contains(body, expectedOutput) {
return fmt.Errorf("substring match mistmatch: execpted \"%s\"", expectedOutput)
}
}

if e.RegexMatch != "" {
pattern, err := regexp.Compile(e.RegexMatch)
if e.RegexMatch {
pattern, err := regexp.Compile(expectedOutput)
if err != nil {
return fmt.Errorf("invalid regex pattern: \"%s\"", err)
return fmt.Errorf("invalid regex pattern: \"%s\"; err: \"%s\"", expectedOutput, err)
}

if !pattern.MatchString(body) {
return fmt.Errorf("regex match mitmatch: expected \"%s\"", e.RegexMatch)
return fmt.Errorf("regex match mismatch: expected \"%s\" got \"%s\"", expectedOutput, body)
}
}

if e.Match != "" {
if body != e.Match {
return fmt.Errorf("match mismatch: expected \"%s\", \"%s\"", e.Match, body)
if e.Match {
if body != expectedOutput {
return fmt.Errorf("match mismatch: expected \"%s\" got \"%s\"", expectedOutput, body)
}
}

if e.Sha256 != "" {
if e.Sha256 {
hash := fmt.Sprintf("%x", sha256.Sum256(bodyBytes))
if hash != e.Sha256 {
return fmt.Errorf("sha256 mismatch: expected \"%s\", \"%s\"", e.Sha256, hash)
if hash != expectedOutput {
return fmt.Errorf("sha256 mismatch: expected \"%s\" got \"%s\"", expectedOutput, hash)
}
}

if e.Md5 != "" {
if e.Md5 {
hash := fmt.Sprintf("%x", md5.Sum(bodyBytes))
if hash != e.Md5 {
return fmt.Errorf("md5 mismatch: expected \"%s\", \"%s\"", e.Md5, hash)
if hash != expectedOutput {
return fmt.Errorf("md5 mismatch: expected \"%s\" got \"%s\"", expectedOutput, hash)
}
}

if e.Sha1 != "" {
if e.Sha1 {
hash := fmt.Sprintf("%x", sha1.Sum(bodyBytes))
if hash != e.Sha1 {
return fmt.Errorf("sha1 mismatch: expected \"%s\", \"%s\"", e.Sha1, hash)
if hash != expectedOutput {
return fmt.Errorf("sha1 mismatch: expected \"%s\" got \"%s\"", expectedOutput, hash)
}
}

return nil
}

func Run(ctx context.Context, target string, command string, expectedOutput string, username string, password string, options map[string]interface{}) (bool, string) {
o := &optionsStruct{}
o.Unmarshal(options)

if !strings.Contains(target, ":") {
target = target + ":21"
}
Expand Down Expand Up @@ -190,13 +167,7 @@ func Run(ctx context.Context, target string, command string, expectedOutput stri
}
defer resp.Close()

output := &expectedOutputStruct{}
err = output.Unmarshal(options)
if err != nil {
return false, fmt.Sprintf("failed to parse expected output: %s", err)
}

err = output.Compare(resp)
err = o.Compare(expectedOutput, resp)
if err != nil {
return false, fmt.Sprintf("failed to compare expected output: %s", err)
}
Expand Down

0 comments on commit 06c5454

Please sign in to comment.