1 개요[ | ]
- Tutorial: Getting started with fuzzing
- 튜토리얼: 퍼징 시작하기
2 Prerequisites[ | ]
3 Create a folder for your code[ | ]
4 Add code to test[ | ]
4.1 Write the code[ | ]
4.2 Run the code[ | ]
5 Add a unit test[ | ]
5.1 Write the code[ | ]
5.2 Run the code[ | ]
6 Add a fuzz test[ | ]
6.1 Write the code[ | ]
6.2 Run the code[ | ]
7 Fix the invalid string error[ | ]
7.1 Diagnose the error[ | ]
7.2 Write the code[ | ]
7.3 Run the code[ | ]
7.4 Fix the error[ | ]
7.4.1 Write the code[ | ]
7.4.2 Run the code[ | ]
8 Fix the double reverse error[ | ]
8.1 Diagnose the error[ | ]
8.1.1 Write the code[ | ]
8.1.2 Run the code[ | ]
8.2 Fix the error[ | ]
8.2.1 Write the code[ | ]
8.2.2 Run the code[ | ]
9 Conclusion[ | ]
10 Completed code[ | ]
main.go
Go
Copy
package main
import (
"errors"
"fmt"
"unicode/utf8"
)
func main() {
input := "The quick brown fox jumped over the lazy dog"
rev, revErr := Reverse(input)
doubleRev, doubleRevErr := Reverse(rev)
fmt.Printf("original: %q\n", input)
fmt.Printf("reversed: %q, err: %v\n", rev, revErr)
fmt.Printf("reversed again: %q, err: %v\n", doubleRev, doubleRevErr)
}
func Reverse(s string) (string, error) {
if !utf8.ValidString(s) {
return s, errors.New("input is not valid UTF-8")
}
r := []rune(s)
for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
r[i], r[j] = r[j], r[i]
}
return string(r), nil
}
reverse_test.go
Go
Copy
package main
import (
"testing"
"unicode/utf8"
)
func FuzzReverse(f *testing.F) {
testcases := []string{"Hello, world", " ", "!12345"}
for _, tc := range testcases {
f.Add(tc) // Use f.Add to provide a seed corpus
}
f.Fuzz(func(t *testing.T, orig string) {
rev, err1 := Reverse(orig)
if err1 != nil {
return
}
doubleRev, err2 := Reverse(rev)
if err2 != nil {
return
}
if orig != doubleRev {
t.Errorf("Before: %q, after: %q", orig, doubleRev)
}
if utf8.ValidString(orig) && !utf8.ValidString(rev) {
t.Errorf("Reverse produced invalid UTF-8 string %q", rev)
}
})
}
11 같이 보기[ | ]
편집자 Jmnote
로그인하시면 댓글을 쓸 수 있습니다.