text_test.go 9.7 KB

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