David Symonds | c37ad66 | 2010-04-07 09:25:13 +1000 | [diff] [blame] | 1 | // Go support for Protocol Buffers - Google's data interchange format |
| 2 | // |
| 3 | // Copyright 2010 Google Inc. 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 | |
| 32 | // Test that we can use protocol buffers that use extensions. |
| 33 | |
| 34 | package main |
| 35 | |
| 36 | import ( |
| 37 | "testing" |
| 38 | |
| 39 | "goprotobuf.googlecode.com/hg/proto" |
| 40 | base "extension_base.pb" |
| 41 | user "extension_user.pb" |
| 42 | ) |
| 43 | |
| 44 | func TestSingleFieldExtension(t *testing.T) { |
Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame^] | 45 | bm := &base.BaseMessage{} |
David Symonds | c37ad66 | 2010-04-07 09:25:13 +1000 | [diff] [blame] | 46 | bm.Height = proto.Int32(178) |
| 47 | |
| 48 | // Use extension within scope of another type. |
| 49 | vol := proto.Uint32(11) |
David Symonds | 9bd0a83 | 2010-04-11 10:02:32 +1000 | [diff] [blame] | 50 | err := proto.SetExtension(bm, user.E_LoudMessage_Volume, vol) |
David Symonds | c37ad66 | 2010-04-07 09:25:13 +1000 | [diff] [blame] | 51 | if err != nil { |
| 52 | t.Fatal("Failed setting extension:", err) |
| 53 | } |
| 54 | buf, err := proto.Marshal(bm) |
| 55 | if err != nil { |
| 56 | t.Fatal("Failed encoding message with extension:", err) |
| 57 | } |
Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame^] | 58 | bm_new := &base.BaseMessage{} |
David Symonds | c37ad66 | 2010-04-07 09:25:13 +1000 | [diff] [blame] | 59 | if err := proto.Unmarshal(buf, bm_new); err != nil { |
| 60 | t.Fatal("Failed decoding message with extension:", err) |
| 61 | } |
David Symonds | 9bd0a83 | 2010-04-11 10:02:32 +1000 | [diff] [blame] | 62 | if !proto.HasExtension(bm_new, user.E_LoudMessage_Volume) { |
David Symonds | c37ad66 | 2010-04-07 09:25:13 +1000 | [diff] [blame] | 63 | t.Fatal("Decoded message didn't contain extension.") |
| 64 | } |
David Symonds | 9bd0a83 | 2010-04-11 10:02:32 +1000 | [diff] [blame] | 65 | vol_out, err := proto.GetExtension(bm_new, user.E_LoudMessage_Volume) |
David Symonds | c37ad66 | 2010-04-07 09:25:13 +1000 | [diff] [blame] | 66 | if err != nil { |
| 67 | t.Fatal("Failed getting extension:", err) |
| 68 | } |
| 69 | if v := vol_out.(*uint32); *v != *vol { |
| 70 | t.Errorf("vol_out = %v, expected %v", *v, *vol) |
| 71 | } |
David Symonds | 9bd0a83 | 2010-04-11 10:02:32 +1000 | [diff] [blame] | 72 | proto.ClearExtension(bm_new, user.E_LoudMessage_Volume) |
| 73 | if proto.HasExtension(bm_new, user.E_LoudMessage_Volume) { |
David Symonds | c37ad66 | 2010-04-07 09:25:13 +1000 | [diff] [blame] | 74 | t.Fatal("Failed clearing extension.") |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | func TestMessageExtension(t *testing.T) { |
Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame^] | 79 | bm := &base.BaseMessage{} |
David Symonds | c37ad66 | 2010-04-07 09:25:13 +1000 | [diff] [blame] | 80 | bm.Height = proto.Int32(179) |
| 81 | |
| 82 | // Use extension that is itself a message. |
| 83 | um := &user.UserMessage{ |
| 84 | Name: proto.String("Dave"), |
| 85 | Rank: proto.String("Major"), |
| 86 | } |
David Symonds | 9bd0a83 | 2010-04-11 10:02:32 +1000 | [diff] [blame] | 87 | err := proto.SetExtension(bm, user.E_LoginMessage_UserMessage, um) |
David Symonds | c37ad66 | 2010-04-07 09:25:13 +1000 | [diff] [blame] | 88 | if err != nil { |
| 89 | t.Fatal("Failed setting extension:", err) |
| 90 | } |
| 91 | buf, err := proto.Marshal(bm) |
| 92 | if err != nil { |
| 93 | t.Fatal("Failed encoding message with extension:", err) |
| 94 | } |
Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame^] | 95 | bm_new := &base.BaseMessage{} |
David Symonds | c37ad66 | 2010-04-07 09:25:13 +1000 | [diff] [blame] | 96 | if err := proto.Unmarshal(buf, bm_new); err != nil { |
| 97 | t.Fatal("Failed decoding message with extension:", err) |
| 98 | } |
David Symonds | 9bd0a83 | 2010-04-11 10:02:32 +1000 | [diff] [blame] | 99 | if !proto.HasExtension(bm_new, user.E_LoginMessage_UserMessage) { |
David Symonds | c37ad66 | 2010-04-07 09:25:13 +1000 | [diff] [blame] | 100 | t.Fatal("Decoded message didn't contain extension.") |
| 101 | } |
David Symonds | 9bd0a83 | 2010-04-11 10:02:32 +1000 | [diff] [blame] | 102 | um_out, err := proto.GetExtension(bm_new, user.E_LoginMessage_UserMessage) |
David Symonds | c37ad66 | 2010-04-07 09:25:13 +1000 | [diff] [blame] | 103 | if err != nil { |
| 104 | t.Fatal("Failed getting extension:", err) |
| 105 | } |
| 106 | if n := um_out.(*user.UserMessage).Name; *n != *um.Name { |
| 107 | t.Errorf("um_out.Name = %q, expected %q", *n, *um.Name) |
| 108 | } |
| 109 | if r := um_out.(*user.UserMessage).Rank; *r != *um.Rank { |
| 110 | t.Errorf("um_out.Rank = %q, expected %q", *r, *um.Rank) |
| 111 | } |
David Symonds | 9bd0a83 | 2010-04-11 10:02:32 +1000 | [diff] [blame] | 112 | proto.ClearExtension(bm_new, user.E_LoginMessage_UserMessage) |
| 113 | if proto.HasExtension(bm_new, user.E_LoginMessage_UserMessage) { |
David Symonds | c37ad66 | 2010-04-07 09:25:13 +1000 | [diff] [blame] | 114 | t.Fatal("Failed clearing extension.") |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | func TestTopLevelExtension(t *testing.T) { |
Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame^] | 119 | bm := &base.BaseMessage{} |
David Symonds | c37ad66 | 2010-04-07 09:25:13 +1000 | [diff] [blame] | 120 | bm.Height = proto.Int32(179) |
| 121 | |
| 122 | width := proto.Int32(17) |
| 123 | err := proto.SetExtension(bm, user.E_Width, width) |
| 124 | if err != nil { |
| 125 | t.Fatal("Failed setting extension:", err) |
| 126 | } |
| 127 | buf, err := proto.Marshal(bm) |
| 128 | if err != nil { |
| 129 | t.Fatal("Failed encoding message with extension:", err) |
| 130 | } |
Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame^] | 131 | bm_new := &base.BaseMessage{} |
David Symonds | c37ad66 | 2010-04-07 09:25:13 +1000 | [diff] [blame] | 132 | if err := proto.Unmarshal(buf, bm_new); err != nil { |
| 133 | t.Fatal("Failed decoding message with extension:", err) |
| 134 | } |
| 135 | if !proto.HasExtension(bm_new, user.E_Width) { |
| 136 | t.Fatal("Decoded message didn't contain extension.") |
| 137 | } |
| 138 | width_out, err := proto.GetExtension(bm_new, user.E_Width) |
| 139 | if err != nil { |
| 140 | t.Fatal("Failed getting extension:", err) |
| 141 | } |
| 142 | if w := width_out.(*int32); *w != *width { |
| 143 | t.Errorf("width_out = %v, expected %v", *w, *width) |
| 144 | } |
| 145 | proto.ClearExtension(bm_new, user.E_Width) |
| 146 | if proto.HasExtension(bm_new, user.E_Width) { |
| 147 | t.Fatal("Failed clearing extension.") |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | func main() { |
| 152 | // simpler than rigging up gotest |
| 153 | testing.Main([]testing.Test{ |
| 154 | testing.Test{"TestSingleFieldExtension", TestSingleFieldExtension}, |
| 155 | testing.Test{"TestMessageExtension", TestMessageExtension}, |
| 156 | testing.Test{"TestTopLevelExtension", TestTopLevelExtension}, |
| 157 | }) |
| 158 | } |