github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/daemon/graphdriver/graphtest/testutil_unix.go (about)

     1  // +build linux freebsd
     2  
     3  package graphtest // import "github.com/demonoid81/moby/daemon/graphdriver/graphtest"
     4  
     5  import (
     6  	"os"
     7  	"syscall"
     8  	"testing"
     9  
    10  	contdriver "github.com/containerd/continuity/driver"
    11  	"github.com/demonoid81/moby/daemon/graphdriver"
    12  	"golang.org/x/sys/unix"
    13  	"gotest.tools/v3/assert"
    14  	is "gotest.tools/v3/assert/cmp"
    15  )
    16  
    17  func verifyFile(t testing.TB, path string, mode os.FileMode, uid, gid uint32) {
    18  	fi, err := os.Stat(path)
    19  	assert.NilError(t, err)
    20  
    21  	actual := fi.Mode()
    22  	assert.Check(t, is.Equal(mode&os.ModeType, actual&os.ModeType), path)
    23  	assert.Check(t, is.Equal(mode&os.ModePerm, actual&os.ModePerm), path)
    24  	assert.Check(t, is.Equal(mode&os.ModeSticky, actual&os.ModeSticky), path)
    25  	assert.Check(t, is.Equal(mode&os.ModeSetuid, actual&os.ModeSetuid), path)
    26  	assert.Check(t, is.Equal(mode&os.ModeSetgid, actual&os.ModeSetgid), path)
    27  
    28  	if stat, ok := fi.Sys().(*syscall.Stat_t); ok {
    29  		assert.Check(t, is.Equal(uid, stat.Uid), path)
    30  		assert.Check(t, is.Equal(gid, stat.Gid), path)
    31  	}
    32  }
    33  
    34  func createBase(t testing.TB, driver graphdriver.Driver, name string) {
    35  	// We need to be able to set any perms
    36  	oldmask := unix.Umask(0)
    37  	defer unix.Umask(oldmask)
    38  
    39  	err := driver.CreateReadWrite(name, "", nil)
    40  	assert.NilError(t, err)
    41  
    42  	dirFS, err := driver.Get(name, "")
    43  	assert.NilError(t, err)
    44  	defer driver.Put(name)
    45  
    46  	subdir := dirFS.Join(dirFS.Path(), "a subdir")
    47  	assert.NilError(t, dirFS.Mkdir(subdir, 0705|os.ModeSticky))
    48  	assert.NilError(t, dirFS.Lchown(subdir, 1, 2))
    49  
    50  	file := dirFS.Join(dirFS.Path(), "a file")
    51  	err = contdriver.WriteFile(dirFS, file, []byte("Some data"), 0222|os.ModeSetuid)
    52  	assert.NilError(t, err)
    53  }
    54  
    55  func verifyBase(t testing.TB, driver graphdriver.Driver, name string) {
    56  	dirFS, err := driver.Get(name, "")
    57  	assert.NilError(t, err)
    58  	defer driver.Put(name)
    59  
    60  	subdir := dirFS.Join(dirFS.Path(), "a subdir")
    61  	verifyFile(t, subdir, 0705|os.ModeDir|os.ModeSticky, 1, 2)
    62  
    63  	file := dirFS.Join(dirFS.Path(), "a file")
    64  	verifyFile(t, file, 0222|os.ModeSetuid, 0, 0)
    65  
    66  	files, err := readDir(dirFS, dirFS.Path())
    67  	assert.NilError(t, err)
    68  	assert.Check(t, is.Len(files, 2))
    69  }