nil_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Copyright 2020 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/proto"
  8. "google.golang.org/protobuf/reflect/protoreflect"
  9. testpb "google.golang.org/protobuf/internal/testprotos/test"
  10. )
  11. // TestNil tests for boundary conditions when nil and typed-nil messages
  12. // are passed to various top-level functions.
  13. // These tests are not necessarily a statement of proper behavior,
  14. // but exist to detect accidental changes in behavior.
  15. func TestNil(t *testing.T) {
  16. nilMsg := (*testpb.TestAllExtensions)(nil)
  17. extType := testpb.E_OptionalBool
  18. extRanger := func(protoreflect.ExtensionType, interface{}) bool { return true }
  19. tests := []struct {
  20. label string
  21. test func()
  22. panic bool
  23. }{{
  24. label: "Size",
  25. test: func() { proto.Size(nil) },
  26. }, {
  27. label: "Size",
  28. test: func() { proto.Size(nilMsg) },
  29. }, {
  30. label: "Marshal",
  31. test: func() { proto.Marshal(nil) },
  32. }, {
  33. label: "Marshal",
  34. test: func() { proto.Marshal(nilMsg) },
  35. }, {
  36. label: "Unmarshal",
  37. test: func() { proto.Unmarshal(nil, nil) },
  38. panic: true,
  39. }, {
  40. label: "Unmarshal",
  41. test: func() { proto.Unmarshal(nil, nilMsg) },
  42. panic: true,
  43. }, {
  44. label: "Merge",
  45. test: func() { proto.Merge(nil, nil) },
  46. panic: true,
  47. }, {
  48. label: "Merge",
  49. test: func() { proto.Merge(nil, nilMsg) },
  50. panic: true,
  51. }, {
  52. label: "Merge",
  53. test: func() { proto.Merge(nilMsg, nil) },
  54. panic: true,
  55. }, {
  56. label: "Merge",
  57. test: func() { proto.Merge(nilMsg, nilMsg) },
  58. panic: true,
  59. }, {
  60. label: "Clone",
  61. test: func() { proto.Clone(nil) },
  62. }, {
  63. label: "Clone",
  64. test: func() { proto.Clone(nilMsg) },
  65. }, {
  66. label: "Equal",
  67. test: func() { proto.Equal(nil, nil) },
  68. }, {
  69. label: "Equal",
  70. test: func() { proto.Equal(nil, nilMsg) },
  71. }, {
  72. label: "Equal",
  73. test: func() { proto.Equal(nilMsg, nil) },
  74. }, {
  75. label: "Equal",
  76. test: func() { proto.Equal(nilMsg, nilMsg) },
  77. }, {
  78. label: "Reset",
  79. test: func() { proto.Reset(nil) },
  80. panic: true,
  81. }, {
  82. label: "Reset",
  83. test: func() { proto.Reset(nilMsg) },
  84. panic: true,
  85. }, {
  86. label: "HasExtension",
  87. test: func() { proto.HasExtension(nil, nil) },
  88. }, {
  89. label: "HasExtension",
  90. test: func() { proto.HasExtension(nil, extType) },
  91. }, {
  92. label: "HasExtension",
  93. test: func() { proto.HasExtension(nilMsg, nil) },
  94. }, {
  95. label: "HasExtension",
  96. test: func() { proto.HasExtension(nilMsg, extType) },
  97. }, {
  98. label: "GetExtension",
  99. test: func() { proto.GetExtension(nil, nil) },
  100. panic: true,
  101. }, {
  102. label: "GetExtension",
  103. test: func() { proto.GetExtension(nil, extType) },
  104. }, {
  105. label: "GetExtension",
  106. test: func() { proto.GetExtension(nilMsg, nil) },
  107. panic: true,
  108. }, {
  109. label: "GetExtension",
  110. test: func() { proto.GetExtension(nilMsg, extType) },
  111. }, {
  112. label: "SetExtension",
  113. test: func() { proto.SetExtension(nil, nil, true) },
  114. panic: true,
  115. }, {
  116. label: "SetExtension",
  117. test: func() { proto.SetExtension(nil, extType, true) },
  118. panic: true,
  119. }, {
  120. label: "SetExtension",
  121. test: func() { proto.SetExtension(nilMsg, nil, true) },
  122. panic: true,
  123. }, {
  124. label: "SetExtension",
  125. test: func() { proto.SetExtension(nilMsg, extType, true) },
  126. panic: true,
  127. }, {
  128. label: "ClearExtension",
  129. test: func() { proto.ClearExtension(nil, nil) },
  130. panic: true,
  131. }, {
  132. label: "ClearExtension",
  133. test: func() { proto.ClearExtension(nil, extType) },
  134. panic: true,
  135. }, {
  136. label: "ClearExtension",
  137. test: func() { proto.ClearExtension(nilMsg, nil) },
  138. panic: true,
  139. }, {
  140. label: "ClearExtension",
  141. test: func() { proto.ClearExtension(nilMsg, extType) },
  142. panic: true,
  143. }, {
  144. label: "RangeExtensions",
  145. test: func() { proto.RangeExtensions(nil, nil) },
  146. }, {
  147. label: "RangeExtensions",
  148. test: func() { proto.RangeExtensions(nil, extRanger) },
  149. }, {
  150. label: "RangeExtensions",
  151. test: func() { proto.RangeExtensions(nilMsg, nil) },
  152. }, {
  153. label: "RangeExtensions",
  154. test: func() { proto.RangeExtensions(nilMsg, extRanger) },
  155. }}
  156. for _, tt := range tests {
  157. t.Run(tt.label, func(t *testing.T) {
  158. defer func() {
  159. switch gotPanic := recover() != nil; {
  160. case gotPanic && !tt.panic:
  161. t.Errorf("unexpected panic")
  162. case !gotPanic && tt.panic:
  163. t.Errorf("expected panic")
  164. }
  165. }()
  166. tt.test()
  167. })
  168. }
  169. }