| 1 |
package filesystem |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"net" |
| 8 |
"os" |
| 9 |
"path" |
| 10 |
"path/filepath" |
| 11 |
"runtime" |
| 12 |
"testing" |
| 13 |
"time" |
| 14 |
|
| 15 |
"github.com/go-git/go-git/v5/plumbing" |
| 16 |
"github.com/go-git/go-git/v5/plumbing/filemode" |
| 17 |
"github.com/go-git/go-git/v5/plumbing/format/index" |
| 18 |
"github.com/go-git/go-git/v5/utils/merkletrie" |
| 19 |
"github.com/go-git/go-git/v5/utils/merkletrie/noder" |
| 20 |
|
| 21 |
"github.com/go-git/go-billy/v5" |
| 22 |
"github.com/go-git/go-billy/v5/memfs" |
| 23 |
"github.com/go-git/go-billy/v5/osfs" |
| 24 |
"github.com/go-git/go-billy/v5/util" |
| 25 |
. "gopkg.in/check.v1" |
| 26 |
) |
| 27 |
|
| 28 |
func Test(t *testing.T) { TestingT(t) } |
| 29 |
|
| 30 |
type NoderSuite struct{} |
| 31 |
|
| 32 |
var _ = Suite(&NoderSuite{}) |
| 33 |
|
| 34 |
func (s *NoderSuite) TestDiff(c *C) { |
| 35 |
fsA := memfs.New() |
| 36 |
WriteFile(fsA, "foo", []byte("foo"), 0644) |
| 37 |
WriteFile(fsA, "qux/bar", []byte("foo"), 0644) |
| 38 |
WriteFile(fsA, "qux/qux", []byte("foo"), 0644) |
| 39 |
fsA.Symlink("foo", "bar") |
| 40 |
|
| 41 |
fsB := memfs.New() |
| 42 |
WriteFile(fsB, "foo", []byte("foo"), 0644) |
| 43 |
WriteFile(fsB, "qux/bar", []byte("foo"), 0644) |
| 44 |
WriteFile(fsB, "qux/qux", []byte("foo"), 0644) |
| 45 |
fsB.Symlink("foo", "bar") |
| 46 |
|
| 47 |
ch, err := merkletrie.DiffTree( |
| 48 |
NewRootNode(fsA, nil), |
| 49 |
NewRootNode(fsB, nil), |
| 50 |
IsEquals, |
| 51 |
) |
| 52 |
|
| 53 |
c.Assert(err, IsNil) |
| 54 |
c.Assert(ch, HasLen, 0) |
| 55 |
} |
| 56 |
|
| 57 |
func (s *NoderSuite) TestDiffChangeLink(c *C) { |
| 58 |
fsA := memfs.New() |
| 59 |
fsA.Symlink("qux", "foo") |
| 60 |
|
| 61 |
fsB := memfs.New() |
| 62 |
fsB.Symlink("bar", "foo") |
| 63 |
|
| 64 |
ch, err := merkletrie.DiffTree( |
| 65 |
NewRootNode(fsA, nil), |
| 66 |
NewRootNode(fsB, nil), |
| 67 |
IsEquals, |
| 68 |
) |
| 69 |
|
| 70 |
c.Assert(err, IsNil) |
| 71 |
c.Assert(ch, HasLen, 1) |
| 72 |
} |
| 73 |
|
| 74 |
func (s *NoderSuite) TestDiffChangeContent(c *C) { |
| 75 |
fsA := memfs.New() |
| 76 |
WriteFile(fsA, "foo", []byte("foo"), 0644) |
| 77 |
WriteFile(fsA, "qux/bar", []byte("foo"), 0644) |
| 78 |
WriteFile(fsA, "qux/qux", []byte("foo"), 0644) |
| 79 |
|
| 80 |
fsB := memfs.New() |
| 81 |
WriteFile(fsB, "foo", []byte("foo"), 0644) |
| 82 |
WriteFile(fsB, "qux/bar", []byte("bar"), 0644) |
| 83 |
WriteFile(fsB, "qux/qux", []byte("foo"), 0644) |
| 84 |
|
| 85 |
ch, err := merkletrie.DiffTree( |
| 86 |
NewRootNode(fsA, nil), |
| 87 |
NewRootNode(fsB, nil), |
| 88 |
IsEquals, |
| 89 |
) |
| 90 |
|
| 91 |
c.Assert(err, IsNil) |
| 92 |
c.Assert(ch, HasLen, 1) |
| 93 |
} |
| 94 |
|
| 95 |
func (s *NoderSuite) TestDiffSymlinkDirOnA(c *C) { |
| 96 |
fsA := memfs.New() |
| 97 |
WriteFile(fsA, "qux/qux", []byte("foo"), 0644) |
| 98 |
|
| 99 |
fsB := memfs.New() |
| 100 |
fsB.Symlink("qux", "foo") |
| 101 |
WriteFile(fsB, "qux/qux", []byte("foo"), 0644) |
| 102 |
|
| 103 |
ch, err := merkletrie.DiffTree( |
| 104 |
NewRootNode(fsA, nil), |
| 105 |
NewRootNode(fsB, nil), |
| 106 |
IsEquals, |
| 107 |
) |
| 108 |
|
| 109 |
c.Assert(err, IsNil) |
| 110 |
c.Assert(ch, HasLen, 1) |
| 111 |
} |
| 112 |
|
| 113 |
func (s *NoderSuite) TestDiffSymlinkDirOnB(c *C) { |
| 114 |
fsA := memfs.New() |
| 115 |
fsA.Symlink("qux", "foo") |
| 116 |
WriteFile(fsA, "qux/qux", []byte("foo"), 0644) |
| 117 |
|
| 118 |
fsB := memfs.New() |
| 119 |
WriteFile(fsB, "qux/qux", []byte("foo"), 0644) |
| 120 |
|
| 121 |
ch, err := merkletrie.DiffTree( |
| 122 |
NewRootNode(fsA, nil), |
| 123 |
NewRootNode(fsB, nil), |
| 124 |
IsEquals, |
| 125 |
) |
| 126 |
|
| 127 |
c.Assert(err, IsNil) |
| 128 |
c.Assert(ch, HasLen, 1) |
| 129 |
} |
| 130 |
|
| 131 |
func (s *NoderSuite) TestDiffChangeMissing(c *C) { |
| 132 |
fsA := memfs.New() |
| 133 |
WriteFile(fsA, "foo", []byte("foo"), 0644) |
| 134 |
|
| 135 |
fsB := memfs.New() |
| 136 |
WriteFile(fsB, "bar", []byte("bar"), 0644) |
| 137 |
|
| 138 |
ch, err := merkletrie.DiffTree( |
| 139 |
NewRootNode(fsA, nil), |
| 140 |
NewRootNode(fsB, nil), |
| 141 |
IsEquals, |
| 142 |
) |
| 143 |
|
| 144 |
c.Assert(err, IsNil) |
| 145 |
c.Assert(ch, HasLen, 2) |
| 146 |
} |
| 147 |
|
| 148 |
func (s *NoderSuite) TestDiffChangeMode(c *C) { |
| 149 |
fsA := memfs.New() |
| 150 |
WriteFile(fsA, "foo", []byte("foo"), 0644) |
| 151 |
|
| 152 |
fsB := memfs.New() |
| 153 |
WriteFile(fsB, "foo", []byte("foo"), 0755) |
| 154 |
|
| 155 |
ch, err := merkletrie.DiffTree( |
| 156 |
NewRootNode(fsA, nil), |
| 157 |
NewRootNode(fsB, nil), |
| 158 |
IsEquals, |
| 159 |
) |
| 160 |
|
| 161 |
c.Assert(err, IsNil) |
| 162 |
c.Assert(ch, HasLen, 1) |
| 163 |
} |
| 164 |
|
| 165 |
func (s *NoderSuite) TestDiffChangeModeNotRelevant(c *C) { |
| 166 |
fsA := memfs.New() |
| 167 |
WriteFile(fsA, "foo", []byte("foo"), 0644) |
| 168 |
|
| 169 |
fsB := memfs.New() |
| 170 |
WriteFile(fsB, "foo", []byte("foo"), 0655) |
| 171 |
|
| 172 |
ch, err := merkletrie.DiffTree( |
| 173 |
NewRootNode(fsA, nil), |
| 174 |
NewRootNode(fsB, nil), |
| 175 |
IsEquals, |
| 176 |
) |
| 177 |
|
| 178 |
c.Assert(err, IsNil) |
| 179 |
c.Assert(ch, HasLen, 0) |
| 180 |
} |
| 181 |
|
| 182 |
func (s *NoderSuite) TestDiffDirectory(c *C) { |
| 183 |
dir := path.Join("qux", "bar") |
| 184 |
fsA := memfs.New() |
| 185 |
fsA.MkdirAll(dir, 0644) |
| 186 |
|
| 187 |
fsB := memfs.New() |
| 188 |
fsB.MkdirAll(dir, 0644) |
| 189 |
|
| 190 |
ch, err := merkletrie.DiffTree( |
| 191 |
NewRootNode(fsA, map[string]plumbing.Hash{ |
| 192 |
dir: plumbing.NewHash("aa102815663d23f8b75a47e7a01965dcdc96468c"), |
| 193 |
}), |
| 194 |
NewRootNode(fsB, map[string]plumbing.Hash{ |
| 195 |
dir: plumbing.NewHash("19102815663d23f8b75a47e7a01965dcdc96468c"), |
| 196 |
}), |
| 197 |
IsEquals, |
| 198 |
) |
| 199 |
|
| 200 |
c.Assert(err, IsNil) |
| 201 |
c.Assert(ch, HasLen, 1) |
| 202 |
|
| 203 |
a, err := ch[0].Action() |
| 204 |
c.Assert(err, IsNil) |
| 205 |
c.Assert(a, Equals, merkletrie.Modify) |
| 206 |
} |
| 207 |
|
| 208 |
func (s *NoderSuite) TestSocket(c *C) { |
| 209 |
if runtime.GOOS == "windows" { |
| 210 |
c.Skip("socket files do not exist on windows") |
| 211 |
} |
| 212 |
|
| 213 |
td, err := os.MkdirTemp(c.MkDir(), "socket-test") |
| 214 |
c.Assert(err, IsNil) |
| 215 |
|
| 216 |
sock, err := net.ListenUnix("unix", &net.UnixAddr{Name: fmt.Sprintf("%s/socket", td), Net: "unix"}) |
| 217 |
c.Assert(err, IsNil) |
| 218 |
defer sock.Close() |
| 219 |
|
| 220 |
fsA := osfs.New(td) |
| 221 |
WriteFile(fsA, "foo", []byte("foo"), 0644) |
| 222 |
|
| 223 |
noder := NewRootNode(fsA, nil) |
| 224 |
childs, err := noder.Children() |
| 225 |
c.Assert(err, IsNil) |
| 226 |
c.Assert(childs, HasLen, 1) |
| 227 |
} |
| 228 |
|
| 229 |
func WriteFile(fs billy.Filesystem, filename string, data []byte, perm os.FileMode) error { |
| 230 |
f, err := fs.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm) |
| 231 |
if err != nil { |
| 232 |
return err |
| 233 |
} |
| 234 |
|
| 235 |
n, err := f.Write(data) |
| 236 |
if err == nil && n < len(data) { |
| 237 |
err = io.ErrShortWrite |
| 238 |
} |
| 239 |
if err1 := f.Close(); err == nil { |
| 240 |
err = err1 |
| 241 |
} |
| 242 |
return err |
| 243 |
} |
| 244 |
|
| 245 |
var empty = make([]byte, 24) |
| 246 |
|
| 247 |
func IsEquals(a, b noder.Hasher) bool { |
| 248 |
if bytes.Equal(a.Hash(), empty) || bytes.Equal(b.Hash(), empty) { |
| 249 |
return false |
| 250 |
} |
| 251 |
|
| 252 |
return bytes.Equal(a.Hash(), b.Hash()) |
| 253 |
} |
| 254 |
|
| 255 |
func (s *NoderSuite) TestRacyGit(c *C) { |
| 256 |
td, err := os.MkdirTemp(c.MkDir(), "racy-git") |
| 257 |
c.Assert(err, IsNil) |
| 258 |
fs := osfs.New(td) |
| 259 |
|
| 260 |
origContent := []byte("foo") |
| 261 |
err = WriteFile(fs, "racyfile", origContent, 0o644) |
| 262 |
c.Assert(err, IsNil) |
| 263 |
|
| 264 |
fi, err := fs.Stat("racyfile") |
| 265 |
c.Assert(err, IsNil) |
| 266 |
modTime := fi.ModTime() |
| 267 |
|
| 268 |
fooHasher := plumbing.NewHasher(plumbing.BlobObject, int64(len(origContent))) |
| 269 |
_, err = fooHasher.Write(origContent) |
| 270 |
c.Assert(err, IsNil) |
| 271 |
fooHash := fooHasher.Sum() |
| 272 |
|
| 273 |
idx := &index.Index{ |
| 274 |
Version: 2, |
| 275 |
Entries: []*index.Entry{ |
| 276 |
{ |
| 277 |
Name: "racyfile", |
| 278 |
Hash: fooHash, |
| 279 |
Size: uint32(len(origContent)), |
| 280 |
ModifiedAt: modTime, |
| 281 |
Mode: filemode.Regular, |
| 282 |
}, |
| 283 |
}, |
| 284 |
ModTime: modTime, |
| 285 |
} |
| 286 |
|
| 287 |
newContent := []byte("bar") |
| 288 |
c.Assert(len(origContent), Equals, len(newContent), Commentf("test setup requires same size")) |
| 289 |
|
| 290 |
err = WriteFile(fs, "racyfile", newContent, 0o644) |
| 291 |
c.Assert(err, IsNil) |
| 292 |
|
| 293 |
err = os.Chtimes(filepath.Join(td, "racyfile"), modTime, modTime) |
| 294 |
c.Assert(err, IsNil) |
| 295 |
|
| 296 |
fi, err = fs.Stat("racyfile") |
| 297 |
c.Assert(err, IsNil) |
| 298 |
c.Assert(uint32(len(origContent)), Equals, uint32(fi.Size()), Commentf("size should match")) |
| 299 |
c.Assert(modTime, Equals, fi.ModTime(), Commentf("mtime should match")) |
| 300 |
|
| 301 |
actualContent, err := util.ReadFile(fs, "racyfile") |
| 302 |
c.Assert(err, IsNil) |
| 303 |
c.Assert(actualContent, DeepEquals, newContent, Commentf("content should have changed")) |
| 304 |
c.Assert(origContent, Not(DeepEquals), actualContent, Commentf("content should be different")) |
| 305 |
|
| 306 |
barHasher := plumbing.NewHasher(plumbing.BlobObject, int64(len(newContent))) |
| 307 |
_, err = barHasher.Write(newContent) |
| 308 |
c.Assert(err, IsNil) |
| 309 |
barHash := barHasher.Sum() |
| 310 |
c.Assert(fooHash, Not(DeepEquals), barHash, Commentf("hashes should be different")) |
| 311 |
|
| 312 |
fsNode := NewRootNodeWithOptions(fs, nil, Options{Index: idx}) |
| 313 |
|
| 314 |
children, err := fsNode.Children() |
| 315 |
c.Assert(err, IsNil) |
| 316 |
c.Assert(children, HasLen, 1, Commentf("should have one file")) |
| 317 |
|
| 318 |
fileNode := children[0] |
| 319 |
fileHash := fileNode.Hash() |
| 320 |
|
| 321 |
expectedHash := append(barHash[:], filemode.Regular.Bytes()...) |
| 322 |
|
| 323 |
if bytes.Equal(fileHash, expectedHash) { |
| 324 |
c.Log("PASS: Correctly detected file change despite metadata match (racy-git handled)") |
| 325 |
} else { |
| 326 |
c.Errorf("FAIL: Racy-git not handled correctly.\nExpected hash: %x (bar)\nGot hash: %x (likely foo)\nThis means the file was not hashed despite being in the racy window.", expectedHash, fileHash) |
| 327 |
} |
| 328 |
|
| 329 |
c.Assert(expectedHash, DeepEquals, fileHash, Commentf("should hash file content when in racy-git window")) |
| 330 |
} |
| 331 |
|
| 332 |
func (s *NoderSuite) TestZeroIndexModTime(c *C) { |
| 333 |
fs := memfs.New() |
| 334 |
|
| 335 |
// Write a file with known content |
| 336 |
content := []byte("foo") |
| 337 |
err := WriteFile(fs, "testfile", content, 0o644) |
| 338 |
c.Assert(err, IsNil) |
| 339 |
|
| 340 |
// Get file info |
| 341 |
fi, err := fs.Stat("testfile") |
| 342 |
c.Assert(err, IsNil) |
| 343 |
modTime := fi.ModTime() |
| 344 |
|
| 345 |
// Calculate the actual hash for the file content |
| 346 |
actualHasher := plumbing.NewHasher(plumbing.BlobObject, int64(len(content))) |
| 347 |
_, err = actualHasher.Write(content) |
| 348 |
c.Assert(err, IsNil) |
| 349 |
actualHash := actualHasher.Sum() |
| 350 |
|
| 351 |
// Create a fake hash for the index entry (different from actual) |
| 352 |
fakeContent := []byte("bar") |
| 353 |
fakeHasher := plumbing.NewHasher(plumbing.BlobObject, int64(len(fakeContent))) |
| 354 |
_, err = fakeHasher.Write(fakeContent) |
| 355 |
c.Assert(err, IsNil) |
| 356 |
fakeHash := fakeHasher.Sum() |
| 357 |
c.Assert(actualHash, Not(DeepEquals), fakeHash, Commentf("test setup: hashes should be different")) |
| 358 |
|
| 359 |
// Create an index with matching metadata but zero ModTime and wrong hash |
| 360 |
idx := &index.Index{ |
| 361 |
Version: 2, |
| 362 |
Entries: []*index.Entry{ |
| 363 |
{ |
| 364 |
Name: "testfile", |
| 365 |
Hash: fakeHash, // Wrong hash to prove it gets re-hashed |
| 366 |
Size: uint32(len(content)), |
| 367 |
ModifiedAt: modTime, |
| 368 |
Mode: filemode.Regular, |
| 369 |
}, |
| 370 |
}, |
| 371 |
ModTime: time.Time{}, // Zero time - simulates in-memory storage |
| 372 |
} |
| 373 |
|
| 374 |
// Create node with this index |
| 375 |
fsNode := NewRootNodeWithOptions(fs, nil, Options{Index: idx}) |
| 376 |
|
| 377 |
children, err := fsNode.Children() |
| 378 |
c.Assert(err, IsNil) |
| 379 |
c.Assert(children, HasLen, 1, Commentf("should have one file")) |
| 380 |
|
| 381 |
fileNode := children[0] |
| 382 |
fileHash := fileNode.Hash() |
| 383 |
|
| 384 |
// The expected hash should be the actual file content, not the fake hash from index |
| 385 |
expectedHash := append(actualHash[:], filemode.Regular.Bytes()...) |
| 386 |
|
| 387 |
c.Assert(expectedHash, DeepEquals, fileHash, Commentf("should hash actual file content when idx.ModTime is zero, not use stale index hash")) |
| 388 |
} |