Skip to content

Commit

Permalink
add sponge mutator
Browse files Browse the repository at this point in the history
  • Loading branch information
batmac committed Aug 9, 2022
1 parent 0024cd8 commit 2fe9599
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
21 changes: 21 additions & 0 deletions pkg/mutators/simple/simpleSponge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package mutators

import (
"bytes"
"io"

"github.com/batmac/ccat/pkg/log"
)

func init() {
simpleRegister("sponge", sponge, withDescription("soak all input before outputting it."))
}

func sponge(w io.WriteCloser, r io.ReadCloser) (int64, error) {
d, err := io.ReadAll(r) // NOT streamable (that the point :p)
if err != nil {
return 0, err
}
log.Debugf("soaked %d bytes\n", len(d))
return io.Copy(w, bytes.NewReader(d))
}
28 changes: 28 additions & 0 deletions pkg/mutators/simple/simpleSponge_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package mutators_test

import (
"testing"

"github.com/batmac/ccat/pkg/mutators"
)

func Test_sponge(t *testing.T) {
tests := []struct {
name, decoded, encoded string
}{
{"empty", "", ""},
{"simple", "{}", "{}"},
{"abc", "abc", "abc"},
{"nl", "\n", "\n"},
{"3x nl", "\n\n\n", "\n\n\n"},
}

f := "sponge"
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := mutators.Run(f, tt.decoded); got != tt.encoded {
t.Errorf("%s = %v, want %v", f, got, tt.encoded)
}
})
}
}

0 comments on commit 2fe9599

Please sign in to comment.