1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
package advrefs_test
import (
"bytes"
"strings"
"gopkg.in/src-d/go-git.v4/core"
"gopkg.in/src-d/go-git.v4/formats/packp"
"gopkg.in/src-d/go-git.v4/formats/packp/advrefs"
"gopkg.in/src-d/go-git.v4/formats/packp/pktline"
. "gopkg.in/check.v1"
)
type SuiteEncoder struct{}
var _ = Suite(&SuiteEncoder{})
// returns a byte slice with the pkt-lines for the given payloads.
func pktlines(c *C, payloads ...[]byte) []byte {
var buf bytes.Buffer
e := pktline.NewEncoder(&buf)
err := e.Encode(payloads...)
c.Assert(err, IsNil, Commentf("building pktlines for %v\n", payloads))
return buf.Bytes()
}
func testEncode(c *C, input *advrefs.AdvRefs, expected []byte) {
var buf bytes.Buffer
e := advrefs.NewEncoder(&buf)
err := e.Encode(input)
c.Assert(err, IsNil)
obtained := buf.Bytes()
comment := Commentf("\nobtained = %s\nexpected = %s\n", string(obtained), string(expected))
c.Assert(obtained, DeepEquals, expected, comment)
}
func (s *SuiteEncoder) TestZeroValue(c *C) {
ar := &advrefs.AdvRefs{}
expected := pktlines(c,
[]byte("0000000000000000000000000000000000000000 capabilities^{}\x00\n"),
pktline.Flush,
)
testEncode(c, ar, expected)
}
func (s *SuiteEncoder) TestHead(c *C) {
hash := core.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
ar := &advrefs.AdvRefs{
Head: &hash,
}
expected := pktlines(c,
[]byte("6ecf0ef2c2dffb796033e5a02219af86ec6584e5 HEAD\x00\n"),
pktline.Flush,
)
testEncode(c, ar, expected)
}
func (s *SuiteEncoder) TestCapsNoHead(c *C) {
capabilities := packp.NewCapabilities()
capabilities.Add("symref", "HEAD:/refs/heads/master")
capabilities.Add("ofs-delta")
capabilities.Add("multi_ack")
ar := &advrefs.AdvRefs{
Capabilities: capabilities,
}
expected := pktlines(c,
[]byte("0000000000000000000000000000000000000000 capabilities^{}\x00multi_ack ofs-delta symref=HEAD:/refs/heads/master\n"),
pktline.Flush,
)
testEncode(c, ar, expected)
}
func (s *SuiteEncoder) TestCapsWithHead(c *C) {
hash := core.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
capabilities := packp.NewCapabilities()
capabilities.Add("symref", "HEAD:/refs/heads/master")
capabilities.Add("ofs-delta")
capabilities.Add("multi_ack")
ar := &advrefs.AdvRefs{
Head: &hash,
Capabilities: capabilities,
}
expected := pktlines(c,
[]byte("6ecf0ef2c2dffb796033e5a02219af86ec6584e5 HEAD\x00multi_ack ofs-delta symref=HEAD:/refs/heads/master\n"),
pktline.Flush,
)
testEncode(c, ar, expected)
}
func (s *SuiteEncoder) TestRefs(c *C) {
references := map[string]core.Hash{
"refs/heads/master": core.NewHash("a6930aaee06755d1bdcfd943fbf614e4d92bb0c7"),
"refs/tags/v2.6.12-tree": core.NewHash("1111111111111111111111111111111111111111"),
"refs/tags/v2.7.13-tree": core.NewHash("3333333333333333333333333333333333333333"),
"refs/tags/v2.6.13-tree": core.NewHash("2222222222222222222222222222222222222222"),
"refs/tags/v2.6.11-tree": core.NewHash("5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c"),
}
ar := &advrefs.AdvRefs{
References: references,
}
expected := pktlines(c,
[]byte("0000000000000000000000000000000000000000 capabilities^{}\x00\n"),
[]byte("a6930aaee06755d1bdcfd943fbf614e4d92bb0c7 refs/heads/master\n"),
[]byte("5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11-tree\n"),
[]byte("1111111111111111111111111111111111111111 refs/tags/v2.6.12-tree\n"),
[]byte("2222222222222222222222222222222222222222 refs/tags/v2.6.13-tree\n"),
[]byte("3333333333333333333333333333333333333333 refs/tags/v2.7.13-tree\n"),
pktline.Flush,
)
testEncode(c, ar, expected)
}
func (s *SuiteEncoder) TestPeeled(c *C) {
references := map[string]core.Hash{
"refs/heads/master": core.NewHash("a6930aaee06755d1bdcfd943fbf614e4d92bb0c7"),
"refs/tags/v2.6.12-tree": core.NewHash("1111111111111111111111111111111111111111"),
"refs/tags/v2.7.13-tree": core.NewHash("3333333333333333333333333333333333333333"),
"refs/tags/v2.6.13-tree": core.NewHash("2222222222222222222222222222222222222222"),
"refs/tags/v2.6.11-tree": core.NewHash("5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c"),
}
peeled := map[string]core.Hash{
"refs/tags/v2.7.13-tree": core.NewHash("4444444444444444444444444444444444444444"),
"refs/tags/v2.6.12-tree": core.NewHash("5555555555555555555555555555555555555555"),
}
ar := &advrefs.AdvRefs{
References: references,
Peeled: peeled,
}
expected := pktlines(c,
[]byte("0000000000000000000000000000000000000000 capabilities^{}\x00\n"),
[]byte("a6930aaee06755d1bdcfd943fbf614e4d92bb0c7 refs/heads/master\n"),
[]byte("5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11-tree\n"),
[]byte("1111111111111111111111111111111111111111 refs/tags/v2.6.12-tree\n"),
[]byte("5555555555555555555555555555555555555555 refs/tags/v2.6.12-tree^{}\n"),
[]byte("2222222222222222222222222222222222222222 refs/tags/v2.6.13-tree\n"),
[]byte("3333333333333333333333333333333333333333 refs/tags/v2.7.13-tree\n"),
[]byte("4444444444444444444444444444444444444444 refs/tags/v2.7.13-tree^{}\n"),
pktline.Flush,
)
testEncode(c, ar, expected)
}
func (s *SuiteEncoder) TestShallow(c *C) {
shallows := []core.Hash{
core.NewHash("1111111111111111111111111111111111111111"),
core.NewHash("4444444444444444444444444444444444444444"),
core.NewHash("3333333333333333333333333333333333333333"),
core.NewHash("2222222222222222222222222222222222222222"),
}
ar := &advrefs.AdvRefs{
Shallows: shallows,
}
expected := pktlines(c,
[]byte("0000000000000000000000000000000000000000 capabilities^{}\x00\n"),
[]byte("shallow 1111111111111111111111111111111111111111\n"),
[]byte("shallow 2222222222222222222222222222222222222222\n"),
[]byte("shallow 3333333333333333333333333333333333333333\n"),
[]byte("shallow 4444444444444444444444444444444444444444\n"),
pktline.Flush,
)
testEncode(c, ar, expected)
}
func (s *SuiteEncoder) TestAll(c *C) {
hash := core.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
capabilities := packp.NewCapabilities()
capabilities.Add("symref", "HEAD:/refs/heads/master")
capabilities.Add("ofs-delta")
capabilities.Add("multi_ack")
references := map[string]core.Hash{
"refs/heads/master": core.NewHash("a6930aaee06755d1bdcfd943fbf614e4d92bb0c7"),
"refs/tags/v2.6.12-tree": core.NewHash("1111111111111111111111111111111111111111"),
"refs/tags/v2.7.13-tree": core.NewHash("3333333333333333333333333333333333333333"),
"refs/tags/v2.6.13-tree": core.NewHash("2222222222222222222222222222222222222222"),
"refs/tags/v2.6.11-tree": core.NewHash("5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c"),
}
peeled := map[string]core.Hash{
"refs/tags/v2.7.13-tree": core.NewHash("4444444444444444444444444444444444444444"),
"refs/tags/v2.6.12-tree": core.NewHash("5555555555555555555555555555555555555555"),
}
shallows := []core.Hash{
core.NewHash("1111111111111111111111111111111111111111"),
core.NewHash("4444444444444444444444444444444444444444"),
core.NewHash("3333333333333333333333333333333333333333"),
core.NewHash("2222222222222222222222222222222222222222"),
}
ar := &advrefs.AdvRefs{
Head: &hash,
Capabilities: capabilities,
References: references,
Peeled: peeled,
Shallows: shallows,
}
expected := pktlines(c,
[]byte("6ecf0ef2c2dffb796033e5a02219af86ec6584e5 HEAD\x00multi_ack ofs-delta symref=HEAD:/refs/heads/master\n"),
[]byte("a6930aaee06755d1bdcfd943fbf614e4d92bb0c7 refs/heads/master\n"),
[]byte("5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11-tree\n"),
[]byte("1111111111111111111111111111111111111111 refs/tags/v2.6.12-tree\n"),
[]byte("5555555555555555555555555555555555555555 refs/tags/v2.6.12-tree^{}\n"),
[]byte("2222222222222222222222222222222222222222 refs/tags/v2.6.13-tree\n"),
[]byte("3333333333333333333333333333333333333333 refs/tags/v2.7.13-tree\n"),
[]byte("4444444444444444444444444444444444444444 refs/tags/v2.7.13-tree^{}\n"),
[]byte("shallow 1111111111111111111111111111111111111111\n"),
[]byte("shallow 2222222222222222222222222222222222222222\n"),
[]byte("shallow 3333333333333333333333333333333333333333\n"),
[]byte("shallow 4444444444444444444444444444444444444444\n"),
pktline.Flush,
)
testEncode(c, ar, expected)
}
func (s *SuiteEncoder) TestErrorTooLong(c *C) {
references := map[string]core.Hash{
strings.Repeat("a", pktline.MaxPayloadSize): core.NewHash("a6930aaee06755d1bdcfd943fbf614e4d92bb0c7"),
}
ar := &advrefs.AdvRefs{
References: references,
}
var buf bytes.Buffer
e := advrefs.NewEncoder(&buf)
err := e.Encode(ar)
c.Assert(err, ErrorMatches, ".*payload is too long.*")
}
|