Skip to content

Commit

Permalink
update old method
Browse files Browse the repository at this point in the history
  • Loading branch information
firefart committed Oct 28, 2023
1 parent 4313627 commit c6784bc
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
12 changes: 11 additions & 1 deletion libgobuster/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,27 @@ func lineCounter_old(r io.Reader) (int, error) {
buf := make([]byte, 32*1024)
count := 1
lineSep := []byte{'\n'}
var lastChar byte

for {
c, err := r.Read(buf)
count += bytes.Count(buf[:c], lineSep)

// store last character received if we got any bytes
if c > 0 {
lastChar = buf[c-1]
}

switch {
case errors.Is(err, io.EOF):
// account for trailing new line
if lastChar == '\n' {
count = count - 1
}
return count, nil

case err != nil:
return count, err
return -1, err
}
}
}
Expand Down
62 changes: 62 additions & 0 deletions libgobuster/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package libgobuster
import (
"errors"
"fmt"
"os"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -169,6 +170,8 @@ func TestLineCounter(t *testing.T) {
{"3 Lines cr lf", "TestString\r\nTest\r\n1234", 3},
{"3 Lines cr lf with comment", "TestString\r\n# Test\r\n1234", 2},
{"Empty", "", 0},
{"Empty 2", "\n", 0},
{"Empty 3", "\r\n", 0},
}
for _, x := range tt {
x := x // NOTE: https://github.com/golang/go/wiki/CommonMistakes#using-goroutines-on-loop-iterator-variables
Expand All @@ -186,6 +189,65 @@ func TestLineCounter(t *testing.T) {
}
}

func TestLineCounterOld(t *testing.T) {
t.Parallel()
var tt = []struct {
testName string
s string
expected int
}{
{"One Line", "test", 1},
{"3 Lines", "TestString\nTest\n1234", 3},
{"Trailing newline", "TestString\nTest\n1234\n", 3},
{"3 Lines cr lf", "TestString\r\nTest\r\n1234", 3},
{"Empty", "", 1}, // these are wrong but I've found no good way to handle those
{"Empty 2", "\n", 1}, // these are wrong but I've found no good way to handle those
{"Empty 3", "\r\n", 1}, // these are wrong but I've found no good way to handle those
}
for _, x := range tt {
x := x // NOTE: https://github.com/golang/go/wiki/CommonMistakes#using-goroutines-on-loop-iterator-variables
t.Run(x.testName, func(t *testing.T) {
t.Parallel()
r := strings.NewReader(x.s)
l, err := lineCounter_old(r)
if err != nil {
t.Fatalf("Got error: %v", err)
}
if l != x.expected {
t.Fatalf("wrong line count! Got %d expected %d", l, x.expected)
}
})
}
}

func BenchmarkLineCounter(b *testing.B) {
for i := 0; i < b.N; i++ {
r, err := os.Open("../rockyou.txt")
if err != nil {
b.Fatalf("Got error: %v", err)
}
defer r.Close()
_, err = lineCounter(r)
if err != nil {
b.Fatalf("Got error: %v", err)
}
}
}

func BenchmarkLineCounterOld(b *testing.B) {
for i := 0; i < b.N; i++ {
r, err := os.Open("../rockyou.txt")
if err != nil {
b.Fatalf("Got error: %v", err)
}
defer r.Close()
_, err = lineCounter_old(r)
if err != nil {
b.Fatalf("Got error: %v", err)
}
}
}

func TestLineCounterError(t *testing.T) {
t.Parallel()
r := iotest.TimeoutReader(strings.NewReader("test"))
Expand Down

0 comments on commit c6784bc

Please sign in to comment.