| 1 |
package index |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"strings" |
| 6 |
"testing" |
| 7 |
"time" |
| 8 |
|
| 9 |
"github.com/go-git/go-git/v5/plumbing" |
| 10 |
"github.com/stretchr/testify/assert" |
| 11 |
"github.com/stretchr/testify/require" |
| 12 |
|
| 13 |
"github.com/google/go-cmp/cmp" |
| 14 |
. "gopkg.in/check.v1" |
| 15 |
) |
| 16 |
|
| 17 |
func (s *IndexSuite) TestEncode(c *C) { |
| 18 |
idx := &Index{ |
| 19 |
Version: 2, |
| 20 |
Entries: []*Entry{{ |
| 21 |
CreatedAt: time.Now(), |
| 22 |
ModifiedAt: time.Now(), |
| 23 |
Dev: 4242, |
| 24 |
Inode: 424242, |
| 25 |
UID: 84, |
| 26 |
GID: 8484, |
| 27 |
Size: 42, |
| 28 |
Stage: TheirMode, |
| 29 |
Hash: plumbing.NewHash("e25b29c8946e0e192fae2edc1dabf7be71e8ecf3"), |
| 30 |
Name: "foo", |
| 31 |
}, { |
| 32 |
CreatedAt: time.Now(), |
| 33 |
ModifiedAt: time.Now(), |
| 34 |
Name: "bar", |
| 35 |
Size: 82, |
| 36 |
}, { |
| 37 |
CreatedAt: time.Now(), |
| 38 |
ModifiedAt: time.Now(), |
| 39 |
Name: strings.Repeat(" ", 20), |
| 40 |
Size: 82, |
| 41 |
}}, |
| 42 |
} |
| 43 |
|
| 44 |
buf := bytes.NewBuffer(nil) |
| 45 |
e := NewEncoder(buf) |
| 46 |
err := e.Encode(idx) |
| 47 |
c.Assert(err, IsNil) |
| 48 |
|
| 49 |
output := &Index{} |
| 50 |
d := NewDecoder(buf) |
| 51 |
err = d.Decode(output) |
| 52 |
c.Assert(err, IsNil) |
| 53 |
|
| 54 |
c.Assert(cmp.Equal(idx, output), Equals, true) |
| 55 |
|
| 56 |
c.Assert(output.Entries[0].Name, Equals, strings.Repeat(" ", 20)) |
| 57 |
c.Assert(output.Entries[1].Name, Equals, "bar") |
| 58 |
c.Assert(output.Entries[2].Name, Equals, "foo") |
| 59 |
} |
| 60 |
|
| 61 |
func TestEncodeLongName(t *testing.T) { |
| 62 |
t.Parallel() |
| 63 |
|
| 64 |
// Entry names >= 4095 bytes overflow the 12-bit length field in V2/V3 |
| 65 |
// flags, which stores nameMask (0xFFF). The decoder must scan for the |
| 66 |
// NUL terminator to find the real length rather than trusting the field. |
| 67 |
longName := strings.Repeat("a", 5000) |
| 68 |
idx := &Index{ |
| 69 |
Version: 2, |
| 70 |
Entries: []*Entry{ |
| 71 |
{ |
| 72 |
CreatedAt: time.Now(), |
| 73 |
ModifiedAt: time.Now(), |
| 74 |
Name: longName, |
| 75 |
Size: 1, |
| 76 |
}, |
| 77 |
{ |
| 78 |
CreatedAt: time.Now(), |
| 79 |
ModifiedAt: time.Now(), |
| 80 |
Name: "short", |
| 81 |
Size: 2, |
| 82 |
}, |
| 83 |
}, |
| 84 |
} |
| 85 |
|
| 86 |
buf := bytes.NewBuffer(nil) |
| 87 |
err := NewEncoder(buf).Encode(idx) |
| 88 |
require.NoError(t, err) |
| 89 |
|
| 90 |
output := &Index{} |
| 91 |
err = NewDecoder(buf).Decode(output) |
| 92 |
require.NoError(t, err) |
| 93 |
|
| 94 |
require.Len(t, output.Entries, 2) |
| 95 |
assert.Equal(t, longName, output.Entries[0].Name) |
| 96 |
assert.Equal(t, "short", output.Entries[1].Name) |
| 97 |
assert.Equal(t, uint32(1), output.Entries[0].Size) |
| 98 |
assert.Equal(t, uint32(2), output.Entries[1].Size) |
| 99 |
} |
| 100 |
|
| 101 |
func TestEncodeV4(t *testing.T) { |
| 102 |
t.Parallel() |
| 103 |
idx := &Index{ |
| 104 |
Version: 4, |
| 105 |
Entries: []*Entry{{ |
| 106 |
CreatedAt: time.Now(), |
| 107 |
ModifiedAt: time.Now(), |
| 108 |
Dev: 4242, |
| 109 |
Inode: 424242, |
| 110 |
UID: 84, |
| 111 |
GID: 8484, |
| 112 |
Size: 42, |
| 113 |
Stage: TheirMode, |
| 114 |
Hash: plumbing.NewHash("e25b29c8946e0e192fae2edc1dabf7be71e8ecf3"), |
| 115 |
Name: "foo", |
| 116 |
}, { |
| 117 |
CreatedAt: time.Now(), |
| 118 |
ModifiedAt: time.Now(), |
| 119 |
Name: "bar", |
| 120 |
Size: 82, |
| 121 |
}, { |
| 122 |
CreatedAt: time.Now(), |
| 123 |
ModifiedAt: time.Now(), |
| 124 |
Name: strings.Repeat(" ", 20), |
| 125 |
Size: 82, |
| 126 |
}, { |
| 127 |
CreatedAt: time.Now(), |
| 128 |
ModifiedAt: time.Now(), |
| 129 |
Name: "baz/bar", |
| 130 |
Size: 82, |
| 131 |
}, { |
| 132 |
CreatedAt: time.Now(), |
| 133 |
ModifiedAt: time.Now(), |
| 134 |
Name: "baz/bar/bar", |
| 135 |
Size: 82, |
| 136 |
}}, |
| 137 |
} |
| 138 |
|
| 139 |
buf := bytes.NewBuffer(nil) |
| 140 |
e := NewEncoder(buf) |
| 141 |
err := e.Encode(idx) |
| 142 |
require.NoError(t, err) |
| 143 |
|
| 144 |
output := &Index{} |
| 145 |
d := NewDecoder(buf) |
| 146 |
err = d.Decode(output) |
| 147 |
require.NoError(t, err) |
| 148 |
|
| 149 |
assert.EqualExportedValues(t, idx, output) |
| 150 |
|
| 151 |
assert.Equal(t, strings.Repeat(" ", 20), output.Entries[0].Name) |
| 152 |
assert.Equal(t, "bar", output.Entries[1].Name) |
| 153 |
assert.Equal(t, "baz/bar", output.Entries[2].Name) |
| 154 |
assert.Equal(t, "baz/bar/bar", output.Entries[3].Name) |
| 155 |
assert.Equal(t, "foo", output.Entries[4].Name) |
| 156 |
} |
| 157 |
|
| 158 |
func (s *IndexSuite) TestEncodeUnsupportedVersion(c *C) { |
| 159 |
idx := &Index{Version: 5} |
| 160 |
|
| 161 |
buf := bytes.NewBuffer(nil) |
| 162 |
e := NewEncoder(buf) |
| 163 |
err := e.Encode(idx) |
| 164 |
c.Assert(err, Equals, ErrUnsupportedVersion) |
| 165 |
} |
| 166 |
|
| 167 |
func (s *IndexSuite) TestEncodeWithIntentToAddUnsupportedVersion(c *C) { |
| 168 |
idx := &Index{ |
| 169 |
Version: 3, |
| 170 |
Entries: []*Entry{{IntentToAdd: true}}, |
| 171 |
} |
| 172 |
|
| 173 |
buf := bytes.NewBuffer(nil) |
| 174 |
e := NewEncoder(buf) |
| 175 |
err := e.Encode(idx) |
| 176 |
c.Assert(err, IsNil) |
| 177 |
|
| 178 |
output := &Index{} |
| 179 |
d := NewDecoder(buf) |
| 180 |
err = d.Decode(output) |
| 181 |
c.Assert(err, IsNil) |
| 182 |
|
| 183 |
c.Assert(cmp.Equal(idx, output), Equals, true) |
| 184 |
c.Assert(output.Entries[0].IntentToAdd, Equals, true) |
| 185 |
} |
| 186 |
|
| 187 |
func (s *IndexSuite) TestEncodeWithSkipWorktreeUnsupportedVersion(c *C) { |
| 188 |
idx := &Index{ |
| 189 |
Version: 3, |
| 190 |
Entries: []*Entry{{SkipWorktree: true}}, |
| 191 |
} |
| 192 |
|
| 193 |
buf := bytes.NewBuffer(nil) |
| 194 |
e := NewEncoder(buf) |
| 195 |
err := e.Encode(idx) |
| 196 |
c.Assert(err, IsNil) |
| 197 |
|
| 198 |
output := &Index{} |
| 199 |
d := NewDecoder(buf) |
| 200 |
err = d.Decode(output) |
| 201 |
c.Assert(err, IsNil) |
| 202 |
|
| 203 |
c.Assert(cmp.Equal(idx, output), Equals, true) |
| 204 |
c.Assert(output.Entries[0].SkipWorktree, Equals, true) |
| 205 |
} |