text_parser_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. proto3pb "./proto3_proto"
  37. . "./testdata"
  38. . "github.com/golang/protobuf/proto"
  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
  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 with escaped apostrophe
  97. {
  98. in: `count:42 name: "HOLIDAY - New Year\'s Day"`,
  99. out: &MyMessage{
  100. Count: Int32(42),
  101. Name: String("HOLIDAY - New Year's Day"),
  102. },
  103. },
  104. // Quoted string with single quote
  105. {
  106. in: `count:42 name: 'Roger "The Ramster" Ramjet'`,
  107. out: &MyMessage{
  108. Count: Int32(42),
  109. Name: String(`Roger "The Ramster" Ramjet`),
  110. },
  111. },
  112. // Quoted string with all the accepted special characters from the C++ test
  113. {
  114. in: `count:42 name: ` + "\"\\\"A string with \\' characters \\n and \\r newlines and \\t tabs and \\001 slashes \\\\ and multiple spaces\"",
  115. out: &MyMessage{
  116. Count: Int32(42),
  117. Name: String("\"A string with ' characters \n and \r newlines and \t tabs and \001 slashes \\ and multiple spaces"),
  118. },
  119. },
  120. // Quoted string with quoted backslash
  121. {
  122. in: `count:42 name: "\\'xyz"`,
  123. out: &MyMessage{
  124. Count: Int32(42),
  125. Name: String(`\'xyz`),
  126. },
  127. },
  128. // Quoted string with UTF-8 bytes.
  129. {
  130. in: "count:42 name: '\303\277\302\201\xAB'",
  131. out: &MyMessage{
  132. Count: Int32(42),
  133. Name: String("\303\277\302\201\xAB"),
  134. },
  135. },
  136. // Bad quoted string
  137. {
  138. in: `inner: < host: "\0" >` + "\n",
  139. err: `line 1.15: invalid quoted string "\0"`,
  140. },
  141. // Number too large for int64
  142. {
  143. in: "count: 1 others { key: 123456789012345678901 }",
  144. err: "line 1.23: invalid int64: 123456789012345678901",
  145. },
  146. // Number too large for int32
  147. {
  148. in: "count: 1234567890123",
  149. err: "line 1.7: invalid int32: 1234567890123",
  150. },
  151. // Number in hexadecimal
  152. {
  153. in: "count: 0x2beef",
  154. out: &MyMessage{
  155. Count: Int32(0x2beef),
  156. },
  157. },
  158. // Number in octal
  159. {
  160. in: "count: 024601",
  161. out: &MyMessage{
  162. Count: Int32(024601),
  163. },
  164. },
  165. // Floating point number with "f" suffix
  166. {
  167. in: "count: 4 others:< weight: 17.0f >",
  168. out: &MyMessage{
  169. Count: Int32(4),
  170. Others: []*OtherMessage{
  171. {
  172. Weight: Float32(17),
  173. },
  174. },
  175. },
  176. },
  177. // Floating point positive infinity
  178. {
  179. in: "count: 4 bigfloat: inf",
  180. out: &MyMessage{
  181. Count: Int32(4),
  182. Bigfloat: Float64(math.Inf(1)),
  183. },
  184. },
  185. // Floating point negative infinity
  186. {
  187. in: "count: 4 bigfloat: -inf",
  188. out: &MyMessage{
  189. Count: Int32(4),
  190. Bigfloat: Float64(math.Inf(-1)),
  191. },
  192. },
  193. // Number too large for float32
  194. {
  195. in: "others:< weight: 12345678901234567890123456789012345678901234567890 >",
  196. err: "line 1.17: invalid float32: 12345678901234567890123456789012345678901234567890",
  197. },
  198. // Number posing as a quoted string
  199. {
  200. in: `inner: < host: 12 >` + "\n",
  201. err: `line 1.15: invalid string: 12`,
  202. },
  203. // Quoted string posing as int32
  204. {
  205. in: `count: "12"`,
  206. err: `line 1.7: invalid int32: "12"`,
  207. },
  208. // Quoted string posing a float32
  209. {
  210. in: `others:< weight: "17.4" >`,
  211. err: `line 1.17: invalid float32: "17.4"`,
  212. },
  213. // Enum
  214. {
  215. in: `count:42 bikeshed: BLUE`,
  216. out: &MyMessage{
  217. Count: Int32(42),
  218. Bikeshed: MyMessage_BLUE.Enum(),
  219. },
  220. },
  221. // Repeated field
  222. {
  223. in: `count:42 pet: "horsey" pet:"bunny"`,
  224. out: &MyMessage{
  225. Count: Int32(42),
  226. Pet: []string{"horsey", "bunny"},
  227. },
  228. },
  229. // Repeated message with/without colon and <>/{}
  230. {
  231. in: `count:42 others:{} others{} others:<> others:{}`,
  232. out: &MyMessage{
  233. Count: Int32(42),
  234. Others: []*OtherMessage{
  235. {},
  236. {},
  237. {},
  238. {},
  239. },
  240. },
  241. },
  242. // Missing colon for inner message
  243. {
  244. in: `count:42 inner < host: "cauchy.syd" >`,
  245. out: &MyMessage{
  246. Count: Int32(42),
  247. Inner: &InnerMessage{
  248. Host: String("cauchy.syd"),
  249. },
  250. },
  251. },
  252. // Missing colon for string field
  253. {
  254. in: `name "Dave"`,
  255. err: `line 1.5: expected ':', found "\"Dave\""`,
  256. },
  257. // Missing colon for int32 field
  258. {
  259. in: `count 42`,
  260. err: `line 1.6: expected ':', found "42"`,
  261. },
  262. // Missing required field
  263. {
  264. in: `name: "Pawel"`,
  265. err: `proto: required field "testdata.MyMessage.count" not set`,
  266. out: &MyMessage{
  267. Name: String("Pawel"),
  268. },
  269. },
  270. // Repeated non-repeated field
  271. {
  272. in: `name: "Rob" name: "Russ"`,
  273. err: `line 1.12: non-repeated field "name" was repeated`,
  274. },
  275. // Group
  276. {
  277. in: `count: 17 SomeGroup { group_field: 12 }`,
  278. out: &MyMessage{
  279. Count: Int32(17),
  280. Somegroup: &MyMessage_SomeGroup{
  281. GroupField: Int32(12),
  282. },
  283. },
  284. },
  285. // Semicolon between fields
  286. {
  287. in: `count:3;name:"Calvin"`,
  288. out: &MyMessage{
  289. Count: Int32(3),
  290. Name: String("Calvin"),
  291. },
  292. },
  293. // Comma between fields
  294. {
  295. in: `count:4,name:"Ezekiel"`,
  296. out: &MyMessage{
  297. Count: Int32(4),
  298. Name: String("Ezekiel"),
  299. },
  300. },
  301. // Extension
  302. buildExtStructTest(`count: 42 [testdata.Ext.more]:<data:"Hello, world!" >`),
  303. buildExtStructTest(`count: 42 [testdata.Ext.more] {data:"Hello, world!"}`),
  304. buildExtDataTest(`count: 42 [testdata.Ext.text]:"Hello, world!" [testdata.Ext.number]:1729`),
  305. buildExtRepStringTest(`count: 42 [testdata.greeting]:"bula" [testdata.greeting]:"hola"`),
  306. // Big all-in-one
  307. {
  308. in: "count:42 # Meaning\n" +
  309. `name:"Dave" ` +
  310. `quote:"\"I didn't want to go.\"" ` +
  311. `pet:"bunny" ` +
  312. `pet:"kitty" ` +
  313. `pet:"horsey" ` +
  314. `inner:<` +
  315. ` host:"footrest.syd" ` +
  316. ` port:7001 ` +
  317. ` connected:true ` +
  318. `> ` +
  319. `others:<` +
  320. ` key:3735928559 ` +
  321. ` value:"\x01A\a\f" ` +
  322. `> ` +
  323. `others:<` +
  324. " weight:58.9 # Atomic weight of Co\n" +
  325. ` inner:<` +
  326. ` host:"lesha.mtv" ` +
  327. ` port:8002 ` +
  328. ` >` +
  329. `>`,
  330. out: &MyMessage{
  331. Count: Int32(42),
  332. Name: String("Dave"),
  333. Quote: String(`"I didn't want to go."`),
  334. Pet: []string{"bunny", "kitty", "horsey"},
  335. Inner: &InnerMessage{
  336. Host: String("footrest.syd"),
  337. Port: Int32(7001),
  338. Connected: Bool(true),
  339. },
  340. Others: []*OtherMessage{
  341. {
  342. Key: Int64(3735928559),
  343. Value: []byte{0x1, 'A', '\a', '\f'},
  344. },
  345. {
  346. Weight: Float32(58.9),
  347. Inner: &InnerMessage{
  348. Host: String("lesha.mtv"),
  349. Port: Int32(8002),
  350. },
  351. },
  352. },
  353. },
  354. },
  355. }
  356. func TestUnmarshalText(t *testing.T) {
  357. for i, test := range unMarshalTextTests {
  358. pb := new(MyMessage)
  359. err := UnmarshalText(test.in, pb)
  360. if test.err == "" {
  361. // We don't expect failure.
  362. if err != nil {
  363. t.Errorf("Test %d: Unexpected error: %v", i, err)
  364. } else if !reflect.DeepEqual(pb, test.out) {
  365. t.Errorf("Test %d: Incorrect populated \nHave: %v\nWant: %v",
  366. i, pb, test.out)
  367. }
  368. } else {
  369. // We do expect failure.
  370. if err == nil {
  371. t.Errorf("Test %d: Didn't get expected error: %v", i, test.err)
  372. } else if err.Error() != test.err {
  373. t.Errorf("Test %d: Incorrect error.\nHave: %v\nWant: %v",
  374. i, err.Error(), test.err)
  375. } else if _, ok := err.(*RequiredNotSetError); ok && test.out != nil && !reflect.DeepEqual(pb, test.out) {
  376. t.Errorf("Test %d: Incorrect populated \nHave: %v\nWant: %v",
  377. i, pb, test.out)
  378. }
  379. }
  380. }
  381. }
  382. func TestUnmarshalTextCustomMessage(t *testing.T) {
  383. msg := &textMessage{}
  384. if err := UnmarshalText("custom", msg); err != nil {
  385. t.Errorf("Unexpected error from custom unmarshal: %v", err)
  386. }
  387. if UnmarshalText("not custom", msg) == nil {
  388. t.Errorf("Didn't get expected error from custom unmarshal")
  389. }
  390. }
  391. // Regression test; this caused a panic.
  392. func TestRepeatedEnum(t *testing.T) {
  393. pb := new(RepeatedEnum)
  394. if err := UnmarshalText("color: RED", pb); err != nil {
  395. t.Fatal(err)
  396. }
  397. exp := &RepeatedEnum{
  398. Color: []RepeatedEnum_Color{RepeatedEnum_RED},
  399. }
  400. if !Equal(pb, exp) {
  401. t.Errorf("Incorrect populated \nHave: %v\nWant: %v", pb, exp)
  402. }
  403. }
  404. func TestProto3TextParsing(t *testing.T) {
  405. m := new(proto3pb.Message)
  406. const in = `name: "Wallace" true_scotsman: true`
  407. want := &proto3pb.Message{
  408. Name: "Wallace",
  409. TrueScotsman: true,
  410. }
  411. if err := UnmarshalText(in, m); err != nil {
  412. t.Fatal(err)
  413. }
  414. if !Equal(m, want) {
  415. t.Errorf("\n got %v\nwant %v", m, want)
  416. }
  417. }
  418. func TestMapParsing(t *testing.T) {
  419. m := new(MessageWithMap)
  420. const in = `name_mapping:<key:1234 value:"Feist"> name_mapping:<key:1 value:"Beatles">` +
  421. `msg_mapping:<key:-4 value:<f: 2.0>>` +
  422. `msg_mapping<key:-2 value<f: 4.0>>` + // no colon after "value"
  423. `byte_mapping:<key:true value:"so be it">`
  424. want := &MessageWithMap{
  425. NameMapping: map[int32]string{
  426. 1: "Beatles",
  427. 1234: "Feist",
  428. },
  429. MsgMapping: map[int64]*FloatingPoint{
  430. -4: {F: Float64(2.0)},
  431. -2: {F: Float64(4.0)},
  432. },
  433. ByteMapping: map[bool][]byte{
  434. true: []byte("so be it"),
  435. },
  436. }
  437. if err := UnmarshalText(in, m); err != nil {
  438. t.Fatal(err)
  439. }
  440. if !Equal(m, want) {
  441. t.Errorf("\n got %v\nwant %v", m, want)
  442. }
  443. }
  444. var benchInput string
  445. func init() {
  446. benchInput = "count: 4\n"
  447. for i := 0; i < 1000; i++ {
  448. benchInput += "pet: \"fido\"\n"
  449. }
  450. // Check it is valid input.
  451. pb := new(MyMessage)
  452. err := UnmarshalText(benchInput, pb)
  453. if err != nil {
  454. panic("Bad benchmark input: " + err.Error())
  455. }
  456. }
  457. func BenchmarkUnmarshalText(b *testing.B) {
  458. pb := new(MyMessage)
  459. for i := 0; i < b.N; i++ {
  460. UnmarshalText(benchInput, pb)
  461. }
  462. b.SetBytes(int64(len(benchInput)))
  463. }