text_parser_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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. // Extension
  342. buildExtStructTest(`count: 42 [testdata.Ext.more]:<data:"Hello, world!" >`),
  343. buildExtStructTest(`count: 42 [testdata.Ext.more] {data:"Hello, world!"}`),
  344. buildExtDataTest(`count: 42 [testdata.Ext.text]:"Hello, world!" [testdata.Ext.number]:1729`),
  345. buildExtRepStringTest(`count: 42 [testdata.greeting]:"bula" [testdata.greeting]:"hola"`),
  346. // Big all-in-one
  347. {
  348. in: "count:42 # Meaning\n" +
  349. `name:"Dave" ` +
  350. `quote:"\"I didn't want to go.\"" ` +
  351. `pet:"bunny" ` +
  352. `pet:"kitty" ` +
  353. `pet:"horsey" ` +
  354. `inner:<` +
  355. ` host:"footrest.syd" ` +
  356. ` port:7001 ` +
  357. ` connected:true ` +
  358. `> ` +
  359. `others:<` +
  360. ` key:3735928559 ` +
  361. ` value:"\x01A\a\f" ` +
  362. `> ` +
  363. `others:<` +
  364. " weight:58.9 # Atomic weight of Co\n" +
  365. ` inner:<` +
  366. ` host:"lesha.mtv" ` +
  367. ` port:8002 ` +
  368. ` >` +
  369. `>`,
  370. out: &MyMessage{
  371. Count: Int32(42),
  372. Name: String("Dave"),
  373. Quote: String(`"I didn't want to go."`),
  374. Pet: []string{"bunny", "kitty", "horsey"},
  375. Inner: &InnerMessage{
  376. Host: String("footrest.syd"),
  377. Port: Int32(7001),
  378. Connected: Bool(true),
  379. },
  380. Others: []*OtherMessage{
  381. {
  382. Key: Int64(3735928559),
  383. Value: []byte{0x1, 'A', '\a', '\f'},
  384. },
  385. {
  386. Weight: Float32(58.9),
  387. Inner: &InnerMessage{
  388. Host: String("lesha.mtv"),
  389. Port: Int32(8002),
  390. },
  391. },
  392. },
  393. },
  394. },
  395. }
  396. func TestUnmarshalText(t *testing.T) {
  397. for i, test := range unMarshalTextTests {
  398. pb := new(MyMessage)
  399. err := UnmarshalText(test.in, pb)
  400. if test.err == "" {
  401. // We don't expect failure.
  402. if err != nil {
  403. t.Errorf("Test %d: Unexpected error: %v", i, err)
  404. } else if !reflect.DeepEqual(pb, test.out) {
  405. t.Errorf("Test %d: Incorrect populated \nHave: %v\nWant: %v",
  406. i, pb, test.out)
  407. }
  408. } else {
  409. // We do expect failure.
  410. if err == nil {
  411. t.Errorf("Test %d: Didn't get expected error: %v", i, test.err)
  412. } else if err.Error() != test.err {
  413. t.Errorf("Test %d: Incorrect error.\nHave: %v\nWant: %v",
  414. i, err.Error(), test.err)
  415. } else if _, ok := err.(*RequiredNotSetError); ok && test.out != nil && !reflect.DeepEqual(pb, test.out) {
  416. t.Errorf("Test %d: Incorrect populated \nHave: %v\nWant: %v",
  417. i, pb, test.out)
  418. }
  419. }
  420. }
  421. }
  422. func TestUnmarshalTextCustomMessage(t *testing.T) {
  423. msg := &textMessage{}
  424. if err := UnmarshalText("custom", msg); err != nil {
  425. t.Errorf("Unexpected error from custom unmarshal: %v", err)
  426. }
  427. if UnmarshalText("not custom", msg) == nil {
  428. t.Errorf("Didn't get expected error from custom unmarshal")
  429. }
  430. }
  431. // Regression test; this caused a panic.
  432. func TestRepeatedEnum(t *testing.T) {
  433. pb := new(RepeatedEnum)
  434. if err := UnmarshalText("color: RED", pb); err != nil {
  435. t.Fatal(err)
  436. }
  437. exp := &RepeatedEnum{
  438. Color: []RepeatedEnum_Color{RepeatedEnum_RED},
  439. }
  440. if !Equal(pb, exp) {
  441. t.Errorf("Incorrect populated \nHave: %v\nWant: %v", pb, exp)
  442. }
  443. }
  444. func TestProto3TextParsing(t *testing.T) {
  445. m := new(proto3pb.Message)
  446. const in = `name: "Wallace" true_scotsman: true`
  447. want := &proto3pb.Message{
  448. Name: "Wallace",
  449. TrueScotsman: true,
  450. }
  451. if err := UnmarshalText(in, m); err != nil {
  452. t.Fatal(err)
  453. }
  454. if !Equal(m, want) {
  455. t.Errorf("\n got %v\nwant %v", m, want)
  456. }
  457. }
  458. func TestMapParsing(t *testing.T) {
  459. m := new(MessageWithMap)
  460. const in = `name_mapping:<key:1234 value:"Feist"> name_mapping:<key:1 value:"Beatles">` +
  461. `msg_mapping:<key:-4, value:<f: 2.0>,>` + // separating commas are okay
  462. `msg_mapping<key:-2 value<f: 4.0>>` + // no colon after "value"
  463. `byte_mapping:<key:true value:"so be it">`
  464. want := &MessageWithMap{
  465. NameMapping: map[int32]string{
  466. 1: "Beatles",
  467. 1234: "Feist",
  468. },
  469. MsgMapping: map[int64]*FloatingPoint{
  470. -4: {F: Float64(2.0)},
  471. -2: {F: Float64(4.0)},
  472. },
  473. ByteMapping: map[bool][]byte{
  474. true: []byte("so be it"),
  475. },
  476. }
  477. if err := UnmarshalText(in, m); err != nil {
  478. t.Fatal(err)
  479. }
  480. if !Equal(m, want) {
  481. t.Errorf("\n got %v\nwant %v", m, want)
  482. }
  483. }
  484. func TestOneofParsing(t *testing.T) {
  485. const in = `name:"Shrek"`
  486. m := new(Communique)
  487. want := &Communique{Union: &Communique_Name{"Shrek"}}
  488. if err := UnmarshalText(in, m); err != nil {
  489. t.Fatal(err)
  490. }
  491. if !Equal(m, want) {
  492. t.Errorf("\n got %v\nwant %v", m, want)
  493. }
  494. }
  495. var benchInput string
  496. func init() {
  497. benchInput = "count: 4\n"
  498. for i := 0; i < 1000; i++ {
  499. benchInput += "pet: \"fido\"\n"
  500. }
  501. // Check it is valid input.
  502. pb := new(MyMessage)
  503. err := UnmarshalText(benchInput, pb)
  504. if err != nil {
  505. panic("Bad benchmark input: " + err.Error())
  506. }
  507. }
  508. func BenchmarkUnmarshalText(b *testing.B) {
  509. pb := new(MyMessage)
  510. for i := 0; i < b.N; i++ {
  511. UnmarshalText(benchInput, pb)
  512. }
  513. b.SetBytes(int64(len(benchInput)))
  514. }