text_parser_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2010 The Go Authors. All rights reserved.
  4. // http://code.google.com/p/goprotobuf/
  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. . "./testdata"
  37. . "code.google.com/p/goprotobuf/proto"
  38. )
  39. type UnmarshalTextTest struct {
  40. in string
  41. err string // if "", no error expected
  42. out *MyMessage
  43. }
  44. func buildExtStructTest(text string) UnmarshalTextTest {
  45. msg := &MyMessage{
  46. Count: Int32(42),
  47. }
  48. SetExtension(msg, E_Ext_More, &Ext{
  49. Data: String("Hello, world!"),
  50. })
  51. return UnmarshalTextTest{in: text, out: msg}
  52. }
  53. func buildExtDataTest(text string) UnmarshalTextTest {
  54. msg := &MyMessage{
  55. Count: Int32(42),
  56. }
  57. SetExtension(msg, E_Ext_Text, String("Hello, world!"))
  58. SetExtension(msg, E_Ext_Number, Int32(1729))
  59. return UnmarshalTextTest{in: text, out: msg}
  60. }
  61. func buildExtRepStringTest(text string) UnmarshalTextTest {
  62. msg := &MyMessage{
  63. Count: Int32(42),
  64. }
  65. if err := SetExtension(msg, E_Greeting, []string{"bula", "hola"}); err != nil {
  66. panic(err)
  67. }
  68. return UnmarshalTextTest{in: text, out: msg}
  69. }
  70. var unMarshalTextTests = []UnmarshalTextTest{
  71. // Basic
  72. {
  73. in: " count:42\n name:\"Dave\" ",
  74. out: &MyMessage{
  75. Count: Int32(42),
  76. Name: String("Dave"),
  77. },
  78. },
  79. // Empty quoted string
  80. {
  81. in: `count:42 name:""`,
  82. out: &MyMessage{
  83. Count: Int32(42),
  84. Name: String(""),
  85. },
  86. },
  87. // Quoted string concatenation
  88. {
  89. in: `count:42 name: "My name is "` + "\n" + `"elsewhere"`,
  90. out: &MyMessage{
  91. Count: Int32(42),
  92. Name: String("My name is elsewhere"),
  93. },
  94. },
  95. // Quoted string with escaped apostrophe
  96. {
  97. in: `count:42 name: "HOLIDAY - New Year\'s Day"`,
  98. out: &MyMessage{
  99. Count: Int32(42),
  100. Name: String("HOLIDAY - New Year's Day"),
  101. },
  102. },
  103. // Quoted string with single quote
  104. {
  105. in: `count:42 name: 'Roger "The Ramster" Ramjet'`,
  106. out: &MyMessage{
  107. Count: Int32(42),
  108. Name: String(`Roger "The Ramster" Ramjet`),
  109. },
  110. },
  111. // Quoted string with all the accepted special characters from the C++ test
  112. {
  113. in: `count:42 name: ` + "\"\\\"A string with \\' characters \\n and \\r newlines and \\t tabs and \\001 slashes \\\\ and multiple spaces\"",
  114. out: &MyMessage{
  115. Count: Int32(42),
  116. Name: String("\"A string with ' characters \n and \r newlines and \t tabs and \001 slashes \\ and multiple spaces"),
  117. },
  118. },
  119. // Quoted string with quoted backslash
  120. {
  121. in: `count:42 name: "\\'xyz"`,
  122. out: &MyMessage{
  123. Count: Int32(42),
  124. Name: String(`\'xyz`),
  125. },
  126. },
  127. // Quoted string with UTF-8 bytes.
  128. {
  129. in: "count:42 name: '\303\277\302\201\xAB'",
  130. out: &MyMessage{
  131. Count: Int32(42),
  132. Name: String("\303\277\302\201\xAB"),
  133. },
  134. },
  135. // Bad quoted string
  136. {
  137. in: `inner: < host: "\0" >` + "\n",
  138. err: `line 1.15: invalid quoted string "\0"`,
  139. },
  140. // Number too large for int64
  141. {
  142. in: "count: 123456789012345678901",
  143. err: "line 1.7: invalid int32: 123456789012345678901",
  144. },
  145. // Number too large for int32
  146. {
  147. in: "count: 1234567890123",
  148. err: "line 1.7: invalid int32: 1234567890123",
  149. },
  150. // Number in hexadecimal
  151. {
  152. in: "count: 0x2beef",
  153. out: &MyMessage{
  154. Count: Int32(0x2beef),
  155. },
  156. },
  157. // Number in octal
  158. {
  159. in: "count: 024601",
  160. out: &MyMessage{
  161. Count: Int32(024601),
  162. },
  163. },
  164. // Floating point number with "f" suffix
  165. {
  166. in: "count: 4 others:< weight: 17.0f >",
  167. out: &MyMessage{
  168. Count: Int32(4),
  169. Others: []*OtherMessage{
  170. {
  171. Weight: Float32(17),
  172. },
  173. },
  174. },
  175. },
  176. // Floating point positive infinity
  177. {
  178. in: "count: 4 bigfloat: inf",
  179. out: &MyMessage{
  180. Count: Int32(4),
  181. Bigfloat: Float64(math.Inf(1)),
  182. },
  183. },
  184. // Floating point negative infinity
  185. {
  186. in: "count: 4 bigfloat: -inf",
  187. out: &MyMessage{
  188. Count: Int32(4),
  189. Bigfloat: Float64(math.Inf(-1)),
  190. },
  191. },
  192. // Number too large for float32
  193. {
  194. in: "others:< weight: 12345678901234567890123456789012345678901234567890 >",
  195. err: "line 1.17: invalid float32: 12345678901234567890123456789012345678901234567890",
  196. },
  197. // Number posing as a quoted string
  198. {
  199. in: `inner: < host: 12 >` + "\n",
  200. err: `line 1.15: invalid string: 12`,
  201. },
  202. // Quoted string posing as int32
  203. {
  204. in: `count: "12"`,
  205. err: `line 1.7: invalid int32: "12"`,
  206. },
  207. // Quoted string posing a float32
  208. {
  209. in: `others:< weight: "17.4" >`,
  210. err: `line 1.17: invalid float32: "17.4"`,
  211. },
  212. // Enum
  213. {
  214. in: `count:42 bikeshed: BLUE`,
  215. out: &MyMessage{
  216. Count: Int32(42),
  217. Bikeshed: MyMessage_BLUE.Enum(),
  218. },
  219. },
  220. // Repeated field
  221. {
  222. in: `count:42 pet: "horsey" pet:"bunny"`,
  223. out: &MyMessage{
  224. Count: Int32(42),
  225. Pet: []string{"horsey", "bunny"},
  226. },
  227. },
  228. // Repeated message with/without colon and <>/{}
  229. {
  230. in: `count:42 others:{} others{} others:<> others:{}`,
  231. out: &MyMessage{
  232. Count: Int32(42),
  233. Others: []*OtherMessage{
  234. {},
  235. {},
  236. {},
  237. {},
  238. },
  239. },
  240. },
  241. // Missing colon for inner message
  242. {
  243. in: `count:42 inner < host: "cauchy.syd" >`,
  244. out: &MyMessage{
  245. Count: Int32(42),
  246. Inner: &InnerMessage{
  247. Host: String("cauchy.syd"),
  248. },
  249. },
  250. },
  251. // Missing colon for string field
  252. {
  253. in: `name "Dave"`,
  254. err: `line 1.5: expected ':', found "\"Dave\""`,
  255. },
  256. // Missing colon for int32 field
  257. {
  258. in: `count 42`,
  259. err: `line 1.6: expected ':', found "42"`,
  260. },
  261. // Missing required field
  262. {
  263. in: ``,
  264. err: `line 1.0: message testdata.MyMessage missing required field "count"`,
  265. },
  266. // Repeated non-repeated field
  267. {
  268. in: `name: "Rob" name: "Russ"`,
  269. err: `line 1.12: non-repeated field "name" was repeated`,
  270. },
  271. // Group
  272. {
  273. in: `count: 17 SomeGroup { group_field: 12 }`,
  274. out: &MyMessage{
  275. Count: Int32(17),
  276. Somegroup: &MyMessage_SomeGroup{
  277. GroupField: Int32(12),
  278. },
  279. },
  280. },
  281. // Semicolon between fields
  282. {
  283. in: `count:3;name:"Calvin"`,
  284. out: &MyMessage{
  285. Count: Int32(3),
  286. Name: String("Calvin"),
  287. },
  288. },
  289. // Comma between fields
  290. {
  291. in: `count:4,name:"Ezekiel"`,
  292. out: &MyMessage{
  293. Count: Int32(4),
  294. Name: String("Ezekiel"),
  295. },
  296. },
  297. // Extension
  298. buildExtStructTest(`count: 42 [testdata.Ext.more]:<data:"Hello, world!" >`),
  299. buildExtStructTest(`count: 42 [testdata.Ext.more] {data:"Hello, world!"}`),
  300. buildExtDataTest(`count: 42 [testdata.Ext.text]:"Hello, world!" [testdata.Ext.number]:1729`),
  301. buildExtRepStringTest(`count: 42 [testdata.greeting]:"bula" [testdata.greeting]:"hola"`),
  302. // Big all-in-one
  303. {
  304. in: "count:42 # Meaning\n" +
  305. `name:"Dave" ` +
  306. `quote:"\"I didn't want to go.\"" ` +
  307. `pet:"bunny" ` +
  308. `pet:"kitty" ` +
  309. `pet:"horsey" ` +
  310. `inner:<` +
  311. ` host:"footrest.syd" ` +
  312. ` port:7001 ` +
  313. ` connected:true ` +
  314. `> ` +
  315. `others:<` +
  316. ` key:3735928559 ` +
  317. ` value:"\x01A\a\f" ` +
  318. `> ` +
  319. `others:<` +
  320. " weight:58.9 # Atomic weight of Co\n" +
  321. ` inner:<` +
  322. ` host:"lesha.mtv" ` +
  323. ` port:8002 ` +
  324. ` >` +
  325. `>`,
  326. out: &MyMessage{
  327. Count: Int32(42),
  328. Name: String("Dave"),
  329. Quote: String(`"I didn't want to go."`),
  330. Pet: []string{"bunny", "kitty", "horsey"},
  331. Inner: &InnerMessage{
  332. Host: String("footrest.syd"),
  333. Port: Int32(7001),
  334. Connected: Bool(true),
  335. },
  336. Others: []*OtherMessage{
  337. {
  338. Key: Int64(3735928559),
  339. Value: []byte{0x1, 'A', '\a', '\f'},
  340. },
  341. {
  342. Weight: Float32(58.9),
  343. Inner: &InnerMessage{
  344. Host: String("lesha.mtv"),
  345. Port: Int32(8002),
  346. },
  347. },
  348. },
  349. },
  350. },
  351. }
  352. func TestUnmarshalText(t *testing.T) {
  353. for i, test := range unMarshalTextTests {
  354. pb := new(MyMessage)
  355. err := UnmarshalText(test.in, pb)
  356. if test.err == "" {
  357. // We don't expect failure.
  358. if err != nil {
  359. t.Errorf("Test %d: Unexpected error: %v", i, err)
  360. } else if !reflect.DeepEqual(pb, test.out) {
  361. t.Errorf("Test %d: Incorrect populated \nHave: %v\nWant: %v",
  362. i, pb, test.out)
  363. }
  364. } else {
  365. // We do expect failure.
  366. if err == nil {
  367. t.Errorf("Test %d: Didn't get expected error: %v", i, test.err)
  368. } else if err.Error() != test.err {
  369. t.Errorf("Test %d: Incorrect error.\nHave: %v\nWant: %v",
  370. i, err.Error(), test.err)
  371. }
  372. }
  373. }
  374. }
  375. // Regression test; this caused a panic.
  376. func TestRepeatedEnum(t *testing.T) {
  377. pb := new(RepeatedEnum)
  378. if err := UnmarshalText("color: RED", pb); err != nil {
  379. t.Fatal(err)
  380. }
  381. exp := &RepeatedEnum{
  382. Color: []RepeatedEnum_Color{RepeatedEnum_RED},
  383. }
  384. if !Equal(pb, exp) {
  385. t.Errorf("Incorrect populated \nHave: %v\nWant: %v", pb, exp)
  386. }
  387. }
  388. var benchInput string
  389. func init() {
  390. benchInput = "count: 4\n"
  391. for i := 0; i < 1000; i++ {
  392. benchInput += "pet: \"fido\"\n"
  393. }
  394. // Check it is valid input.
  395. pb := new(MyMessage)
  396. err := UnmarshalText(benchInput, pb)
  397. if err != nil {
  398. panic("Bad benchmark input: " + err.Error())
  399. }
  400. }
  401. func BenchmarkUnmarshalText(b *testing.B) {
  402. pb := new(MyMessage)
  403. for i := 0; i < b.N; i++ {
  404. UnmarshalText(benchInput, pb)
  405. }
  406. b.SetBytes(int64(len(benchInput)))
  407. }