diff options
| -rw-r--r-- | cmd/rescribe/gbook.go | 12 | ||||
| -rw-r--r-- | cmd/rescribe/gbook_test.go | 17 | ||||
| -rw-r--r-- | cmd/rescribe/testdata/fuzz/FuzzGetBookIdFromUrl/174f82f558636f2a | 2 | ||||
| -rw-r--r-- | cmd/rescribe/testdata/fuzz/FuzzGetBookIdFromUrl/60892155cf2f7963 | 2 | 
4 files changed, 28 insertions, 5 deletions
| diff --git a/cmd/rescribe/gbook.go b/cmd/rescribe/gbook.go index 320f574..a011181 100644 --- a/cmd/rescribe/gbook.go +++ b/cmd/rescribe/gbook.go @@ -41,7 +41,9 @@ func formatAuthors(authors []string) string {  	s = strings.ToUpper(s)  	if len(s) > maxPartLength { -		s = s[:maxPartLength] +		// truncate to maxPartLength +		m := fmt.Sprintf("%%.%ds", maxPartLength) +		s = fmt.Sprintf(m, s)  	}  	s = strings.Map(stripNonLetters, s) @@ -63,7 +65,9 @@ func stripNonLetters(r rune) rune {  func formatTitle(title string) string {  	s := strings.Map(stripNonLetters, title)  	if len(s) > maxPartLength { -		s = s[:maxPartLength] +		// truncate to maxPartLength +		m := fmt.Sprintf("%%.%ds", maxPartLength) +		s = fmt.Sprintf(m, s)  	}  	return s  } @@ -232,7 +236,7 @@ func getBookIdFromUrl(url string) (string, error) {  		if start >= 0 {  			start += 4 -			if len(url[start:]) < 12 { +			if len(url) - start < 12 {  				return "", fmt.Errorf("Could not find book ID in URL")  			}  			return url[start : start+12], nil @@ -245,7 +249,7 @@ func getBookIdFromUrl(url string) (string, error) {  		if start >= 0 {  			start += 10 -			if len(url[start:]) < 12 { +			if len(url) - start < 12 {  				return "", fmt.Errorf("Could not find book ID in URL")  			}  			return url[start : start+12], nil diff --git a/cmd/rescribe/gbook_test.go b/cmd/rescribe/gbook_test.go index 56b4b40..f7df595 100644 --- a/cmd/rescribe/gbook_test.go +++ b/cmd/rescribe/gbook_test.go @@ -8,7 +8,7 @@ import (  	"testing"  ) -func Test_getBookIdFromUrl(t *testing.T) { +func TestGetBookIdFromUrl(t *testing.T) {  	cases := []struct {  		url string  		id  string @@ -29,3 +29,18 @@ func Test_getBookIdFromUrl(t *testing.T) {  		})  	}  } + +func FuzzGetBookIdFromUrl(f *testing.F) { +	cases := []string { +		"https://books.google.it/books?id=QjQepCuN8JYC", +		"https://www.google.it/books/edition/_/VJbr-Oe2au0C", +	} + +	for _, c := range cases { +		f.Add(c) +	} + +	f.Fuzz(func(t *testing.T, url string) { +		getBookIdFromUrl(url) +	}) +} diff --git a/cmd/rescribe/testdata/fuzz/FuzzGetBookIdFromUrl/174f82f558636f2a b/cmd/rescribe/testdata/fuzz/FuzzGetBookIdFromUrl/174f82f558636f2a new file mode 100644 index 0000000..1a7ed9c --- /dev/null +++ b/cmd/rescribe/testdata/fuzz/FuzzGetBookIdFromUrl/174f82f558636f2a @@ -0,0 +1,2 @@ +go test fuzz v1 +string("https://www0google\xf7/books/edition/_/") diff --git a/cmd/rescribe/testdata/fuzz/FuzzGetBookIdFromUrl/60892155cf2f7963 b/cmd/rescribe/testdata/fuzz/FuzzGetBookIdFromUrl/60892155cf2f7963 new file mode 100644 index 0000000..b637539 --- /dev/null +++ b/cmd/rescribe/testdata/fuzz/FuzzGetBookIdFromUrl/60892155cf2f7963 @@ -0,0 +1,2 @@ +go test fuzz v1 +string("https://Books.google\xc1&id=") | 
