-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #568 from oasisprotocol/pro-wh/bugfix/chainutf8
evmnfts, evmtokens: utf-8-clean strings from chain
- Loading branch information
Showing
4 changed files
with
63 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package evm | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
"unicode/utf8" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestJSONRoundTrip(t *testing.T) { | ||
for _, s := range []string{ | ||
// Invalid UTF-8. | ||
"{\"a\": \"\xff\"}", | ||
"{\"\xff\": \"a\"}", | ||
// NUL bytes. | ||
"{\"a\": \"\x00\"}", | ||
"{\"\x00\": \"a\"}", | ||
// Codepoint zero. | ||
"{\"a\": \"\\u0000\"}", | ||
"{\"\\u0000\": \"a\"}", | ||
} { | ||
var parsed any | ||
err := json.Unmarshal([]byte(s), &parsed) | ||
if err != nil { | ||
// Erroring out while unmarshalling is fine. | ||
t.Logf("%#v -> parsing: %v\n", s, err) | ||
continue | ||
} | ||
roundTripBuf, err := json.Marshal(parsed) | ||
if err != nil { | ||
// Erroring out while marshalling is uncool, but not a showstopper. | ||
t.Logf("%#v -> stringifying: %v\n", s, err) | ||
continue | ||
} | ||
roundTrip := string(roundTripBuf) | ||
// If it gets through the round trip, it'd better be clean. | ||
require.True(t, utf8.ValidString(roundTrip), "%#v -> invalid UTF-8 %#v", s, roundTrip) | ||
require.NotContains(t, roundTrip, 0x0, "%#v -> contains NUL byte %#v", s, roundTrip) | ||
t.Logf("%#v -> %#v\n", s, roundTrip) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters