text_parser_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2010 The Go Authors. All rights reserved.
  4. // https://github.com/golang/protobuf
  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. package proto_test
  32. import (
  33. "math"
  34. "reflect"
  35. "testing"
  36. . "github.com/golang/protobuf/proto"
  37. proto3pb "github.com/golang/protobuf/proto/proto3_proto"
  38. . "github.com/golang/protobuf/proto/testdata"
  39. )
  40. type UnmarshalTextTest struct {
  41. in string
  42. err string // if "", no error expected
  43. out *MyMessage
  44. }
  45. func buildExtStructTest(text string) UnmarshalTextTest {
  46. msg := &MyMessage{
  47. Count: Int32(42),
  48. }
  49. SetExtension(msg, E_Ext_More, &Ext{
  50. Data: String("Hello, world!"),
  51. })
  52. return UnmarshalTextTest{in: text, out: msg}
  53. }
  54. func buildExtDataTest(text string) UnmarshalTextTest {
  55. msg := &MyMessage{
  56. Count: Int32(42),
  57. }
  58. SetExtension(msg, E_Ext_Text, String("Hello, world!"))
  59. SetExtension(msg, E_Ext_Number, Int32(1729))
  60. return UnmarshalTextTest{in: text, out: msg}
  61. }
  62. func buildExtRepStringTest(text string) UnmarshalTextTest {
  63. msg := &MyMessage{
  64. Count: Int32(42),
  65. }
  66. if err := SetExtension(msg, E_Greeting, []string{"bula", "hola"}); err != nil {
  67. panic(err)
  68. }
  69. return UnmarshalTextTest{in: text, out: msg}
  70. }
  71. var unMarshalTextTests = []UnmarshalTextTest{
  72. // Basic
  73. {
  74. in: " count:42\n name:\"Dave\" ",
  75. out: &MyMessage{
  76. Count: Int32(42),
  77. Name: String("Dave"),
  78. },
  79. },
  80. // Empty quoted string
  81. {
  82. in: `count:42 name:""`,
  83. out: &MyMessage{
  84. Count: Int32(42),
  85. Name: String(""),
  86. },
  87. },
  88. // Quoted string concatenation with double quotes
  89. {
  90. in: `count:42 name: "My name is "` + "\n" + `"elsewhere"`,
  91. out: &MyMessage{
  92. Count: Int32(42),
  93. Name: String("My name is elsewhere"),
  94. },
  95. },
  96. // Quoted string concatenation with single quotes
  97. {
  98. in: "count:42 name: 'My name is '\n'elsewhere'",
  99. out: &MyMessage{
  100. Count: Int32(42),
  101. Name: String("My name is elsewhere"),
  102. },
  103. },
  104. // Quoted string concatenations with mixed quotes
  105. {
  106. in: "count:42 name: 'My name is '\n\"elsewhere\"",
  107. out: &MyMessage{
  108. Count: Int32(42),
  109. Name: String("My name is elsewhere"),
  110. },
  111. },
  112. {
  113. in: "count:42 name: \"My name is \"\n'elsewhere'",
  114. out: &MyMessage{
  115. Count: Int32(42),
  116. Name: String("My name is elsewhere"),
  117. },
  118. },
  119. // Quoted string with escaped apostrophe
  120. {
  121. in: `count:42 name: "HOLIDAY - New Year\'s Day"`,
  122. out: &MyMessage{
  123. Count: Int32(42),
  124. Name: String("HOLIDAY - New Year's Day"),
  125. },
  126. },
  127. // Quoted string with single quote
  128. {
  129. in: `count:42 name: 'Roger "The Ramster" Ramjet'`,
  130. out: &MyMessage{
  131. Count: Int32(42),
  132. Name: String(`Roger "The Ramster" Ramjet`),
  133. },
  134. },
  135. // Quoted string with all the accepted special characters from the C++ test
  136. {
  137. in: `count:42 name: ` + "\"\\\"A string with \\' characters \\n and \\r newlines and \\t tabs and \\001 slashes \\\\ and multiple spaces\"",
  138. out: &MyMessage{
  139. Count: Int32(42),
  140. Name: String("\"A string with ' characters \n and \r newlines and \t tabs and \001 slashes \\ and multiple spaces"),
  141. },
  142. },
  143. // Quoted string with quoted backslash
  144. {
  145. in: `count:42 name: "\\'xyz"`,
  146. out: &MyMessage{
  147. Count: Int32(42),
  148. Name: String(`\'xyz`),
  149. },
  150. },
  151. // Quoted string with UTF-8 bytes.
  152. {
  153. in: "count:42 name: '\303\277\302\201\xAB'",
  154. out: &MyMessage{
  155. Count: Int32(42),
  156. Name: String("\303\277\302\201\xAB"),
  157. },
  158. },
  159. // Bad quoted string
  160. {
  161. in: `inner: < host: "\0" >` + "\n",
  162. err: `line 1.15: invalid quoted string "\0": \0 requires 2 following digits`,
  163. },
  164. // Number too large for int64
  165. {
  166. in: "count: 1 others { key: 123456789012345678901 }",
  167. err: "line 1.23: invalid int64: 123456789012345678901",
  168. },
  169. // Number too large for int32
  170. {
  171. in: "count: 1234567890123",
  172. err: "line 1.7: invalid int32: 1234567890123",
  173. },
  174. // Number in hexadecimal
  175. {
  176. in: "count: 0x2beef",
  177. out: &MyMessage{
  178. Count: Int32(0x2beef),
  179. },
  180. },
  181. // Number in octal
  182. {
  183. in: "count: 024601",
  184. out: &MyMessage{
  185. Count: Int32(024601),
  186. },
  187. },
  188. // Floating point number with "f" suffix
  189. {
  190. in: "count: 4 others:< weight: 17.0f >",
  191. out: &MyMessage{
  192. Count: Int32(4),
  193. Others: []*OtherMessage{
  194. {
  195. Weight: Float32(17),
  196. },
  197. },
  198. },
  199. },
  200. // Floating point positive infinity
  201. {
  202. in: "count: 4 bigfloat: inf",
  203. out: &MyMessage{
  204. Count: Int32(4),
  205. Bigfloat: Float64(math.Inf(1)),
  206. },
  207. },
  208. // Floating point negative infinity
  209. {
  210. in: "count: 4 bigfloat: -inf",
  211. out: &MyMessage{
  212. Count: Int32(4),
  213. Bigfloat: Float64(math.Inf(-1)),
  214. },
  215. },
  216. // Number too large for float32
  217. {
  218. in: "others:< weight: 12345678901234567890123456789012345678901234567890 >",
  219. err: "line 1.17: invalid float32: 12345678901234567890123456789012345678901234567890",
  220. },
  221. // Number posing as a quoted string
  222. {
  223. in: `inner: < host: 12 >` + "\n",
  224. err: `line 1.15: invalid string: 12`,
  225. },
  226. // Quoted string posing as int32
  227. {
  228. in: `count: "12"`,
  229. err: `line 1.7: invalid int32: "12"`,
  230. },
  231. // Quoted string posing a float32
  232. {
  233. in: `others:< weight: "17.4" >`,
  234. err: `line 1.17: invalid float32: "17.4"`,
  235. },
  236. // Enum
  237. {
  238. in: `count:42 bikeshed: BLUE`,
  239. out: &MyMessage{
  240. Count: Int32(42),
  241. Bikeshed: MyMessage_BLUE.Enum(),
  242. },
  243. },
  244. // Repeated field
  245. {
  246. in: `count:42 pet: "horsey" pet:"bunny"`,
  247. out: &MyMessage{
  248. Count: Int32(42),
  249. Pet: []string{"horsey", "bunny"},
  250. },
  251. },
  252. // Repeated field with list notation
  253. {
  254. in: `count:42 pet: ["horsey", "bunny"]`,
  255. out: &MyMessage{
  256. Count: Int32(42),
  257. Pet: []string{"horsey", "bunny"},
  258. },
  259. },
  260. // Repeated message with/without colon and <>/{}
  261. {
  262. in: `count:42 others:{} others{} others:<> others:{}`,
  263. out: &MyMessage{
  264. Count: Int32(42),
  265. Others: []*OtherMessage{
  266. {},
  267. {},
  268. {},
  269. {},
  270. },
  271. },
  272. },
  273. // Missing colon for inner message
  274. {
  275. in: `count:42 inner < host: "cauchy.syd" >`,
  276. out: &MyMessage{
  277. Count: Int32(42),
  278. Inner: &InnerMessage{
  279. Host: String("cauchy.syd"),
  280. },
  281. },
  282. },
  283. // Missing colon for string field
  284. {
  285. in: `name "Dave"`,
  286. err: `line 1.5: expected ':', found "\"Dave\""`,
  287. },
  288. // Missing colon for int32 field
  289. {
  290. in: `count 42`,
  291. err: `line 1.6: expected ':', found "42"`,
  292. },
  293. // Missing required field
  294. {
  295. in: `name: "Pawel"`,
  296. err: `proto: required field "testdata.MyMessage.count" not set`,
  297. out: &MyMessage{
  298. Name: String("Pawel"),
  299. },
  300. },
  301. // Missing required field in a required submessage
  302. {
  303. in: `count: 42 we_must_go_deeper < leo_finally_won_an_oscar <> >`,
  304. err: `proto: required field "testdata.InnerMessage.host" not set`,
  305. out: &MyMessage{
  306. Count: Int32(42),
  307. WeMustGoDeeper: &RequiredInnerMessage{LeoFinallyWonAnOscar: &InnerMessage{}},
  308. },
  309. },
  310. // Repeated non-repeated field
  311. {
  312. in: `name: "Rob" name: "Russ"`,
  313. err: `line 1.12: non-repeated field "name" was repeated`,
  314. },
  315. // Group
  316. {
  317. in: `count: 17 SomeGroup { group_field: 12 }`,
  318. out: &MyMessage{
  319. Count: Int32(17),
  320. Somegroup: &MyMessage_SomeGroup{
  321. GroupField: Int32(12),
  322. },
  323. },
  324. },
  325. // Semicolon between fields
  326. {
  327. in: `count:3;name:"Calvin"`,
  328. out: &MyMessage{
  329. Count: Int32(3),
  330. Name: String("Calvin"),
  331. },
  332. },
  333. // Comma between fields
  334. {
  335. in: `count:4,name:"Ezekiel"`,
  336. out: &MyMessage{
  337. Count: Int32(4),
  338. Name: String("Ezekiel"),
  339. },
  340. },
  341. // Boolean false
  342. {
  343. in: `count:42 inner { host: "example.com" connected: false }`,
  344. out: &MyMessage{
  345. Count: Int32(42),
  346. Inner: &InnerMessage{
  347. Host: String("example.com"),
  348. Connected: Bool(false),
  349. },
  350. },
  351. },
  352. // Boolean true
  353. {
  354. in: `count:42 inner { host: "example.com" connected: true }`,
  355. out: &MyMessage{
  356. Count: Int32(42),
  357. Inner: &InnerMessage{
  358. Host: String("example.com"),
  359. Connected: Bool(true),
  360. },
  361. },
  362. },
  363. // Boolean 0
  364. {
  365. in: `count:42 inner { host: "example.com" connected: 0 }`,
  366. out: &MyMessage{
  367. Count: Int32(42),
  368. Inner: &InnerMessage{
  369. Host: String("example.com"),
  370. Connected: Bool(false),
  371. },
  372. },
  373. },
  374. // Boolean 1
  375. {
  376. in: `count:42 inner { host: "example.com" connected: 1 }`,
  377. out: &MyMessage{
  378. Count: Int32(42),
  379. Inner: &InnerMessage{
  380. Host: String("example.com"),
  381. Connected: Bool(true),
  382. },
  383. },
  384. },
  385. // Boolean f
  386. {
  387. in: `count:42 inner { host: "example.com" connected: f }`,
  388. out: &MyMessage{
  389. Count: Int32(42),
  390. Inner: &InnerMessage{
  391. Host: String("example.com"),
  392. Connected: Bool(false),
  393. },
  394. },
  395. },
  396. // Boolean t
  397. {
  398. in: `count:42 inner { host: "example.com" connected: t }`,
  399. out: &MyMessage{
  400. Count: Int32(42),
  401. Inner: &InnerMessage{
  402. Host: String("example.com"),
  403. Connected: Bool(true),
  404. },
  405. },
  406. },
  407. // Boolean False
  408. {
  409. in: `count:42 inner { host: "example.com" connected: False }`,
  410. out: &MyMessage{
  411. Count: Int32(42),
  412. Inner: &InnerMessage{
  413. Host: String("example.com"),
  414. Connected: Bool(false),
  415. },
  416. },
  417. },
  418. // Boolean True
  419. {
  420. in: `count:42 inner { host: "example.com" connected: True }`,
  421. out: &MyMessage{
  422. Count: Int32(42),
  423. Inner: &InnerMessage{
  424. Host: String("example.com"),
  425. Connected: Bool(true),
  426. },
  427. },
  428. },
  429. // Extension
  430. buildExtStructTest(`count: 42 [testdata.Ext.more]:<data:"Hello, world!" >`),
  431. buildExtStructTest(`count: 42 [testdata.Ext.more] {data:"Hello, world!"}`),
  432. buildExtDataTest(`count: 42 [testdata.Ext.text]:"Hello, world!" [testdata.Ext.number]:1729`),
  433. buildExtRepStringTest(`count: 42 [testdata.greeting]:"bula" [testdata.greeting]:"hola"`),
  434. // Big all-in-one
  435. {
  436. in: "count:42 # Meaning\n" +
  437. `name:"Dave" ` +
  438. `quote:"\"I didn't want to go.\"" ` +
  439. `pet:"bunny" ` +
  440. `pet:"kitty" ` +
  441. `pet:"horsey" ` +
  442. `inner:<` +
  443. ` host:"footrest.syd" ` +
  444. ` port:7001 ` +
  445. ` connected:true ` +
  446. `> ` +
  447. `others:<` +
  448. ` key:3735928559 ` +
  449. ` value:"\x01A\a\f" ` +
  450. `> ` +
  451. `others:<` +
  452. " weight:58.9 # Atomic weight of Co\n" +
  453. ` inner:<` +
  454. ` host:"lesha.mtv" ` +
  455. ` port:8002 ` +
  456. ` >` +
  457. `>`,
  458. out: &MyMessage{
  459. Count: Int32(42),
  460. Name: String("Dave"),
  461. Quote: String(`"I didn't want to go."`),
  462. Pet: []string{"bunny", "kitty", "horsey"},
  463. Inner: &InnerMessage{
  464. Host: String("footrest.syd"),
  465. Port: Int32(7001),
  466. Connected: Bool(true),
  467. },
  468. Others: []*OtherMessage{
  469. {
  470. Key: Int64(3735928559),
  471. Value: []byte{0x1, 'A', '\a', '\f'},
  472. },
  473. {
  474. Weight: Float32(58.9),
  475. Inner: &InnerMessage{
  476. Host: String("lesha.mtv"),
  477. Port: Int32(8002),
  478. },
  479. },
  480. },
  481. },
  482. },
  483. }
  484. func TestUnmarshalText(t *testing.T) {
  485. for i, test := range unMarshalTextTests {
  486. pb := new(MyMessage)
  487. err := UnmarshalText(test.in, pb)
  488. if test.err == "" {
  489. // We don't expect failure.
  490. if err != nil {
  491. t.Errorf("Test %d: Unexpected error: %v", i, err)
  492. } else if !reflect.DeepEqual(pb, test.out) {
  493. t.Errorf("Test %d: Incorrect populated \nHave: %v\nWant: %v",
  494. i, pb, test.out)
  495. }
  496. } else {
  497. // We do expect failure.
  498. if err == nil {
  499. t.Errorf("Test %d: Didn't get expected error: %v", i, test.err)
  500. } else if err.Error() != test.err {
  501. t.Errorf("Test %d: Incorrect error.\nHave: %v\nWant: %v",
  502. i, err.Error(), test.err)
  503. } else if _, ok := err.(*RequiredNotSetError); ok && test.out != nil && !reflect.DeepEqual(pb, test.out) {
  504. t.Errorf("Test %d: Incorrect populated \nHave: %v\nWant: %v",
  505. i, pb, test.out)
  506. }
  507. }
  508. }
  509. }
  510. func TestUnmarshalTextCustomMessage(t *testing.T) {
  511. msg := &textMessage{}
  512. if err := UnmarshalText("custom", msg); err != nil {
  513. t.Errorf("Unexpected error from custom unmarshal: %v", err)
  514. }
  515. if UnmarshalText("not custom", msg) == nil {
  516. t.Errorf("Didn't get expected error from custom unmarshal")
  517. }
  518. }
  519. // Regression test; this caused a panic.
  520. func TestRepeatedEnum(t *testing.T) {
  521. pb := new(RepeatedEnum)
  522. if err := UnmarshalText("color: RED", pb); err != nil {
  523. t.Fatal(err)
  524. }
  525. exp := &RepeatedEnum{
  526. Color: []RepeatedEnum_Color{RepeatedEnum_RED},
  527. }
  528. if !Equal(pb, exp) {
  529. t.Errorf("Incorrect populated \nHave: %v\nWant: %v", pb, exp)
  530. }
  531. }
  532. func TestProto3TextParsing(t *testing.T) {
  533. m := new(proto3pb.Message)
  534. const in = `name: "Wallace" true_scotsman: true`
  535. want := &proto3pb.Message{
  536. Name: "Wallace",
  537. TrueScotsman: true,
  538. }
  539. if err := UnmarshalText(in, m); err != nil {
  540. t.Fatal(err)
  541. }
  542. if !Equal(m, want) {
  543. t.Errorf("\n got %v\nwant %v", m, want)
  544. }
  545. }
  546. func TestMapParsing(t *testing.T) {
  547. m := new(MessageWithMap)
  548. const in = `name_mapping:<key:1234 value:"Feist"> name_mapping:<key:1 value:"Beatles">` +
  549. `msg_mapping:<key:-4, value:<f: 2.0>,>` + // separating commas are okay
  550. `msg_mapping<key:-2 value<f: 4.0>>` + // no colon after "value"
  551. `msg_mapping:<value:<f: 5.0>>` + // omitted key
  552. `msg_mapping:<key:1>` + // omitted value
  553. `byte_mapping:<key:true value:"so be it">` +
  554. `byte_mapping:<>` // omitted key and value
  555. want := &MessageWithMap{
  556. NameMapping: map[int32]string{
  557. 1: "Beatles",
  558. 1234: "Feist",
  559. },
  560. MsgMapping: map[int64]*FloatingPoint{
  561. -4: {F: Float64(2.0)},
  562. -2: {F: Float64(4.0)},
  563. 0: {F: Float64(5.0)},
  564. 1: nil,
  565. },
  566. ByteMapping: map[bool][]byte{
  567. false: nil,
  568. true: []byte("so be it"),
  569. },
  570. }
  571. if err := UnmarshalText(in, m); err != nil {
  572. t.Fatal(err)
  573. }
  574. if !Equal(m, want) {
  575. t.Errorf("\n got %v\nwant %v", m, want)
  576. }
  577. }
  578. func TestOneofParsing(t *testing.T) {
  579. const in = `name:"Shrek"`
  580. m := new(Communique)
  581. want := &Communique{Union: &Communique_Name{"Shrek"}}
  582. if err := UnmarshalText(in, m); err != nil {
  583. t.Fatal(err)
  584. }
  585. if !Equal(m, want) {
  586. t.Errorf("\n got %v\nwant %v", m, want)
  587. }
  588. }
  589. var benchInput string
  590. func init() {
  591. benchInput = "count: 4\n"
  592. for i := 0; i < 1000; i++ {
  593. benchInput += "pet: \"fido\"\n"
  594. }
  595. // Check it is valid input.
  596. pb := new(MyMessage)
  597. err := UnmarshalText(benchInput, pb)
  598. if err != nil {
  599. panic("Bad benchmark input: " + err.Error())
  600. }
  601. }
  602. func BenchmarkUnmarshalText(b *testing.B) {
  603. pb := new(MyMessage)
  604. for i := 0; i < b.N; i++ {
  605. UnmarshalText(benchInput, pb)
  606. }
  607. b.SetBytes(int64(len(benchInput)))
  608. }