extensions_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2014 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 proto_test
  32. import (
  33. "testing"
  34. pb "./testdata"
  35. "github.com/golang/protobuf/proto"
  36. )
  37. func TestGetExtensionsWithMissingExtensions(t *testing.T) {
  38. msg := &pb.MyMessage{}
  39. ext1 := &pb.Ext{}
  40. if err := proto.SetExtension(msg, pb.E_Ext_More, ext1); err != nil {
  41. t.Fatalf("Could not set ext1: %s", ext1)
  42. }
  43. exts, err := proto.GetExtensions(msg, []*proto.ExtensionDesc{
  44. pb.E_Ext_More,
  45. pb.E_Ext_Text,
  46. })
  47. if err != nil {
  48. t.Fatalf("GetExtensions() failed: %s", err)
  49. }
  50. if exts[0] != ext1 {
  51. t.Errorf("ext1 not in returned extensions: %T %v", exts[0], exts[0])
  52. }
  53. if exts[1] != nil {
  54. t.Errorf("ext2 in returned extensions: %T %v", exts[1], exts[1])
  55. }
  56. }
  57. func TestGetExtensionStability(t *testing.T) {
  58. check := func(m *pb.MyMessage) bool {
  59. ext1, err := proto.GetExtension(m, pb.E_Ext_More)
  60. if err != nil {
  61. t.Fatalf("GetExtension() failed: %s", err)
  62. }
  63. ext2, err := proto.GetExtension(m, pb.E_Ext_More)
  64. if err != nil {
  65. t.Fatalf("GetExtension() failed: %s", err)
  66. }
  67. return ext1 == ext2
  68. }
  69. msg := &pb.MyMessage{Count: proto.Int32(4)}
  70. ext0 := &pb.Ext{}
  71. if err := proto.SetExtension(msg, pb.E_Ext_More, ext0); err != nil {
  72. t.Fatalf("Could not set ext1: %s", ext0)
  73. }
  74. if !check(msg) {
  75. t.Errorf("GetExtension() not stable before marshaling")
  76. }
  77. bb, err := proto.Marshal(msg)
  78. if err != nil {
  79. t.Fatalf("Marshal() failed: %s", err)
  80. }
  81. msg1 := &pb.MyMessage{}
  82. err = proto.Unmarshal(bb, msg1)
  83. if err != nil {
  84. t.Fatalf("Unmarshal() failed: %s", err)
  85. }
  86. if !check(msg1) {
  87. t.Errorf("GetExtension() not stable after unmarshaling")
  88. }
  89. }
  90. func TestExtensionsRoundTrip(t *testing.T) {
  91. msg := &pb.MyMessage{}
  92. ext1 := &pb.Ext{
  93. Data: proto.String("hi"),
  94. }
  95. ext2 := &pb.Ext{
  96. Data: proto.String("there"),
  97. }
  98. exists := proto.HasExtension(msg, pb.E_Ext_More)
  99. if exists {
  100. t.Error("Extension More present unexpectedly")
  101. }
  102. if err := proto.SetExtension(msg, pb.E_Ext_More, ext1); err != nil {
  103. t.Error(err)
  104. }
  105. if err := proto.SetExtension(msg, pb.E_Ext_More, ext2); err != nil {
  106. t.Error(err)
  107. }
  108. e, err := proto.GetExtension(msg, pb.E_Ext_More)
  109. if err != nil {
  110. t.Error(err)
  111. }
  112. x, ok := e.(*pb.Ext)
  113. if !ok {
  114. t.Errorf("e has type %T, expected testdata.Ext", e)
  115. } else if *x.Data != "there" {
  116. t.Errorf("SetExtension failed to overwrite, got %+v, not 'there'", x)
  117. }
  118. proto.ClearExtension(msg, pb.E_Ext_More)
  119. if _, err = proto.GetExtension(msg, pb.E_Ext_More); err != proto.ErrMissingExtension {
  120. t.Errorf("got %v, expected ErrMissingExtension", e)
  121. }
  122. if _, err := proto.GetExtension(msg, pb.E_X215); err == nil {
  123. t.Error("expected bad extension error, got nil")
  124. }
  125. if err := proto.SetExtension(msg, pb.E_X215, 12); err == nil {
  126. t.Error("expected extension err")
  127. }
  128. if err := proto.SetExtension(msg, pb.E_Ext_More, 12); err == nil {
  129. t.Error("expected some sort of type mismatch error, got nil")
  130. }
  131. }