github.com/nektos/act@v0.2.63/pkg/runner/action_cache_test.go (about)

     1  package runner
     2  
     3  import (
     4  	"archive/tar"
     5  	"bytes"
     6  	"context"
     7  	"io"
     8  	"os"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  //nolint:gosec
    15  func TestActionCache(t *testing.T) {
    16  	a := assert.New(t)
    17  	cache := &GoGitActionCache{
    18  		Path: os.TempDir(),
    19  	}
    20  	ctx := context.Background()
    21  	cacheDir := "nektos/act-test-actions"
    22  	repo := "https://github.com/nektos/act-test-actions"
    23  	refs := []struct {
    24  		Name     string
    25  		CacheDir string
    26  		Repo     string
    27  		Ref      string
    28  	}{
    29  		{
    30  			Name:     "Fetch Branch Name",
    31  			CacheDir: cacheDir,
    32  			Repo:     repo,
    33  			Ref:      "main",
    34  		},
    35  		{
    36  			Name:     "Fetch Branch Name Absolutely",
    37  			CacheDir: cacheDir,
    38  			Repo:     repo,
    39  			Ref:      "refs/heads/main",
    40  		},
    41  		{
    42  			Name:     "Fetch HEAD",
    43  			CacheDir: cacheDir,
    44  			Repo:     repo,
    45  			Ref:      "HEAD",
    46  		},
    47  		{
    48  			Name:     "Fetch Sha",
    49  			CacheDir: cacheDir,
    50  			Repo:     repo,
    51  			Ref:      "de984ca37e4df4cb9fd9256435a3b82c4a2662b1",
    52  		},
    53  	}
    54  	for _, c := range refs {
    55  		t.Run(c.Name, func(t *testing.T) {
    56  			sha, err := cache.Fetch(ctx, c.CacheDir, c.Repo, c.Ref, "")
    57  			if !a.NoError(err) || !a.NotEmpty(sha) {
    58  				return
    59  			}
    60  			atar, err := cache.GetTarArchive(ctx, c.CacheDir, sha, "js")
    61  			if !a.NoError(err) || !a.NotEmpty(atar) {
    62  				return
    63  			}
    64  			mytar := tar.NewReader(atar)
    65  			th, err := mytar.Next()
    66  			if !a.NoError(err) || !a.NotEqual(0, th.Size) {
    67  				return
    68  			}
    69  			buf := &bytes.Buffer{}
    70  			// G110: Potential DoS vulnerability via decompression bomb (gosec)
    71  			_, err = io.Copy(buf, mytar)
    72  			a.NoError(err)
    73  			str := buf.String()
    74  			a.NotEmpty(str)
    75  		})
    76  	}
    77  }