| 1 |
package pktline_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"strings" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/go-git/go-git/v5/plumbing/format/pktline" |
| 9 |
|
| 10 |
. "gopkg.in/check.v1" |
| 11 |
) |
| 12 |
|
| 13 |
func Test(t *testing.T) { TestingT(t) } |
| 14 |
|
| 15 |
type SuiteEncoder struct{} |
| 16 |
|
| 17 |
var _ = Suite(&SuiteEncoder{}) |
| 18 |
|
| 19 |
func (s *SuiteEncoder) TestFlush(c *C) { |
| 20 |
var buf bytes.Buffer |
| 21 |
e := pktline.NewEncoder(&buf) |
| 22 |
|
| 23 |
err := e.Flush() |
| 24 |
c.Assert(err, IsNil) |
| 25 |
|
| 26 |
obtained := buf.Bytes() |
| 27 |
c.Assert(obtained, DeepEquals, pktline.FlushPkt) |
| 28 |
} |
| 29 |
|
| 30 |
func (s *SuiteEncoder) TestEncode(c *C) { |
| 31 |
for i, test := range [...]struct { |
| 32 |
input [][]byte |
| 33 |
expected []byte |
| 34 |
}{ |
| 35 |
{ |
| 36 |
input: [][]byte{ |
| 37 |
[]byte("hello\n"), |
| 38 |
}, |
| 39 |
expected: []byte("000ahello\n"), |
| 40 |
}, { |
| 41 |
input: [][]byte{ |
| 42 |
[]byte("hello\n"), |
| 43 |
pktline.Flush, |
| 44 |
}, |
| 45 |
expected: []byte("000ahello\n0000"), |
| 46 |
}, { |
| 47 |
input: [][]byte{ |
| 48 |
[]byte("hello\n"), |
| 49 |
[]byte("world!\n"), |
| 50 |
[]byte("foo"), |
| 51 |
}, |
| 52 |
expected: []byte("000ahello\n000bworld!\n0007foo"), |
| 53 |
}, { |
| 54 |
input: [][]byte{ |
| 55 |
[]byte("hello\n"), |
| 56 |
pktline.Flush, |
| 57 |
[]byte("world!\n"), |
| 58 |
[]byte("foo"), |
| 59 |
pktline.Flush, |
| 60 |
}, |
| 61 |
expected: []byte("000ahello\n0000000bworld!\n0007foo0000"), |
| 62 |
}, { |
| 63 |
input: [][]byte{ |
| 64 |
[]byte(strings.Repeat("a", pktline.MaxPayloadSize)), |
| 65 |
}, |
| 66 |
expected: []byte( |
| 67 |
"fff0" + strings.Repeat("a", pktline.MaxPayloadSize)), |
| 68 |
}, { |
| 69 |
input: [][]byte{ |
| 70 |
[]byte(strings.Repeat("a", pktline.MaxPayloadSize)), |
| 71 |
[]byte(strings.Repeat("b", pktline.MaxPayloadSize)), |
| 72 |
}, |
| 73 |
expected: []byte( |
| 74 |
"fff0" + strings.Repeat("a", pktline.MaxPayloadSize) + |
| 75 |
"fff0" + strings.Repeat("b", pktline.MaxPayloadSize)), |
| 76 |
}, |
| 77 |
} { |
| 78 |
comment := Commentf("input %d = %v\n", i, test.input) |
| 79 |
|
| 80 |
var buf bytes.Buffer |
| 81 |
e := pktline.NewEncoder(&buf) |
| 82 |
|
| 83 |
err := e.Encode(test.input...) |
| 84 |
c.Assert(err, IsNil, comment) |
| 85 |
|
| 86 |
c.Assert(buf.Bytes(), DeepEquals, test.expected, comment) |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
func (s *SuiteEncoder) TestEncodeErrPayloadTooLong(c *C) { |
| 91 |
for i, input := range [...][][]byte{ |
| 92 |
{ |
| 93 |
[]byte(strings.Repeat("a", pktline.MaxPayloadSize+1)), |
| 94 |
}, |
| 95 |
{ |
| 96 |
[]byte("hello world!"), |
| 97 |
[]byte(strings.Repeat("a", pktline.MaxPayloadSize+1)), |
| 98 |
}, |
| 99 |
{ |
| 100 |
[]byte("hello world!"), |
| 101 |
[]byte(strings.Repeat("a", pktline.MaxPayloadSize+1)), |
| 102 |
[]byte("foo"), |
| 103 |
}, |
| 104 |
} { |
| 105 |
comment := Commentf("input %d = %v\n", i, input) |
| 106 |
|
| 107 |
var buf bytes.Buffer |
| 108 |
e := pktline.NewEncoder(&buf) |
| 109 |
|
| 110 |
err := e.Encode(input...) |
| 111 |
c.Assert(err, Equals, pktline.ErrPayloadTooLong, comment) |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
func (s *SuiteEncoder) TestEncodeStrings(c *C) { |
| 116 |
for i, test := range [...]struct { |
| 117 |
input []string |
| 118 |
expected []byte |
| 119 |
}{ |
| 120 |
{ |
| 121 |
input: []string{ |
| 122 |
"hello\n", |
| 123 |
}, |
| 124 |
expected: []byte("000ahello\n"), |
| 125 |
}, { |
| 126 |
input: []string{ |
| 127 |
"hello\n", |
| 128 |
pktline.FlushString, |
| 129 |
}, |
| 130 |
expected: []byte("000ahello\n0000"), |
| 131 |
}, { |
| 132 |
input: []string{ |
| 133 |
"hello\n", |
| 134 |
"world!\n", |
| 135 |
"foo", |
| 136 |
}, |
| 137 |
expected: []byte("000ahello\n000bworld!\n0007foo"), |
| 138 |
}, { |
| 139 |
input: []string{ |
| 140 |
"hello\n", |
| 141 |
pktline.FlushString, |
| 142 |
"world!\n", |
| 143 |
"foo", |
| 144 |
pktline.FlushString, |
| 145 |
}, |
| 146 |
expected: []byte("000ahello\n0000000bworld!\n0007foo0000"), |
| 147 |
}, { |
| 148 |
input: []string{ |
| 149 |
strings.Repeat("a", pktline.MaxPayloadSize), |
| 150 |
}, |
| 151 |
expected: []byte( |
| 152 |
"fff0" + strings.Repeat("a", pktline.MaxPayloadSize)), |
| 153 |
}, { |
| 154 |
input: []string{ |
| 155 |
strings.Repeat("a", pktline.MaxPayloadSize), |
| 156 |
strings.Repeat("b", pktline.MaxPayloadSize), |
| 157 |
}, |
| 158 |
expected: []byte( |
| 159 |
"fff0" + strings.Repeat("a", pktline.MaxPayloadSize) + |
| 160 |
"fff0" + strings.Repeat("b", pktline.MaxPayloadSize)), |
| 161 |
}, |
| 162 |
} { |
| 163 |
comment := Commentf("input %d = %v\n", i, test.input) |
| 164 |
|
| 165 |
var buf bytes.Buffer |
| 166 |
e := pktline.NewEncoder(&buf) |
| 167 |
|
| 168 |
err := e.EncodeString(test.input...) |
| 169 |
c.Assert(err, IsNil, comment) |
| 170 |
c.Assert(buf.Bytes(), DeepEquals, test.expected, comment) |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
func (s *SuiteEncoder) TestEncodeStringErrPayloadTooLong(c *C) { |
| 175 |
for i, input := range [...][]string{ |
| 176 |
{ |
| 177 |
strings.Repeat("a", pktline.MaxPayloadSize+1), |
| 178 |
}, |
| 179 |
{ |
| 180 |
"hello world!", |
| 181 |
strings.Repeat("a", pktline.MaxPayloadSize+1), |
| 182 |
}, |
| 183 |
{ |
| 184 |
"hello world!", |
| 185 |
strings.Repeat("a", pktline.MaxPayloadSize+1), |
| 186 |
"foo", |
| 187 |
}, |
| 188 |
} { |
| 189 |
comment := Commentf("input %d = %v\n", i, input) |
| 190 |
|
| 191 |
var buf bytes.Buffer |
| 192 |
e := pktline.NewEncoder(&buf) |
| 193 |
|
| 194 |
err := e.EncodeString(input...) |
| 195 |
c.Assert(err, Equals, pktline.ErrPayloadTooLong, comment) |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
func (s *SuiteEncoder) TestEncodef(c *C) { |
| 200 |
format := " %s %d\n" |
| 201 |
str := "foo" |
| 202 |
d := 42 |
| 203 |
|
| 204 |
var buf bytes.Buffer |
| 205 |
e := pktline.NewEncoder(&buf) |
| 206 |
|
| 207 |
err := e.Encodef(format, str, d) |
| 208 |
c.Assert(err, IsNil) |
| 209 |
|
| 210 |
expected := []byte("000c foo 42\n") |
| 211 |
c.Assert(buf.Bytes(), DeepEquals, expected) |
| 212 |
} |