encoding/protojson: add random whitespaces in encoding output
This is meant to deter users from doing byte for byte comparison.
Change-Id: If005d2dc1eba45eaa4254171d2f247820db109e4
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/194037
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
diff --git a/internal/encoding/json/encode.go b/internal/encoding/json/encode.go
index 39a5014..741f34f 100644
--- a/internal/encoding/json/encode.go
+++ b/internal/encoding/json/encode.go
@@ -8,6 +8,7 @@
"strconv"
"strings"
+ "google.golang.org/protobuf/internal/detrand"
"google.golang.org/protobuf/internal/errors"
)
@@ -132,6 +133,11 @@
if e.lastType&(Null|Bool|Number|String|EndObject|EndArray) != 0 &&
next&(Name|Null|Bool|Number|String|StartObject|StartArray) != 0 {
e.out = append(e.out, ',')
+ // For single-line output, add a random extra space after each
+ // comma to make output unstable.
+ if detrand.Bool() {
+ e.out = append(e.out, ' ')
+ }
}
return
}
@@ -160,5 +166,10 @@
case e.lastType&Name != 0:
e.out = append(e.out, ' ')
+ // For multi-line output, add a random extra space after key: to make
+ // output unstable.
+ if detrand.Bool() {
+ e.out = append(e.out, ' ')
+ }
}
}
diff --git a/internal/encoding/json/encode_test.go b/internal/encoding/json/encode_test.go
index b98f98b..99ecb1f 100644
--- a/internal/encoding/json/encode_test.go
+++ b/internal/encoding/json/encode_test.go
@@ -11,9 +11,13 @@
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
+ "google.golang.org/protobuf/internal/detrand"
"google.golang.org/protobuf/internal/encoding/json"
)
+// Disable detrand to enable direct comparisons on outputs.
+func init() { detrand.Disable() }
+
// splitLines is a cmpopts.Option for comparing strings with line breaks.
var splitLines = cmpopts.AcyclicTransformer("SplitLines", func(s string) []string {
return strings.Split(s, "\n")