| 1 |
package sideband |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"errors" |
| 6 |
"io" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/go-git/go-git/v5/plumbing/format/pktline" |
| 10 |
|
| 11 |
. "gopkg.in/check.v1" |
| 12 |
) |
| 13 |
|
| 14 |
func Test(t *testing.T) { TestingT(t) } |
| 15 |
|
| 16 |
type SidebandSuite struct{} |
| 17 |
|
| 18 |
var _ = Suite(&SidebandSuite{}) |
| 19 |
|
| 20 |
func (s *SidebandSuite) TestDecode(c *C) { |
| 21 |
expected := []byte("abcdefghijklmnopqrstuvwxyz") |
| 22 |
|
| 23 |
buf := bytes.NewBuffer(nil) |
| 24 |
e := pktline.NewEncoder(buf) |
| 25 |
e.Encode(PackData.WithPayload(expected[0:8])) |
| 26 |
e.Encode(ProgressMessage.WithPayload([]byte{'F', 'O', 'O', '\n'})) |
| 27 |
e.Encode(PackData.WithPayload(expected[8:16])) |
| 28 |
e.Encode(PackData.WithPayload(expected[16:26])) |
| 29 |
|
| 30 |
content := make([]byte, 26) |
| 31 |
d := NewDemuxer(Sideband64k, buf) |
| 32 |
n, err := io.ReadFull(d, content) |
| 33 |
c.Assert(err, IsNil) |
| 34 |
c.Assert(n, Equals, 26) |
| 35 |
c.Assert(content, DeepEquals, expected) |
| 36 |
} |
| 37 |
|
| 38 |
func (s *SidebandSuite) TestDecodeMoreThanContain(c *C) { |
| 39 |
expected := []byte("abcdefghijklmnopqrstuvwxyz") |
| 40 |
|
| 41 |
buf := bytes.NewBuffer(nil) |
| 42 |
e := pktline.NewEncoder(buf) |
| 43 |
e.Encode(PackData.WithPayload(expected)) |
| 44 |
|
| 45 |
content := make([]byte, 42) |
| 46 |
d := NewDemuxer(Sideband64k, buf) |
| 47 |
n, err := io.ReadFull(d, content) |
| 48 |
c.Assert(err, Equals, io.ErrUnexpectedEOF) |
| 49 |
c.Assert(n, Equals, 26) |
| 50 |
c.Assert(content[0:26], DeepEquals, expected) |
| 51 |
} |
| 52 |
|
| 53 |
func (s *SidebandSuite) TestDecodeWithError(c *C) { |
| 54 |
expected := []byte("abcdefghijklmnopqrstuvwxyz") |
| 55 |
|
| 56 |
buf := bytes.NewBuffer(nil) |
| 57 |
e := pktline.NewEncoder(buf) |
| 58 |
e.Encode(PackData.WithPayload(expected[0:8])) |
| 59 |
e.Encode(ErrorMessage.WithPayload([]byte{'F', 'O', 'O', '\n'})) |
| 60 |
e.Encode(PackData.WithPayload(expected[8:16])) |
| 61 |
e.Encode(PackData.WithPayload(expected[16:26])) |
| 62 |
|
| 63 |
content := make([]byte, 26) |
| 64 |
d := NewDemuxer(Sideband64k, buf) |
| 65 |
n, err := io.ReadFull(d, content) |
| 66 |
c.Assert(err, ErrorMatches, "unexpected error: FOO\n") |
| 67 |
c.Assert(n, Equals, 8) |
| 68 |
c.Assert(content[0:8], DeepEquals, expected[0:8]) |
| 69 |
} |
| 70 |
|
| 71 |
type mockReader struct{} |
| 72 |
|
| 73 |
func (r *mockReader) Read([]byte) (int, error) { return 0, errors.New("foo") } |
| 74 |
|
| 75 |
func (s *SidebandSuite) TestDecodeFromFailingReader(c *C) { |
| 76 |
content := make([]byte, 26) |
| 77 |
d := NewDemuxer(Sideband64k, &mockReader{}) |
| 78 |
n, err := io.ReadFull(d, content) |
| 79 |
c.Assert(err, ErrorMatches, "foo") |
| 80 |
c.Assert(n, Equals, 0) |
| 81 |
} |
| 82 |
|
| 83 |
func (s *SidebandSuite) TestDecodeWithProgress(c *C) { |
| 84 |
expected := []byte("abcdefghijklmnopqrstuvwxyz") |
| 85 |
|
| 86 |
input := bytes.NewBuffer(nil) |
| 87 |
e := pktline.NewEncoder(input) |
| 88 |
e.Encode(PackData.WithPayload(expected[0:8])) |
| 89 |
e.Encode(ProgressMessage.WithPayload([]byte{'F', 'O', 'O', '\n'})) |
| 90 |
e.Encode(PackData.WithPayload(expected[8:16])) |
| 91 |
e.Encode(PackData.WithPayload(expected[16:26])) |
| 92 |
|
| 93 |
output := bytes.NewBuffer(nil) |
| 94 |
content := make([]byte, 26) |
| 95 |
d := NewDemuxer(Sideband64k, input) |
| 96 |
d.Progress = output |
| 97 |
|
| 98 |
n, err := io.ReadFull(d, content) |
| 99 |
c.Assert(err, IsNil) |
| 100 |
c.Assert(n, Equals, 26) |
| 101 |
c.Assert(content, DeepEquals, expected) |
| 102 |
|
| 103 |
progress, err := io.ReadAll(output) |
| 104 |
c.Assert(err, IsNil) |
| 105 |
c.Assert(progress, DeepEquals, []byte{'F', 'O', 'O', '\n'}) |
| 106 |
} |
| 107 |
|
| 108 |
func (s *SidebandSuite) TestDecodeFlushEOF(c *C) { |
| 109 |
expected := []byte("abcdefghijklmnopqrstuvwxyz") |
| 110 |
|
| 111 |
input := bytes.NewBuffer(nil) |
| 112 |
e := pktline.NewEncoder(input) |
| 113 |
e.Encode(PackData.WithPayload(expected[0:8])) |
| 114 |
e.Encode(ProgressMessage.WithPayload([]byte{'F', 'O', 'O', '\n'})) |
| 115 |
e.Encode(PackData.WithPayload(expected[8:16])) |
| 116 |
e.Encode(PackData.WithPayload(expected[16:26])) |
| 117 |
e.Flush() |
| 118 |
e.Encode(PackData.WithPayload([]byte("bar\n"))) |
| 119 |
|
| 120 |
output := bytes.NewBuffer(nil) |
| 121 |
content := bytes.NewBuffer(nil) |
| 122 |
d := NewDemuxer(Sideband64k, input) |
| 123 |
d.Progress = output |
| 124 |
|
| 125 |
n, err := content.ReadFrom(d) |
| 126 |
c.Assert(err, IsNil) |
| 127 |
c.Assert(n, Equals, int64(26)) |
| 128 |
c.Assert(content.Bytes(), DeepEquals, expected) |
| 129 |
|
| 130 |
progress, err := io.ReadAll(output) |
| 131 |
c.Assert(err, IsNil) |
| 132 |
c.Assert(progress, DeepEquals, []byte{'F', 'O', 'O', '\n'}) |
| 133 |
} |
| 134 |
|
| 135 |
func (s *SidebandSuite) TestDecodeWithUnknownChannel(c *C) { |
| 136 |
buf := bytes.NewBuffer(nil) |
| 137 |
e := pktline.NewEncoder(buf) |
| 138 |
e.Encode([]byte{'4', 'F', 'O', 'O', '\n'}) |
| 139 |
|
| 140 |
content := make([]byte, 26) |
| 141 |
d := NewDemuxer(Sideband64k, buf) |
| 142 |
n, err := io.ReadFull(d, content) |
| 143 |
c.Assert(err, ErrorMatches, "unknown channel 4FOO\n") |
| 144 |
c.Assert(n, Equals, 0) |
| 145 |
} |
| 146 |
|
| 147 |
func (s *SidebandSuite) TestDecodeWithPending(c *C) { |
| 148 |
expected := []byte("abcdefghijklmnopqrstuvwxyz") |
| 149 |
|
| 150 |
buf := bytes.NewBuffer(nil) |
| 151 |
e := pktline.NewEncoder(buf) |
| 152 |
e.Encode(PackData.WithPayload(expected[0:8])) |
| 153 |
e.Encode(PackData.WithPayload(expected[8:16])) |
| 154 |
e.Encode(PackData.WithPayload(expected[16:26])) |
| 155 |
|
| 156 |
content := make([]byte, 13) |
| 157 |
d := NewDemuxer(Sideband64k, buf) |
| 158 |
n, err := io.ReadFull(d, content) |
| 159 |
c.Assert(err, IsNil) |
| 160 |
c.Assert(n, Equals, 13) |
| 161 |
c.Assert(content, DeepEquals, expected[0:13]) |
| 162 |
|
| 163 |
n, err = d.Read(content) |
| 164 |
c.Assert(err, IsNil) |
| 165 |
c.Assert(n, Equals, 13) |
| 166 |
c.Assert(content, DeepEquals, expected[13:26]) |
| 167 |
} |
| 168 |
|
| 169 |
func (s *SidebandSuite) TestDecodeErrMaxPacked(c *C) { |
| 170 |
buf := bytes.NewBuffer(nil) |
| 171 |
e := pktline.NewEncoder(buf) |
| 172 |
e.Encode(PackData.WithPayload(bytes.Repeat([]byte{'0'}, MaxPackedSize+1))) |
| 173 |
|
| 174 |
content := make([]byte, 13) |
| 175 |
d := NewDemuxer(Sideband, buf) |
| 176 |
n, err := io.ReadFull(d, content) |
| 177 |
c.Assert(err, Equals, ErrMaxPackedExceeded) |
| 178 |
c.Assert(n, Equals, 0) |
| 179 |
} |