weak_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2019 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package proto_test
  5. import (
  6. "testing"
  7. "google.golang.org/protobuf/internal/flags"
  8. "google.golang.org/protobuf/internal/protobuild"
  9. "google.golang.org/protobuf/proto"
  10. "google.golang.org/protobuf/testing/protopack"
  11. testpb "google.golang.org/protobuf/internal/testprotos/test"
  12. weakpb "google.golang.org/protobuf/internal/testprotos/test/weak1"
  13. )
  14. func init() {
  15. if flags.ProtoLegacy {
  16. testValidMessages = append(testValidMessages, testWeakValidMessages...)
  17. testInvalidMessages = append(testInvalidMessages, testWeakInvalidMessages...)
  18. testMerges = append(testMerges, testWeakMerges...)
  19. }
  20. }
  21. var testWeakValidMessages = []testProto{
  22. {
  23. desc: "weak message",
  24. decodeTo: []proto.Message{
  25. func() proto.Message {
  26. if !flags.ProtoLegacy {
  27. return nil
  28. }
  29. m := &testpb.TestWeak{}
  30. m.SetWeakMessage1(&weakpb.WeakImportMessage1{
  31. A: proto.Int32(1000),
  32. })
  33. m.ProtoReflect().SetUnknown(protopack.Message{
  34. protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  35. protopack.Tag{1, protopack.VarintType}, protopack.Varint(2000),
  36. }),
  37. }.Marshal())
  38. return m
  39. }(),
  40. },
  41. wire: protopack.Message{
  42. protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  43. protopack.Tag{1, protopack.VarintType}, protopack.Varint(1000),
  44. }),
  45. protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  46. protopack.Tag{1, protopack.VarintType}, protopack.Varint(2000),
  47. }),
  48. }.Marshal(),
  49. },
  50. }
  51. var testWeakInvalidMessages = []testProto{
  52. {
  53. desc: "invalid field number 0 in weak message",
  54. decodeTo: []proto.Message{(*testpb.TestWeak)(nil)},
  55. wire: protopack.Message{
  56. protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  57. protopack.Tag{0, protopack.VarintType}, protopack.Varint(1000),
  58. }),
  59. }.Marshal(),
  60. },
  61. }
  62. var testWeakMerges = []testMerge{
  63. {
  64. desc: "clone weak message",
  65. src: protobuild.Message{
  66. "weak_message1": protobuild.Message{
  67. "a": 1,
  68. },
  69. },
  70. types: []proto.Message{&testpb.TestWeak{}},
  71. }, {
  72. desc: "merge weak message",
  73. dst: protobuild.Message{
  74. "weak_message1": protobuild.Message{
  75. "a": 1,
  76. },
  77. },
  78. src: protobuild.Message{
  79. "weak_message1": protobuild.Message{
  80. "a": 2,
  81. },
  82. },
  83. want: protobuild.Message{
  84. "weak_message1": protobuild.Message{
  85. "a": 2,
  86. },
  87. },
  88. types: []proto.Message{&testpb.TestWeak{}},
  89. },
  90. }
  91. func TestWeakNil(t *testing.T) {
  92. if !flags.ProtoLegacy {
  93. t.SkipNow()
  94. }
  95. m := new(testpb.TestWeak)
  96. if v, ok := m.GetWeakMessage1().(*weakpb.WeakImportMessage1); !ok || v != nil {
  97. t.Errorf("m.GetWeakMessage1() = type %[1]T(%[1]v), want (*weakpb.WeakImportMessage1)", v)
  98. }
  99. }
  100. func TestWeakMarshalNil(t *testing.T) {
  101. if !flags.ProtoLegacy {
  102. t.SkipNow()
  103. }
  104. m := new(testpb.TestWeak)
  105. m.SetWeakMessage1(nil)
  106. if b, err := proto.Marshal(m); err != nil || len(b) != 0 {
  107. t.Errorf("Marshal(weak field set to nil) = [%x], %v; want [], nil", b, err)
  108. }
  109. m.SetWeakMessage1((*weakpb.WeakImportMessage1)(nil))
  110. if b, err := proto.Marshal(m); err != nil || len(b) != 0 {
  111. t.Errorf("Marshal(weak field set to typed nil) = [%x], %v; want [], nil", b, err)
  112. }
  113. }