Skip to content

Commit

Permalink
add the possibility to move forward (with L & l) and backwards (with …
Browse files Browse the repository at this point in the history
…H & h)
  • Loading branch information
brenol committed Oct 17, 2020
1 parent 2d5bc7a commit e7c22da
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 4 deletions.
28 changes: 24 additions & 4 deletions gui/keybinds.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,37 @@ import (
"github.com/nakabonne/ali/attacker"
)

func navigateCharts(chartFuncs []func()) func(bool) {
position := -1
numFuncs := len(chartFuncs)
return func(backwards bool) {
position++
if backwards {
position -= 2
if position < 0 {
position = numFuncs - 1
}
}
chartFuncs[position%numFuncs]()
}
}

func keybinds(ctx context.Context, cancel context.CancelFunc, c *container.Container, dr *drawer, targetURL string, opts attacker.Options) func(*terminalapi.Keyboard) {
funcs := []func(){
func() { c.Update(chartID, dr.gridOpts.latency...) },
func() { c.Update(chartID, dr.gridOpts.percentiles...) },
}
navigateFunc := navigateCharts(funcs)
return func(k *terminalapi.Keyboard) {
switch k.Key {
case keyboard.KeyCtrlC: // Quit
cancel()
case keyboard.KeyEnter: // Attack
attack(ctx, dr, targetURL, opts)
case keyboard.KeyCtrlP: // percentiles chart
c.Update(chartID, dr.gridOpts.percentiles...)
case keyboard.KeyCtrlL: // latency chart
c.Update(chartID, dr.gridOpts.latency...)
case 'H', 'h': // backwards
navigateFunc(true)
case 'L', 'l': // forwards
navigateFunc(false)
}
}
}
Expand Down
138 changes: 138 additions & 0 deletions gui/keybinds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,144 @@ func TestKeybinds(t *testing.T) {
}
}

func TestNavigateCharts(t *testing.T) {
type test struct {
name string
modifiedByFuncs string
assert func(*testing.T, *test)

callTimes int
backwards bool
funcs func(st *test) []func()
}

tests := []test{
{
name: "A single func should always be called, when called forwardly",
funcs: func(st *test) []func() {
return []func(){func() { st.modifiedByFuncs += "a" }}
},
modifiedByFuncs: "",
callTimes: 5,
backwards: false,
assert: func(t *testing.T, tst *test) {
want := "aaaaa"
if tst.modifiedByFuncs != want {
t.Errorf("unexpected result of modifiedByFuncs: want: %s; got: %s", want, tst.modifiedByFuncs)
}
},
},
{
name: "A single func should always be called backwards as well",
funcs: func(st *test) []func() {
return []func(){func() { st.modifiedByFuncs += "a" }}
},
modifiedByFuncs: "",
callTimes: 5,
backwards: true,
assert: func(t *testing.T, tst *test) {
want := "aaaaa"
if tst.modifiedByFuncs != want {
t.Errorf("unexpected result of modifiedByFuncs: want: %s; got: %s", want, tst.modifiedByFuncs)
}
},
},
{
name: "Navigate functions ",
funcs: func(st *test) []func() {
return []func(){
func() { st.modifiedByFuncs += "a" },
func() { st.modifiedByFuncs += "b" },
}
},
modifiedByFuncs: "",
callTimes: 5,
backwards: false,
assert: func(t *testing.T, tst *test) {
want := "ababa"
if tst.modifiedByFuncs != want {
t.Errorf("unexpected result of modifiedByFuncs: want: %s; got: %s", want, tst.modifiedByFuncs)
}
},
},
{
name: "navigate backwards",
funcs: func(st *test) []func() {
return []func(){
func() { st.modifiedByFuncs += "a" },
func() { st.modifiedByFuncs += "b" },
}
},
modifiedByFuncs: "",
callTimes: 5,
backwards: true,
assert: func(t *testing.T, tst *test) {
want := "babab"
if tst.modifiedByFuncs != want {
t.Errorf("unexpected result of modifiedByFuncs: want: %s; got: %s", want, tst.modifiedByFuncs)
}
},
},
{
name: "with way more funcs",
funcs: func(st *test) []func() {
return []func(){
func() { st.modifiedByFuncs += "a" },
func() { st.modifiedByFuncs += "b" },
func() { st.modifiedByFuncs += "c" },
func() { st.modifiedByFuncs += "d" },
func() { st.modifiedByFuncs += "e" },
}
},
modifiedByFuncs: "",
callTimes: 10,
backwards: false,
assert: func(t *testing.T, tst *test) {
want := "abcdeabcde"
if tst.modifiedByFuncs != want {
t.Errorf("unexpected result of modifiedByFuncs: want: %s; got: %s", want, tst.modifiedByFuncs)
}
},
},
{
name: "with two greedy funcs that do not want others to get work done",
funcs: func(st *test) []func() {
return []func(){
func() {
st.backwards = false
st.modifiedByFuncs += "a"
},
func() {
st.backwards = true
st.modifiedByFuncs += "b"
},
func() { st.modifiedByFuncs += "c" },
func() { st.modifiedByFuncs += "d" },
func() { st.modifiedByFuncs += "e" },
}
},
modifiedByFuncs: "",
callTimes: 10,
backwards: false,
assert: func(t *testing.T, tst *test) {
want := "ababababab"
if tst.modifiedByFuncs != want {
t.Errorf("unexpected result of modifiedByFuncs: want: %s; got: %s", want, tst.modifiedByFuncs)
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fn := navigateCharts(tt.funcs(&tt))
for i := 0; i < tt.callTimes; i++ {
fn(tt.backwards)
}
tt.assert(t, &tt)
})
}
}

func TestAttack(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
Expand Down

0 comments on commit e7c22da

Please sign in to comment.