2017-02-16 18:09:53 +00:00
|
|
|
package audit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/vault/helper/salt"
|
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
)
|
|
|
|
|
|
|
|
type noopFormatWriter struct {
|
2017-05-24 00:36:20 +00:00
|
|
|
salt *salt.Salt
|
|
|
|
SaltFunc func() (*salt.Salt, error)
|
2017-02-16 18:09:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopFormatWriter) WriteRequest(_ io.Writer, _ *AuditRequestEntry) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *noopFormatWriter) WriteResponse(_ io.Writer, _ *AuditResponseEntry) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-24 00:36:20 +00:00
|
|
|
func (n *noopFormatWriter) Salt() (*salt.Salt, error) {
|
|
|
|
if n.salt != nil {
|
|
|
|
return n.salt, nil
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
n.salt, err = salt.NewSalt(nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-02-16 18:09:53 +00:00
|
|
|
}
|
2017-05-24 00:36:20 +00:00
|
|
|
return n.salt, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFormatRequestErrors(t *testing.T) {
|
|
|
|
config := FormatterConfig{}
|
2017-02-16 18:09:53 +00:00
|
|
|
formatter := AuditFormatter{
|
|
|
|
AuditFormatWriter: &noopFormatWriter{},
|
|
|
|
}
|
|
|
|
|
2018-03-02 17:18:39 +00:00
|
|
|
if err := formatter.FormatRequest(ioutil.Discard, config, &LogInput{}); err == nil {
|
2017-02-16 18:09:53 +00:00
|
|
|
t.Fatal("expected error due to nil request")
|
|
|
|
}
|
2018-03-02 17:18:39 +00:00
|
|
|
|
|
|
|
in := &LogInput{
|
|
|
|
Request: &logical.Request{},
|
|
|
|
}
|
|
|
|
if err := formatter.FormatRequest(nil, config, in); err == nil {
|
2017-02-16 18:09:53 +00:00
|
|
|
t.Fatal("expected error due to nil writer")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFormatResponseErrors(t *testing.T) {
|
2017-05-24 00:36:20 +00:00
|
|
|
config := FormatterConfig{}
|
2017-02-16 18:09:53 +00:00
|
|
|
formatter := AuditFormatter{
|
|
|
|
AuditFormatWriter: &noopFormatWriter{},
|
|
|
|
}
|
|
|
|
|
2018-03-02 17:18:39 +00:00
|
|
|
if err := formatter.FormatResponse(ioutil.Discard, config, &LogInput{}); err == nil {
|
2017-02-16 18:09:53 +00:00
|
|
|
t.Fatal("expected error due to nil request")
|
|
|
|
}
|
2018-03-02 17:18:39 +00:00
|
|
|
|
|
|
|
in := &LogInput{
|
|
|
|
Request: &logical.Request{},
|
|
|
|
}
|
|
|
|
if err := formatter.FormatResponse(nil, config, in); err == nil {
|
2017-02-16 18:09:53 +00:00
|
|
|
t.Fatal("expected error due to nil writer")
|
|
|
|
}
|
|
|
|
}
|