1 // Go support for Protocol Buffers - Google's data interchange format
3 // Copyright 2010 The Go Authors. All rights reserved.
4 // https://github.com/golang/protobuf
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
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
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.
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.
39 . "github.com/golang/protobuf/proto"
40 proto3pb "github.com/golang/protobuf/proto/proto3_proto"
41 . "github.com/golang/protobuf/proto/testdata"
44 type UnmarshalTextTest struct {
46 err string // if "", no error expected
50 func buildExtStructTest(text string) UnmarshalTextTest {
54 SetExtension(msg, E_Ext_More, &Ext{
55 Data: String("Hello, world!"),
57 return UnmarshalTextTest{in: text, out: msg}
60 func buildExtDataTest(text string) UnmarshalTextTest {
64 SetExtension(msg, E_Ext_Text, String("Hello, world!"))
65 SetExtension(msg, E_Ext_Number, Int32(1729))
66 return UnmarshalTextTest{in: text, out: msg}
69 func buildExtRepStringTest(text string) UnmarshalTextTest {
73 if err := SetExtension(msg, E_Greeting, []string{"bula", "hola"}); err != nil {
76 return UnmarshalTextTest{in: text, out: msg}
79 var unMarshalTextTests = []UnmarshalTextTest{
82 in: " count:42\n name:\"Dave\" ",
89 // Empty quoted string
91 in: `count:42 name:""`,
98 // Quoted string concatenation with double quotes
100 in: `count:42 name: "My name is "` + "\n" + `"elsewhere"`,
103 Name: String("My name is elsewhere"),
107 // Quoted string concatenation with single quotes
109 in: "count:42 name: 'My name is '\n'elsewhere'",
112 Name: String("My name is elsewhere"),
116 // Quoted string concatenations with mixed quotes
118 in: "count:42 name: 'My name is '\n\"elsewhere\"",
121 Name: String("My name is elsewhere"),
125 in: "count:42 name: \"My name is \"\n'elsewhere'",
128 Name: String("My name is elsewhere"),
132 // Quoted string with escaped apostrophe
134 in: `count:42 name: "HOLIDAY - New Year\'s Day"`,
137 Name: String("HOLIDAY - New Year's Day"),
141 // Quoted string with single quote
143 in: `count:42 name: 'Roger "The Ramster" Ramjet'`,
146 Name: String(`Roger "The Ramster" Ramjet`),
150 // Quoted string with all the accepted special characters from the C++ test
152 in: `count:42 name: ` + "\"\\\"A string with \\' characters \\n and \\r newlines and \\t tabs and \\001 slashes \\\\ and multiple spaces\"",
155 Name: String("\"A string with ' characters \n and \r newlines and \t tabs and \001 slashes \\ and multiple spaces"),
159 // Quoted string with quoted backslash
161 in: `count:42 name: "\\'xyz"`,
164 Name: String(`\'xyz`),
168 // Quoted string with UTF-8 bytes.
170 in: "count:42 name: '\303\277\302\201\xAB'",
173 Name: String("\303\277\302\201\xAB"),
179 in: `inner: < host: "\0" >` + "\n",
180 err: `line 1.15: invalid quoted string "\0": \0 requires 2 following digits`,
183 // Number too large for int64
185 in: "count: 1 others { key: 123456789012345678901 }",
186 err: "line 1.23: invalid int64: 123456789012345678901",
189 // Number too large for int32
191 in: "count: 1234567890123",
192 err: "line 1.7: invalid int32: 1234567890123",
195 // Number in hexadecimal
197 in: "count: 0x2beef",
199 Count: Int32(0x2beef),
207 Count: Int32(024601),
211 // Floating point number with "f" suffix
213 in: "count: 4 others:< weight: 17.0f >",
216 Others: []*OtherMessage{
224 // Floating point positive infinity
226 in: "count: 4 bigfloat: inf",
229 Bigfloat: Float64(math.Inf(1)),
233 // Floating point negative infinity
235 in: "count: 4 bigfloat: -inf",
238 Bigfloat: Float64(math.Inf(-1)),
242 // Number too large for float32
244 in: "others:< weight: 12345678901234567890123456789012345678901234567890 >",
245 err: "line 1.17: invalid float32: 12345678901234567890123456789012345678901234567890",
248 // Number posing as a quoted string
250 in: `inner: < host: 12 >` + "\n",
251 err: `line 1.15: invalid string: 12`,
254 // Quoted string posing as int32
257 err: `line 1.7: invalid int32: "12"`,
260 // Quoted string posing a float32
262 in: `others:< weight: "17.4" >`,
263 err: `line 1.17: invalid float32: "17.4"`,
268 in: `count:42 bikeshed: BLUE`,
271 Bikeshed: MyMessage_BLUE.Enum(),
277 in: `count:42 pet: "horsey" pet:"bunny"`,
280 Pet: []string{"horsey", "bunny"},
284 // Repeated field with list notation
286 in: `count:42 pet: ["horsey", "bunny"]`,
289 Pet: []string{"horsey", "bunny"},
293 // Repeated message with/without colon and <>/{}
295 in: `count:42 others:{} others{} others:<> others:{}`,
298 Others: []*OtherMessage{
307 // Missing colon for inner message
309 in: `count:42 inner < host: "cauchy.syd" >`,
312 Inner: &InnerMessage{
313 Host: String("cauchy.syd"),
318 // Missing colon for string field
321 err: `line 1.5: expected ':', found "\"Dave\""`,
324 // Missing colon for int32 field
327 err: `line 1.6: expected ':', found "42"`,
330 // Missing required field
333 err: `proto: required field "testdata.MyMessage.count" not set`,
335 Name: String("Pawel"),
339 // Missing required field in a required submessage
341 in: `count: 42 we_must_go_deeper < leo_finally_won_an_oscar <> >`,
342 err: `proto: required field "testdata.InnerMessage.host" not set`,
345 WeMustGoDeeper: &RequiredInnerMessage{LeoFinallyWonAnOscar: &InnerMessage{}},
349 // Repeated non-repeated field
351 in: `name: "Rob" name: "Russ"`,
352 err: `line 1.12: non-repeated field "name" was repeated`,
357 in: `count: 17 SomeGroup { group_field: 12 }`,
360 Somegroup: &MyMessage_SomeGroup{
361 GroupField: Int32(12),
366 // Semicolon between fields
368 in: `count:3;name:"Calvin"`,
371 Name: String("Calvin"),
374 // Comma between fields
376 in: `count:4,name:"Ezekiel"`,
379 Name: String("Ezekiel"),
385 in: `count:42 inner { host: "example.com" connected: false }`,
388 Inner: &InnerMessage{
389 Host: String("example.com"),
390 Connected: Bool(false),
396 in: `count:42 inner { host: "example.com" connected: true }`,
399 Inner: &InnerMessage{
400 Host: String("example.com"),
401 Connected: Bool(true),
407 in: `count:42 inner { host: "example.com" connected: 0 }`,
410 Inner: &InnerMessage{
411 Host: String("example.com"),
412 Connected: Bool(false),
418 in: `count:42 inner { host: "example.com" connected: 1 }`,
421 Inner: &InnerMessage{
422 Host: String("example.com"),
423 Connected: Bool(true),
429 in: `count:42 inner { host: "example.com" connected: f }`,
432 Inner: &InnerMessage{
433 Host: String("example.com"),
434 Connected: Bool(false),
440 in: `count:42 inner { host: "example.com" connected: t }`,
443 Inner: &InnerMessage{
444 Host: String("example.com"),
445 Connected: Bool(true),
451 in: `count:42 inner { host: "example.com" connected: False }`,
454 Inner: &InnerMessage{
455 Host: String("example.com"),
456 Connected: Bool(false),
462 in: `count:42 inner { host: "example.com" connected: True }`,
465 Inner: &InnerMessage{
466 Host: String("example.com"),
467 Connected: Bool(true),
473 buildExtStructTest(`count: 42 [testdata.Ext.more]:<data:"Hello, world!" >`),
474 buildExtStructTest(`count: 42 [testdata.Ext.more] {data:"Hello, world!"}`),
475 buildExtDataTest(`count: 42 [testdata.Ext.text]:"Hello, world!" [testdata.Ext.number]:1729`),
476 buildExtRepStringTest(`count: 42 [testdata.greeting]:"bula" [testdata.greeting]:"hola"`),
480 in: "count:42 # Meaning\n" +
482 `quote:"\"I didn't want to go.\"" ` +
487 ` host:"footrest.syd" ` +
493 ` value:"\x01A\a\f" ` +
496 " weight:58.9 # Atomic weight of Co\n" +
498 ` host:"lesha.mtv" ` +
504 Name: String("Dave"),
505 Quote: String(`"I didn't want to go."`),
506 Pet: []string{"bunny", "kitty", "horsey"},
507 Inner: &InnerMessage{
508 Host: String("footrest.syd"),
510 Connected: Bool(true),
512 Others: []*OtherMessage{
514 Key: Int64(3735928559),
515 Value: []byte{0x1, 'A', '\a', '\f'},
518 Weight: Float32(58.9),
519 Inner: &InnerMessage{
520 Host: String("lesha.mtv"),
529 func TestUnmarshalText(t *testing.T) {
530 for i, test := range unMarshalTextTests {
532 err := UnmarshalText(test.in, pb)
534 // We don't expect failure.
536 t.Errorf("Test %d: Unexpected error: %v", i, err)
537 } else if !reflect.DeepEqual(pb, test.out) {
538 t.Errorf("Test %d: Incorrect populated \nHave: %v\nWant: %v",
542 // We do expect failure.
544 t.Errorf("Test %d: Didn't get expected error: %v", i, test.err)
545 } else if err.Error() != test.err {
546 t.Errorf("Test %d: Incorrect error.\nHave: %v\nWant: %v",
547 i, err.Error(), test.err)
548 } else if _, ok := err.(*RequiredNotSetError); ok && test.out != nil && !reflect.DeepEqual(pb, test.out) {
549 t.Errorf("Test %d: Incorrect populated \nHave: %v\nWant: %v",
556 func TestUnmarshalTextCustomMessage(t *testing.T) {
557 msg := &textMessage{}
558 if err := UnmarshalText("custom", msg); err != nil {
559 t.Errorf("Unexpected error from custom unmarshal: %v", err)
561 if UnmarshalText("not custom", msg) == nil {
562 t.Errorf("Didn't get expected error from custom unmarshal")
566 // Regression test; this caused a panic.
567 func TestRepeatedEnum(t *testing.T) {
568 pb := new(RepeatedEnum)
569 if err := UnmarshalText("color: RED", pb); err != nil {
572 exp := &RepeatedEnum{
573 Color: []RepeatedEnum_Color{RepeatedEnum_RED},
576 t.Errorf("Incorrect populated \nHave: %v\nWant: %v", pb, exp)
580 func TestProto3TextParsing(t *testing.T) {
581 m := new(proto3pb.Message)
582 const in = `name: "Wallace" true_scotsman: true`
583 want := &proto3pb.Message{
587 if err := UnmarshalText(in, m); err != nil {
591 t.Errorf("\n got %v\nwant %v", m, want)
595 func TestMapParsing(t *testing.T) {
596 m := new(MessageWithMap)
597 const in = `name_mapping:<key:1234 value:"Feist"> name_mapping:<key:1 value:"Beatles">` +
598 `msg_mapping:<key:-4, value:<f: 2.0>,>` + // separating commas are okay
599 `msg_mapping<key:-2 value<f: 4.0>>` + // no colon after "value"
600 `msg_mapping:<value:<f: 5.0>>` + // omitted key
601 `msg_mapping:<key:1>` + // omitted value
602 `byte_mapping:<key:true value:"so be it">` +
603 `byte_mapping:<>` // omitted key and value
604 want := &MessageWithMap{
605 NameMapping: map[int32]string{
609 MsgMapping: map[int64]*FloatingPoint{
610 -4: {F: Float64(2.0)},
611 -2: {F: Float64(4.0)},
612 0: {F: Float64(5.0)},
615 ByteMapping: map[bool][]byte{
617 true: []byte("so be it"),
620 if err := UnmarshalText(in, m); err != nil {
624 t.Errorf("\n got %v\nwant %v", m, want)
628 func TestOneofParsing(t *testing.T) {
629 const in = `name:"Shrek"`
631 want := &Communique{Union: &Communique_Name{"Shrek"}}
632 if err := UnmarshalText(in, m); err != nil {
636 t.Errorf("\n got %v\nwant %v", m, want)
639 const inOverwrite = `name:"Shrek" number:42`
641 testErr := "line 1.13: field 'number' would overwrite already parsed oneof 'Union'"
642 if err := UnmarshalText(inOverwrite, m); err == nil {
643 t.Errorf("TestOneofParsing: Didn't get expected error: %v", testErr)
644 } else if err.Error() != testErr {
645 t.Errorf("TestOneofParsing: Incorrect error.\nHave: %v\nWant: %v",
646 err.Error(), testErr)
651 var benchInput string
654 benchInput = "count: 4\n"
655 for i := 0; i < 1000; i++ {
656 benchInput += "pet: \"fido\"\n"
659 // Check it is valid input.
661 err := UnmarshalText(benchInput, pb)
663 panic("Bad benchmark input: " + err.Error())
667 func BenchmarkUnmarshalText(b *testing.B) {
669 for i := 0; i < b.N; i++ {
670 UnmarshalText(benchInput, pb)
672 b.SetBytes(int64(len(benchInput)))