text_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. "bytes"
  34. "errors"
  35. "io/ioutil"
  36. "math"
  37. "strings"
  38. "testing"
  39. "github.com/golang/protobuf/proto"
  40. proto3pb "github.com/golang/protobuf/proto/proto3_proto"
  41. pb "github.com/golang/protobuf/proto/testdata"
  42. )
  43. // textMessage implements the methods that allow it to marshal and unmarshal
  44. // itself as text.
  45. type textMessage struct {
  46. }
  47. func (*textMessage) MarshalText() ([]byte, error) {
  48. return []byte("custom"), nil
  49. }
  50. func (*textMessage) UnmarshalText(bytes []byte) error {
  51. if string(bytes) != "custom" {
  52. return errors.New("expected 'custom'")
  53. }
  54. return nil
  55. }
  56. func (*textMessage) Reset() {}
  57. func (*textMessage) String() string { return "" }
  58. func (*textMessage) ProtoMessage() {}
  59. func newTestMessage() *pb.MyMessage {
  60. msg := &pb.MyMessage{
  61. Count: proto.Int32(42),
  62. Name: proto.String("Dave"),
  63. Quote: proto.String(`"I didn't want to go."`),
  64. Pet: []string{"bunny", "kitty", "horsey"},
  65. Inner: &pb.InnerMessage{
  66. Host: proto.String("footrest.syd"),
  67. Port: proto.Int32(7001),
  68. Connected: proto.Bool(true),
  69. },
  70. Others: []*pb.OtherMessage{
  71. {
  72. Key: proto.Int64(0xdeadbeef),
  73. Value: []byte{1, 65, 7, 12},
  74. },
  75. {
  76. Weight: proto.Float32(6.022),
  77. Inner: &pb.InnerMessage{
  78. Host: proto.String("lesha.mtv"),
  79. Port: proto.Int32(8002),
  80. },
  81. },
  82. },
  83. Bikeshed: pb.MyMessage_BLUE.Enum(),
  84. Somegroup: &pb.MyMessage_SomeGroup{
  85. GroupField: proto.Int32(8),
  86. },
  87. // One normally wouldn't do this.
  88. // This is an undeclared tag 13, as a varint (wire type 0) with value 4.
  89. XXX_unrecognized: []byte{13<<3 | 0, 4},
  90. }
  91. ext := &pb.Ext{
  92. Data: proto.String("Big gobs for big rats"),
  93. }
  94. if err := proto.SetExtension(msg, pb.E_Ext_More, ext); err != nil {
  95. panic(err)
  96. }
  97. greetings := []string{"adg", "easy", "cow"}
  98. if err := proto.SetExtension(msg, pb.E_Greeting, greetings); err != nil {
  99. panic(err)
  100. }
  101. // Add an unknown extension. We marshal a pb.Ext, and fake the ID.
  102. b, err := proto.Marshal(&pb.Ext{Data: proto.String("3G skiing")})
  103. if err != nil {
  104. panic(err)
  105. }
  106. b = append(proto.EncodeVarint(201<<3|proto.WireBytes), b...)
  107. proto.SetRawExtension(msg, 201, b)
  108. // Extensions can be plain fields, too, so let's test that.
  109. b = append(proto.EncodeVarint(202<<3|proto.WireVarint), 19)
  110. proto.SetRawExtension(msg, 202, b)
  111. return msg
  112. }
  113. const text = `count: 42
  114. name: "Dave"
  115. quote: "\"I didn't want to go.\""
  116. pet: "bunny"
  117. pet: "kitty"
  118. pet: "horsey"
  119. inner: <
  120. host: "footrest.syd"
  121. port: 7001
  122. connected: true
  123. >
  124. others: <
  125. key: 3735928559
  126. value: "\001A\007\014"
  127. >
  128. others: <
  129. weight: 6.022
  130. inner: <
  131. host: "lesha.mtv"
  132. port: 8002
  133. >
  134. >
  135. bikeshed: BLUE
  136. SomeGroup {
  137. group_field: 8
  138. }
  139. /* 2 unknown bytes */
  140. 13: 4
  141. [testdata.Ext.more]: <
  142. data: "Big gobs for big rats"
  143. >
  144. [testdata.greeting]: "adg"
  145. [testdata.greeting]: "easy"
  146. [testdata.greeting]: "cow"
  147. /* 13 unknown bytes */
  148. 201: "\t3G skiing"
  149. /* 3 unknown bytes */
  150. 202: 19
  151. `
  152. func TestMarshalText(t *testing.T) {
  153. buf := new(bytes.Buffer)
  154. if err := proto.MarshalText(buf, newTestMessage()); err != nil {
  155. t.Fatalf("proto.MarshalText: %v", err)
  156. }
  157. s := buf.String()
  158. if s != text {
  159. t.Errorf("Got:\n===\n%v===\nExpected:\n===\n%v===\n", s, text)
  160. }
  161. }
  162. func TestMarshalTextCustomMessage(t *testing.T) {
  163. buf := new(bytes.Buffer)
  164. if err := proto.MarshalText(buf, &textMessage{}); err != nil {
  165. t.Fatalf("proto.MarshalText: %v", err)
  166. }
  167. s := buf.String()
  168. if s != "custom" {
  169. t.Errorf("Got %q, expected %q", s, "custom")
  170. }
  171. }
  172. func TestMarshalTextNil(t *testing.T) {
  173. want := "<nil>"
  174. tests := []proto.Message{nil, (*pb.MyMessage)(nil)}
  175. for i, test := range tests {
  176. buf := new(bytes.Buffer)
  177. if err := proto.MarshalText(buf, test); err != nil {
  178. t.Fatal(err)
  179. }
  180. if got := buf.String(); got != want {
  181. t.Errorf("%d: got %q want %q", i, got, want)
  182. }
  183. }
  184. }
  185. func TestMarshalTextUnknownEnum(t *testing.T) {
  186. // The Color enum only specifies values 0-2.
  187. m := &pb.MyMessage{Bikeshed: pb.MyMessage_Color(3).Enum()}
  188. got := m.String()
  189. const want = `bikeshed:3 `
  190. if got != want {
  191. t.Errorf("\n got %q\nwant %q", got, want)
  192. }
  193. }
  194. func TestTextOneof(t *testing.T) {
  195. tests := []struct {
  196. m proto.Message
  197. want string
  198. }{
  199. // zero message
  200. {&pb.Communique{}, ``},
  201. // scalar field
  202. {&pb.Communique{Union: &pb.Communique_Number{4}}, `number:4`},
  203. // message field
  204. {&pb.Communique{Union: &pb.Communique_Msg{
  205. &pb.Strings{StringField: proto.String("why hello!")},
  206. }}, `msg:<string_field:"why hello!" >`},
  207. // bad oneof (should not panic)
  208. {&pb.Communique{Union: &pb.Communique_Msg{nil}}, `msg:/* nil */`},
  209. }
  210. for _, test := range tests {
  211. got := strings.TrimSpace(test.m.String())
  212. if got != test.want {
  213. t.Errorf("\n got %s\nwant %s", got, test.want)
  214. }
  215. }
  216. }
  217. func BenchmarkMarshalTextBuffered(b *testing.B) {
  218. buf := new(bytes.Buffer)
  219. m := newTestMessage()
  220. for i := 0; i < b.N; i++ {
  221. buf.Reset()
  222. proto.MarshalText(buf, m)
  223. }
  224. }
  225. func BenchmarkMarshalTextUnbuffered(b *testing.B) {
  226. w := ioutil.Discard
  227. m := newTestMessage()
  228. for i := 0; i < b.N; i++ {
  229. proto.MarshalText(w, m)
  230. }
  231. }
  232. func compact(src string) string {
  233. // s/[ \n]+/ /g; s/ $//;
  234. dst := make([]byte, len(src))
  235. space, comment := false, false
  236. j := 0
  237. for i := 0; i < len(src); i++ {
  238. if strings.HasPrefix(src[i:], "/*") {
  239. comment = true
  240. i++
  241. continue
  242. }
  243. if comment && strings.HasPrefix(src[i:], "*/") {
  244. comment = false
  245. i++
  246. continue
  247. }
  248. if comment {
  249. continue
  250. }
  251. c := src[i]
  252. if c == ' ' || c == '\n' {
  253. space = true
  254. continue
  255. }
  256. if j > 0 && (dst[j-1] == ':' || dst[j-1] == '<' || dst[j-1] == '{') {
  257. space = false
  258. }
  259. if c == '{' {
  260. space = false
  261. }
  262. if space {
  263. dst[j] = ' '
  264. j++
  265. space = false
  266. }
  267. dst[j] = c
  268. j++
  269. }
  270. if space {
  271. dst[j] = ' '
  272. j++
  273. }
  274. return string(dst[0:j])
  275. }
  276. var compactText = compact(text)
  277. func TestCompactText(t *testing.T) {
  278. s := proto.CompactTextString(newTestMessage())
  279. if s != compactText {
  280. t.Errorf("Got:\n===\n%v===\nExpected:\n===\n%v\n===\n", s, compactText)
  281. }
  282. }
  283. func TestStringEscaping(t *testing.T) {
  284. testCases := []struct {
  285. in *pb.Strings
  286. out string
  287. }{
  288. {
  289. // Test data from C++ test (TextFormatTest.StringEscape).
  290. // Single divergence: we don't escape apostrophes.
  291. &pb.Strings{StringField: proto.String("\"A string with ' characters \n and \r newlines and \t tabs and \001 slashes \\ and multiple spaces")},
  292. "string_field: \"\\\"A string with ' characters \\n and \\r newlines and \\t tabs and \\001 slashes \\\\ and multiple spaces\"\n",
  293. },
  294. {
  295. // Test data from the same C++ test.
  296. &pb.Strings{StringField: proto.String("\350\260\267\346\255\214")},
  297. "string_field: \"\\350\\260\\267\\346\\255\\214\"\n",
  298. },
  299. {
  300. // Some UTF-8.
  301. &pb.Strings{StringField: proto.String("\x00\x01\xff\x81")},
  302. `string_field: "\000\001\377\201"` + "\n",
  303. },
  304. }
  305. for i, tc := range testCases {
  306. var buf bytes.Buffer
  307. if err := proto.MarshalText(&buf, tc.in); err != nil {
  308. t.Errorf("proto.MarsalText: %v", err)
  309. continue
  310. }
  311. s := buf.String()
  312. if s != tc.out {
  313. t.Errorf("#%d: Got:\n%s\nExpected:\n%s\n", i, s, tc.out)
  314. continue
  315. }
  316. // Check round-trip.
  317. pb := new(pb.Strings)
  318. if err := proto.UnmarshalText(s, pb); err != nil {
  319. t.Errorf("#%d: UnmarshalText: %v", i, err)
  320. continue
  321. }
  322. if !proto.Equal(pb, tc.in) {
  323. t.Errorf("#%d: Round-trip failed:\nstart: %v\n end: %v", i, tc.in, pb)
  324. }
  325. }
  326. }
  327. // A limitedWriter accepts some output before it fails.
  328. // This is a proxy for something like a nearly-full or imminently-failing disk,
  329. // or a network connection that is about to die.
  330. type limitedWriter struct {
  331. b bytes.Buffer
  332. limit int
  333. }
  334. var outOfSpace = errors.New("proto: insufficient space")
  335. func (w *limitedWriter) Write(p []byte) (n int, err error) {
  336. var avail = w.limit - w.b.Len()
  337. if avail <= 0 {
  338. return 0, outOfSpace
  339. }
  340. if len(p) <= avail {
  341. return w.b.Write(p)
  342. }
  343. n, _ = w.b.Write(p[:avail])
  344. return n, outOfSpace
  345. }
  346. func TestMarshalTextFailing(t *testing.T) {
  347. // Try lots of different sizes to exercise more error code-paths.
  348. for lim := 0; lim < len(text); lim++ {
  349. buf := new(limitedWriter)
  350. buf.limit = lim
  351. err := proto.MarshalText(buf, newTestMessage())
  352. // We expect a certain error, but also some partial results in the buffer.
  353. if err != outOfSpace {
  354. t.Errorf("Got:\n===\n%v===\nExpected:\n===\n%v===\n", err, outOfSpace)
  355. }
  356. s := buf.b.String()
  357. x := text[:buf.limit]
  358. if s != x {
  359. t.Errorf("Got:\n===\n%v===\nExpected:\n===\n%v===\n", s, x)
  360. }
  361. }
  362. }
  363. func TestFloats(t *testing.T) {
  364. tests := []struct {
  365. f float64
  366. want string
  367. }{
  368. {0, "0"},
  369. {4.7, "4.7"},
  370. {math.Inf(1), "inf"},
  371. {math.Inf(-1), "-inf"},
  372. {math.NaN(), "nan"},
  373. }
  374. for _, test := range tests {
  375. msg := &pb.FloatingPoint{F: &test.f}
  376. got := strings.TrimSpace(msg.String())
  377. want := `f:` + test.want
  378. if got != want {
  379. t.Errorf("f=%f: got %q, want %q", test.f, got, want)
  380. }
  381. }
  382. }
  383. func TestRepeatedNilText(t *testing.T) {
  384. m := &pb.MessageList{
  385. Message: []*pb.MessageList_Message{
  386. nil,
  387. &pb.MessageList_Message{
  388. Name: proto.String("Horse"),
  389. },
  390. nil,
  391. },
  392. }
  393. want := `Message <nil>
  394. Message {
  395. name: "Horse"
  396. }
  397. Message <nil>
  398. `
  399. if s := proto.MarshalTextString(m); s != want {
  400. t.Errorf(" got: %s\nwant: %s", s, want)
  401. }
  402. }
  403. func TestProto3Text(t *testing.T) {
  404. tests := []struct {
  405. m proto.Message
  406. want string
  407. }{
  408. // zero message
  409. {&proto3pb.Message{}, ``},
  410. // zero message except for an empty byte slice
  411. {&proto3pb.Message{Data: []byte{}}, ``},
  412. // trivial case
  413. {&proto3pb.Message{Name: "Rob", HeightInCm: 175}, `name:"Rob" height_in_cm:175`},
  414. // empty map
  415. {&pb.MessageWithMap{}, ``},
  416. // non-empty map; map format is the same as a repeated struct,
  417. // and they are sorted by key (numerically for numeric keys).
  418. {
  419. &pb.MessageWithMap{NameMapping: map[int32]string{
  420. -1: "Negatory",
  421. 7: "Lucky",
  422. 1234: "Feist",
  423. 6345789: "Otis",
  424. }},
  425. `name_mapping:<key:-1 value:"Negatory" > ` +
  426. `name_mapping:<key:7 value:"Lucky" > ` +
  427. `name_mapping:<key:1234 value:"Feist" > ` +
  428. `name_mapping:<key:6345789 value:"Otis" >`,
  429. },
  430. // map with nil value; not well-defined, but we shouldn't crash
  431. {
  432. &pb.MessageWithMap{MsgMapping: map[int64]*pb.FloatingPoint{7: nil}},
  433. `msg_mapping:<key:7 >`,
  434. },
  435. }
  436. for _, test := range tests {
  437. got := strings.TrimSpace(test.m.String())
  438. if got != test.want {
  439. t.Errorf("\n got %s\nwant %s", got, test.want)
  440. }
  441. }
  442. }