2021-02-22 17:27:13 +05:30
|
|
|
package zipartifacts_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"compress/gzip"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2022-01-26 12:08:38 +05:30
|
|
|
zip "gitlab.com/gitlab-org/golang-archive-zip"
|
2021-02-22 17:27:13 +05:30
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
"gitlab.com/gitlab-org/gitlab/workhorse/internal/zipartifacts"
|
2021-02-22 17:27:13 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
func generateTestArchive(w io.Writer) error {
|
|
|
|
archive := zip.NewWriter(w)
|
|
|
|
|
|
|
|
// non-POSIX paths are here just to test if we never enter infinite loop
|
|
|
|
files := []string{"file1", "some/file/dir/", "some/file/dir/file2", "../../test12/test",
|
|
|
|
"/usr/bin/test", `c:\windows\win32.exe`, `c:/windows/win.dll`, "./f/asd", "/"}
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
archiveFile, err := archive.Create(file)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprint(archiveFile, file)
|
|
|
|
}
|
|
|
|
|
|
|
|
return archive.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateMetadata(r io.Reader) error {
|
|
|
|
gz, err := gzip.NewReader(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
meta, err := ioutil.ReadAll(gz)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
paths := []string{"file1", "some/", "some/file/", "some/file/dir/", "some/file/dir/file2"}
|
|
|
|
for _, path := range paths {
|
|
|
|
if !bytes.Contains(meta, []byte(path+"\x00")) {
|
|
|
|
return fmt.Errorf(fmt.Sprintf("zipartifacts: metadata for path %q not found", path))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenerateZipMetadataFromFile(t *testing.T) {
|
|
|
|
var metaBuffer bytes.Buffer
|
|
|
|
|
|
|
|
f, err := ioutil.TempFile("", "workhorse-metadata.zip-")
|
|
|
|
if f != nil {
|
|
|
|
defer os.Remove(f.Name())
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
err = generateTestArchive(f)
|
|
|
|
require.NoError(t, err)
|
|
|
|
f.Close()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
archive, err := zipartifacts.OpenArchive(ctx, f.Name())
|
|
|
|
require.NoError(t, err, "zipartifacts: OpenArchive failed")
|
|
|
|
|
|
|
|
err = zipartifacts.GenerateZipMetadata(&metaBuffer, archive)
|
|
|
|
require.NoError(t, err, "zipartifacts: GenerateZipMetadata failed")
|
|
|
|
|
|
|
|
err = validateMetadata(&metaBuffer)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestErrNotAZip(t *testing.T) {
|
|
|
|
f, err := ioutil.TempFile("", "workhorse-metadata.zip-")
|
|
|
|
if f != nil {
|
|
|
|
defer os.Remove(f.Name())
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
_, err = fmt.Fprint(f, "Not a zip file")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
_, err = zipartifacts.OpenArchive(ctx, f.Name())
|
|
|
|
require.Equal(t, zipartifacts.ErrorCode[zipartifacts.CodeNotZip], err, "OpenArchive requires a zip file")
|
|
|
|
}
|