From: Russ Cox Date: Sat, 13 Jul 2013 00:40:50 +0000 (-0400) Subject: encoding/json: add more tests for UTF-8 coercion X-Git-Tag: go1.2rc2~1057 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=4274d074dcf06fc67318d11962994ea19b2aff6b;p=gostls13.git encoding/json: add more tests for UTF-8 coercion Suggested by Rob in CL 11211045, but the mail arrived moments after hg submit completed. R=golang-dev, r CC=golang-dev https://golang.org/cl/11138045 --- diff --git a/src/pkg/encoding/json/decode_test.go b/src/pkg/encoding/json/decode_test.go index 65e6d6ec38..e868f4f1b7 100644 --- a/src/pkg/encoding/json/decode_test.go +++ b/src/pkg/encoding/json/decode_test.go @@ -391,12 +391,23 @@ func TestMarshal(t *testing.T) { } } +var badUTF8 = []struct { + in, out string +}{ + {"hello\xffworld", `"hello\ufffdworld"`}, + {"", `""`}, + {"\xff", `"\ufffd"`}, + {"\xff\xff", `"\ufffd\ufffd"`}, + {"a\xffb", `"a\ufffdb"`}, + {"\xe6\x97\xa5\xe6\x9c\xac\xff\xaa\x9e", `"日本\ufffd\ufffd\ufffd"`}, +} + func TestMarshalBadUTF8(t *testing.T) { - s := "hello\xffworld" - const enc = `"hello\ufffdworld"` - b, err := Marshal(s) - if string(b) != enc || err != nil { - t.Errorf("Marshal(%q) = %#q, %v, want %#q, nil", s, b, err, enc) + for _, tt := range badUTF8 { + b, err := Marshal(tt.in) + if string(b) != tt.out || err != nil { + t.Errorf("Marshal(%q) = %#q, %v, want %#q, nil", tt.in, b, err, tt.out) + } } }