| 1 |
package pktline_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"errors" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"strings" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/go-git/go-git/v5/plumbing/format/pktline" |
| 12 |
"github.com/stretchr/testify/assert" |
| 13 |
|
| 14 |
. "gopkg.in/check.v1" |
| 15 |
) |
| 16 |
|
| 17 |
type SuiteScanner struct{} |
| 18 |
|
| 19 |
var _ = Suite(&SuiteScanner{}) |
| 20 |
|
| 21 |
func (s *SuiteScanner) TestInvalid(c *C) { |
| 22 |
for _, test := range [...]string{ |
| 23 |
"0001", "0002", "0003", "0004", |
| 24 |
"0001asdfsadf", "0004foo", |
| 25 |
"fff5", "ffff", |
| 26 |
"FFF5", "FFFF", |
| 27 |
"gorka", |
| 28 |
"0", "003", |
| 29 |
" 5a", "5 a", "5 \n", |
| 30 |
"-001", "-000", |
| 31 |
} { |
| 32 |
r := strings.NewReader(test) |
| 33 |
sc := pktline.NewScanner(r) |
| 34 |
_ = sc.Scan() |
| 35 |
c.Assert(sc.Err(), ErrorMatches, pktline.ErrInvalidPktLen.Error(), |
| 36 |
Commentf("data = %q", test)) |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
func (s *SuiteScanner) TestDecodeOversizePktLines(c *C) { |
| 41 |
for _, test := range [...]string{ |
| 42 |
"fff1" + strings.Repeat("a", 0xfff1), |
| 43 |
"fff2" + strings.Repeat("a", 0xfff2), |
| 44 |
"fff3" + strings.Repeat("a", 0xfff3), |
| 45 |
"fff4" + strings.Repeat("a", 0xfff4), |
| 46 |
} { |
| 47 |
r := strings.NewReader(test) |
| 48 |
sc := pktline.NewScanner(r) |
| 49 |
_ = sc.Scan() |
| 50 |
c.Assert(sc.Err(), IsNil) |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
func TestValidPktSizes(t *testing.T) { |
| 55 |
for _, test := range [...]string{ |
| 56 |
"01fe" + strings.Repeat("a", 0x01fe-4), |
| 57 |
"01FE" + strings.Repeat("a", 0x01fe-4), |
| 58 |
"00b5" + strings.Repeat("a", 0x00b5-4), |
| 59 |
"00B5" + strings.Repeat("a", 0x00b5-4), |
| 60 |
} { |
| 61 |
r := strings.NewReader(test) |
| 62 |
sc := pktline.NewScanner(r) |
| 63 |
hasPayload := sc.Scan() |
| 64 |
obtained := sc.Bytes() |
| 65 |
|
| 66 |
assert.True(t, hasPayload) |
| 67 |
assert.NoError(t, sc.Err()) |
| 68 |
assert.Equal(t, []byte(test[4:]), obtained) |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
func (s *SuiteScanner) TestEmptyReader(c *C) { |
| 73 |
r := strings.NewReader("") |
| 74 |
sc := pktline.NewScanner(r) |
| 75 |
hasPayload := sc.Scan() |
| 76 |
c.Assert(hasPayload, Equals, false) |
| 77 |
c.Assert(sc.Err(), Equals, nil) |
| 78 |
} |
| 79 |
|
| 80 |
func (s *SuiteScanner) TestFlush(c *C) { |
| 81 |
var buf bytes.Buffer |
| 82 |
e := pktline.NewEncoder(&buf) |
| 83 |
err := e.Flush() |
| 84 |
c.Assert(err, IsNil) |
| 85 |
|
| 86 |
sc := pktline.NewScanner(&buf) |
| 87 |
c.Assert(sc.Scan(), Equals, true) |
| 88 |
|
| 89 |
payload := sc.Bytes() |
| 90 |
c.Assert(len(payload), Equals, 0) |
| 91 |
} |
| 92 |
|
| 93 |
func (s *SuiteScanner) TestPktLineTooShort(c *C) { |
| 94 |
r := strings.NewReader("010cfoobar") |
| 95 |
|
| 96 |
sc := pktline.NewScanner(r) |
| 97 |
|
| 98 |
c.Assert(sc.Scan(), Equals, false) |
| 99 |
c.Assert(sc.Err(), ErrorMatches, "unexpected EOF") |
| 100 |
} |
| 101 |
|
| 102 |
func (s *SuiteScanner) TestScanAndPayload(c *C) { |
| 103 |
for _, test := range [...]string{ |
| 104 |
"a", |
| 105 |
"a\n", |
| 106 |
strings.Repeat("a", 100), |
| 107 |
strings.Repeat("a", 100) + "\n", |
| 108 |
strings.Repeat("\x00", 100), |
| 109 |
strings.Repeat("\x00", 100) + "\n", |
| 110 |
strings.Repeat("a", pktline.MaxPayloadSize), |
| 111 |
strings.Repeat("a", pktline.MaxPayloadSize-1) + "\n", |
| 112 |
} { |
| 113 |
var buf bytes.Buffer |
| 114 |
e := pktline.NewEncoder(&buf) |
| 115 |
err := e.EncodeString(test) |
| 116 |
c.Assert(err, IsNil, |
| 117 |
Commentf("input len=%x, contents=%.10q\n", len(test), test)) |
| 118 |
|
| 119 |
sc := pktline.NewScanner(&buf) |
| 120 |
c.Assert(sc.Scan(), Equals, true, |
| 121 |
Commentf("test = %.20q...", test)) |
| 122 |
|
| 123 |
obtained := sc.Bytes() |
| 124 |
c.Assert(obtained, DeepEquals, []byte(test), |
| 125 |
Commentf("in = %.20q out = %.20q", test, string(obtained))) |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
func (s *SuiteScanner) TestSkip(c *C) { |
| 130 |
for _, test := range [...]struct { |
| 131 |
input []string |
| 132 |
n int |
| 133 |
expected []byte |
| 134 |
}{ |
| 135 |
{ |
| 136 |
input: []string{ |
| 137 |
"first", |
| 138 |
"second", |
| 139 |
"third"}, |
| 140 |
n: 1, |
| 141 |
expected: []byte("second"), |
| 142 |
}, |
| 143 |
{ |
| 144 |
input: []string{ |
| 145 |
"first", |
| 146 |
"second", |
| 147 |
"third"}, |
| 148 |
n: 2, |
| 149 |
expected: []byte("third"), |
| 150 |
}, |
| 151 |
} { |
| 152 |
var buf bytes.Buffer |
| 153 |
e := pktline.NewEncoder(&buf) |
| 154 |
err := e.EncodeString(test.input...) |
| 155 |
c.Assert(err, IsNil) |
| 156 |
|
| 157 |
sc := pktline.NewScanner(&buf) |
| 158 |
for i := 0; i < test.n; i++ { |
| 159 |
c.Assert(sc.Scan(), Equals, true, |
| 160 |
Commentf("scan error = %s", sc.Err())) |
| 161 |
} |
| 162 |
c.Assert(sc.Scan(), Equals, true, |
| 163 |
Commentf("scan error = %s", sc.Err())) |
| 164 |
|
| 165 |
obtained := sc.Bytes() |
| 166 |
c.Assert(obtained, DeepEquals, test.expected, |
| 167 |
Commentf("\nin = %.20q\nout = %.20q\nexp = %.20q", |
| 168 |
test.input, obtained, test.expected)) |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
func (s *SuiteScanner) TestEOF(c *C) { |
| 173 |
var buf bytes.Buffer |
| 174 |
e := pktline.NewEncoder(&buf) |
| 175 |
err := e.EncodeString("first", "second") |
| 176 |
c.Assert(err, IsNil) |
| 177 |
|
| 178 |
sc := pktline.NewScanner(&buf) |
| 179 |
for sc.Scan() { |
| 180 |
} |
| 181 |
c.Assert(sc.Err(), IsNil) |
| 182 |
} |
| 183 |
|
| 184 |
type mockReader struct{} |
| 185 |
|
| 186 |
func (r *mockReader) Read([]byte) (int, error) { return 0, errors.New("foo") } |
| 187 |
|
| 188 |
func (s *SuiteScanner) TestInternalReadError(c *C) { |
| 189 |
sc := pktline.NewScanner(&mockReader{}) |
| 190 |
c.Assert(sc.Scan(), Equals, false) |
| 191 |
c.Assert(sc.Err(), ErrorMatches, "foo") |
| 192 |
} |
| 193 |
|
| 194 |
// A section are several non flush-pkt lines followed by a flush-pkt, which |
| 195 |
// how the git protocol sends long messages. |
| 196 |
func (s *SuiteScanner) TestReadSomeSections(c *C) { |
| 197 |
nSections := 2 |
| 198 |
nLines := 4 |
| 199 |
data := sectionsExample(c, nSections, nLines) |
| 200 |
sc := pktline.NewScanner(data) |
| 201 |
|
| 202 |
sectionCounter := 0 |
| 203 |
lineCounter := 0 |
| 204 |
for sc.Scan() { |
| 205 |
if len(sc.Bytes()) == 0 { |
| 206 |
sectionCounter++ |
| 207 |
} |
| 208 |
lineCounter++ |
| 209 |
} |
| 210 |
c.Assert(sc.Err(), IsNil) |
| 211 |
c.Assert(sectionCounter, Equals, nSections) |
| 212 |
c.Assert(lineCounter, Equals, (1+nLines)*nSections) |
| 213 |
} |
| 214 |
|
| 215 |
// returns nSection sections, each of them with nLines pkt-lines (not |
| 216 |
// counting the flush-pkt: |
| 217 |
// |
| 218 |
// 0009 0.0\n |
| 219 |
// 0009 0.1\n |
| 220 |
// ... |
| 221 |
// 0000 |
| 222 |
// and so on |
| 223 |
func sectionsExample(c *C, nSections, nLines int) io.Reader { |
| 224 |
var buf bytes.Buffer |
| 225 |
e := pktline.NewEncoder(&buf) |
| 226 |
|
| 227 |
for section := 0; section < nSections; section++ { |
| 228 |
ss := []string{} |
| 229 |
for line := 0; line < nLines; line++ { |
| 230 |
line := fmt.Sprintf(" %d.%d\n", section, line) |
| 231 |
ss = append(ss, line) |
| 232 |
} |
| 233 |
err := e.EncodeString(ss...) |
| 234 |
c.Assert(err, IsNil) |
| 235 |
err = e.Flush() |
| 236 |
c.Assert(err, IsNil) |
| 237 |
} |
| 238 |
|
| 239 |
return &buf |
| 240 |
} |