extension_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2010 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. // Test that we can use protocol buffers that use extensions.
  32. package testdata
  33. import (
  34. "bytes"
  35. "regexp"
  36. "testing"
  37. "github.com/golang/protobuf/proto"
  38. base "github.com/golang/protobuf/protoc-gen-go/testdata/extension_base"
  39. user "github.com/golang/protobuf/protoc-gen-go/testdata/extension_user"
  40. )
  41. func TestSingleFieldExtension(t *testing.T) {
  42. bm := &base.BaseMessage{
  43. Height: proto.Int32(178),
  44. }
  45. // Use extension within scope of another type.
  46. vol := proto.Uint32(11)
  47. err := proto.SetExtension(bm, user.E_LoudMessage_Volume, vol)
  48. if err != nil {
  49. t.Fatal("Failed setting extension:", err)
  50. }
  51. buf, err := proto.Marshal(bm)
  52. if err != nil {
  53. t.Fatal("Failed encoding message with extension:", err)
  54. }
  55. bm_new := new(base.BaseMessage)
  56. if err := proto.Unmarshal(buf, bm_new); err != nil {
  57. t.Fatal("Failed decoding message with extension:", err)
  58. }
  59. if !proto.HasExtension(bm_new, user.E_LoudMessage_Volume) {
  60. t.Fatal("Decoded message didn't contain extension.")
  61. }
  62. vol_out, err := proto.GetExtension(bm_new, user.E_LoudMessage_Volume)
  63. if err != nil {
  64. t.Fatal("Failed getting extension:", err)
  65. }
  66. if v := vol_out.(*uint32); *v != *vol {
  67. t.Errorf("vol_out = %v, expected %v", *v, *vol)
  68. }
  69. proto.ClearExtension(bm_new, user.E_LoudMessage_Volume)
  70. if proto.HasExtension(bm_new, user.E_LoudMessage_Volume) {
  71. t.Fatal("Failed clearing extension.")
  72. }
  73. }
  74. func TestMessageExtension(t *testing.T) {
  75. bm := &base.BaseMessage{
  76. Height: proto.Int32(179),
  77. }
  78. // Use extension that is itself a message.
  79. um := &user.UserMessage{
  80. Name: proto.String("Dave"),
  81. Rank: proto.String("Major"),
  82. }
  83. err := proto.SetExtension(bm, user.E_LoginMessage_UserMessage, um)
  84. if err != nil {
  85. t.Fatal("Failed setting extension:", err)
  86. }
  87. buf, err := proto.Marshal(bm)
  88. if err != nil {
  89. t.Fatal("Failed encoding message with extension:", err)
  90. }
  91. bm_new := new(base.BaseMessage)
  92. if err := proto.Unmarshal(buf, bm_new); err != nil {
  93. t.Fatal("Failed decoding message with extension:", err)
  94. }
  95. if !proto.HasExtension(bm_new, user.E_LoginMessage_UserMessage) {
  96. t.Fatal("Decoded message didn't contain extension.")
  97. }
  98. um_out, err := proto.GetExtension(bm_new, user.E_LoginMessage_UserMessage)
  99. if err != nil {
  100. t.Fatal("Failed getting extension:", err)
  101. }
  102. if n := um_out.(*user.UserMessage).Name; *n != *um.Name {
  103. t.Errorf("um_out.Name = %q, expected %q", *n, *um.Name)
  104. }
  105. if r := um_out.(*user.UserMessage).Rank; *r != *um.Rank {
  106. t.Errorf("um_out.Rank = %q, expected %q", *r, *um.Rank)
  107. }
  108. proto.ClearExtension(bm_new, user.E_LoginMessage_UserMessage)
  109. if proto.HasExtension(bm_new, user.E_LoginMessage_UserMessage) {
  110. t.Fatal("Failed clearing extension.")
  111. }
  112. }
  113. func TestTopLevelExtension(t *testing.T) {
  114. bm := &base.BaseMessage{
  115. Height: proto.Int32(179),
  116. }
  117. width := proto.Int32(17)
  118. err := proto.SetExtension(bm, user.E_Width, width)
  119. if err != nil {
  120. t.Fatal("Failed setting extension:", err)
  121. }
  122. buf, err := proto.Marshal(bm)
  123. if err != nil {
  124. t.Fatal("Failed encoding message with extension:", err)
  125. }
  126. bm_new := new(base.BaseMessage)
  127. if err := proto.Unmarshal(buf, bm_new); err != nil {
  128. t.Fatal("Failed decoding message with extension:", err)
  129. }
  130. if !proto.HasExtension(bm_new, user.E_Width) {
  131. t.Fatal("Decoded message didn't contain extension.")
  132. }
  133. width_out, err := proto.GetExtension(bm_new, user.E_Width)
  134. if err != nil {
  135. t.Fatal("Failed getting extension:", err)
  136. }
  137. if w := width_out.(*int32); *w != *width {
  138. t.Errorf("width_out = %v, expected %v", *w, *width)
  139. }
  140. proto.ClearExtension(bm_new, user.E_Width)
  141. if proto.HasExtension(bm_new, user.E_Width) {
  142. t.Fatal("Failed clearing extension.")
  143. }
  144. }
  145. func TestMessageSetWireFormat(t *testing.T) {
  146. osm := new(base.OldStyleMessage)
  147. osp := &user.OldStyleParcel{
  148. Name: proto.String("Dave"),
  149. Height: proto.Int32(178),
  150. }
  151. err := proto.SetExtension(osm, user.E_OldStyleParcel_MessageSetExtension, osp)
  152. if err != nil {
  153. t.Fatal("Failed setting extension:", err)
  154. }
  155. buf, err := proto.Marshal(osm)
  156. if err != nil {
  157. t.Fatal("Failed encoding message:", err)
  158. }
  159. // Data generated from Python implementation.
  160. expected := []byte{
  161. 11, 16, 209, 15, 26, 9, 10, 4, 68, 97, 118, 101, 16, 178, 1, 12,
  162. }
  163. if !bytes.Equal(expected, buf) {
  164. t.Errorf("Encoding mismatch.\nwant %+v\n got %+v", expected, buf)
  165. }
  166. // Check that it is restored correctly.
  167. osm = new(base.OldStyleMessage)
  168. if err := proto.Unmarshal(buf, osm); err != nil {
  169. t.Fatal("Failed decoding message:", err)
  170. }
  171. osp_out, err := proto.GetExtension(osm, user.E_OldStyleParcel_MessageSetExtension)
  172. if err != nil {
  173. t.Fatal("Failed getting extension:", err)
  174. }
  175. osp = osp_out.(*user.OldStyleParcel)
  176. if *osp.Name != "Dave" || *osp.Height != 178 {
  177. t.Errorf("Retrieved extension from decoded message is not correct: %+v", osp)
  178. }
  179. }
  180. func main() {
  181. // simpler than rigging up gotest
  182. testing.Main(regexp.MatchString, []testing.InternalTest{
  183. {"TestSingleFieldExtension", TestSingleFieldExtension},
  184. {"TestMessageExtension", TestMessageExtension},
  185. {"TestTopLevelExtension", TestTopLevelExtension},
  186. },
  187. []testing.InternalBenchmark{},
  188. []testing.InternalExample{})
  189. }