any_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2016 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 ptypes
  5. import (
  6. "testing"
  7. "github.com/golang/protobuf/proto"
  8. pb "github.com/golang/protobuf/protoc-gen-go/descriptor"
  9. "github.com/golang/protobuf/ptypes/any"
  10. )
  11. func TestMarshalUnmarshal(t *testing.T) {
  12. orig := &any.Any{Value: []byte("test")}
  13. packed, err := MarshalAny(orig)
  14. if err != nil {
  15. t.Errorf("MarshalAny(%+v): got: _, %v exp: _, nil", orig, err)
  16. }
  17. unpacked := &any.Any{}
  18. err = UnmarshalAny(packed, unpacked)
  19. if err != nil || !proto.Equal(unpacked, orig) {
  20. t.Errorf("got: %v, %+v; want nil, %+v", err, unpacked, orig)
  21. }
  22. }
  23. func TestIs(t *testing.T) {
  24. a, err := MarshalAny(&pb.FileDescriptorProto{})
  25. if err != nil {
  26. t.Fatal(err)
  27. }
  28. if Is(a, &pb.DescriptorProto{}) {
  29. // No spurious match for message names of different length.
  30. t.Error("FileDescriptorProto is not a DescriptorProto, but Is says it is")
  31. }
  32. if Is(a, &pb.EnumDescriptorProto{}) {
  33. // No spurious match for message names of equal length.
  34. t.Error("FileDescriptorProto is not an EnumDescriptorProto, but Is says it is")
  35. }
  36. if !Is(a, &pb.FileDescriptorProto{}) {
  37. t.Error("FileDescriptorProto is indeed a FileDescriptorProto, but Is says it is not")
  38. }
  39. }
  40. func TestIsDifferentUrlPrefixes(t *testing.T) {
  41. m := &pb.FileDescriptorProto{}
  42. a := &any.Any{TypeUrl: "foo/bar/" + proto.MessageName(m)}
  43. if !Is(a, m) {
  44. t.Errorf("message with type url %q didn't satisfy Is for type %q", a.TypeUrl, proto.MessageName(m))
  45. }
  46. }
  47. func TestIsCornerCases(t *testing.T) {
  48. m := &pb.FileDescriptorProto{}
  49. if Is(nil, m) {
  50. t.Errorf("message with nil type url incorrectly claimed to be %q", proto.MessageName(m))
  51. }
  52. noPrefix := &any.Any{TypeUrl: proto.MessageName(m)}
  53. if Is(noPrefix, m) {
  54. t.Errorf("message with type url %q incorrectly claimed to be %q", noPrefix.TypeUrl, proto.MessageName(m))
  55. }
  56. shortPrefix := &any.Any{TypeUrl: "/" + proto.MessageName(m)}
  57. if !Is(shortPrefix, m) {
  58. t.Errorf("message with type url %q didn't satisfy Is for type %q", shortPrefix.TypeUrl, proto.MessageName(m))
  59. }
  60. }
  61. func TestUnmarshalDynamic(t *testing.T) {
  62. want := &pb.FileDescriptorProto{Name: proto.String("foo")}
  63. a, err := MarshalAny(want)
  64. if err != nil {
  65. t.Fatal(err)
  66. }
  67. var got DynamicAny
  68. if err := UnmarshalAny(a, &got); err != nil {
  69. t.Fatal(err)
  70. }
  71. if !proto.Equal(got.Message, want) {
  72. t.Errorf("invalid result from UnmarshalAny, got %q want %q", got.Message, want)
  73. }
  74. }
  75. func TestEmpty(t *testing.T) {
  76. want := &pb.FileDescriptorProto{}
  77. a, err := MarshalAny(want)
  78. if err != nil {
  79. t.Fatal(err)
  80. }
  81. got, err := Empty(a)
  82. if err != nil {
  83. t.Fatal(err)
  84. }
  85. if !proto.Equal(got, want) {
  86. t.Errorf("unequal empty message, got %q, want %q", got, want)
  87. }
  88. // that's a valid type_url for a message which shouldn't be linked into this
  89. // test binary. We want an error.
  90. a.TypeUrl = "type.googleapis.com/google.protobuf.compiler.Version"
  91. if _, err := Empty(a); err == nil {
  92. t.Errorf("got no error for an attempt to create a message of type %q, which shouldn't be linked in", a.TypeUrl)
  93. }
  94. }
  95. func TestEmptyCornerCases(t *testing.T) {
  96. _, err := Empty(nil)
  97. if err == nil {
  98. t.Error("expected Empty for nil to fail")
  99. }
  100. want := &pb.FileDescriptorProto{}
  101. noPrefix := &any.Any{TypeUrl: proto.MessageName(want)}
  102. _, err = Empty(noPrefix)
  103. if err == nil {
  104. t.Errorf("expected Empty for any type %q to fail", noPrefix.TypeUrl)
  105. }
  106. shortPrefix := &any.Any{TypeUrl: "/" + proto.MessageName(want)}
  107. got, err := Empty(shortPrefix)
  108. if err != nil {
  109. t.Errorf("Empty for any type %q failed: %s", shortPrefix.TypeUrl, err)
  110. }
  111. if !proto.Equal(got, want) {
  112. t.Errorf("Empty for any type %q differs, got %q, want %q", shortPrefix.TypeUrl, got, want)
  113. }
  114. }