message.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  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 prototest exercises protobuf reflection.
  5. package prototest
  6. import (
  7. "bytes"
  8. "fmt"
  9. "math"
  10. "reflect"
  11. "sort"
  12. "strings"
  13. "testing"
  14. "google.golang.org/protobuf/encoding/prototext"
  15. "google.golang.org/protobuf/encoding/protowire"
  16. "google.golang.org/protobuf/proto"
  17. "google.golang.org/protobuf/reflect/protoreflect"
  18. "google.golang.org/protobuf/reflect/protoregistry"
  19. )
  20. // TODO: Test invalid field descriptors or oneof descriptors.
  21. // TODO: This should test the functionality that can be provided by fast-paths.
  22. // Message tests a message implementation.
  23. type Message struct {
  24. // Resolver is used to determine the list of extension fields to test with.
  25. // If nil, this defaults to using protoregistry.GlobalTypes.
  26. Resolver interface {
  27. FindExtensionByName(field protoreflect.FullName) (protoreflect.ExtensionType, error)
  28. FindExtensionByNumber(message protoreflect.FullName, field protoreflect.FieldNumber) (protoreflect.ExtensionType, error)
  29. RangeExtensionsByMessage(message protoreflect.FullName, f func(protoreflect.ExtensionType) bool)
  30. }
  31. }
  32. // Test performs tests on a MessageType implementation.
  33. func (test Message) Test(t testing.TB, mt protoreflect.MessageType) {
  34. testType(t, mt)
  35. md := mt.Descriptor()
  36. m1 := mt.New()
  37. for i := 0; i < md.Fields().Len(); i++ {
  38. fd := md.Fields().Get(i)
  39. testField(t, m1, fd)
  40. }
  41. if test.Resolver == nil {
  42. test.Resolver = protoregistry.GlobalTypes
  43. }
  44. var extTypes []protoreflect.ExtensionType
  45. test.Resolver.RangeExtensionsByMessage(md.FullName(), func(e protoreflect.ExtensionType) bool {
  46. extTypes = append(extTypes, e)
  47. return true
  48. })
  49. for _, xt := range extTypes {
  50. testField(t, m1, xt.TypeDescriptor())
  51. }
  52. for i := 0; i < md.Oneofs().Len(); i++ {
  53. testOneof(t, m1, md.Oneofs().Get(i))
  54. }
  55. testUnknown(t, m1)
  56. // Test round-trip marshal/unmarshal.
  57. m2 := mt.New().Interface()
  58. populateMessage(m2.ProtoReflect(), 1, nil)
  59. for _, xt := range extTypes {
  60. m2.ProtoReflect().Set(xt.TypeDescriptor(), newValue(m2.ProtoReflect(), xt.TypeDescriptor(), 1, nil))
  61. }
  62. b, err := proto.MarshalOptions{
  63. AllowPartial: true,
  64. }.Marshal(m2)
  65. if err != nil {
  66. t.Errorf("Marshal() = %v, want nil\n%v", err, prototext.Format(m2))
  67. }
  68. m3 := mt.New().Interface()
  69. if err := (proto.UnmarshalOptions{
  70. AllowPartial: true,
  71. Resolver: test.Resolver,
  72. }.Unmarshal(b, m3)); err != nil {
  73. t.Errorf("Unmarshal() = %v, want nil\n%v", err, prototext.Format(m2))
  74. }
  75. if !proto.Equal(m2, m3) {
  76. t.Errorf("round-trip marshal/unmarshal did not preserve message\nOriginal:\n%v\nNew:\n%v", prototext.Format(m2), prototext.Format(m3))
  77. }
  78. }
  79. func testType(t testing.TB, mt protoreflect.MessageType) {
  80. m := mt.New().Interface()
  81. want := reflect.TypeOf(m)
  82. if got := reflect.TypeOf(m.ProtoReflect().Interface()); got != want {
  83. t.Errorf("type mismatch: reflect.TypeOf(m) != reflect.TypeOf(m.ProtoReflect().Interface()): %v != %v", got, want)
  84. }
  85. if got := reflect.TypeOf(m.ProtoReflect().New().Interface()); got != want {
  86. t.Errorf("type mismatch: reflect.TypeOf(m) != reflect.TypeOf(m.ProtoReflect().New().Interface()): %v != %v", got, want)
  87. }
  88. if got := reflect.TypeOf(m.ProtoReflect().Type().Zero().Interface()); got != want {
  89. t.Errorf("type mismatch: reflect.TypeOf(m) != reflect.TypeOf(m.ProtoReflect().Type().Zero().Interface()): %v != %v", got, want)
  90. }
  91. if mt, ok := mt.(protoreflect.MessageFieldTypes); ok {
  92. testFieldTypes(t, mt)
  93. }
  94. }
  95. func testFieldTypes(t testing.TB, mt protoreflect.MessageFieldTypes) {
  96. descName := func(d protoreflect.Descriptor) protoreflect.FullName {
  97. if d == nil {
  98. return "<nil>"
  99. }
  100. return d.FullName()
  101. }
  102. typeName := func(mt protoreflect.MessageType) protoreflect.FullName {
  103. if mt == nil {
  104. return "<nil>"
  105. }
  106. return mt.Descriptor().FullName()
  107. }
  108. adjustExpr := func(idx int, expr string) string {
  109. expr = strings.Replace(expr, "fd.", "md.Fields().Get(i).", -1)
  110. expr = strings.Replace(expr, "(fd)", "(md.Fields().Get(i))", -1)
  111. expr = strings.Replace(expr, "mti.", "mt.Message(i).", -1)
  112. expr = strings.Replace(expr, "(i)", fmt.Sprintf("(%d)", idx), -1)
  113. return expr
  114. }
  115. checkEnumDesc := func(idx int, gotExpr, wantExpr string, got, want protoreflect.EnumDescriptor) {
  116. if got != want {
  117. t.Errorf("descriptor mismatch: %v != %v: %v != %v", adjustExpr(idx, gotExpr), adjustExpr(idx, wantExpr), descName(got), descName(want))
  118. }
  119. }
  120. checkMessageDesc := func(idx int, gotExpr, wantExpr string, got, want protoreflect.MessageDescriptor) {
  121. if got != want {
  122. t.Errorf("descriptor mismatch: %v != %v: %v != %v", adjustExpr(idx, gotExpr), adjustExpr(idx, wantExpr), descName(got), descName(want))
  123. }
  124. }
  125. checkMessageType := func(idx int, gotExpr, wantExpr string, got, want protoreflect.MessageType) {
  126. if got != want {
  127. t.Errorf("type mismatch: %v != %v: %v != %v", adjustExpr(idx, gotExpr), adjustExpr(idx, wantExpr), typeName(got), typeName(want))
  128. }
  129. }
  130. fds := mt.Descriptor().Fields()
  131. m := mt.New()
  132. for i := 0; i < fds.Len(); i++ {
  133. fd := fds.Get(i)
  134. switch {
  135. case fd.IsList():
  136. if fd.Enum() != nil {
  137. checkEnumDesc(i,
  138. "mt.Enum(i).Descriptor()", "fd.Enum()",
  139. mt.Enum(i).Descriptor(), fd.Enum())
  140. }
  141. if fd.Message() != nil {
  142. checkMessageDesc(i,
  143. "mt.Message(i).Descriptor()", "fd.Message()",
  144. mt.Message(i).Descriptor(), fd.Message())
  145. checkMessageType(i,
  146. "mt.Message(i)", "m.NewField(fd).List().NewElement().Message().Type()",
  147. mt.Message(i), m.NewField(fd).List().NewElement().Message().Type())
  148. }
  149. case fd.IsMap():
  150. mti := mt.Message(i)
  151. if m := mti.New(); m != nil {
  152. checkMessageDesc(i,
  153. "m.Descriptor()", "fd.Message()",
  154. m.Descriptor(), fd.Message())
  155. }
  156. if m := mti.Zero(); m != nil {
  157. checkMessageDesc(i,
  158. "m.Descriptor()", "fd.Message()",
  159. m.Descriptor(), fd.Message())
  160. }
  161. checkMessageDesc(i,
  162. "mti.Descriptor()", "fd.Message()",
  163. mti.Descriptor(), fd.Message())
  164. if mti := mti.(protoreflect.MessageFieldTypes); mti != nil {
  165. if fd.MapValue().Enum() != nil {
  166. checkEnumDesc(i,
  167. "mti.Enum(fd.MapValue().Index()).Descriptor()", "fd.MapValue().Enum()",
  168. mti.Enum(fd.MapValue().Index()).Descriptor(), fd.MapValue().Enum())
  169. }
  170. if fd.MapValue().Message() != nil {
  171. checkMessageDesc(i,
  172. "mti.Message(fd.MapValue().Index()).Descriptor()", "fd.MapValue().Message()",
  173. mti.Message(fd.MapValue().Index()).Descriptor(), fd.MapValue().Message())
  174. checkMessageType(i,
  175. "mti.Message(fd.MapValue().Index())", "m.NewField(fd).Map().NewValue().Message().Type()",
  176. mti.Message(fd.MapValue().Index()), m.NewField(fd).Map().NewValue().Message().Type())
  177. }
  178. }
  179. default:
  180. if fd.Enum() != nil {
  181. checkEnumDesc(i,
  182. "mt.Enum(i).Descriptor()", "fd.Enum()",
  183. mt.Enum(i).Descriptor(), fd.Enum())
  184. }
  185. if fd.Message() != nil {
  186. checkMessageDesc(i,
  187. "mt.Message(i).Descriptor()", "fd.Message()",
  188. mt.Message(i).Descriptor(), fd.Message())
  189. checkMessageType(i,
  190. "mt.Message(i)", "m.NewField(fd).Message().Type()",
  191. mt.Message(i), m.NewField(fd).Message().Type())
  192. }
  193. }
  194. }
  195. }
  196. // testField exercises set/get/has/clear of a field.
  197. func testField(t testing.TB, m protoreflect.Message, fd protoreflect.FieldDescriptor) {
  198. name := fd.FullName()
  199. num := fd.Number()
  200. switch {
  201. case fd.IsList():
  202. testFieldList(t, m, fd)
  203. case fd.IsMap():
  204. testFieldMap(t, m, fd)
  205. case fd.Message() != nil:
  206. default:
  207. if got, want := m.NewField(fd), fd.Default(); !valueEqual(got, want) {
  208. t.Errorf("Message.NewField(%v) = %v, want default value %v", name, formatValue(got), formatValue(want))
  209. }
  210. if fd.Kind() == protoreflect.FloatKind || fd.Kind() == protoreflect.DoubleKind {
  211. testFieldFloat(t, m, fd)
  212. }
  213. }
  214. // Set to a non-zero value, the zero value, different non-zero values.
  215. for _, n := range []seed{1, 0, minVal, maxVal} {
  216. v := newValue(m, fd, n, nil)
  217. m.Set(fd, v)
  218. wantHas := true
  219. if n == 0 {
  220. if fd.Syntax() == protoreflect.Proto3 && fd.Message() == nil {
  221. wantHas = false
  222. }
  223. if fd.IsExtension() {
  224. wantHas = true
  225. }
  226. if fd.Cardinality() == protoreflect.Repeated {
  227. wantHas = false
  228. }
  229. if fd.ContainingOneof() != nil {
  230. wantHas = true
  231. }
  232. }
  233. if fd.Syntax() == protoreflect.Proto3 && fd.Cardinality() != protoreflect.Repeated && fd.ContainingOneof() == nil && fd.Kind() == protoreflect.EnumKind && v.Enum() == 0 {
  234. wantHas = false
  235. }
  236. if got, want := m.Has(fd), wantHas; got != want {
  237. t.Errorf("after setting %q to %v:\nMessage.Has(%v) = %v, want %v", name, formatValue(v), num, got, want)
  238. }
  239. if got, want := m.Get(fd), v; !valueEqual(got, want) {
  240. t.Errorf("after setting %q:\nMessage.Get(%v) = %v, want %v", name, num, formatValue(got), formatValue(want))
  241. }
  242. found := false
  243. m.Range(func(d protoreflect.FieldDescriptor, got protoreflect.Value) bool {
  244. if fd != d {
  245. return true
  246. }
  247. found = true
  248. if want := v; !valueEqual(got, want) {
  249. t.Errorf("after setting %q:\nMessage.Range got value %v, want %v", name, formatValue(got), formatValue(want))
  250. }
  251. return true
  252. })
  253. if got, want := wantHas, found; got != want {
  254. t.Errorf("after setting %q:\nMessageRange saw field: %v, want %v", name, got, want)
  255. }
  256. }
  257. m.Clear(fd)
  258. if got, want := m.Has(fd), false; got != want {
  259. t.Errorf("after clearing %q:\nMessage.Has(%v) = %v, want %v", name, num, got, want)
  260. }
  261. switch {
  262. case fd.IsList():
  263. if got := m.Get(fd); got.List().Len() != 0 {
  264. t.Errorf("after clearing %q:\nMessage.Get(%v) = %v, want empty list", name, num, formatValue(got))
  265. }
  266. case fd.IsMap():
  267. if got := m.Get(fd); got.Map().Len() != 0 {
  268. t.Errorf("after clearing %q:\nMessage.Get(%v) = %v, want empty map", name, num, formatValue(got))
  269. }
  270. case fd.Message() == nil:
  271. if got, want := m.Get(fd), fd.Default(); !valueEqual(got, want) {
  272. t.Errorf("after clearing %q:\nMessage.Get(%v) = %v, want default %v", name, num, formatValue(got), formatValue(want))
  273. }
  274. }
  275. // Set to the default value.
  276. switch {
  277. case fd.IsList() || fd.IsMap():
  278. m.Set(fd, m.Mutable(fd))
  279. if got, want := m.Has(fd), (fd.IsExtension() && fd.Cardinality() != protoreflect.Repeated) || fd.ContainingOneof() != nil; got != want {
  280. t.Errorf("after setting %q to default:\nMessage.Has(%v) = %v, want %v", name, num, got, want)
  281. }
  282. case fd.Message() == nil:
  283. m.Set(fd, m.Get(fd))
  284. if got, want := m.Get(fd), fd.Default(); !valueEqual(got, want) {
  285. t.Errorf("after setting %q to default:\nMessage.Get(%v) = %v, want default %v", name, num, formatValue(got), formatValue(want))
  286. }
  287. }
  288. m.Clear(fd)
  289. // Set to the wrong type.
  290. v := protoreflect.ValueOfString("")
  291. if fd.Kind() == protoreflect.StringKind {
  292. v = protoreflect.ValueOfInt32(0)
  293. }
  294. if !panics(func() {
  295. m.Set(fd, v)
  296. }) {
  297. t.Errorf("setting %v to %T succeeds, want panic", name, v.Interface())
  298. }
  299. }
  300. // testFieldMap tests set/get/has/clear of entries in a map field.
  301. func testFieldMap(t testing.TB, m protoreflect.Message, fd protoreflect.FieldDescriptor) {
  302. name := fd.FullName()
  303. num := fd.Number()
  304. // New values.
  305. m.Clear(fd) // start with an empty map
  306. mapv := m.Get(fd).Map()
  307. if mapv.IsValid() {
  308. t.Errorf("after clearing field: message.Get(%v).IsValid() = true, want false", name)
  309. }
  310. if got, want := mapv.NewValue(), newMapValue(fd, mapv, 0, nil); !valueEqual(got, want) {
  311. t.Errorf("message.Get(%v).NewValue() = %v, want %v", name, formatValue(got), formatValue(want))
  312. }
  313. if !panics(func() {
  314. m.Set(fd, protoreflect.ValueOfMap(mapv))
  315. }) {
  316. t.Errorf("message.Set(%v, <invalid>) does not panic", name)
  317. }
  318. if !panics(func() {
  319. mapv.Set(newMapKey(fd, 0), newMapValue(fd, mapv, 0, nil))
  320. }) {
  321. t.Errorf("message.Get(%v).Set(...) of invalid map does not panic", name)
  322. }
  323. mapv = m.Mutable(fd).Map() // mutable map
  324. if !mapv.IsValid() {
  325. t.Errorf("message.Mutable(%v).IsValid() = false, want true", name)
  326. }
  327. if got, want := mapv.NewValue(), newMapValue(fd, mapv, 0, nil); !valueEqual(got, want) {
  328. t.Errorf("message.Mutable(%v).NewValue() = %v, want %v", name, formatValue(got), formatValue(want))
  329. }
  330. // Add values.
  331. want := make(testMap)
  332. for i, n := range []seed{1, 0, minVal, maxVal} {
  333. if got, want := m.Has(fd), i > 0; got != want {
  334. t.Errorf("after inserting %d elements to %q:\nMessage.Has(%v) = %v, want %v", i, name, num, got, want)
  335. }
  336. k := newMapKey(fd, n)
  337. v := newMapValue(fd, mapv, n, nil)
  338. mapv.Set(k, v)
  339. want.Set(k, v)
  340. if got, want := m.Get(fd), protoreflect.ValueOfMap(want); !valueEqual(got, want) {
  341. t.Errorf("after inserting %d elements to %q:\nMessage.Get(%v) = %v, want %v", i, name, num, formatValue(got), formatValue(want))
  342. }
  343. }
  344. // Set values.
  345. want.Range(func(k protoreflect.MapKey, v protoreflect.Value) bool {
  346. nv := newMapValue(fd, mapv, 10, nil)
  347. mapv.Set(k, nv)
  348. want.Set(k, nv)
  349. if got, want := m.Get(fd), protoreflect.ValueOfMap(want); !valueEqual(got, want) {
  350. t.Errorf("after setting element %v of %q:\nMessage.Get(%v) = %v, want %v", formatValue(k.Value()), name, num, formatValue(got), formatValue(want))
  351. }
  352. return true
  353. })
  354. // Clear values.
  355. want.Range(func(k protoreflect.MapKey, v protoreflect.Value) bool {
  356. mapv.Clear(k)
  357. want.Clear(k)
  358. if got, want := m.Has(fd), want.Len() > 0; got != want {
  359. t.Errorf("after clearing elements of %q:\nMessage.Has(%v) = %v, want %v", name, num, got, want)
  360. }
  361. if got, want := m.Get(fd), protoreflect.ValueOfMap(want); !valueEqual(got, want) {
  362. t.Errorf("after clearing elements of %q:\nMessage.Get(%v) = %v, want %v", name, num, formatValue(got), formatValue(want))
  363. }
  364. return true
  365. })
  366. if mapv := m.Get(fd).Map(); mapv.IsValid() {
  367. t.Errorf("after clearing all elements: message.Get(%v).IsValid() = true, want false %v", name, formatValue(protoreflect.ValueOfMap(mapv)))
  368. }
  369. // Non-existent map keys.
  370. missingKey := newMapKey(fd, 1)
  371. if got, want := mapv.Has(missingKey), false; got != want {
  372. t.Errorf("non-existent map key in %q: Map.Has(%v) = %v, want %v", name, formatValue(missingKey.Value()), got, want)
  373. }
  374. if got, want := mapv.Get(missingKey).IsValid(), false; got != want {
  375. t.Errorf("non-existent map key in %q: Map.Get(%v).IsValid() = %v, want %v", name, formatValue(missingKey.Value()), got, want)
  376. }
  377. mapv.Clear(missingKey) // noop
  378. // Mutable.
  379. if fd.MapValue().Message() == nil {
  380. if !panics(func() {
  381. mapv.Mutable(newMapKey(fd, 1))
  382. }) {
  383. t.Errorf("Mutable on %q succeeds, want panic", name)
  384. }
  385. } else {
  386. k := newMapKey(fd, 1)
  387. v := mapv.Mutable(k)
  388. if got, want := mapv.Len(), 1; got != want {
  389. t.Errorf("after Mutable on %q, Map.Len() = %v, want %v", name, got, want)
  390. }
  391. populateMessage(v.Message(), 1, nil)
  392. if !valueEqual(mapv.Get(k), v) {
  393. t.Errorf("after Mutable on %q, changing new mutable value does not change map entry", name)
  394. }
  395. mapv.Clear(k)
  396. }
  397. }
  398. type testMap map[interface{}]protoreflect.Value
  399. func (m testMap) Get(k protoreflect.MapKey) protoreflect.Value { return m[k.Interface()] }
  400. func (m testMap) Set(k protoreflect.MapKey, v protoreflect.Value) { m[k.Interface()] = v }
  401. func (m testMap) Has(k protoreflect.MapKey) bool { return m.Get(k).IsValid() }
  402. func (m testMap) Clear(k protoreflect.MapKey) { delete(m, k.Interface()) }
  403. func (m testMap) Mutable(k protoreflect.MapKey) protoreflect.Value { panic("unimplemented") }
  404. func (m testMap) Len() int { return len(m) }
  405. func (m testMap) NewValue() protoreflect.Value { panic("unimplemented") }
  406. func (m testMap) Range(f func(protoreflect.MapKey, protoreflect.Value) bool) {
  407. for k, v := range m {
  408. if !f(protoreflect.ValueOf(k).MapKey(), v) {
  409. return
  410. }
  411. }
  412. }
  413. func (m testMap) IsValid() bool { return true }
  414. // testFieldList exercises set/get/append/truncate of values in a list.
  415. func testFieldList(t testing.TB, m protoreflect.Message, fd protoreflect.FieldDescriptor) {
  416. name := fd.FullName()
  417. num := fd.Number()
  418. m.Clear(fd) // start with an empty list
  419. list := m.Get(fd).List()
  420. if list.IsValid() {
  421. t.Errorf("message.Get(%v).IsValid() = true, want false", name)
  422. }
  423. if !panics(func() {
  424. m.Set(fd, protoreflect.ValueOfList(list))
  425. }) {
  426. t.Errorf("message.Set(%v, <invalid>) does not panic", name)
  427. }
  428. if !panics(func() {
  429. list.Append(newListElement(fd, list, 0, nil))
  430. }) {
  431. t.Errorf("message.Get(%v).Append(...) of invalid list does not panic", name)
  432. }
  433. if got, want := list.NewElement(), newListElement(fd, list, 0, nil); !valueEqual(got, want) {
  434. t.Errorf("message.Get(%v).NewElement() = %v, want %v", name, formatValue(got), formatValue(want))
  435. }
  436. list = m.Mutable(fd).List() // mutable list
  437. if !list.IsValid() {
  438. t.Errorf("message.Get(%v).IsValid() = false, want true", name)
  439. }
  440. if got, want := list.NewElement(), newListElement(fd, list, 0, nil); !valueEqual(got, want) {
  441. t.Errorf("message.Mutable(%v).NewElement() = %v, want %v", name, formatValue(got), formatValue(want))
  442. }
  443. // Append values.
  444. var want protoreflect.List = &testList{}
  445. for i, n := range []seed{1, 0, minVal, maxVal} {
  446. if got, want := m.Has(fd), i > 0; got != want {
  447. t.Errorf("after appending %d elements to %q:\nMessage.Has(%v) = %v, want %v", i, name, num, got, want)
  448. }
  449. v := newListElement(fd, list, n, nil)
  450. want.Append(v)
  451. list.Append(v)
  452. if got, want := m.Get(fd), protoreflect.ValueOfList(want); !valueEqual(got, want) {
  453. t.Errorf("after appending %d elements to %q:\nMessage.Get(%v) = %v, want %v", i+1, name, num, formatValue(got), formatValue(want))
  454. }
  455. }
  456. // Set values.
  457. for i := 0; i < want.Len(); i++ {
  458. v := newListElement(fd, list, seed(i+10), nil)
  459. want.Set(i, v)
  460. list.Set(i, v)
  461. if got, want := m.Get(fd), protoreflect.ValueOfList(want); !valueEqual(got, want) {
  462. t.Errorf("after setting element %d of %q:\nMessage.Get(%v) = %v, want %v", i, name, num, formatValue(got), formatValue(want))
  463. }
  464. }
  465. // Truncate.
  466. for want.Len() > 0 {
  467. n := want.Len() - 1
  468. want.Truncate(n)
  469. list.Truncate(n)
  470. if got, want := m.Has(fd), want.Len() > 0; got != want {
  471. t.Errorf("after truncating %q to %d:\nMessage.Has(%v) = %v, want %v", name, n, num, got, want)
  472. }
  473. if got, want := m.Get(fd), protoreflect.ValueOfList(want); !valueEqual(got, want) {
  474. t.Errorf("after truncating %q to %d:\nMessage.Get(%v) = %v, want %v", name, n, num, formatValue(got), formatValue(want))
  475. }
  476. }
  477. // AppendMutable.
  478. if fd.Message() == nil {
  479. if !panics(func() {
  480. list.AppendMutable()
  481. }) {
  482. t.Errorf("AppendMutable on %q succeeds, want panic", name)
  483. }
  484. } else {
  485. v := list.AppendMutable()
  486. if got, want := list.Len(), 1; got != want {
  487. t.Errorf("after AppendMutable on %q, list.Len() = %v, want %v", name, got, want)
  488. }
  489. populateMessage(v.Message(), 1, nil)
  490. if !valueEqual(list.Get(0), v) {
  491. t.Errorf("after AppendMutable on %q, changing new mutable value does not change list item 0", name)
  492. }
  493. want.Truncate(0)
  494. }
  495. }
  496. type testList struct {
  497. a []protoreflect.Value
  498. }
  499. func (l *testList) Append(v protoreflect.Value) { l.a = append(l.a, v) }
  500. func (l *testList) AppendMutable() protoreflect.Value { panic("unimplemented") }
  501. func (l *testList) Get(n int) protoreflect.Value { return l.a[n] }
  502. func (l *testList) Len() int { return len(l.a) }
  503. func (l *testList) Set(n int, v protoreflect.Value) { l.a[n] = v }
  504. func (l *testList) Truncate(n int) { l.a = l.a[:n] }
  505. func (l *testList) NewElement() protoreflect.Value { panic("unimplemented") }
  506. func (l *testList) IsValid() bool { return true }
  507. // testFieldFloat exercises some interesting floating-point scalar field values.
  508. func testFieldFloat(t testing.TB, m protoreflect.Message, fd protoreflect.FieldDescriptor) {
  509. name := fd.FullName()
  510. num := fd.Number()
  511. for _, v := range []float64{math.Inf(-1), math.Inf(1), math.NaN(), math.Copysign(0, -1)} {
  512. var val protoreflect.Value
  513. if fd.Kind() == protoreflect.FloatKind {
  514. val = protoreflect.ValueOfFloat32(float32(v))
  515. } else {
  516. val = protoreflect.ValueOfFloat64(float64(v))
  517. }
  518. m.Set(fd, val)
  519. // Note that Has is true for -0.
  520. if got, want := m.Has(fd), true; got != want {
  521. t.Errorf("after setting %v to %v: Message.Has(%v) = %v, want %v", name, v, num, got, want)
  522. }
  523. if got, want := m.Get(fd), val; !valueEqual(got, want) {
  524. t.Errorf("after setting %v: Message.Get(%v) = %v, want %v", name, num, formatValue(got), formatValue(want))
  525. }
  526. }
  527. }
  528. // testOneof tests the behavior of fields in a oneof.
  529. func testOneof(t testing.TB, m protoreflect.Message, od protoreflect.OneofDescriptor) {
  530. for _, mutable := range []bool{false, true} {
  531. for i := 0; i < od.Fields().Len(); i++ {
  532. fda := od.Fields().Get(i)
  533. if mutable {
  534. // Set fields by requesting a mutable reference.
  535. if !fda.IsMap() && !fda.IsList() && fda.Message() == nil {
  536. continue
  537. }
  538. _ = m.Mutable(fda)
  539. } else {
  540. // Set fields explicitly.
  541. m.Set(fda, newValue(m, fda, 1, nil))
  542. }
  543. if got, want := m.WhichOneof(od), fda; got != want {
  544. t.Errorf("after setting oneof field %q:\nWhichOneof(%q) = %v, want %v", fda.FullName(), fda.Name(), got, want)
  545. }
  546. for j := 0; j < od.Fields().Len(); j++ {
  547. fdb := od.Fields().Get(j)
  548. if got, want := m.Has(fdb), i == j; got != want {
  549. t.Errorf("after setting oneof field %q:\nGet(%q) = %v, want %v", fda.FullName(), fdb.FullName(), got, want)
  550. }
  551. }
  552. }
  553. }
  554. }
  555. // testUnknown tests the behavior of unknown fields.
  556. func testUnknown(t testing.TB, m protoreflect.Message) {
  557. var b []byte
  558. b = protowire.AppendTag(b, 1000, protowire.VarintType)
  559. b = protowire.AppendVarint(b, 1001)
  560. m.SetUnknown(protoreflect.RawFields(b))
  561. if got, want := []byte(m.GetUnknown()), b; !bytes.Equal(got, want) {
  562. t.Errorf("after setting unknown fields:\nGetUnknown() = %v, want %v", got, want)
  563. }
  564. }
  565. func formatValue(v protoreflect.Value) string {
  566. switch v := v.Interface().(type) {
  567. case protoreflect.List:
  568. var buf bytes.Buffer
  569. buf.WriteString("list[")
  570. for i := 0; i < v.Len(); i++ {
  571. if i > 0 {
  572. buf.WriteString(" ")
  573. }
  574. buf.WriteString(formatValue(v.Get(i)))
  575. }
  576. buf.WriteString("]")
  577. return buf.String()
  578. case protoreflect.Map:
  579. var buf bytes.Buffer
  580. buf.WriteString("map[")
  581. var keys []protoreflect.MapKey
  582. v.Range(func(k protoreflect.MapKey, v protoreflect.Value) bool {
  583. keys = append(keys, k)
  584. return true
  585. })
  586. sort.Slice(keys, func(i, j int) bool {
  587. return keys[i].String() < keys[j].String()
  588. })
  589. for i, k := range keys {
  590. if i > 0 {
  591. buf.WriteString(" ")
  592. }
  593. buf.WriteString(formatValue(k.Value()))
  594. buf.WriteString(":")
  595. buf.WriteString(formatValue(v.Get(k)))
  596. }
  597. buf.WriteString("]")
  598. return buf.String()
  599. case protoreflect.Message:
  600. b, err := prototext.Marshal(v.Interface())
  601. if err != nil {
  602. return fmt.Sprintf("<%v>", err)
  603. }
  604. return fmt.Sprintf("%v{%s}", v.Descriptor().FullName(), b)
  605. case string:
  606. return fmt.Sprintf("%q", v)
  607. default:
  608. return fmt.Sprint(v)
  609. }
  610. }
  611. func valueEqual(a, b protoreflect.Value) bool {
  612. ai, bi := a.Interface(), b.Interface()
  613. switch ai.(type) {
  614. case protoreflect.Message:
  615. return proto.Equal(
  616. a.Message().Interface(),
  617. b.Message().Interface(),
  618. )
  619. case protoreflect.List:
  620. lista, listb := a.List(), b.List()
  621. if lista.Len() != listb.Len() {
  622. return false
  623. }
  624. for i := 0; i < lista.Len(); i++ {
  625. if !valueEqual(lista.Get(i), listb.Get(i)) {
  626. return false
  627. }
  628. }
  629. return true
  630. case protoreflect.Map:
  631. mapa, mapb := a.Map(), b.Map()
  632. if mapa.Len() != mapb.Len() {
  633. return false
  634. }
  635. equal := true
  636. mapa.Range(func(k protoreflect.MapKey, v protoreflect.Value) bool {
  637. if !valueEqual(v, mapb.Get(k)) {
  638. equal = false
  639. return false
  640. }
  641. return true
  642. })
  643. return equal
  644. case []byte:
  645. return bytes.Equal(a.Bytes(), b.Bytes())
  646. case float32:
  647. // NaNs are equal, but must be the same NaN.
  648. return math.Float32bits(ai.(float32)) == math.Float32bits(bi.(float32))
  649. case float64:
  650. // NaNs are equal, but must be the same NaN.
  651. return math.Float64bits(ai.(float64)) == math.Float64bits(bi.(float64))
  652. default:
  653. return ai == bi
  654. }
  655. }
  656. // A seed is used to vary the content of a value.
  657. //
  658. // A seed of 0 is the zero value. Messages do not have a zero-value; a 0-seeded messages
  659. // is unpopulated.
  660. //
  661. // A seed of minVal or maxVal is the least or greatest value of the value type.
  662. type seed int
  663. const (
  664. minVal seed = -1
  665. maxVal seed = -2
  666. )
  667. // newSeed creates new seed values from a base, for example to create seeds for the
  668. // elements in a list. If the input seed is minVal or maxVal, so is the output.
  669. func newSeed(n seed, adjust ...int) seed {
  670. switch n {
  671. case minVal, maxVal:
  672. return n
  673. }
  674. for _, a := range adjust {
  675. n = 10*n + seed(a)
  676. }
  677. return n
  678. }
  679. // newValue returns a new value assignable to a field.
  680. //
  681. // The stack parameter is used to avoid infinite recursion when populating circular
  682. // data structures.
  683. func newValue(m protoreflect.Message, fd protoreflect.FieldDescriptor, n seed, stack []protoreflect.MessageDescriptor) protoreflect.Value {
  684. switch {
  685. case fd.IsList():
  686. if n == 0 {
  687. return m.New().Mutable(fd)
  688. }
  689. list := m.NewField(fd).List()
  690. list.Append(newListElement(fd, list, 0, stack))
  691. list.Append(newListElement(fd, list, minVal, stack))
  692. list.Append(newListElement(fd, list, maxVal, stack))
  693. list.Append(newListElement(fd, list, n, stack))
  694. return protoreflect.ValueOfList(list)
  695. case fd.IsMap():
  696. if n == 0 {
  697. return m.New().Mutable(fd)
  698. }
  699. mapv := m.NewField(fd).Map()
  700. mapv.Set(newMapKey(fd, 0), newMapValue(fd, mapv, 0, stack))
  701. mapv.Set(newMapKey(fd, minVal), newMapValue(fd, mapv, minVal, stack))
  702. mapv.Set(newMapKey(fd, maxVal), newMapValue(fd, mapv, maxVal, stack))
  703. mapv.Set(newMapKey(fd, n), newMapValue(fd, mapv, newSeed(n, 0), stack))
  704. return protoreflect.ValueOfMap(mapv)
  705. case fd.Message() != nil:
  706. return populateMessage(m.NewField(fd).Message(), n, stack)
  707. default:
  708. return newScalarValue(fd, n)
  709. }
  710. }
  711. func newListElement(fd protoreflect.FieldDescriptor, list protoreflect.List, n seed, stack []protoreflect.MessageDescriptor) protoreflect.Value {
  712. if fd.Message() == nil {
  713. return newScalarValue(fd, n)
  714. }
  715. return populateMessage(list.NewElement().Message(), n, stack)
  716. }
  717. func newMapKey(fd protoreflect.FieldDescriptor, n seed) protoreflect.MapKey {
  718. kd := fd.MapKey()
  719. return newScalarValue(kd, n).MapKey()
  720. }
  721. func newMapValue(fd protoreflect.FieldDescriptor, mapv protoreflect.Map, n seed, stack []protoreflect.MessageDescriptor) protoreflect.Value {
  722. vd := fd.MapValue()
  723. if vd.Message() == nil {
  724. return newScalarValue(vd, n)
  725. }
  726. return populateMessage(mapv.NewValue().Message(), n, stack)
  727. }
  728. func newScalarValue(fd protoreflect.FieldDescriptor, n seed) protoreflect.Value {
  729. switch fd.Kind() {
  730. case protoreflect.BoolKind:
  731. return protoreflect.ValueOfBool(n != 0)
  732. case protoreflect.EnumKind:
  733. vals := fd.Enum().Values()
  734. var i int
  735. switch n {
  736. case minVal:
  737. i = 0
  738. case maxVal:
  739. i = vals.Len() - 1
  740. default:
  741. i = int(n) % vals.Len()
  742. }
  743. return protoreflect.ValueOfEnum(vals.Get(i).Number())
  744. case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind:
  745. switch n {
  746. case minVal:
  747. return protoreflect.ValueOfInt32(math.MinInt32)
  748. case maxVal:
  749. return protoreflect.ValueOfInt32(math.MaxInt32)
  750. default:
  751. return protoreflect.ValueOfInt32(int32(n))
  752. }
  753. case protoreflect.Uint32Kind, protoreflect.Fixed32Kind:
  754. switch n {
  755. case minVal:
  756. // Only use 0 for the zero value.
  757. return protoreflect.ValueOfUint32(1)
  758. case maxVal:
  759. return protoreflect.ValueOfUint32(math.MaxInt32)
  760. default:
  761. return protoreflect.ValueOfUint32(uint32(n))
  762. }
  763. case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind:
  764. switch n {
  765. case minVal:
  766. return protoreflect.ValueOfInt64(math.MinInt64)
  767. case maxVal:
  768. return protoreflect.ValueOfInt64(math.MaxInt64)
  769. default:
  770. return protoreflect.ValueOfInt64(int64(n))
  771. }
  772. case protoreflect.Uint64Kind, protoreflect.Fixed64Kind:
  773. switch n {
  774. case minVal:
  775. // Only use 0 for the zero value.
  776. return protoreflect.ValueOfUint64(1)
  777. case maxVal:
  778. return protoreflect.ValueOfUint64(math.MaxInt64)
  779. default:
  780. return protoreflect.ValueOfUint64(uint64(n))
  781. }
  782. case protoreflect.FloatKind:
  783. switch n {
  784. case minVal:
  785. return protoreflect.ValueOfFloat32(math.SmallestNonzeroFloat32)
  786. case maxVal:
  787. return protoreflect.ValueOfFloat32(math.MaxFloat32)
  788. default:
  789. return protoreflect.ValueOfFloat32(1.5 * float32(n))
  790. }
  791. case protoreflect.DoubleKind:
  792. switch n {
  793. case minVal:
  794. return protoreflect.ValueOfFloat64(math.SmallestNonzeroFloat64)
  795. case maxVal:
  796. return protoreflect.ValueOfFloat64(math.MaxFloat64)
  797. default:
  798. return protoreflect.ValueOfFloat64(1.5 * float64(n))
  799. }
  800. case protoreflect.StringKind:
  801. if n == 0 {
  802. return protoreflect.ValueOfString("")
  803. }
  804. return protoreflect.ValueOfString(fmt.Sprintf("%d", n))
  805. case protoreflect.BytesKind:
  806. if n == 0 {
  807. return protoreflect.ValueOfBytes(nil)
  808. }
  809. return protoreflect.ValueOfBytes([]byte{byte(n >> 24), byte(n >> 16), byte(n >> 8), byte(n)})
  810. }
  811. panic("unhandled kind")
  812. }
  813. func populateMessage(m protoreflect.Message, n seed, stack []protoreflect.MessageDescriptor) protoreflect.Value {
  814. if n == 0 {
  815. return protoreflect.ValueOfMessage(m)
  816. }
  817. md := m.Descriptor()
  818. for _, x := range stack {
  819. if md == x {
  820. return protoreflect.ValueOfMessage(m)
  821. }
  822. }
  823. stack = append(stack, md)
  824. for i := 0; i < md.Fields().Len(); i++ {
  825. fd := md.Fields().Get(i)
  826. if fd.IsWeak() {
  827. continue
  828. }
  829. m.Set(fd, newValue(m, fd, newSeed(n, i), stack))
  830. }
  831. return protoreflect.ValueOfMessage(m)
  832. }
  833. func panics(f func()) (didPanic bool) {
  834. defer func() {
  835. if err := recover(); err != nil {
  836. didPanic = true
  837. }
  838. }()
  839. f()
  840. return false
  841. }