| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1 | // Go support for Protocol Buffers - Google's data interchange format |
| 2 | // |
| David Symonds | ee6e9c5 | 2012-11-29 08:51:07 +1100 | [diff] [blame] | 3 | // Copyright 2010 The Go Authors. All rights reserved. |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 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 | package proto_test |
| 33 | |
| 34 | import ( |
| 35 | "bytes" |
| David Symonds | 4dc589e | 2012-12-06 14:05:48 +1100 | [diff] [blame] | 36 | "errors" |
| 37 | "io/ioutil" |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 38 | "strings" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 39 | "testing" |
| David Symonds | aa922ff | 2011-07-19 14:58:06 +1000 | [diff] [blame] | 40 | |
| Rob Pike | 3f6f2d8 | 2011-12-18 13:55:35 -0800 | [diff] [blame] | 41 | "code.google.com/p/goprotobuf/proto" |
| David Symonds | aa922ff | 2011-07-19 14:58:06 +1000 | [diff] [blame] | 42 | |
| David Symonds | 704096f | 2012-03-15 15:10:26 +1100 | [diff] [blame] | 43 | pb "./testdata" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 44 | ) |
| 45 | |
| David Symonds | aa922ff | 2011-07-19 14:58:06 +1000 | [diff] [blame] | 46 | func newTestMessage() *pb.MyMessage { |
| 47 | msg := &pb.MyMessage{ |
| 48 | Count: proto.Int32(42), |
| 49 | Name: proto.String("Dave"), |
| 50 | Quote: proto.String(`"I didn't want to go."`), |
| Rob Pike | 9caa5b9 | 2010-05-11 16:04:57 -0700 | [diff] [blame] | 51 | Pet: []string{"bunny", "kitty", "horsey"}, |
| David Symonds | aa922ff | 2011-07-19 14:58:06 +1000 | [diff] [blame] | 52 | Inner: &pb.InnerMessage{ |
| 53 | Host: proto.String("footrest.syd"), |
| 54 | Port: proto.Int32(7001), |
| 55 | Connected: proto.Bool(true), |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 56 | }, |
| David Symonds | aa922ff | 2011-07-19 14:58:06 +1000 | [diff] [blame] | 57 | Others: []*pb.OtherMessage{ |
| David Symonds | 92dd6c1 | 2012-03-23 10:59:49 +1100 | [diff] [blame] | 58 | { |
| David Symonds | aa922ff | 2011-07-19 14:58:06 +1000 | [diff] [blame] | 59 | Key: proto.Int64(0xdeadbeef), |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 60 | Value: []byte{1, 65, 7, 12}, |
| 61 | }, |
| David Symonds | 92dd6c1 | 2012-03-23 10:59:49 +1100 | [diff] [blame] | 62 | { |
| David Symonds | aa922ff | 2011-07-19 14:58:06 +1000 | [diff] [blame] | 63 | Weight: proto.Float32(6.022), |
| 64 | Inner: &pb.InnerMessage{ |
| 65 | Host: proto.String("lesha.mtv"), |
| 66 | Port: proto.Int32(8002), |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 67 | }, |
| 68 | }, |
| 69 | }, |
| David Symonds | efeca9a | 2012-05-08 10:36:04 +1000 | [diff] [blame] | 70 | Bikeshed: pb.MyMessage_BLUE.Enum(), |
| David Symonds | aa922ff | 2011-07-19 14:58:06 +1000 | [diff] [blame] | 71 | Somegroup: &pb.MyMessage_SomeGroup{ |
| 72 | GroupField: proto.Int32(8), |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 73 | }, |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 74 | // One normally wouldn't do this. |
| 75 | // This is an undeclared tag 13, as a varint (wire type 0) with value 4. |
| 76 | XXX_unrecognized: []byte{13<<3 | 0, 4}, |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 77 | } |
| David Symonds | aa922ff | 2011-07-19 14:58:06 +1000 | [diff] [blame] | 78 | ext := &pb.Ext{ |
| 79 | Data: proto.String("Big gobs for big rats"), |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 80 | } |
| David Symonds | aa922ff | 2011-07-19 14:58:06 +1000 | [diff] [blame] | 81 | if err := proto.SetExtension(msg, pb.E_Ext_More, ext); err != nil { |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 82 | panic(err) |
| 83 | } |
| David Symonds | 61826da | 2012-05-05 09:31:28 +1000 | [diff] [blame] | 84 | greetings := []string{"adg", "easy", "cow"} |
| 85 | if err := proto.SetExtension(msg, pb.E_Greeting, greetings); err != nil { |
| 86 | panic(err) |
| 87 | } |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 88 | |
| 89 | // Add an unknown extension. We marshal a pb.Ext, and fake the ID. |
| 90 | b, err := proto.Marshal(&pb.Ext{Data: proto.String("3G skiing")}) |
| 91 | if err != nil { |
| 92 | panic(err) |
| 93 | } |
| David Symonds | 5453105 | 2011-12-08 12:00:31 +1100 | [diff] [blame] | 94 | b = append(proto.EncodeVarint(201<<3|proto.WireBytes), b...) |
| 95 | proto.SetRawExtension(msg, 201, b) |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 96 | |
| 97 | // Extensions can be plain fields, too, so let's test that. |
| David Symonds | 5453105 | 2011-12-08 12:00:31 +1100 | [diff] [blame] | 98 | b = append(proto.EncodeVarint(202<<3|proto.WireVarint), 19) |
| 99 | proto.SetRawExtension(msg, 202, b) |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 100 | |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 101 | return msg |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | const text = `count: 42 |
| 105 | name: "Dave" |
| 106 | quote: "\"I didn't want to go.\"" |
| 107 | pet: "bunny" |
| 108 | pet: "kitty" |
| 109 | pet: "horsey" |
| 110 | inner: < |
| 111 | host: "footrest.syd" |
| 112 | port: 7001 |
| 113 | connected: true |
| 114 | > |
| 115 | others: < |
| 116 | key: 3735928559 |
| David Symonds | 4c95bfe | 2011-09-13 14:43:27 +1000 | [diff] [blame] | 117 | value: "\001A\007\014" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 118 | > |
| 119 | others: < |
| 120 | weight: 6.022 |
| 121 | inner: < |
| 122 | host: "lesha.mtv" |
| 123 | port: 8002 |
| 124 | > |
| 125 | > |
| 126 | bikeshed: BLUE |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 127 | SomeGroup { |
| 128 | group_field: 8 |
| 129 | } |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 130 | /* 2 unknown bytes */ |
| David Symonds | 2037090 | 2013-03-23 17:20:01 +1100 | [diff] [blame^] | 131 | 13: 4 |
| Rob Pike | b7907bf | 2012-02-13 14:25:20 +1100 | [diff] [blame] | 132 | [testdata.Ext.more]: < |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 133 | data: "Big gobs for big rats" |
| 134 | > |
| David Symonds | 61826da | 2012-05-05 09:31:28 +1000 | [diff] [blame] | 135 | [testdata.greeting]: "adg" |
| 136 | [testdata.greeting]: "easy" |
| 137 | [testdata.greeting]: "cow" |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 138 | /* 13 unknown bytes */ |
| David Symonds | 2037090 | 2013-03-23 17:20:01 +1100 | [diff] [blame^] | 139 | 201: "\t3G skiing" |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 140 | /* 3 unknown bytes */ |
| David Symonds | 2037090 | 2013-03-23 17:20:01 +1100 | [diff] [blame^] | 141 | 202: 19 |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 142 | ` |
| 143 | |
| David Symonds | 4dc589e | 2012-12-06 14:05:48 +1100 | [diff] [blame] | 144 | func TestMarshalText(t *testing.T) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 145 | buf := new(bytes.Buffer) |
| David Symonds | 4dc589e | 2012-12-06 14:05:48 +1100 | [diff] [blame] | 146 | if err := proto.MarshalText(buf, newTestMessage()); err != nil { |
| 147 | t.Fatalf("proto.MarshalText: %v", err) |
| 148 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 149 | s := buf.String() |
| 150 | if s != text { |
| 151 | t.Errorf("Got:\n===\n%v===\nExpected:\n===\n%v===\n", s, text) |
| 152 | } |
| 153 | } |
| 154 | |
| David Symonds | 6e8ab87 | 2013-01-30 17:07:26 +1100 | [diff] [blame] | 155 | func TestMarshalTextNil(t *testing.T) { |
| 156 | want := "<nil>" |
| 157 | tests := []proto.Message{nil, (*pb.MyMessage)(nil)} |
| 158 | for i, test := range tests { |
| 159 | buf := new(bytes.Buffer) |
| 160 | if err := proto.MarshalText(buf, test); err != nil { |
| 161 | t.Fatal(err) |
| 162 | } |
| 163 | if got := buf.String(); got != want { |
| 164 | t.Errorf("%d: got %q want %q", i, got, want) |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| David Symonds | 4dc589e | 2012-12-06 14:05:48 +1100 | [diff] [blame] | 169 | func BenchmarkMarshalTextBuffered(b *testing.B) { |
| David Symonds | dbdc421 | 2012-11-08 08:20:35 +1100 | [diff] [blame] | 170 | buf := new(bytes.Buffer) |
| 171 | m := newTestMessage() |
| 172 | for i := 0; i < b.N; i++ { |
| 173 | buf.Reset() |
| 174 | proto.MarshalText(buf, m) |
| 175 | } |
| 176 | } |
| 177 | |
| David Symonds | 4dc589e | 2012-12-06 14:05:48 +1100 | [diff] [blame] | 178 | func BenchmarkMarshalTextUnbuffered(b *testing.B) { |
| 179 | w := ioutil.Discard |
| 180 | m := newTestMessage() |
| 181 | for i := 0; i < b.N; i++ { |
| 182 | proto.MarshalText(w, m) |
| 183 | } |
| 184 | } |
| 185 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 186 | func compact(src string) string { |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 187 | // s/[ \n]+/ /g; s/ $//; |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 188 | dst := make([]byte, len(src)) |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 189 | space, comment := false, false |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 190 | j := 0 |
| 191 | for i := 0; i < len(src); i++ { |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 192 | if strings.HasPrefix(src[i:], "/*") { |
| 193 | comment = true |
| 194 | i++ |
| 195 | continue |
| 196 | } |
| 197 | if comment && strings.HasPrefix(src[i:], "*/") { |
| 198 | comment = false |
| 199 | i++ |
| 200 | continue |
| 201 | } |
| 202 | if comment { |
| 203 | continue |
| 204 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 205 | c := src[i] |
| 206 | if c == ' ' || c == '\n' { |
| 207 | space = true |
| 208 | continue |
| 209 | } |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 210 | if j > 0 && (dst[j-1] == ':' || dst[j-1] == '<' || dst[j-1] == '{') { |
| 211 | space = false |
| 212 | } |
| 213 | if c == '{' { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 214 | space = false |
| 215 | } |
| 216 | if space { |
| 217 | dst[j] = ' ' |
| 218 | j++ |
| 219 | space = false |
| 220 | } |
| 221 | dst[j] = c |
| 222 | j++ |
| 223 | } |
| 224 | if space { |
| 225 | dst[j] = ' ' |
| 226 | j++ |
| 227 | } |
| 228 | return string(dst[0:j]) |
| 229 | } |
| 230 | |
| 231 | var compactText = compact(text) |
| 232 | |
| 233 | func TestCompactText(t *testing.T) { |
| David Symonds | aa922ff | 2011-07-19 14:58:06 +1000 | [diff] [blame] | 234 | s := proto.CompactTextString(newTestMessage()) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 235 | if s != compactText { |
| David Symonds | 4c95bfe | 2011-09-13 14:43:27 +1000 | [diff] [blame] | 236 | t.Errorf("Got:\n===\n%v===\nExpected:\n===\n%v\n===\n", s, compactText) |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | func TestStringEscaping(t *testing.T) { |
| 241 | testCases := []struct { |
| 242 | in *pb.Strings |
| 243 | out string |
| 244 | }{ |
| 245 | { |
| 246 | // Test data from C++ test (TextFormatTest.StringEscape). |
| 247 | // Single divergence: we don't escape apostrophes. |
| 248 | &pb.Strings{StringField: proto.String("\"A string with ' characters \n and \r newlines and \t tabs and \001 slashes \\ and multiple spaces")}, |
| 249 | "string_field: \"\\\"A string with ' characters \\n and \\r newlines and \\t tabs and \\001 slashes \\\\ and multiple spaces\"\n", |
| 250 | }, |
| 251 | { |
| 252 | // Test data from the same C++ test. |
| 253 | &pb.Strings{StringField: proto.String("\350\260\267\346\255\214")}, |
| 254 | "string_field: \"\\350\\260\\267\\346\\255\\214\"\n", |
| 255 | }, |
| David Symonds | fa94a1e | 2012-09-24 13:21:49 +1000 | [diff] [blame] | 256 | { |
| 257 | // Some UTF-8. |
| 258 | &pb.Strings{StringField: proto.String("\x00\x01\xff\x81")}, |
| 259 | `string_field: "\000\001\377\201"` + "\n", |
| 260 | }, |
| David Symonds | 4c95bfe | 2011-09-13 14:43:27 +1000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | for i, tc := range testCases { |
| 264 | var buf bytes.Buffer |
| David Symonds | 4dc589e | 2012-12-06 14:05:48 +1100 | [diff] [blame] | 265 | if err := proto.MarshalText(&buf, tc.in); err != nil { |
| 266 | t.Errorf("proto.MarsalText: %v", err) |
| 267 | continue |
| 268 | } |
| David Symonds | fa94a1e | 2012-09-24 13:21:49 +1000 | [diff] [blame] | 269 | s := buf.String() |
| 270 | if s != tc.out { |
| David Symonds | 4c95bfe | 2011-09-13 14:43:27 +1000 | [diff] [blame] | 271 | t.Errorf("#%d: Got:\n%s\nExpected:\n%s\n", i, s, tc.out) |
| David Symonds | fa94a1e | 2012-09-24 13:21:49 +1000 | [diff] [blame] | 272 | continue |
| 273 | } |
| 274 | |
| 275 | // Check round-trip. |
| 276 | pb := new(pb.Strings) |
| 277 | if err := proto.UnmarshalText(s, pb); err != nil { |
| 278 | t.Errorf("#%d: UnmarshalText: %v", i, err) |
| 279 | continue |
| 280 | } |
| 281 | if !proto.Equal(pb, tc.in) { |
| 282 | t.Errorf("#%d: Round-trip failed:\nstart: %v\n end: %v", i, tc.in, pb) |
| David Symonds | 4c95bfe | 2011-09-13 14:43:27 +1000 | [diff] [blame] | 283 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 284 | } |
| 285 | } |
| David Symonds | 4dc589e | 2012-12-06 14:05:48 +1100 | [diff] [blame] | 286 | |
| 287 | // A limitedWriter accepts some output before it fails. |
| 288 | // This is a proxy for something like a nearly-full or imminently-failing disk, |
| 289 | // or a network connection that is about to die. |
| 290 | type limitedWriter struct { |
| 291 | b bytes.Buffer |
| 292 | limit int |
| 293 | } |
| 294 | |
| 295 | var outOfSpace = errors.New("insufficient space") |
| 296 | |
| 297 | func (w *limitedWriter) Write(p []byte) (n int, err error) { |
| 298 | var avail = w.limit - w.b.Len() |
| 299 | if avail <= 0 { |
| 300 | return 0, outOfSpace |
| 301 | } |
| 302 | if len(p) <= avail { |
| 303 | return w.b.Write(p) |
| 304 | } |
| 305 | n, _ = w.b.Write(p[:avail]) |
| 306 | return n, outOfSpace |
| 307 | } |
| 308 | |
| 309 | func TestMarshalTextFailing(t *testing.T) { |
| 310 | // Try lots of different sizes to exercise more error code-paths. |
| 311 | for lim := 0; lim < len(text); lim++ { |
| 312 | buf := new(limitedWriter) |
| 313 | buf.limit = lim |
| 314 | err := proto.MarshalText(buf, newTestMessage()) |
| 315 | // We expect a certain error, but also some partial results in the buffer. |
| 316 | if err != outOfSpace { |
| 317 | t.Errorf("Got:\n===\n%v===\nExpected:\n===\n%v===\n", err, outOfSpace) |
| 318 | } |
| 319 | s := buf.b.String() |
| 320 | x := text[:buf.limit] |
| 321 | if s != x { |
| 322 | t.Errorf("Got:\n===\n%v===\nExpected:\n===\n%v===\n", s, x) |
| 323 | } |
| 324 | } |
| 325 | } |