-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
file reader pattern, credits for this to @evilsocket
- Loading branch information
1 parent
cba2bab
commit 713ee5c
Showing
3 changed files
with
105 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package fuzzer | ||
|
||
import ( | ||
"bufio" | ||
"io" | ||
"os" | ||
) | ||
|
||
func Reader(filename string, noff int64) (chan string, int64, error) { | ||
fp, err := os.Open(filename) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
|
||
// if offset defined then start from there | ||
if noff > 0 { | ||
// and go to the start of the line | ||
b := make([]byte, 1) | ||
for b[0] != '\n' { | ||
noff-- | ||
if _, err := fp.Seek(noff, io.SeekStart); err != nil { | ||
fp.Close() | ||
return nil, 0, err | ||
} | ||
if _, err := fp.Read(b); err != nil { | ||
fp.Close() | ||
return nil, 0, err | ||
} | ||
} | ||
noff++ | ||
} | ||
|
||
out := make(chan string) | ||
go func() { | ||
defer fp.Close() | ||
defer close(out) | ||
scanner := bufio.NewScanner(fp) | ||
scanner.Split(bufio.ScanLines) | ||
for scanner.Scan() { | ||
if _, err := fp.Seek(0, io.SeekCurrent); err != nil { | ||
return // Stop reading and return if seeking fails | ||
} | ||
out <- scanner.Text() | ||
noff += int64(len(scanner.Bytes()) + 1) // Update noff | ||
} | ||
}() | ||
|
||
return out, noff, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package fuzzer_test | ||
|
||
import ( | ||
"os" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/CyberRoute/bruter/pkg/fuzzer" | ||
) | ||
|
||
func TestReader(t *testing.T) { | ||
// Create a temporary file with some content | ||
tmpfile, err := os.CreateTemp("", "example") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer os.Remove(tmpfile.Name()) // Clean up the file | ||
|
||
// Write some content to the file | ||
content := "line1\nline2\nline3\n" | ||
if _, err := tmpfile.WriteString(content); err != nil { | ||
t.Fatal(err) | ||
} | ||
tmpfile.Close() | ||
|
||
// Test the Reader function with default offset (0) | ||
ch, offset, err := fuzzer.Reader(tmpfile.Name(), 0) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if offset != 0 { | ||
t.Errorf("Expected default offset should be %d", offset) | ||
} | ||
|
||
// Read all lines from the channel into a slice | ||
var lines []string | ||
for line := range ch { | ||
lines = append(lines, line) | ||
} | ||
|
||
// Verify the lines read from the channel | ||
expectedLines := []string{"line1", "line2", "line3"} | ||
if !reflect.DeepEqual(lines, expectedLines) { | ||
t.Errorf("Expected lines %v, got %v", expectedLines, lines) | ||
} | ||
} |