text_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 "./proto3_proto"
  41. pb "./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 BenchmarkMarshalTextBuffered(b *testing.B) {
  195. buf := new(bytes.Buffer)
  196. m := newTestMessage()
  197. for i := 0; i < b.N; i++ {
  198. buf.Reset()
  199. proto.MarshalText(buf, m)
  200. }
  201. }
  202. func BenchmarkMarshalTextUnbuffered(b *testing.B) {
  203. w := ioutil.Discard
  204. m := newTestMessage()
  205. for i := 0; i < b.N; i++ {
  206. proto.MarshalText(w, m)
  207. }
  208. }
  209. func compact(src string) string {
  210. // s/[ \n]+/ /g; s/ $//;
  211. dst := make([]byte, len(src))
  212. space, comment := false, false
  213. j := 0
  214. for i := 0; i < len(src); i++ {
  215. if strings.HasPrefix(src[i:], "/*") {
  216. comment = true
  217. i++
  218. continue
  219. }
  220. if comment && strings.HasPrefix(src[i:], "*/") {
  221. comment = false
  222. i++
  223. continue
  224. }
  225. if comment {
  226. continue
  227. }
  228. c := src[i]
  229. if c == ' ' || c == '\n' {
  230. space = true
  231. continue
  232. }
  233. if j > 0 && (dst[j-1] == ':' || dst[j-1] == '<' || dst[j-1] == '{') {
  234. space = false
  235. }
  236. if c == '{' {
  237. space = false
  238. }
  239. if space {
  240. dst[j] = ' '
  241. j++
  242. space = false
  243. }
  244. dst[j] = c
  245. j++
  246. }
  247. if space {
  248. dst[j] = ' '
  249. j++
  250. }
  251. return string(dst[0:j])
  252. }
  253. var compactText = compact(text)
  254. func TestCompactText(t *testing.T) {
  255. s := proto.CompactTextString(newTestMessage())
  256. if s != compactText {
  257. t.Errorf("Got:\n===\n%v===\nExpected:\n===\n%v\n===\n", s, compactText)
  258. }
  259. }
  260. func TestStringEscaping(t *testing.T) {
  261. testCases := []struct {
  262. in *pb.Strings
  263. out string
  264. }{
  265. {
  266. // Test data from C++ test (TextFormatTest.StringEscape).
  267. // Single divergence: we don't escape apostrophes.
  268. &pb.Strings{StringField: proto.String("\"A string with ' characters \n and \r newlines and \t tabs and \001 slashes \\ and multiple spaces")},
  269. "string_field: \"\\\"A string with ' characters \\n and \\r newlines and \\t tabs and \\001 slashes \\\\ and multiple spaces\"\n",
  270. },
  271. {
  272. // Test data from the same C++ test.
  273. &pb.Strings{StringField: proto.String("\350\260\267\346\255\214")},
  274. "string_field: \"\\350\\260\\267\\346\\255\\214\"\n",
  275. },
  276. {
  277. // Some UTF-8.
  278. &pb.Strings{StringField: proto.String("\x00\x01\xff\x81")},
  279. `string_field: "\000\001\377\201"` + "\n",
  280. },
  281. }
  282. for i, tc := range testCases {
  283. var buf bytes.Buffer
  284. if err := proto.MarshalText(&buf, tc.in); err != nil {
  285. t.Errorf("proto.MarsalText: %v", err)
  286. continue
  287. }
  288. s := buf.String()
  289. if s != tc.out {
  290. t.Errorf("#%d: Got:\n%s\nExpected:\n%s\n", i, s, tc.out)
  291. continue
  292. }
  293. // Check round-trip.
  294. pb := new(pb.Strings)
  295. if err := proto.UnmarshalText(s, pb); err != nil {
  296. t.Errorf("#%d: UnmarshalText: %v", i, err)
  297. continue
  298. }
  299. if !proto.Equal(pb, tc.in) {
  300. t.Errorf("#%d: Round-trip failed:\nstart: %v\n end: %v", i, tc.in, pb)
  301. }
  302. }
  303. }
  304. // A limitedWriter accepts some output before it fails.
  305. // This is a proxy for something like a nearly-full or imminently-failing disk,
  306. // or a network connection that is about to die.
  307. type limitedWriter struct {
  308. b bytes.Buffer
  309. limit int
  310. }
  311. var outOfSpace = errors.New("proto: insufficient space")
  312. func (w *limitedWriter) Write(p []byte) (n int, err error) {
  313. var avail = w.limit - w.b.Len()
  314. if avail <= 0 {
  315. return 0, outOfSpace
  316. }
  317. if len(p) <= avail {
  318. return w.b.Write(p)
  319. }
  320. n, _ = w.b.Write(p[:avail])
  321. return n, outOfSpace
  322. }
  323. func TestMarshalTextFailing(t *testing.T) {
  324. // Try lots of different sizes to exercise more error code-paths.
  325. for lim := 0; lim < len(text); lim++ {
  326. buf := new(limitedWriter)
  327. buf.limit = lim
  328. err := proto.MarshalText(buf, newTestMessage())
  329. // We expect a certain error, but also some partial results in the buffer.
  330. if err != outOfSpace {
  331. t.Errorf("Got:\n===\n%v===\nExpected:\n===\n%v===\n", err, outOfSpace)
  332. }
  333. s := buf.b.String()
  334. x := text[:buf.limit]
  335. if s != x {
  336. t.Errorf("Got:\n===\n%v===\nExpected:\n===\n%v===\n", s, x)
  337. }
  338. }
  339. }
  340. func TestFloats(t *testing.T) {
  341. tests := []struct {
  342. f float64
  343. want string
  344. }{
  345. {0, "0"},
  346. {4.7, "4.7"},
  347. {math.Inf(1), "inf"},
  348. {math.Inf(-1), "-inf"},
  349. {math.NaN(), "nan"},
  350. }
  351. for _, test := range tests {
  352. msg := &pb.FloatingPoint{F: &test.f}
  353. got := strings.TrimSpace(msg.String())
  354. want := `f:` + test.want
  355. if got != want {
  356. t.Errorf("f=%f: got %q, want %q", test.f, got, want)
  357. }
  358. }
  359. }
  360. func TestRepeatedNilText(t *testing.T) {
  361. m := &pb.MessageList{
  362. Message: []*pb.MessageList_Message{
  363. nil,
  364. &pb.MessageList_Message{
  365. Name: proto.String("Horse"),
  366. },
  367. nil,
  368. },
  369. }
  370. want := `Message <nil>
  371. Message {
  372. name: "Horse"
  373. }
  374. Message <nil>
  375. `
  376. if s := proto.MarshalTextString(m); s != want {
  377. t.Errorf(" got: %s\nwant: %s", s, want)
  378. }
  379. }
  380. func TestProto3Text(t *testing.T) {
  381. tests := []struct {
  382. m proto.Message
  383. want string
  384. }{
  385. // zero message
  386. {&proto3pb.Message{}, ``},
  387. // zero message except for an empty byte slice
  388. {&proto3pb.Message{Data: []byte{}}, ``},
  389. // trivial case
  390. {&proto3pb.Message{Name: "Rob", HeightInCm: 175}, `name:"Rob" height_in_cm:175`},
  391. // empty map
  392. {&pb.MessageWithMap{}, ``},
  393. // non-empty map; current map format is the same as a repeated struct
  394. {
  395. &pb.MessageWithMap{NameMapping: map[int32]string{1234: "Feist"}},
  396. `name_mapping:<key:1234 value:"Feist" >`,
  397. },
  398. }
  399. for _, test := range tests {
  400. got := strings.TrimSpace(test.m.String())
  401. if got != test.want {
  402. t.Errorf("\n got %s\nwant %s", got, test.want)
  403. }
  404. }
  405. }