2015-09-23 17:30:00 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import "testing"
|
|
|
|
|
|
|
|
func TestStringValidation(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
value string
|
|
|
|
result bool
|
2015-09-23 19:13:54 +00:00
|
|
|
offset int
|
2015-09-23 17:30:00 +00:00
|
|
|
}{
|
2015-09-23 19:13:54 +00:00
|
|
|
{"aä\uFFFD本☺", false, 3},
|
|
|
|
{"aä本☺", true, -1},
|
2015-09-23 17:30:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
2015-09-23 19:13:54 +00:00
|
|
|
if i := InvalidUtf8Index([]byte(c.value)); i != c.offset {
|
|
|
|
t.Errorf("string %q - offset expected %d, got %d", c.value, c.offset, i)
|
|
|
|
}
|
2015-09-23 17:30:00 +00:00
|
|
|
if got := IsValidUtf8String(c.value); got != c.result {
|
|
|
|
t.Errorf("string %q - expected %v, got %v", c.value, c.result, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-23 19:13:54 +00:00
|
|
|
|
2015-09-23 17:30:00 +00:00
|
|
|
func TestBytesValidation(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
value []byte
|
|
|
|
result bool
|
2015-09-23 19:13:54 +00:00
|
|
|
offset int
|
2015-09-23 17:30:00 +00:00
|
|
|
}{
|
2015-09-23 19:13:54 +00:00
|
|
|
{[]byte{0xE4}, false, 0},
|
|
|
|
{[]byte("aä本☺"), true, -1},
|
2015-09-23 17:30:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
2015-09-23 19:13:54 +00:00
|
|
|
if i := InvalidUtf8Index(c.value); i != c.offset {
|
|
|
|
t.Errorf("bytes %q - offset expected %d, got %d", c.value, c.offset, i)
|
|
|
|
}
|
2015-09-23 17:30:00 +00:00
|
|
|
if got := IsValidUtf8Btyes(c.value); got != c.result {
|
|
|
|
t.Errorf("bytes %q - expected %v, got %v", c.value, c.result, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|