You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

155 lines
5.2 KiB

  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. // No spurious match for message names of different length.
  57. t.Error("FileDescriptorProto is not a DescriptorProto, but Is says it is")
  58. }
  59. if Is(a, &pb.EnumDescriptorProto{}) {
  60. // No spurious match for message names of equal length.
  61. t.Error("FileDescriptorProto is not an EnumDescriptorProto, but Is says it is")
  62. }
  63. if !Is(a, &pb.FileDescriptorProto{}) {
  64. t.Error("FileDescriptorProto is indeed a FileDescriptorProto, but Is says it is not")
  65. }
  66. }
  67. func TestIsDifferentUrlPrefixes(t *testing.T) {
  68. m := &pb.FileDescriptorProto{}
  69. a := &any.Any{TypeUrl: "foo/bar/" + proto.MessageName(m)}
  70. if !Is(a, m) {
  71. t.Errorf("message with type url %q didn't satisfy Is for type %q", a.TypeUrl, proto.MessageName(m))
  72. }
  73. }
  74. func TestIsCornerCases(t *testing.T) {
  75. m := &pb.FileDescriptorProto{}
  76. if Is(nil, m) {
  77. t.Errorf("message with nil type url incorrectly claimed to be %q", proto.MessageName(m))
  78. }
  79. noPrefix := &any.Any{TypeUrl: proto.MessageName(m)}
  80. if Is(noPrefix, m) {
  81. t.Errorf("message with type url %q incorrectly claimed to be %q", noPrefix.TypeUrl, proto.MessageName(m))
  82. }
  83. shortPrefix := &any.Any{TypeUrl: "/" + proto.MessageName(m)}
  84. if !Is(shortPrefix, m) {
  85. t.Errorf("message with type url %q didn't satisfy Is for type %q", shortPrefix.TypeUrl, proto.MessageName(m))
  86. }
  87. }
  88. func TestUnmarshalDynamic(t *testing.T) {
  89. want := &pb.FileDescriptorProto{Name: proto.String("foo")}
  90. a, err := MarshalAny(want)
  91. if err != nil {
  92. t.Fatal(err)
  93. }
  94. var got DynamicAny
  95. if err := UnmarshalAny(a, &got); err != nil {
  96. t.Fatal(err)
  97. }
  98. if !proto.Equal(got.Message, want) {
  99. t.Errorf("invalid result from UnmarshalAny, got %q want %q", got.Message, want)
  100. }
  101. }
  102. func TestEmpty(t *testing.T) {
  103. want := &pb.FileDescriptorProto{}
  104. a, err := MarshalAny(want)
  105. if err != nil {
  106. t.Fatal(err)
  107. }
  108. got, err := Empty(a)
  109. if err != nil {
  110. t.Fatal(err)
  111. }
  112. if !proto.Equal(got, want) {
  113. t.Errorf("unequal empty message, got %q, want %q", got, want)
  114. }
  115. // that's a valid type_url for a message which shouldn't be linked into this
  116. // test binary. We want an error.
  117. a.TypeUrl = "type.googleapis.com/google.protobuf.FieldMask"
  118. if _, err := Empty(a); err == nil {
  119. t.Errorf("got no error for an attempt to create a message of type %q, which shouldn't be linked in", a.TypeUrl)
  120. }
  121. }
  122. func TestEmptyCornerCases(t *testing.T) {
  123. _, err := Empty(nil)
  124. if err == nil {
  125. t.Error("expected Empty for nil to fail")
  126. }
  127. want := &pb.FileDescriptorProto{}
  128. noPrefix := &any.Any{TypeUrl: proto.MessageName(want)}
  129. _, err = Empty(noPrefix)
  130. if err == nil {
  131. t.Errorf("expected Empty for any type %q to fail", noPrefix.TypeUrl)
  132. }
  133. shortPrefix := &any.Any{TypeUrl: "/" + proto.MessageName(want)}
  134. got, err := Empty(shortPrefix)
  135. if err != nil {
  136. t.Errorf("Empty for any type %q failed: %s", shortPrefix.TypeUrl, err)
  137. }
  138. if !proto.Equal(got, want) {
  139. t.Errorf("Empty for any type %q differs, got %q, want %q", shortPrefix.TypeUrl, got, want)
  140. }
  141. }