zotregistry.dev/zot@v1.4.4-0.20240314164342-eec277e14d20/pkg/test/common/fs_test.go (about)

     1  package common_test
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"os"
     7  	"path"
     8  	"path/filepath"
     9  	"testing"
    10  	"time"
    11  
    12  	ispec "github.com/opencontainers/image-spec/specs-go/v1"
    13  	. "github.com/smartystreets/goconvey/convey"
    14  	"golang.org/x/crypto/bcrypt"
    15  
    16  	tcommon "zotregistry.dev/zot/pkg/test/common"
    17  )
    18  
    19  var ErrTestError = errors.New("ErrTestError")
    20  
    21  func TestCopyFiles(t *testing.T) {
    22  	Convey("sourceDir does not exist", t, func() {
    23  		err := tcommon.CopyFiles("/path/to/some/unexisting/directory", os.TempDir())
    24  		So(err, ShouldNotBeNil)
    25  	})
    26  	Convey("destDir is a file", t, func() {
    27  		dir := t.TempDir()
    28  
    29  		err := tcommon.CopyFiles("../../../test/data", dir)
    30  		So(err, ShouldBeNil)
    31  
    32  		err = tcommon.CopyFiles(dir, "/etc/passwd")
    33  		So(err, ShouldNotBeNil)
    34  	})
    35  	Convey("sourceDir does not have read permissions", t, func() {
    36  		dir := t.TempDir()
    37  
    38  		err := os.Chmod(dir, 0o300)
    39  		So(err, ShouldBeNil)
    40  
    41  		err = tcommon.CopyFiles(dir, os.TempDir())
    42  		So(err, ShouldNotBeNil)
    43  	})
    44  	Convey("sourceDir has a subfolder that does not have read permissions", t, func() {
    45  		dir := t.TempDir()
    46  
    47  		sdir := "subdir"
    48  		err := os.Mkdir(path.Join(dir, sdir), 0o300)
    49  		So(err, ShouldBeNil)
    50  
    51  		err = tcommon.CopyFiles(dir, os.TempDir())
    52  		So(err, ShouldNotBeNil)
    53  	})
    54  	Convey("sourceDir has a file that does not have read permissions", t, func() {
    55  		dir := t.TempDir()
    56  
    57  		filePath := path.Join(dir, "file.txt")
    58  		err := os.WriteFile(filePath, []byte("some dummy file content"), 0o644) //nolint: gosec
    59  		if err != nil {
    60  			panic(err)
    61  		}
    62  
    63  		err = os.Chmod(filePath, 0o300)
    64  		So(err, ShouldBeNil)
    65  
    66  		err = tcommon.CopyFiles(dir, os.TempDir())
    67  		So(err, ShouldNotBeNil)
    68  	})
    69  	Convey("sourceDir contains a folder starting with invalid characters", t, func() {
    70  		srcDir := t.TempDir()
    71  		dstDir := t.TempDir()
    72  
    73  		err := os.MkdirAll(path.Join(srcDir, "_trivy", "db"), 0o755)
    74  		if err != nil {
    75  			panic(err)
    76  		}
    77  
    78  		err = os.MkdirAll(path.Join(srcDir, "test-index"), 0o755)
    79  		if err != nil {
    80  			panic(err)
    81  		}
    82  
    83  		filePathTrivy := path.Join(srcDir, "_trivy", "db", "trivy.db")
    84  		err = os.WriteFile(filePathTrivy, []byte("some dummy file content"), 0o644) //nolint: gosec
    85  		if err != nil {
    86  			panic(err)
    87  		}
    88  
    89  		var index ispec.Index
    90  		content, err := json.Marshal(index)
    91  		if err != nil {
    92  			panic(err)
    93  		}
    94  
    95  		err = os.WriteFile(path.Join(srcDir, "test-index", "index.json"), content, 0o644) //nolint: gosec
    96  		if err != nil {
    97  			panic(err)
    98  		}
    99  
   100  		err = tcommon.CopyFiles(srcDir, dstDir)
   101  		So(err, ShouldBeNil)
   102  
   103  		_, err = os.Stat(path.Join(dstDir, "_trivy", "db", "trivy.db"))
   104  		So(err, ShouldNotBeNil)
   105  		So(os.IsNotExist(err), ShouldBeTrue)
   106  
   107  		_, err = os.Stat(path.Join(dstDir, "test-index", "index.json"))
   108  		So(err, ShouldBeNil)
   109  	})
   110  }
   111  
   112  func TestCopyFile(t *testing.T) {
   113  	Convey("destFilePath does not exist", t, func() {
   114  		err := tcommon.CopyFile("/path/to/srcFile", "~/path/to/some/unexisting/destDir/file")
   115  		So(err, ShouldNotBeNil)
   116  	})
   117  
   118  	Convey("sourceFile does not exist", t, func() {
   119  		err := tcommon.CopyFile("/path/to/some/unexisting/file", path.Join(t.TempDir(), "destFile.txt"))
   120  		So(err, ShouldNotBeNil)
   121  	})
   122  }
   123  
   124  func TestReadLogFileAndSearchString(t *testing.T) {
   125  	logFile, err := os.CreateTemp(t.TempDir(), "zot-log*.txt")
   126  	if err != nil {
   127  		panic(err)
   128  	}
   129  
   130  	logPath := logFile.Name()
   131  	defer os.Remove(logPath)
   132  
   133  	Convey("Invalid path", t, func() {
   134  		_, err = tcommon.ReadLogFileAndSearchString("invalidPath",
   135  			"cve-db update completed, next update scheduled after interval", 1*time.Second)
   136  		So(err, ShouldNotBeNil)
   137  	})
   138  
   139  	Convey("Time too short", t, func() {
   140  		ok, err := tcommon.ReadLogFileAndSearchString(logPath, "invalid string", time.Microsecond)
   141  		So(err, ShouldBeNil)
   142  		So(ok, ShouldBeFalse)
   143  	})
   144  }
   145  
   146  func TestReadLogFileAndCountStringOccurence(t *testing.T) {
   147  	logFile, err := os.CreateTemp(t.TempDir(), "zot-log*.txt")
   148  	if err != nil {
   149  		panic(err)
   150  	}
   151  
   152  	_, err = logFile.WriteString("line1\n line2\n line3 line1 line2\n line1")
   153  	if err != nil {
   154  		panic(err)
   155  	}
   156  
   157  	logPath := logFile.Name()
   158  	defer os.Remove(logPath)
   159  
   160  	Convey("Invalid path", t, func() {
   161  		_, err = tcommon.ReadLogFileAndCountStringOccurence("invalidPath",
   162  			"cve-db update completed, next update scheduled after interval", 1*time.Second, 1)
   163  		So(err, ShouldNotBeNil)
   164  	})
   165  
   166  	Convey("Time too short", t, func() {
   167  		ok, err := tcommon.ReadLogFileAndCountStringOccurence(logPath, "invalid string", time.Microsecond, 1)
   168  		So(err, ShouldBeNil)
   169  		So(ok, ShouldBeFalse)
   170  	})
   171  
   172  	Convey("Count occurrence working", t, func() {
   173  		ok, err := tcommon.ReadLogFileAndCountStringOccurence(logPath, "line1", 90*time.Second, 3)
   174  		So(err, ShouldBeNil)
   175  		So(ok, ShouldBeTrue)
   176  	})
   177  }
   178  
   179  func TestCopyTestKeysAndCerts(t *testing.T) {
   180  	Convey("CopyTestKeysAndCerts", t, func() {
   181  		// ------- Make test files unreadable -------
   182  		dir := t.TempDir()
   183  		file := filepath.Join(dir, "ca.crt")
   184  
   185  		_, err := os.Create(file)
   186  		So(err, ShouldBeNil)
   187  
   188  		err = os.Chmod(file, 0o000)
   189  		So(err, ShouldBeNil)
   190  
   191  		err = tcommon.CopyTestKeysAndCerts(dir)
   192  		So(err, ShouldNotBeNil)
   193  
   194  		err = os.Chmod(file, 0o777)
   195  		So(err, ShouldBeNil)
   196  
   197  		// ------- Copy fails -------
   198  
   199  		err = os.Chmod(dir, 0o000)
   200  		So(err, ShouldBeNil)
   201  
   202  		err = tcommon.CopyTestKeysAndCerts(file)
   203  		So(err, ShouldNotBeNil)
   204  
   205  		err = os.Chmod(dir, 0o777)
   206  		So(err, ShouldBeNil)
   207  
   208  		// ------- Folder creation fails -------
   209  
   210  		file = filepath.Join(dir, "a-file.file")
   211  		_, err = os.Create(file)
   212  		So(err, ShouldBeNil)
   213  
   214  		_, err = os.Stat(file)
   215  		So(err, ShouldBeNil)
   216  
   217  		err = tcommon.CopyTestKeysAndCerts(file)
   218  		So(err, ShouldNotBeNil)
   219  
   220  		// ----- /test/data doesn't exist ------
   221  		workDir, err := os.Getwd()
   222  		So(err, ShouldBeNil)
   223  		defer func() { _ = os.Chdir(workDir) }()
   224  
   225  		dir = t.TempDir()
   226  		file = filepath.Join(dir, "go.mod")
   227  		_, err = os.Create(file)
   228  		So(err, ShouldBeNil)
   229  		_, err = os.Stat(file)
   230  		So(err, ShouldBeNil)
   231  		err = os.Chdir(dir)
   232  		So(err, ShouldBeNil)
   233  		err = tcommon.CopyTestKeysAndCerts(dir)
   234  		So(err, ShouldNotBeNil)
   235  		So(err.Error(), ShouldContainSubstring, "CopyFiles os.Stat failed")
   236  
   237  		// --- GetProjectRootDir call fails -----
   238  		err = os.Chdir(os.TempDir())
   239  		So(err, ShouldBeNil)
   240  		err = tcommon.CopyTestKeysAndCerts(os.TempDir())
   241  		So(err, ShouldNotBeNil)
   242  		So(err, ShouldEqual, tcommon.ErrNoGoModFileFound)
   243  	})
   244  }
   245  
   246  func TestGetProjectRootDir(t *testing.T) {
   247  	Convey("GetProjectRootDir", t, func() {
   248  		path, err := tcommon.GetProjectRootDir()
   249  		So(err, ShouldBeNil)
   250  		So(len(path), ShouldBeGreaterThan, 0)
   251  	})
   252  	Convey("GetProjectRootDir negative testing", t, func() {
   253  		workDir, err := os.Getwd()
   254  		So(err, ShouldBeNil)
   255  		defer func() { _ = os.Chdir(workDir) }()
   256  
   257  		err = os.Chdir(os.TempDir())
   258  		So(err, ShouldBeNil)
   259  		path, err := tcommon.GetProjectRootDir()
   260  		So(err, ShouldNotBeNil)
   261  		So(err, ShouldEqual, tcommon.ErrNoGoModFileFound)
   262  		So(path, ShouldBeZeroValue)
   263  	})
   264  }
   265  
   266  func TestGetCredString(t *testing.T) {
   267  	Convey("GetCredString panics", t, func() {
   268  		passwordSize := 100
   269  		pass := make([]byte, passwordSize)
   270  		for i := 0; i < passwordSize; i++ {
   271  			pass[i] = 'Y'
   272  		}
   273  		f := func() { tcommon.GetCredString("testUser", string(pass)) }
   274  		So(f, ShouldPanicWith, bcrypt.ErrPasswordTooLong)
   275  	})
   276  }