| 1 |
package packp |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"runtime" |
| 6 |
"time" |
| 7 |
|
| 8 |
"github.com/go-git/go-git/v5/plumbing" |
| 9 |
"github.com/go-git/go-git/v5/plumbing/format/pktline" |
| 10 |
"github.com/go-git/go-git/v5/plumbing/protocol/packp/capability" |
| 11 |
|
| 12 |
. "gopkg.in/check.v1" |
| 13 |
) |
| 14 |
|
| 15 |
type UlReqEncodeSuite struct{} |
| 16 |
|
| 17 |
var _ = Suite(&UlReqEncodeSuite{}) |
| 18 |
|
| 19 |
func testUlReqEncode(c *C, ur *UploadRequest, expectedPayloads []string) { |
| 20 |
var buf bytes.Buffer |
| 21 |
e := newUlReqEncoder(&buf) |
| 22 |
|
| 23 |
err := e.Encode(ur) |
| 24 |
c.Assert(err, IsNil) |
| 25 |
obtained := buf.Bytes() |
| 26 |
|
| 27 |
expected := pktlines(c, expectedPayloads...) |
| 28 |
|
| 29 |
comment := Commentf("\nobtained = %s\nexpected = %s\n", string(obtained), string(expected)) |
| 30 |
|
| 31 |
c.Assert(obtained, DeepEquals, expected, comment) |
| 32 |
} |
| 33 |
|
| 34 |
func testUlReqEncodeError(c *C, ur *UploadRequest, expectedErrorRegEx string) { |
| 35 |
var buf bytes.Buffer |
| 36 |
e := newUlReqEncoder(&buf) |
| 37 |
|
| 38 |
err := e.Encode(ur) |
| 39 |
c.Assert(err, ErrorMatches, expectedErrorRegEx) |
| 40 |
} |
| 41 |
|
| 42 |
func (s *UlReqEncodeSuite) TestZeroValue(c *C) { |
| 43 |
ur := NewUploadRequest() |
| 44 |
expectedErrorRegEx := ".*empty wants.*" |
| 45 |
|
| 46 |
testUlReqEncodeError(c, ur, expectedErrorRegEx) |
| 47 |
} |
| 48 |
|
| 49 |
func (s *UlReqEncodeSuite) TestOneWant(c *C) { |
| 50 |
ur := NewUploadRequest() |
| 51 |
ur.Wants = append(ur.Wants, plumbing.NewHash("1111111111111111111111111111111111111111")) |
| 52 |
|
| 53 |
expected := []string{ |
| 54 |
"want 1111111111111111111111111111111111111111\n", |
| 55 |
pktline.FlushString, |
| 56 |
} |
| 57 |
|
| 58 |
testUlReqEncode(c, ur, expected) |
| 59 |
} |
| 60 |
|
| 61 |
func (s *UlReqEncodeSuite) TestOneWantWithCapabilities(c *C) { |
| 62 |
ur := NewUploadRequest() |
| 63 |
ur.Wants = append(ur.Wants, plumbing.NewHash("1111111111111111111111111111111111111111")) |
| 64 |
ur.Capabilities.Add(capability.MultiACK) |
| 65 |
ur.Capabilities.Add(capability.OFSDelta) |
| 66 |
ur.Capabilities.Add(capability.Sideband) |
| 67 |
ur.Capabilities.Add(capability.SymRef, "HEAD:/refs/heads/master") |
| 68 |
ur.Capabilities.Add(capability.ThinPack) |
| 69 |
|
| 70 |
expected := []string{ |
| 71 |
"want 1111111111111111111111111111111111111111 multi_ack ofs-delta side-band symref=HEAD:/refs/heads/master thin-pack\n", |
| 72 |
pktline.FlushString, |
| 73 |
} |
| 74 |
|
| 75 |
testUlReqEncode(c, ur, expected) |
| 76 |
} |
| 77 |
|
| 78 |
func (s *UlReqEncodeSuite) TestWants(c *C) { |
| 79 |
ur := NewUploadRequest() |
| 80 |
ur.Wants = append(ur.Wants, |
| 81 |
plumbing.NewHash("4444444444444444444444444444444444444444"), |
| 82 |
plumbing.NewHash("1111111111111111111111111111111111111111"), |
| 83 |
plumbing.NewHash("3333333333333333333333333333333333333333"), |
| 84 |
plumbing.NewHash("2222222222222222222222222222222222222222"), |
| 85 |
plumbing.NewHash("5555555555555555555555555555555555555555"), |
| 86 |
) |
| 87 |
|
| 88 |
expected := []string{ |
| 89 |
"want 1111111111111111111111111111111111111111\n", |
| 90 |
"want 2222222222222222222222222222222222222222\n", |
| 91 |
"want 3333333333333333333333333333333333333333\n", |
| 92 |
"want 4444444444444444444444444444444444444444\n", |
| 93 |
"want 5555555555555555555555555555555555555555\n", |
| 94 |
pktline.FlushString, |
| 95 |
} |
| 96 |
|
| 97 |
testUlReqEncode(c, ur, expected) |
| 98 |
} |
| 99 |
|
| 100 |
func (s *UlReqEncodeSuite) TestWantsDuplicates(c *C) { |
| 101 |
ur := NewUploadRequest() |
| 102 |
ur.Wants = append(ur.Wants, |
| 103 |
plumbing.NewHash("4444444444444444444444444444444444444444"), |
| 104 |
plumbing.NewHash("1111111111111111111111111111111111111111"), |
| 105 |
plumbing.NewHash("3333333333333333333333333333333333333333"), |
| 106 |
plumbing.NewHash("1111111111111111111111111111111111111111"), |
| 107 |
plumbing.NewHash("2222222222222222222222222222222222222222"), |
| 108 |
plumbing.NewHash("1111111111111111111111111111111111111111"), |
| 109 |
) |
| 110 |
|
| 111 |
expected := []string{ |
| 112 |
"want 1111111111111111111111111111111111111111\n", |
| 113 |
"want 2222222222222222222222222222222222222222\n", |
| 114 |
"want 3333333333333333333333333333333333333333\n", |
| 115 |
"want 4444444444444444444444444444444444444444\n", |
| 116 |
pktline.FlushString, |
| 117 |
} |
| 118 |
|
| 119 |
testUlReqEncode(c, ur, expected) |
| 120 |
} |
| 121 |
|
| 122 |
func (s *UlReqEncodeSuite) TestWantsWithCapabilities(c *C) { |
| 123 |
ur := NewUploadRequest() |
| 124 |
ur.Wants = append(ur.Wants, |
| 125 |
plumbing.NewHash("4444444444444444444444444444444444444444"), |
| 126 |
plumbing.NewHash("1111111111111111111111111111111111111111"), |
| 127 |
plumbing.NewHash("3333333333333333333333333333333333333333"), |
| 128 |
plumbing.NewHash("2222222222222222222222222222222222222222"), |
| 129 |
plumbing.NewHash("5555555555555555555555555555555555555555"), |
| 130 |
) |
| 131 |
|
| 132 |
ur.Capabilities.Add(capability.MultiACK) |
| 133 |
ur.Capabilities.Add(capability.OFSDelta) |
| 134 |
ur.Capabilities.Add(capability.Sideband) |
| 135 |
ur.Capabilities.Add(capability.SymRef, "HEAD:/refs/heads/master") |
| 136 |
ur.Capabilities.Add(capability.ThinPack) |
| 137 |
|
| 138 |
expected := []string{ |
| 139 |
"want 1111111111111111111111111111111111111111 multi_ack ofs-delta side-band symref=HEAD:/refs/heads/master thin-pack\n", |
| 140 |
"want 2222222222222222222222222222222222222222\n", |
| 141 |
"want 3333333333333333333333333333333333333333\n", |
| 142 |
"want 4444444444444444444444444444444444444444\n", |
| 143 |
"want 5555555555555555555555555555555555555555\n", |
| 144 |
pktline.FlushString, |
| 145 |
} |
| 146 |
|
| 147 |
testUlReqEncode(c, ur, expected) |
| 148 |
} |
| 149 |
|
| 150 |
func (s *UlReqEncodeSuite) TestShallow(c *C) { |
| 151 |
ur := NewUploadRequest() |
| 152 |
ur.Wants = append(ur.Wants, plumbing.NewHash("1111111111111111111111111111111111111111")) |
| 153 |
ur.Capabilities.Add(capability.MultiACK) |
| 154 |
ur.Shallows = append(ur.Shallows, plumbing.NewHash("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")) |
| 155 |
|
| 156 |
expected := []string{ |
| 157 |
"want 1111111111111111111111111111111111111111 multi_ack\n", |
| 158 |
"shallow aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n", |
| 159 |
pktline.FlushString, |
| 160 |
} |
| 161 |
|
| 162 |
testUlReqEncode(c, ur, expected) |
| 163 |
} |
| 164 |
|
| 165 |
func (s *UlReqEncodeSuite) TestManyShallows(c *C) { |
| 166 |
ur := NewUploadRequest() |
| 167 |
ur.Wants = append(ur.Wants, plumbing.NewHash("1111111111111111111111111111111111111111")) |
| 168 |
ur.Capabilities.Add(capability.MultiACK) |
| 169 |
ur.Shallows = append(ur.Shallows, |
| 170 |
plumbing.NewHash("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"), |
| 171 |
plumbing.NewHash("dddddddddddddddddddddddddddddddddddddddd"), |
| 172 |
plumbing.NewHash("cccccccccccccccccccccccccccccccccccccccc"), |
| 173 |
plumbing.NewHash("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"), |
| 174 |
) |
| 175 |
|
| 176 |
expected := []string{ |
| 177 |
"want 1111111111111111111111111111111111111111 multi_ack\n", |
| 178 |
"shallow aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n", |
| 179 |
"shallow bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n", |
| 180 |
"shallow cccccccccccccccccccccccccccccccccccccccc\n", |
| 181 |
"shallow dddddddddddddddddddddddddddddddddddddddd\n", |
| 182 |
pktline.FlushString, |
| 183 |
} |
| 184 |
|
| 185 |
testUlReqEncode(c, ur, expected) |
| 186 |
} |
| 187 |
|
| 188 |
func (s *UlReqEncodeSuite) TestShallowsDuplicate(c *C) { |
| 189 |
ur := NewUploadRequest() |
| 190 |
ur.Wants = append(ur.Wants, plumbing.NewHash("1111111111111111111111111111111111111111")) |
| 191 |
ur.Capabilities.Add(capability.MultiACK) |
| 192 |
ur.Shallows = append(ur.Shallows, |
| 193 |
plumbing.NewHash("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"), |
| 194 |
plumbing.NewHash("cccccccccccccccccccccccccccccccccccccccc"), |
| 195 |
plumbing.NewHash("cccccccccccccccccccccccccccccccccccccccc"), |
| 196 |
plumbing.NewHash("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"), |
| 197 |
) |
| 198 |
|
| 199 |
expected := []string{ |
| 200 |
"want 1111111111111111111111111111111111111111 multi_ack\n", |
| 201 |
"shallow aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n", |
| 202 |
"shallow bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n", |
| 203 |
"shallow cccccccccccccccccccccccccccccccccccccccc\n", |
| 204 |
pktline.FlushString, |
| 205 |
} |
| 206 |
|
| 207 |
testUlReqEncode(c, ur, expected) |
| 208 |
} |
| 209 |
|
| 210 |
func (s *UlReqEncodeSuite) TestDepthCommits(c *C) { |
| 211 |
ur := NewUploadRequest() |
| 212 |
ur.Wants = append(ur.Wants, plumbing.NewHash("1111111111111111111111111111111111111111")) |
| 213 |
ur.Depth = DepthCommits(1234) |
| 214 |
|
| 215 |
expected := []string{ |
| 216 |
"want 1111111111111111111111111111111111111111\n", |
| 217 |
"deepen 1234\n", |
| 218 |
pktline.FlushString, |
| 219 |
} |
| 220 |
|
| 221 |
testUlReqEncode(c, ur, expected) |
| 222 |
} |
| 223 |
|
| 224 |
func (s *UlReqEncodeSuite) TestDepthSinceUTC(c *C) { |
| 225 |
ur := NewUploadRequest() |
| 226 |
ur.Wants = append(ur.Wants, plumbing.NewHash("1111111111111111111111111111111111111111")) |
| 227 |
since := time.Date(2015, time.January, 2, 3, 4, 5, 0, time.UTC) |
| 228 |
ur.Depth = DepthSince(since) |
| 229 |
|
| 230 |
expected := []string{ |
| 231 |
"want 1111111111111111111111111111111111111111\n", |
| 232 |
"deepen-since 1420167845\n", |
| 233 |
pktline.FlushString, |
| 234 |
} |
| 235 |
|
| 236 |
testUlReqEncode(c, ur, expected) |
| 237 |
} |
| 238 |
|
| 239 |
func (s *UlReqEncodeSuite) TestDepthSinceNonUTC(c *C) { |
| 240 |
if runtime.GOOS == "js" { |
| 241 |
c.Skip("time.LoadLocation not supported in wasm") |
| 242 |
} |
| 243 |
|
| 244 |
ur := NewUploadRequest() |
| 245 |
ur.Wants = append(ur.Wants, plumbing.NewHash("1111111111111111111111111111111111111111")) |
| 246 |
berlin, err := time.LoadLocation("Europe/Berlin") |
| 247 |
c.Assert(err, IsNil) |
| 248 |
since := time.Date(2015, time.January, 2, 3, 4, 5, 0, berlin) |
| 249 |
// since value is 2015-01-02 03:04:05 +0100 UTC (Europe/Berlin) or |
| 250 |
// 2015-01-02 02:04:05 +0000 UTC, which is 1420164245 Unix seconds. |
| 251 |
ur.Depth = DepthSince(since) |
| 252 |
|
| 253 |
expected := []string{ |
| 254 |
"want 1111111111111111111111111111111111111111\n", |
| 255 |
"deepen-since 1420164245\n", |
| 256 |
pktline.FlushString, |
| 257 |
} |
| 258 |
|
| 259 |
testUlReqEncode(c, ur, expected) |
| 260 |
} |
| 261 |
|
| 262 |
func (s *UlReqEncodeSuite) TestDepthReference(c *C) { |
| 263 |
ur := NewUploadRequest() |
| 264 |
ur.Wants = append(ur.Wants, plumbing.NewHash("1111111111111111111111111111111111111111")) |
| 265 |
ur.Depth = DepthReference("refs/heads/feature-foo") |
| 266 |
|
| 267 |
expected := []string{ |
| 268 |
"want 1111111111111111111111111111111111111111\n", |
| 269 |
"deepen-not refs/heads/feature-foo\n", |
| 270 |
pktline.FlushString, |
| 271 |
} |
| 272 |
|
| 273 |
testUlReqEncode(c, ur, expected) |
| 274 |
} |
| 275 |
|
| 276 |
func (s *UlReqEncodeSuite) TestFilter(c *C) { |
| 277 |
ur := NewUploadRequest() |
| 278 |
ur.Wants = append(ur.Wants, plumbing.NewHash("1111111111111111111111111111111111111111")) |
| 279 |
ur.Filter = FilterTreeDepth(0) |
| 280 |
|
| 281 |
expected := []string{ |
| 282 |
"want 1111111111111111111111111111111111111111\n", |
| 283 |
"filter tree:0\n", |
| 284 |
pktline.FlushString, |
| 285 |
} |
| 286 |
|
| 287 |
testUlReqEncode(c, ur, expected) |
| 288 |
} |
| 289 |
|
| 290 |
func (s *UlReqEncodeSuite) TestAll(c *C) { |
| 291 |
ur := NewUploadRequest() |
| 292 |
ur.Wants = append(ur.Wants, |
| 293 |
plumbing.NewHash("4444444444444444444444444444444444444444"), |
| 294 |
plumbing.NewHash("1111111111111111111111111111111111111111"), |
| 295 |
plumbing.NewHash("3333333333333333333333333333333333333333"), |
| 296 |
plumbing.NewHash("2222222222222222222222222222222222222222"), |
| 297 |
plumbing.NewHash("5555555555555555555555555555555555555555"), |
| 298 |
) |
| 299 |
|
| 300 |
ur.Capabilities.Add(capability.MultiACK) |
| 301 |
ur.Capabilities.Add(capability.OFSDelta) |
| 302 |
ur.Capabilities.Add(capability.Sideband) |
| 303 |
ur.Capabilities.Add(capability.SymRef, "HEAD:/refs/heads/master") |
| 304 |
ur.Capabilities.Add(capability.ThinPack) |
| 305 |
|
| 306 |
ur.Shallows = append(ur.Shallows, plumbing.NewHash("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")) |
| 307 |
ur.Shallows = append(ur.Shallows, plumbing.NewHash("dddddddddddddddddddddddddddddddddddddddd")) |
| 308 |
ur.Shallows = append(ur.Shallows, plumbing.NewHash("cccccccccccccccccccccccccccccccccccccccc")) |
| 309 |
ur.Shallows = append(ur.Shallows, plumbing.NewHash("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")) |
| 310 |
|
| 311 |
since := time.Date(2015, time.January, 2, 3, 4, 5, 0, time.UTC) |
| 312 |
ur.Depth = DepthSince(since) |
| 313 |
|
| 314 |
expected := []string{ |
| 315 |
"want 1111111111111111111111111111111111111111 multi_ack ofs-delta side-band symref=HEAD:/refs/heads/master thin-pack\n", |
| 316 |
"want 2222222222222222222222222222222222222222\n", |
| 317 |
"want 3333333333333333333333333333333333333333\n", |
| 318 |
"want 4444444444444444444444444444444444444444\n", |
| 319 |
"want 5555555555555555555555555555555555555555\n", |
| 320 |
"shallow aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n", |
| 321 |
"shallow bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n", |
| 322 |
"shallow cccccccccccccccccccccccccccccccccccccccc\n", |
| 323 |
"shallow dddddddddddddddddddddddddddddddddddddddd\n", |
| 324 |
"deepen-since 1420167845\n", |
| 325 |
pktline.FlushString, |
| 326 |
} |
| 327 |
|
| 328 |
testUlReqEncode(c, ur, expected) |
| 329 |
} |