text_test.go 8.9 KB

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