Skip to content

Commit df20b24

Browse files
committed
proto: add explicit test for a non-nil but empty byte slice
I encountered this usage pattern in a production application and noticed that when breaking the size calculation, only the conformance_test fails (indirectly), but there was no explicit test before this commit. Change-Id: I15a125b7cff3364f24fe372e4a3a046bb7552413 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/710855 Reviewed-by: Chressie Himpel <chressie@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent 059534d commit df20b24

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

proto/size_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55
package proto_test
66

77
import (
8+
"testing"
9+
10+
"google.golang.org/protobuf/encoding/prototext"
11+
"google.golang.org/protobuf/encoding/protowire"
812
"google.golang.org/protobuf/proto"
13+
"google.golang.org/protobuf/types/known/anypb"
914
)
1015

1116
// Checking if [Size] returns 0 is an easy way to recognize empty messages:
@@ -16,3 +21,15 @@ func ExampleSize() {
1621
// skip processing this message, or return an error, or similar.
1722
}
1823
}
24+
25+
func TestSizeAnyNonNilButEmpty(t *testing.T) {
26+
ne := &anypb.Any{
27+
TypeUrl: "abc",
28+
Value: []byte{},
29+
}
30+
31+
want := protowire.SizeBytes(len("abc")) + protowire.SizeTag(1 /* TypeUrl */)
32+
if got := proto.Size(ne); got != want {
33+
t.Errorf("proto.Size(%v) = %v, want %v", prototext.Format(ne), got, want)
34+
}
35+
}

0 commit comments

Comments
 (0)