| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1 | // Go support for Protocol Buffers - Google's data interchange format |
| 2 | // |
| 3 | // Copyright 2010 Google Inc. 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 | |
| 32 | package proto_test |
| 33 | |
| 34 | import ( |
| 35 | "bytes" |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 36 | "strings" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 37 | "testing" |
| David Symonds | aa922ff | 2011-07-19 14:58:06 +1000 | [diff] [blame] | 38 | |
| Rob Pike | 3f6f2d8 | 2011-12-18 13:55:35 -0800 | [diff] [blame] | 39 | "code.google.com/p/goprotobuf/proto" |
| David Symonds | aa922ff | 2011-07-19 14:58:06 +1000 | [diff] [blame] | 40 | |
| David Symonds | 704096f | 2012-03-15 15:10:26 +1100 | [diff] [blame] | 41 | pb "./testdata" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 42 | ) |
| 43 | |
| David Symonds | aa922ff | 2011-07-19 14:58:06 +1000 | [diff] [blame] | 44 | func newTestMessage() *pb.MyMessage { |
| 45 | msg := &pb.MyMessage{ |
| 46 | Count: proto.Int32(42), |
| 47 | Name: proto.String("Dave"), |
| 48 | Quote: proto.String(`"I didn't want to go."`), |
| Rob Pike | 9caa5b9 | 2010-05-11 16:04:57 -0700 | [diff] [blame] | 49 | Pet: []string{"bunny", "kitty", "horsey"}, |
| David Symonds | aa922ff | 2011-07-19 14:58:06 +1000 | [diff] [blame] | 50 | Inner: &pb.InnerMessage{ |
| 51 | Host: proto.String("footrest.syd"), |
| 52 | Port: proto.Int32(7001), |
| 53 | Connected: proto.Bool(true), |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 54 | }, |
| David Symonds | aa922ff | 2011-07-19 14:58:06 +1000 | [diff] [blame] | 55 | Others: []*pb.OtherMessage{ |
| David Symonds | 92dd6c1 | 2012-03-23 10:59:49 +1100 | [diff] [blame^] | 56 | { |
| David Symonds | aa922ff | 2011-07-19 14:58:06 +1000 | [diff] [blame] | 57 | Key: proto.Int64(0xdeadbeef), |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 58 | Value: []byte{1, 65, 7, 12}, |
| 59 | }, |
| David Symonds | 92dd6c1 | 2012-03-23 10:59:49 +1100 | [diff] [blame^] | 60 | { |
| David Symonds | aa922ff | 2011-07-19 14:58:06 +1000 | [diff] [blame] | 61 | Weight: proto.Float32(6.022), |
| 62 | Inner: &pb.InnerMessage{ |
| 63 | Host: proto.String("lesha.mtv"), |
| 64 | Port: proto.Int32(8002), |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 65 | }, |
| 66 | }, |
| 67 | }, |
| David Symonds | aa922ff | 2011-07-19 14:58:06 +1000 | [diff] [blame] | 68 | Bikeshed: pb.NewMyMessage_Color(pb.MyMessage_BLUE), |
| 69 | Somegroup: &pb.MyMessage_SomeGroup{ |
| 70 | GroupField: proto.Int32(8), |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 71 | }, |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 72 | // One normally wouldn't do this. |
| 73 | // This is an undeclared tag 13, as a varint (wire type 0) with value 4. |
| 74 | XXX_unrecognized: []byte{13<<3 | 0, 4}, |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 75 | } |
| David Symonds | aa922ff | 2011-07-19 14:58:06 +1000 | [diff] [blame] | 76 | ext := &pb.Ext{ |
| 77 | Data: proto.String("Big gobs for big rats"), |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 78 | } |
| David Symonds | aa922ff | 2011-07-19 14:58:06 +1000 | [diff] [blame] | 79 | if err := proto.SetExtension(msg, pb.E_Ext_More, ext); err != nil { |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 80 | panic(err) |
| 81 | } |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 82 | |
| 83 | // Add an unknown extension. We marshal a pb.Ext, and fake the ID. |
| 84 | b, err := proto.Marshal(&pb.Ext{Data: proto.String("3G skiing")}) |
| 85 | if err != nil { |
| 86 | panic(err) |
| 87 | } |
| David Symonds | 5453105 | 2011-12-08 12:00:31 +1100 | [diff] [blame] | 88 | b = append(proto.EncodeVarint(201<<3|proto.WireBytes), b...) |
| 89 | proto.SetRawExtension(msg, 201, b) |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 90 | |
| 91 | // Extensions can be plain fields, too, so let's test that. |
| David Symonds | 5453105 | 2011-12-08 12:00:31 +1100 | [diff] [blame] | 92 | b = append(proto.EncodeVarint(202<<3|proto.WireVarint), 19) |
| 93 | proto.SetRawExtension(msg, 202, b) |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 94 | |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 95 | return msg |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | const text = `count: 42 |
| 99 | name: "Dave" |
| 100 | quote: "\"I didn't want to go.\"" |
| 101 | pet: "bunny" |
| 102 | pet: "kitty" |
| 103 | pet: "horsey" |
| 104 | inner: < |
| 105 | host: "footrest.syd" |
| 106 | port: 7001 |
| 107 | connected: true |
| 108 | > |
| 109 | others: < |
| 110 | key: 3735928559 |
| David Symonds | 4c95bfe | 2011-09-13 14:43:27 +1000 | [diff] [blame] | 111 | value: "\001A\007\014" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 112 | > |
| 113 | others: < |
| 114 | weight: 6.022 |
| 115 | inner: < |
| 116 | host: "lesha.mtv" |
| 117 | port: 8002 |
| 118 | > |
| 119 | > |
| 120 | bikeshed: BLUE |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 121 | SomeGroup { |
| 122 | group_field: 8 |
| 123 | } |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 124 | /* 2 unknown bytes */ |
| 125 | tag13: 4 |
| Rob Pike | b7907bf | 2012-02-13 14:25:20 +1100 | [diff] [blame] | 126 | [testdata.Ext.more]: < |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 127 | data: "Big gobs for big rats" |
| 128 | > |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 129 | /* 13 unknown bytes */ |
| David Symonds | 5453105 | 2011-12-08 12:00:31 +1100 | [diff] [blame] | 130 | tag201: "\t3G skiing" |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 131 | /* 3 unknown bytes */ |
| David Symonds | 5453105 | 2011-12-08 12:00:31 +1100 | [diff] [blame] | 132 | tag202: 19 |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 133 | ` |
| 134 | |
| 135 | func TestMarshalTextFull(t *testing.T) { |
| 136 | buf := new(bytes.Buffer) |
| David Symonds | aa922ff | 2011-07-19 14:58:06 +1000 | [diff] [blame] | 137 | proto.MarshalText(buf, newTestMessage()) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 138 | s := buf.String() |
| 139 | if s != text { |
| 140 | t.Errorf("Got:\n===\n%v===\nExpected:\n===\n%v===\n", s, text) |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | func compact(src string) string { |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 145 | // s/[ \n]+/ /g; s/ $//; |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 146 | dst := make([]byte, len(src)) |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 147 | space, comment := false, false |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 148 | j := 0 |
| 149 | for i := 0; i < len(src); i++ { |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 150 | if strings.HasPrefix(src[i:], "/*") { |
| 151 | comment = true |
| 152 | i++ |
| 153 | continue |
| 154 | } |
| 155 | if comment && strings.HasPrefix(src[i:], "*/") { |
| 156 | comment = false |
| 157 | i++ |
| 158 | continue |
| 159 | } |
| 160 | if comment { |
| 161 | continue |
| 162 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 163 | c := src[i] |
| 164 | if c == ' ' || c == '\n' { |
| 165 | space = true |
| 166 | continue |
| 167 | } |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 168 | if j > 0 && (dst[j-1] == ':' || dst[j-1] == '<' || dst[j-1] == '{') { |
| 169 | space = false |
| 170 | } |
| 171 | if c == '{' { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 172 | space = false |
| 173 | } |
| 174 | if space { |
| 175 | dst[j] = ' ' |
| 176 | j++ |
| 177 | space = false |
| 178 | } |
| 179 | dst[j] = c |
| 180 | j++ |
| 181 | } |
| 182 | if space { |
| 183 | dst[j] = ' ' |
| 184 | j++ |
| 185 | } |
| 186 | return string(dst[0:j]) |
| 187 | } |
| 188 | |
| 189 | var compactText = compact(text) |
| 190 | |
| 191 | func TestCompactText(t *testing.T) { |
| David Symonds | aa922ff | 2011-07-19 14:58:06 +1000 | [diff] [blame] | 192 | s := proto.CompactTextString(newTestMessage()) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 193 | if s != compactText { |
| David Symonds | 4c95bfe | 2011-09-13 14:43:27 +1000 | [diff] [blame] | 194 | t.Errorf("Got:\n===\n%v===\nExpected:\n===\n%v\n===\n", s, compactText) |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | func TestStringEscaping(t *testing.T) { |
| 199 | testCases := []struct { |
| 200 | in *pb.Strings |
| 201 | out string |
| 202 | }{ |
| 203 | { |
| 204 | // Test data from C++ test (TextFormatTest.StringEscape). |
| 205 | // Single divergence: we don't escape apostrophes. |
| 206 | &pb.Strings{StringField: proto.String("\"A string with ' characters \n and \r newlines and \t tabs and \001 slashes \\ and multiple spaces")}, |
| 207 | "string_field: \"\\\"A string with ' characters \\n and \\r newlines and \\t tabs and \\001 slashes \\\\ and multiple spaces\"\n", |
| 208 | }, |
| 209 | { |
| 210 | // Test data from the same C++ test. |
| 211 | &pb.Strings{StringField: proto.String("\350\260\267\346\255\214")}, |
| 212 | "string_field: \"\\350\\260\\267\\346\\255\\214\"\n", |
| 213 | }, |
| 214 | } |
| 215 | |
| 216 | for i, tc := range testCases { |
| 217 | var buf bytes.Buffer |
| 218 | proto.MarshalText(&buf, tc.in) |
| 219 | if s := buf.String(); s != tc.out { |
| 220 | t.Errorf("#%d: Got:\n%s\nExpected:\n%s\n", i, s, tc.out) |
| 221 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 222 | } |
| 223 | } |
| David Symonds | 92dd6c1 | 2012-03-23 10:59:49 +1100 | [diff] [blame^] | 224 | |
| 225 | func TestNonPtrMessage(t *testing.T) { |
| 226 | // Ensure we don't panic when we pass a non-pointer to MarshalText. |
| 227 | var buf bytes.Buffer |
| 228 | proto.MarshalText(&buf, pb.MyMessage{}) |
| 229 | if s := buf.String(); s != "<struct-by-value>" { |
| 230 | t.Errorf("got: %q, want %q", s, "<struct-by-value>") |
| 231 | } |
| 232 | } |