text_parser_test.go 15 KB

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