| 1 | package output |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/ffuf/ffuf/v2/pkg/ffuf" |
| 8 | ) |
| 9 | |
| 10 | func TestAuditLogger(t *testing.T) { |
| 11 | file, _ := os.CreateTemp("", "prefix") |
| 12 | filename := file.Name() |
| 13 | file.Close() |
| 14 | audit, err := NewAuditLogger(filename) |
| 15 | |
| 16 | if err != nil { |
| 17 | t.Errorf("Error creating audit logger: %s", err) |
| 18 | } |
| 19 | |
| 20 | if audit == nil { |
| 21 | t.Errorf("Audit was nil, expected non-nil") |
| 22 | } |
| 23 | |
| 24 | defer audit.Close() |
| 25 | |
| 26 | err = os.Chmod(filename, 000) |
| 27 | if err != nil { |
| 28 | t.Errorf("Error executing Chmod: %s", err) |
| 29 | } |
| 30 | |
| 31 | audit, err = NewAuditLogger(filename) |
| 32 | |
| 33 | if err == nil { |
| 34 | t.Errorf("Error was nil, expected non-nil") |
| 35 | } |
| 36 | |
| 37 | if audit != nil { |
| 38 | t.Errorf("Audit was non-nil, expected nil") |
| 39 | } |
| 40 | |
| 41 | err = os.Chmod(filename, 6550) |
| 42 | if err != nil { |
| 43 | t.Errorf("Error executing Chmod: %s", err) |
| 44 | } |
| 45 | |
| 46 | err = os.Remove(filename) |
| 47 | if err != nil { |
| 48 | t.Errorf("Error executing Remove: %s", err) |
| 49 | } |
| 50 | |
| 51 | } |
| 52 | |
| 53 | func TestAuditLogWrite(t *testing.T) { |
| 54 | expected := `{"Type":"ffuf.Config","Data":{"auditlog":"","autocalibration":false,"autocalibration_keyword":"","autocalibration_perhost":false,"autocalibration_strategies":null,"autocalibration_strings":null,"colors":false,"cmdline":"","configfile":"","postdata":"{\"quote\":\"I'll still be here tomorrow to high five you yesterday, my friend. Peace.\"}","debuglog":"","delay":{"Min":0,"Max":0,"IsRange":false,"HasDelay":false},"dirsearch_compatibility":false,"encoders":null,"extensions":null,"fmode":"","follow_redirects":false,"headers":{"Content-Type":"application/json","baz":"wibble","foo":"bar"},"ignorebody":false,"ignore_wordlist_comments":false,"inputmode":"","cmd_inputnum":0,"inputproviders":null,"inputshell":"","json":false,"matchers":null,"mmode":"","maxtime":0,"maxtime_job":0,"method":"POST","noninteractive":false,"outputdirectory":"","outputfile":"","outputformat":"","OutputSkipEmptyFile":false,"proxyurl":"","quiet":false,"rate":0,"raw":false,"recursion":false,"recursion_depth":0,"recursion_strategy":"","replayproxyurl":"","requestfile":"","requestproto":"","scraperfile":"","scrapers":"","sni":"","stop_403":false,"stop_all":false,"stop_errors":false,"threads":0,"timeout":0,"url":"http://example.com/aaaa","verbose":false,"wordlists":null,"http2":false,"client-cert":"","client-key":""}} |
| 55 | {"Type":"ffuf.Request","Data":{"Method":"POST","Host":"","Url":"http://example.com/aaaa","Headers":{"Content-Type":"application/json","baz":"wibble","foo":"bar"},"Data":"eyJxdW90ZSI6IkknbGwgc3RpbGwgYmUgaGVyZSB0b21vcnJvdyB0byBoaWdoIGZpdmUgeW91IHllc3RlcmRheSwgbXkgZnJpZW5kLiBQZWFjZS4ifQ==","Input":null,"Position":0,"Raw":"","Error":"","Timestamp":"0001-01-01T00:00:00Z"}} |
| 56 | ` |
| 57 | |
| 58 | headers := make(map[string]string) |
| 59 | headers["foo"] = "bar" |
| 60 | headers["baz"] = "wibble" |
| 61 | headers["Content-Type"] = "application/json" |
| 62 | |
| 63 | data := "{\"quote\":\"I'll still be here tomorrow to high five you yesterday, my friend. Peace.\"}" |
| 64 | |
| 65 | req := ffuf.Request{Method: "POST", Url: "http://example.com/aaaa", Headers: headers, Data: []byte(data)} |
| 66 | config := ffuf.Config{Method: "POST", Url: "http://example.com/aaaa", Headers: headers, Data: data} |
| 67 | |
| 68 | file, _ := os.CreateTemp("", "prefix") |
| 69 | filename := file.Name() |
| 70 | file.Close() |
| 71 | audit, err := NewAuditLogger(filename) |
| 72 | |
| 73 | if err != nil { |
| 74 | t.Errorf("Error creating audit logger: %s", err) |
| 75 | } |
| 76 | |
| 77 | if audit == nil { |
| 78 | t.Errorf("Audit was nil, expected non-nil") |
| 79 | } |
| 80 | |
| 81 | // test attempting to log a bung object and erroring |
| 82 | type A struct{ A *A } // cyclic struct to break JSON Marshalling |
| 83 | a := A{} |
| 84 | a.A = &a |
| 85 | |
| 86 | err = audit.Write(a) |
| 87 | if err == nil { |
| 88 | t.Errorf("Error was nil, expected non-nil") |
| 89 | } |
| 90 | |
| 91 | // test writing the config and request objects |
| 92 | err = audit.Write(config) |
| 93 | if err != nil { |
| 94 | t.Errorf("Error was not nil, expected nil") |
| 95 | } |
| 96 | |
| 97 | err = audit.Write(req) |
| 98 | if err != nil { |
| 99 | t.Errorf("Error was not nil, expected nil") |
| 100 | } |
| 101 | |
| 102 | audit.Close() |
| 103 | |
| 104 | // re-read and check the audit log |
| 105 | f, err := os.ReadFile(filename) |
| 106 | if err != nil { |
| 107 | t.Errorf("Error reading audit log: %s", err) |
| 108 | } |
| 109 | |
| 110 | if string(f) != expected { |
| 111 | t.Errorf("Audit write did not produce the expected logfile.") |
| 112 | } |
| 113 | |
| 114 | // writing to a closed audit log should fail |
| 115 | err = audit.Write(config) |
| 116 | if err == nil { |
| 117 | t.Errorf("Error was nil, expected non-nil") |
| 118 | } |
| 119 | } |