Skip to content

Commit

Permalink
check invalid strings and add fuzzing
Browse files Browse the repository at this point in the history
  • Loading branch information
thevilledev committed May 18, 2023
1 parent 873c944 commit be7a7c9
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 30 deletions.
21 changes: 17 additions & 4 deletions thespine.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
package thespine

import (
"errors"
"unicode/utf8"
)

const THE_SIZE = 3

func Decode(s string) string {
var ErrInvalidString = errors.New("invalid string")

func Decode(s string) (string, error) {
if !utf8.ValidString(s) {
return "", ErrInvalidString
}
sr := []rune(s)
l := len(sr)
g := make([][]rune, 0)
Expand All @@ -19,10 +29,13 @@ func Decode(s string) string {
gs := sr[si:ei]
g = append(g, gs)
}
return concat(g)
return concat(g), nil
}

func Encode(s string) string {
func Encode(s string) (string, error) {
if !utf8.ValidString(s) {
return "", ErrInvalidString
}
sr := []rune(s)
l := len(sr)
g := make([][]rune, 0)
Expand All @@ -42,7 +55,7 @@ func Encode(s string) string {
for i, j := 0, len(g)-1; i < j; i, j = i+1, j-1 {
g[i], g[j] = g[j], g[i]
}
return concat(g)
return concat(g), nil
}

func concat(r [][]rune) string {
Expand Down
130 changes: 104 additions & 26 deletions thespine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,131 @@ import (
"testing"
)

func Test_EncodeDecode(t *testing.T) {
func Test_Decode(t *testing.T) {
tests := []struct {
str string
name string
want string
str string
name string
want string
wantErr bool
}{
{
str: "",
name: "empty string",
want: "",
str: "",
name: "empty string",
want: "",
wantErr: false,
},
{
str: "erecshyrinol",
name: "the song",
want: "nolyricshere",
str: "erecshyrinol",
name: "the song",
want: "nolyricshere",
wantErr: false,
},
{
str: "nespithe",
name: "the album",
want: "thespine",
str: "nespithe",
name: "the album",
want: "thespine",
wantErr: false,
},
{
str: "seteernkub",
name: "the tech",
want: "kubernetes",
str: "seteernkub",
name: "the tech",
want: "kubernetes",
wantErr: false,
},
{
str: "ᚬᚩᚡᚣ",
name: "the runes",
want: "ᚩᚡᚣᚬ",
str: "ᚬᚩᚡᚣ",
name: "the runes",
want: "ᚩᚡᚣᚬ",
wantErr: false,
},
{
str: "\xf0\x9f\x9a\x80ketroc",
name: "the cringe",
want: "rocket\xf0\x9f\x9a\x80",
str: "\xf2",
name: "the invalid string",
want: "",
wantErr: true,
},
{
str: "\xf0\x9f\x9a\x80ketroc",
name: "the cringe",
want: "rocket\xf0\x9f\x9a\x80",
wantErr: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := Decode(test.str)
got, err := Decode(test.str)
if test.wantErr && err == nil {
t.Fatalf("decode err wanted but got none")
}
if got != test.want {
t.Fatalf("decode got: %s\nwant: %s\n", got, test.want)
}
got = Encode(test.want)
if got != test.str {
t.Fatalf("encode got: %s\nwant: %s\n", got, test.str)
})
}
}

func Test_Encode(t *testing.T) {
tests := []struct {
str string
name string
want string
wantErr bool
}{
{
str: "",
name: "empty string",
want: "",
wantErr: false,
},
{
str: "nolyricshere",
name: "the song",
want: "erecshyrinol",
wantErr: false,
},
{
str: "\xf2",
name: "the invalid string",
want: "",
wantErr: true,
},
{
str: "rocket\xf0\x9f\x9a\x80",
name: "the cringe",
want: "\xf0\x9f\x9a\x80ketroc",
wantErr: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
enc, err := Encode(test.str)
if test.wantErr && err == nil {
t.Fatalf("encode err wanted but got none")
}
if enc != test.want {
t.Fatalf("encode got: %s\nwant: %s\n", enc, test.want)
}
})
}
}

func FuzzEncode(f *testing.F) {
tcs := []string{"rocket\xf0\x9f\x9a\x80", "abba acdc", "Hello, 世界!"}
for _, tc := range tcs {
f.Add(tc)
}
f.Fuzz(func(t *testing.T, og string) {
ec, _ := Encode(og)
if ec != "" {
dc, _ := Decode(ec)
ogr := []rune(og)
dcr := []rune(dc)
if og != dc {
t.Errorf("Before: '%q', after: '%q'", og, dc)
}
if len(ogr) != len(dcr) {
t.Errorf("Length before: %d, after: %d", len(og), len(dc))
}
}
})
}

0 comments on commit be7a7c9

Please sign in to comment.