Skip to content

Commit

Permalink
Merge branches 'pub/testutil-pkgdata-empty-names', 'pub/testutil-pkgd…
Browse files Browse the repository at this point in the history
…ata-test-deb' and 'pub/testutil-pkgdata-shorthands' into pub/testutil-pkgdata
  • Loading branch information
woky committed Jun 22, 2023
4 parents afad3d2 + 990f689 + 996381f + a76d63c commit 95c51be
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 1 deletion.
43 changes: 42 additions & 1 deletion internal/testutil/pkgdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"archive/tar"
"bytes"
"encoding/base64"
"strings"
"time"

"github.com/blakesmith/ar"
Expand Down Expand Up @@ -174,7 +175,7 @@ func fixupTarEntry(entry *TarEntry) {
if hdr.Typeflag == 0 {
if hdr.Linkname != "" {
hdr.Typeflag = tar.TypeSymlink
} else if hdr.Name[len(hdr.Name)-1] == '/' {
} else if strings.HasSuffix(hdr.Name, "/") {
hdr.Typeflag = tar.TypeDir
} else {
hdr.Typeflag = tar.TypeReg
Expand Down Expand Up @@ -265,3 +266,43 @@ func MakeDeb(entries []TarEntry) ([]byte, error) {
}
return buf.Bytes(), nil
}

func MakeTestDeb(entries []TarEntry) []byte {
data, err := MakeDeb(entries)
if err != nil {
panic(err)
}
return data
}

func REG(mode int64, path, content string) TarEntry {
return TarEntry{
Header: tar.Header{
Typeflag: tar.TypeReg,
Name: path,
Mode: mode,
},
Content: []byte(content),
}
}

func DIR(mode int64, path string) TarEntry {
return TarEntry{
Header: tar.Header{
Typeflag: tar.TypeDir,
Name: path,
Mode: mode,
},
}
}

func LNK(mode int64, path, target string) TarEntry {
return TarEntry{
Header: tar.Header{
Typeflag: tar.TypeSymlink,
Name: path,
Mode: mode,
Linkname: target,
},
}
}
66 changes: 66 additions & 0 deletions internal/testutil/pkgdata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,21 @@ var pkgdataCheckEntries = []checkTarEntry{{
ModTime: epochStartTime,
Format: tar.FormatGNU,
},
}, {
testutil.TarEntry{
Header: tar.Header{
Name: "",
},
},
tar.Header{
Typeflag: tar.TypeReg,
Name: "",
Mode: 00644,
Uname: "root",
Gname: "root",
ModTime: epochStartTime,
Format: tar.FormatGNU,
},
}}

func (s *pkgdataSuite) TestMakeDeb(c *C) {
Expand Down Expand Up @@ -370,3 +385,54 @@ func (s *pkgdataSuite) TestMakeDeb(c *C) {
_, err = arReader.Next()
c.Assert(err, Equals, io.EOF)
}

func (s *S) TestMakeTestDeb(c *C) {
defer func() {
err := recover()
c.Assert(err, ErrorMatches, `.*: cannot encode header: invalid PAX record: "path = \\x00./foo.*`)
}()
testutil.MakeTestDeb([]testutil.TarEntry{{
Header: tar.Header{
Name: "\000./foo",
},
}})
}

func (s *S) TestTarEntryShortHands(c *C) {
var testCases = []struct {
shorthand testutil.TarEntry
result testutil.TarEntry
}{{
testutil.REG(0600, "./document.txt", "cats are best"),
testutil.TarEntry{
Header: tar.Header{
Typeflag: tar.TypeReg,
Name: "./document.txt",
Mode: 0600,
},
Content: []byte("cats are best"),
},
}, {
testutil.DIR(0755, "./home/user"),
testutil.TarEntry{
Header: tar.Header{
Typeflag: tar.TypeDir,
Name: "./home/user",
Mode: 0755,
},
},
}, {
testutil.LNK(0755, "./lib", "./usr/lib/"),
testutil.TarEntry{
Header: tar.Header{
Typeflag: tar.TypeSymlink,
Name: "./lib",
Mode: 0755,
Linkname: "./usr/lib/",
},
},
}}
for _, test := range testCases {
c.Assert(test.shorthand, DeepEquals, test.result)
}
}

0 comments on commit 95c51be

Please sign in to comment.