File: open_test.go

package info (click to toggle)
golang-github-containers-buildah 1.39.3%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 7,724 kB
  • sloc: sh: 2,398; makefile: 236; perl: 187; asm: 16; awk: 12; ansic: 1
file content (81 lines) | stat: -rw-r--r-- 2,798 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package open

import (
	"io"
	"os"
	"path/filepath"
	"strings"
	"testing"

	"github.com/containers/storage/pkg/reexec"
	"github.com/stretchr/testify/require"
	"golang.org/x/sys/unix"
)

func TestMain(m *testing.M) {
	if reexec.Init() {
		return
	}
	os.Exit(m.Run())
}

func TestOpenInChroot(t *testing.T) {
	tmpdir := t.TempDir()
	firstContents := []byte{0, 1, 2, 3}
	secondContents := []byte{4, 5, 6, 7}
	require.NoErrorf(t, os.WriteFile(filepath.Join(tmpdir, "a"), firstContents, 0o644), "creating first test file")
	require.NoErrorf(t, os.MkdirAll(filepath.Join(tmpdir, tmpdir), 0o755), "creating test subdirectory")
	require.NoErrorf(t, os.WriteFile(filepath.Join(tmpdir, tmpdir, "a"), secondContents, 0o644), "creating second test file")

	result := inChroot(requests{
		Open: []request{
			{
				Path: filepath.Join(tmpdir, "a"),
				Mode: unix.O_RDONLY,
			},
		},
	})
	require.Empty(t, result.Err, "result from first client")
	require.Equal(t, 1, len(result.Open), "results from first client")
	require.Empty(t, result.Open[0].Err, "first (only) result from first client")
	f := os.NewFile(result.Open[0].Fd, "file from first subprocess")
	contents, err := io.ReadAll(f)
	require.NoErrorf(t, err, "reading from file from first subprocess")
	require.Equalf(t, firstContents, contents, "contents of file from first subprocess")
	f.Close()

	result = inChroot(requests{
		Root: tmpdir,
		Open: []request{
			{
				Path: filepath.Join(tmpdir, "a"),
				Mode: unix.O_RDONLY,
			},
		},
	})
	if strings.Contains(result.Err, "operation not permitted") {
		t.Skipf("Skipping test: %v", result.Err)
	}
	require.Empty(t, result.Err, "result from second client")
	require.Equal(t, 1, len(result.Open), "results from second client")
	require.Empty(t, result.Open[0].Err, "first (only) result from second client")
	f = os.NewFile(result.Open[0].Fd, "file from second subprocess")
	contents, err = io.ReadAll(f)
	require.NoErrorf(t, err, "reading from file from second subprocess")
	require.Equalf(t, secondContents, contents, "contents of file from second subprocess")
	f.Close()

	fd, errno, err := InChroot(tmpdir, "", filepath.Join(tmpdir, "a"), unix.O_RDONLY, 0)
	require.NoErrorf(t, err, "wrapper for opening just one item")
	require.Zero(t, errno, "errno from open file")
	f = os.NewFile(uintptr(fd), "file from third subprocess")
	require.NoErrorf(t, err, "reading from file from third subprocess")
	require.Equalf(t, secondContents, contents, "contents of file from third subprocess")
	f.Close()

	fd, errno, err = InChroot(tmpdir, "", filepath.Join(tmpdir, "b"), unix.O_RDONLY, 0)
	require.Errorf(t, err, "attempting to open a non-existent file")
	require.NotZero(t, errno, "attempting to open a non-existent file")
	require.Equal(t, -1, fd, "returned descriptor when open fails")
	f.Close()
}