| 1 |
package desync |
| 2 |
|
| 3 |
import ( |
| 4 |
gnutar "archive/tar" |
| 5 |
"bytes" |
| 6 |
"context" |
| 7 |
"io" |
| 8 |
"os" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/stretchr/testify/require" |
| 12 |
) |
| 13 |
|
| 14 |
func TestGnuTarWrite(t *testing.T) { |
| 15 |
// Input catar archive |
| 16 |
r, err := os.Open("testdata/complex.catar") |
| 17 |
if err != nil { |
| 18 |
t.Fatal(err) |
| 19 |
} |
| 20 |
defer r.Close() |
| 21 |
|
| 22 |
// Expected output |
| 23 |
exp, err := os.ReadFile("testdata/complex.gnu-tar") |
| 24 |
if err != nil { |
| 25 |
t.Fatal(err) |
| 26 |
} |
| 27 |
|
| 28 |
// Output GNU tar archive |
| 29 |
b := new(bytes.Buffer) |
| 30 |
|
| 31 |
// Write in GNU tar format |
| 32 |
fs := NewTarWriter(b) |
| 33 |
if err := UnTar(context.Background(), r, fs); err != nil { |
| 34 |
t.Fatal(err) |
| 35 |
} |
| 36 |
fs.Close() |
| 37 |
|
| 38 |
// Compare to expected |
| 39 |
if !bytes.Equal(b.Bytes(), exp) { |
| 40 |
t.Fatal("tar file does not match expected") |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
func TestTarXattrHelpers(t *testing.T) { |
| 45 |
// Empty/nil input must yield a nil map so the header stays in its |
| 46 |
// original (non-PAX) format. |
| 47 |
require.Nil(t, xattrsToPAXRecords(nil), "expected nil for nil xattrs") |
| 48 |
require.Nil(t, xattrsToPAXRecords(Xattrs{}), "expected nil for empty xattrs") |
| 49 |
|
| 50 |
// Keys must get the SCHILY.xattr. prefix archive/tar uses internally. |
| 51 |
in := Xattrs{"user.comment": "hello", "security.selinux": "ctx"} |
| 52 |
rec := xattrsToPAXRecords(in) |
| 53 |
want := map[string]string{ |
| 54 |
"SCHILY.xattr.user.comment": "hello", |
| 55 |
"SCHILY.xattr.security.selinux": "ctx", |
| 56 |
} |
| 57 |
require.Equal(t, want, rec) |
| 58 |
|
| 59 |
// Reverse must strip the prefix and ignore unrelated PAX records. |
| 60 |
rec["mtime"] = "12345" |
| 61 |
rec["path"] = "some/file" |
| 62 |
require.Equal(t, map[string]string(in), paxRecordsToXattrs(rec)) |
| 63 |
require.Nil(t, paxRecordsToXattrs(map[string]string{"mtime": "1"}), |
| 64 |
"expected nil when no SCHILY records present") |
| 65 |
require.Nil(t, paxRecordsToXattrs(nil), "expected nil for nil records") |
| 66 |
} |
| 67 |
|
| 68 |
func TestTarXattrsRoundTrip(t *testing.T) { |
| 69 |
xattrs := Xattrs{"user.comment": "hello", "user.tag": "v1"} |
| 70 |
|
| 71 |
// CreateDir/File/Symlink hardcode FormatGNU, which archive/tar rejects |
| 72 |
// in combination with xattrs. CreateDevice leaves the format |
| 73 |
// unspecified, so the writer negotiates PAX and the xattr path is |
| 74 |
// actually exercised. |
| 75 |
var buf bytes.Buffer |
| 76 |
w := NewTarWriter(&buf) |
| 77 |
require.NoError(t, w.CreateDevice(NodeDevice{ |
| 78 |
Name: "dev0", Mode: 0644, Major: 1, Minor: 3, Xattrs: xattrs, |
| 79 |
})) |
| 80 |
require.NoError(t, w.CreateDevice(NodeDevice{ |
| 81 |
Name: "dev1", Mode: 0644, Major: 1, Minor: 4, |
| 82 |
})) |
| 83 |
require.NoError(t, w.Close()) |
| 84 |
|
| 85 |
// On the wire the xattrs must be SCHILY.xattr.<name> PAX records, |
| 86 |
// exactly as the deprecated Header.Xattrs field produced them. |
| 87 |
sr := gnutar.NewReader(bytes.NewReader(buf.Bytes())) |
| 88 |
h, err := sr.Next() |
| 89 |
require.NoError(t, err) |
| 90 |
for k, v := range xattrs { |
| 91 |
require.Equal(t, v, h.PAXRecords["SCHILY.xattr."+k], |
| 92 |
"PAX record SCHILY.xattr.%s", k) |
| 93 |
} |
| 94 |
|
| 95 |
// Reading back through TarReader must reconstruct the xattr map, and a |
| 96 |
// node without xattrs must yield a nil map (unchanged behavior). |
| 97 |
rd := NewTarReader(bytes.NewReader(buf.Bytes()), TarReaderOptions{}) |
| 98 |
f0, err := rd.Next() |
| 99 |
require.NoError(t, err) |
| 100 |
require.Equal(t, map[string]string(xattrs), f0.Xattrs) |
| 101 |
f1, err := rd.Next() |
| 102 |
require.NoError(t, err) |
| 103 |
require.Nil(t, f1.Xattrs, "expected nil xattrs for node without any") |
| 104 |
_, err = rd.Next() |
| 105 |
require.ErrorIs(t, err, io.EOF) |
| 106 |
} |