| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [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 | package proto_test |
| 33 | |
| 34 | import ( |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 35 | "reflect" |
| 36 | "testing" |
| David Symonds | 20c7366 | 2012-01-20 07:32:21 +1100 | [diff] [blame] | 37 | |
| 38 | . "./testdata/_obj/test_proto" |
| 39 | . "code.google.com/p/goprotobuf/proto" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 40 | ) |
| 41 | |
| 42 | type UnmarshalTextTest struct { |
| David Symonds | 5ed980c | 2011-11-29 09:52:21 +1100 | [diff] [blame] | 43 | in string |
| 44 | err string // if "", no error expected |
| 45 | out *MyMessage |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| David Symonds | 5453105 | 2011-12-08 12:00:31 +1100 | [diff] [blame] | 48 | func buildExtStructTest(text string) UnmarshalTextTest { |
| 49 | msg := &MyMessage{ |
| 50 | Count: Int32(42), |
| 51 | } |
| 52 | SetExtension(msg, E_Ext_More, &Ext{ |
| 53 | Data: String("Hello, world!"), |
| 54 | }) |
| 55 | return UnmarshalTextTest{in: text, out: msg} |
| 56 | } |
| 57 | |
| 58 | func buildExtDataTest(text string) UnmarshalTextTest { |
| 59 | msg := &MyMessage{ |
| 60 | Count: Int32(42), |
| 61 | } |
| 62 | SetExtension(msg, E_Ext_Text, String("Hello, world!")) |
| 63 | SetExtension(msg, E_Ext_Number, Int32(1729)) |
| 64 | return UnmarshalTextTest{in: text, out: msg} |
| 65 | } |
| 66 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 67 | var unMarshalTextTests = []UnmarshalTextTest{ |
| 68 | // Basic |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 69 | { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 70 | in: " count:42\n name:\"Dave\" ", |
| 71 | out: &MyMessage{ |
| 72 | Count: Int32(42), |
| Rob Pike | 9caa5b9 | 2010-05-11 16:04:57 -0700 | [diff] [blame] | 73 | Name: String("Dave"), |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 74 | }, |
| 75 | }, |
| 76 | |
| 77 | // Empty quoted string |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 78 | { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 79 | in: `count:42 name:""`, |
| 80 | out: &MyMessage{ |
| 81 | Count: Int32(42), |
| Rob Pike | 9caa5b9 | 2010-05-11 16:04:57 -0700 | [diff] [blame] | 82 | Name: String(""), |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 83 | }, |
| 84 | }, |
| 85 | |
| 86 | // Quoted string concatenation |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 87 | { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 88 | in: `count:42 name: "My name is "` + "\n" + `"elsewhere"`, |
| 89 | out: &MyMessage{ |
| 90 | Count: Int32(42), |
| Rob Pike | 9caa5b9 | 2010-05-11 16:04:57 -0700 | [diff] [blame] | 91 | Name: String("My name is elsewhere"), |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 92 | }, |
| 93 | }, |
| 94 | |
| 95 | // Bad quoted string |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 96 | { |
| David Symonds | 5ed980c | 2011-11-29 09:52:21 +1100 | [diff] [blame] | 97 | in: `inner: < host: "\0" >` + "\n", |
| 98 | err: `line 1.15: invalid quoted string "\0"`, |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 99 | }, |
| 100 | |
| 101 | // Number too large for int64 |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 102 | { |
| David Symonds | 5ed980c | 2011-11-29 09:52:21 +1100 | [diff] [blame] | 103 | in: "count: 123456789012345678901", |
| 104 | err: "line 1.7: invalid int32: 123456789012345678901", |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 105 | }, |
| 106 | |
| 107 | // Number too large for int32 |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 108 | { |
| David Symonds | 5ed980c | 2011-11-29 09:52:21 +1100 | [diff] [blame] | 109 | in: "count: 1234567890123", |
| 110 | err: "line 1.7: invalid int32: 1234567890123", |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 111 | }, |
| 112 | |
| 113 | // Number too large for float32 |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 114 | { |
| David Symonds | 5ed980c | 2011-11-29 09:52:21 +1100 | [diff] [blame] | 115 | in: "others:< weight: 12345678901234567890123456789012345678901234567890 >", |
| 116 | err: "line 1.17: invalid float32: 12345678901234567890123456789012345678901234567890", |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 117 | }, |
| 118 | |
| 119 | // Number posing as a quoted string |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 120 | { |
| David Symonds | 5ed980c | 2011-11-29 09:52:21 +1100 | [diff] [blame] | 121 | in: `inner: < host: 12 >` + "\n", |
| 122 | err: `line 1.15: invalid string: 12`, |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 123 | }, |
| 124 | |
| 125 | // Quoted string posing as int32 |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 126 | { |
| David Symonds | 5ed980c | 2011-11-29 09:52:21 +1100 | [diff] [blame] | 127 | in: `count: "12"`, |
| 128 | err: `line 1.7: invalid int32: "12"`, |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 129 | }, |
| 130 | |
| 131 | // Quoted string posing a float32 |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 132 | { |
| David Symonds | 5ed980c | 2011-11-29 09:52:21 +1100 | [diff] [blame] | 133 | in: `others:< weight: "17.4" >`, |
| 134 | err: `line 1.17: invalid float32: "17.4"`, |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 135 | }, |
| 136 | |
| 137 | // Enum |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 138 | { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 139 | in: `count:42 bikeshed: BLUE`, |
| 140 | out: &MyMessage{ |
| Rob Pike | 9caa5b9 | 2010-05-11 16:04:57 -0700 | [diff] [blame] | 141 | Count: Int32(42), |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 142 | Bikeshed: NewMyMessage_Color(MyMessage_BLUE), |
| 143 | }, |
| 144 | }, |
| 145 | |
| 146 | // Repeated field |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 147 | { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 148 | in: `count:42 pet: "horsey" pet:"bunny"`, |
| 149 | out: &MyMessage{ |
| 150 | Count: Int32(42), |
| Rob Pike | 9caa5b9 | 2010-05-11 16:04:57 -0700 | [diff] [blame] | 151 | Pet: []string{"horsey", "bunny"}, |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 152 | }, |
| 153 | }, |
| 154 | |
| Rob Pike | aaf695a | 2010-06-22 15:51:21 -0700 | [diff] [blame] | 155 | // Repeated message with/without colon and <>/{} |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 156 | { |
| Rob Pike | aaf695a | 2010-06-22 15:51:21 -0700 | [diff] [blame] | 157 | in: `count:42 others:{} others{} others:<> others:{}`, |
| 158 | out: &MyMessage{ |
| 159 | Count: Int32(42), |
| 160 | Others: []*OtherMessage{ |
| 161 | &OtherMessage{}, |
| 162 | &OtherMessage{}, |
| 163 | &OtherMessage{}, |
| 164 | &OtherMessage{}, |
| 165 | }, |
| 166 | }, |
| 167 | }, |
| 168 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 169 | // Missing colon for inner message |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 170 | { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 171 | in: `count:42 inner < host: "cauchy.syd" >`, |
| 172 | out: &MyMessage{ |
| 173 | Count: Int32(42), |
| 174 | Inner: &InnerMessage{ |
| 175 | Host: String("cauchy.syd"), |
| 176 | }, |
| 177 | }, |
| 178 | }, |
| 179 | |
| 180 | // Missing colon for string field |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 181 | { |
| David Symonds | 5ed980c | 2011-11-29 09:52:21 +1100 | [diff] [blame] | 182 | in: `name "Dave"`, |
| 183 | err: `line 1.5: expected ':', found "\"Dave\""`, |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 184 | }, |
| 185 | |
| 186 | // Missing colon for int32 field |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 187 | { |
| David Symonds | 5ed980c | 2011-11-29 09:52:21 +1100 | [diff] [blame] | 188 | in: `count 42`, |
| 189 | err: `line 1.6: expected ':', found "42"`, |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 190 | }, |
| 191 | |
| 192 | // Missing required field |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 193 | { |
| David Symonds | 5ed980c | 2011-11-29 09:52:21 +1100 | [diff] [blame] | 194 | in: ``, |
| 195 | err: `line 1.0: message test_proto.MyMessage missing required field "count"`, |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 196 | }, |
| 197 | |
| 198 | // Repeated non-repeated field |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 199 | { |
| David Symonds | 5ed980c | 2011-11-29 09:52:21 +1100 | [diff] [blame] | 200 | in: `name: "Rob" name: "Russ"`, |
| 201 | err: `line 1.12: non-repeated field "name" was repeated`, |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 202 | }, |
| 203 | |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 204 | // Group |
| 205 | { |
| 206 | in: `count: 17 SomeGroup { group_field: 12 }`, |
| 207 | out: &MyMessage{ |
| 208 | Count: Int32(17), |
| 209 | Somegroup: &MyMessage_SomeGroup{ |
| 210 | GroupField: Int32(12), |
| 211 | }, |
| 212 | }, |
| 213 | }, |
| 214 | |
| David Symonds | 5453105 | 2011-12-08 12:00:31 +1100 | [diff] [blame] | 215 | // Extension |
| 216 | buildExtStructTest(`count: 42 [test_proto.Ext.more]:<data:"Hello, world!" >`), |
| 217 | buildExtStructTest(`count: 42 [test_proto.Ext.more] {data:"Hello, world!"}`), |
| 218 | buildExtDataTest(`count: 42 [test_proto.Ext.text]:"Hello, world!" [test_proto.Ext.number]:1729`), |
| 219 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 220 | // Big all-in-one |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 221 | { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 222 | in: "count:42 # Meaning\n" + |
| 223 | `name:"Dave" ` + |
| 224 | `quote:"\"I didn't want to go.\"" ` + |
| 225 | `pet:"bunny" ` + |
| 226 | `pet:"kitty" ` + |
| 227 | `pet:"horsey" ` + |
| 228 | `inner:<` + |
| 229 | ` host:"footrest.syd" ` + |
| 230 | ` port:7001 ` + |
| 231 | ` connected:true ` + |
| 232 | `> ` + |
| 233 | `others:<` + |
| 234 | ` key:3735928559 ` + |
| 235 | ` value:"\x01A\a\f" ` + |
| 236 | `> ` + |
| 237 | `others:<` + |
| 238 | " weight:58.9 # Atomic weight of Co\n" + |
| 239 | ` inner:<` + |
| 240 | ` host:"lesha.mtv" ` + |
| 241 | ` port:8002 ` + |
| 242 | ` >` + |
| 243 | `>`, |
| 244 | out: &MyMessage{ |
| 245 | Count: Int32(42), |
| Rob Pike | 9caa5b9 | 2010-05-11 16:04:57 -0700 | [diff] [blame] | 246 | Name: String("Dave"), |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 247 | Quote: String(`"I didn't want to go."`), |
| Rob Pike | 9caa5b9 | 2010-05-11 16:04:57 -0700 | [diff] [blame] | 248 | Pet: []string{"bunny", "kitty", "horsey"}, |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 249 | Inner: &InnerMessage{ |
| Rob Pike | 9caa5b9 | 2010-05-11 16:04:57 -0700 | [diff] [blame] | 250 | Host: String("footrest.syd"), |
| 251 | Port: Int32(7001), |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 252 | Connected: Bool(true), |
| 253 | }, |
| 254 | Others: []*OtherMessage{ |
| 255 | &OtherMessage{ |
| Rob Pike | 9caa5b9 | 2010-05-11 16:04:57 -0700 | [diff] [blame] | 256 | Key: Int64(3735928559), |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 257 | Value: []byte{0x1, 'A', '\a', '\f'}, |
| 258 | }, |
| 259 | &OtherMessage{ |
| 260 | Weight: Float32(58.9), |
| 261 | Inner: &InnerMessage{ |
| 262 | Host: String("lesha.mtv"), |
| 263 | Port: Int32(8002), |
| 264 | }, |
| 265 | }, |
| 266 | }, |
| 267 | }, |
| 268 | }, |
| 269 | } |
| 270 | |
| 271 | func TestUnmarshalText(t *testing.T) { |
| 272 | for i, test := range unMarshalTextTests { |
| 273 | pb := new(MyMessage) |
| 274 | err := UnmarshalText(test.in, pb) |
| David Symonds | 5ed980c | 2011-11-29 09:52:21 +1100 | [diff] [blame] | 275 | if test.err == "" { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 276 | // We don't expect failure. |
| 277 | if err != nil { |
| 278 | t.Errorf("Test %d: Unexpected error: %v", i, err) |
| 279 | } else if !reflect.DeepEqual(pb, test.out) { |
| David Symonds | ef8f0e8 | 2011-10-13 12:57:34 +1100 | [diff] [blame] | 280 | t.Errorf("Test %d: Incorrect populated \nHave: %v\nWant: %v", |
| 281 | i, pb, test.out) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 282 | } |
| 283 | } else { |
| 284 | // We do expect failure. |
| 285 | if err == nil { |
| David Symonds | 5ed980c | 2011-11-29 09:52:21 +1100 | [diff] [blame] | 286 | t.Errorf("Test %d: Didn't get expected error: %v", i, test.err) |
| 287 | } else if err.Error() != test.err { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 288 | t.Errorf("Test %d: Incorrect error.\nHave: %v\nWant: %v", |
| David Symonds | 5ed980c | 2011-11-29 09:52:21 +1100 | [diff] [blame] | 289 | i, err.Error(), test.err) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | } |
| 293 | } |
| David Symonds | 79eae33 | 2010-10-16 11:33:20 +1100 | [diff] [blame] | 294 | |
| David Symonds | ef8f0e8 | 2011-10-13 12:57:34 +1100 | [diff] [blame] | 295 | // Regression test; this caused a panic. |
| 296 | func TestRepeatedEnum(t *testing.T) { |
| 297 | pb := new(RepeatedEnum) |
| 298 | if err := UnmarshalText("color: RED", pb); err != nil { |
| 299 | t.Fatal(err) |
| 300 | } |
| 301 | exp := &RepeatedEnum{ |
| 302 | Color: []RepeatedEnum_Color{RepeatedEnum_RED}, |
| 303 | } |
| 304 | if !reflect.DeepEqual(pb, exp) { |
| 305 | t.Errorf("Incorrect populated \nHave: %v\nWant: %v", pb, exp) |
| 306 | } |
| 307 | } |
| 308 | |
| David Symonds | 79eae33 | 2010-10-16 11:33:20 +1100 | [diff] [blame] | 309 | var benchInput string |
| 310 | |
| 311 | func init() { |
| 312 | benchInput = "count: 4\n" |
| 313 | for i := 0; i < 1000; i++ { |
| 314 | benchInput += "pet: \"fido\"\n" |
| 315 | } |
| 316 | |
| 317 | // Check it is valid input. |
| 318 | pb := new(MyMessage) |
| 319 | err := UnmarshalText(benchInput, pb) |
| 320 | if err != nil { |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 321 | panic("Bad benchmark input: " + err.Error()) |
| David Symonds | 79eae33 | 2010-10-16 11:33:20 +1100 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
| 325 | func BenchmarkUnmarshalText(b *testing.B) { |
| 326 | pb := new(MyMessage) |
| 327 | for i := 0; i < b.N; i++ { |
| 328 | UnmarshalText(benchInput, pb) |
| 329 | } |
| 330 | b.SetBytes(int64(len(benchInput))) |
| 331 | } |