extension_test.go 6.6 KB

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