any_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2016 The Go Authors. All rights reserved.
  4. // https://github.com/golang/protobuf
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. package ptypes
  32. import (
  33. "testing"
  34. "github.com/golang/protobuf/proto"
  35. pb "github.com/golang/protobuf/protoc-gen-go/descriptor"
  36. "github.com/golang/protobuf/ptypes/any"
  37. )
  38. func TestMarshalUnmarshal(t *testing.T) {
  39. orig := &any.Any{Value: []byte("test")}
  40. packed, err := MarshalAny(orig)
  41. if err != nil {
  42. t.Errorf("MarshalAny(%+v): got: _, %v exp: _, nil", orig, err)
  43. }
  44. unpacked := &any.Any{}
  45. err = UnmarshalAny(packed, unpacked)
  46. if err != nil || !proto.Equal(unpacked, orig) {
  47. t.Errorf("got: %v, %+v; want nil, %+v", err, unpacked, orig)
  48. }
  49. }
  50. func TestIs(t *testing.T) {
  51. a, err := MarshalAny(&pb.FileDescriptorProto{})
  52. if err != nil {
  53. t.Fatal(err)
  54. }
  55. if Is(a, &pb.DescriptorProto{}) {
  56. t.Error("FileDescriptorProto is not a DescriptorProto, but Is says it is")
  57. }
  58. if !Is(a, &pb.FileDescriptorProto{}) {
  59. t.Error("FileDescriptorProto is indeed a FileDescriptorProto, but Is says it is not")
  60. }
  61. }
  62. func TestIsDifferentUrlPrefixes(t *testing.T) {
  63. m := &pb.FileDescriptorProto{}
  64. a := &any.Any{TypeUrl: "foo/bar/" + proto.MessageName(m)}
  65. if !Is(a, m) {
  66. t.Errorf("message with type url %q didn't satisfy Is for type %q", a.TypeUrl, proto.MessageName(m))
  67. }
  68. }
  69. func TestUnmarshalDynamic(t *testing.T) {
  70. want := &pb.FileDescriptorProto{Name: proto.String("foo")}
  71. a, err := MarshalAny(want)
  72. if err != nil {
  73. t.Fatal(err)
  74. }
  75. var got DynamicAny
  76. if err := UnmarshalAny(a, &got); err != nil {
  77. t.Fatal(err)
  78. }
  79. if !proto.Equal(got.Message, want) {
  80. t.Errorf("invalid result from UnmarshalAny, got %q want %q", got.Message, want)
  81. }
  82. }
  83. func TestEmpty(t *testing.T) {
  84. want := &pb.FileDescriptorProto{}
  85. a, err := MarshalAny(want)
  86. if err != nil {
  87. t.Fatal(err)
  88. }
  89. got, err := Empty(a)
  90. if err != nil {
  91. t.Fatal(err)
  92. }
  93. if !proto.Equal(got, want) {
  94. t.Errorf("unequal empty message, got %q, want %q", got, want)
  95. }
  96. // that's a valid type_url for a message which shouldn't be linked into this
  97. // test binary. We want an error.
  98. a.TypeUrl = "type.googleapis.com/google.protobuf.FieldMask"
  99. if _, err := Empty(a); err == nil {
  100. t.Errorf("got no error for an attempt to create a message of type %q, which shouldn't be linked in", a.TypeUrl)
  101. }
  102. }