Skip to content

Commit eb2526d

Browse files
committed
Learnt how to do fuzzing, create a new module called learn fuzzing and gone through the flow of fuzzing. Need furthur study
1 parent 8fb1141 commit eb2526d

File tree

6 files changed

+123
-0
lines changed

6 files changed

+123
-0
lines changed

learn_fuzzing/go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module batsy.com/main
2+
3+
go 1.20

learn_fuzzing/main.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"log"
7+
"unicode/utf8"
8+
)
9+
10+
func ReverseString(str string) (string, error) {
11+
12+
if !utf8.ValidString(str) {
13+
return "", errors.New("The string is not valid UTF-8 %s" + str)
14+
}
15+
b := []rune(str)
16+
17+
for i, j := 0, len(b)-1; i < len(b)/2; i, j = i+1, j-1 {
18+
b[i], b[j] = b[j], b[i]
19+
}
20+
21+
return string(b), nil
22+
}
23+
24+
func main() {
25+
26+
const NEW_STRING = "Aniket"
27+
28+
reversedString, error := ReverseString(NEW_STRING)
29+
30+
if error != nil {
31+
log.Fatal("Some error occured")
32+
}
33+
34+
doubleReversedString, error := ReverseString(reversedString)
35+
36+
if error != nil {
37+
log.Fatal("Some error occured")
38+
}
39+
40+
fmt.Println("The reversed string is", reversedString)
41+
fmt.Println("The double reversed string is", doubleReversedString)
42+
}

learn_fuzzing/main_test.go

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
"unicode/utf8"
7+
)
8+
9+
// This is a normal test case
10+
func TestReverseString(t *testing.T) {
11+
12+
testCases := []struct {
13+
in, want string
14+
}{
15+
{"Aniket", "tekinA"},
16+
{"hie", "eih"},
17+
{"Hue", "euH"},
18+
}
19+
20+
for _, tc := range testCases {
21+
reversedString, error := ReverseString(tc.in)
22+
23+
if error != nil {
24+
t.Errorf("Something Went Wrong")
25+
}
26+
27+
if reversedString != tc.want {
28+
t.Errorf("The input was %s, the reversed string which did not match was %s, which was expected to be %s", tc.in, reversedString, tc.want)
29+
}
30+
}
31+
}
32+
33+
func FuzzReverseString(f *testing.F) {
34+
35+
// add a fuzz test case
36+
37+
testcases := []string{
38+
"Aniket",
39+
"Namaste",
40+
"Jhingalala",
41+
// "",
42+
}
43+
44+
for _, tc := range testcases {
45+
// This adds a seed corpus, in simple terms I think it takes a sample input which it can make permutations of
46+
f.Add(tc)
47+
}
48+
49+
// This represents the actual testing scenario where we would run the test
50+
f.Fuzz(func(t *testing.T, originalString string) {
51+
reversedString, error := ReverseString(originalString)
52+
53+
if error != nil {
54+
fmt.Println(error)
55+
return
56+
}
57+
doubleReverse, error := ReverseString(reversedString)
58+
59+
if error != nil {
60+
t.Errorf("Some error occured in double Reverse string")
61+
return
62+
}
63+
64+
if originalString != doubleReverse {
65+
t.Errorf("The original string reference did not match")
66+
}
67+
68+
if utf8.ValidString(originalString) && !utf8.ValidString(reversedString) {
69+
t.Errorf("Reverse produced invalid UTF-8 string %q", reversedString)
70+
}
71+
})
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
go test fuzz v1
2+
string("̴")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
go test fuzz v1
2+
string("\xd7")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
go test fuzz v1
2+
string("0\xd7")

0 commit comments

Comments
 (0)