| 1 |
// SPDX-License-Identifier: MPL-2.0 |
| 2 |
|
| 3 |
//go:build linux |
| 4 |
|
| 5 |
// Copyright (C) 2024-2025 Aleksa Sarai <[email protected]> |
| 6 |
// Copyright (C) 2024-2025 SUSE LLC |
| 7 |
// |
| 8 |
// This Source Code Form is subject to the terms of the Mozilla Public |
| 9 |
// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 10 |
// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 11 |
|
| 12 |
package testutils |
| 13 |
|
| 14 |
import ( |
| 15 |
"errors" |
| 16 |
"fmt" |
| 17 |
"os" |
| 18 |
"path/filepath" |
| 19 |
"strconv" |
| 20 |
"strings" |
| 21 |
|
| 22 |
"github.com/stretchr/testify/require" |
| 23 |
"golang.org/x/sys/unix" |
| 24 |
|
| 25 |
"github.com/cyphar/filepath-securejoin/pathrs-lite/internal/fd" |
| 26 |
"github.com/cyphar/filepath-securejoin/pathrs-lite/internal/linux" |
| 27 |
) |
| 28 |
|
| 29 |
// RequireRoot skips the current test if we are not root. |
| 30 |
func RequireRoot(t TestingT) { |
| 31 |
if os.Geteuid() != 0 { |
| 32 |
t.Skip("test requires root") |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
// RequireRenameExchange skips the current test if renameat2(2) is not |
| 37 |
// supported on the running system. |
| 38 |
func RequireRenameExchange(t TestingT) { |
| 39 |
err := unix.Renameat2(unix.AT_FDCWD, ".", unix.AT_FDCWD, ".", unix.RENAME_EXCHANGE) |
| 40 |
if errors.Is(err, unix.ENOSYS) { |
| 41 |
t.Skip("test requires RENAME_EXCHANGE support") |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
// TDoFunc is effectively a func(t *testing.T) function but using the |
| 46 |
// [TestingT] interface to allow us to write testutils with non-test code. The |
| 47 |
// argument is virtually guaranteed to be a *testing.T instance so you can just |
| 48 |
// do a type assertion in the body of the closure. |
| 49 |
type TDoFunc func(ti TestingT) |
| 50 |
|
| 51 |
// TRunFunc is a wrapper around t.Run but done with an interface that can be |
| 52 |
// used in non-testing code. To use this, you should just define a wrapper |
| 53 |
// function like this: |
| 54 |
// |
| 55 |
// func tRunWrapper(t *testing.T) testutils.TRunFunc { |
| 56 |
// return func(name string, doFn testutils.TDoFunc) { |
| 57 |
// t.Run(name, func(t *testing.T) { |
| 58 |
// doFn(t) |
| 59 |
// }) |
| 60 |
// } |
| 61 |
// } |
| 62 |
// |
| 63 |
// and then use it with [WithWithoutOpenat2] like so: |
| 64 |
// |
| 65 |
// testutils.WithWithoutOpenat2(true, tRunWrapper(t), func(ti testutils.TestingT) { |
| 66 |
// t := ti.(*testing.T) //nolint:forcetypeassert // guaranteed to be true and in test code |
| 67 |
// /* test code */ |
| 68 |
// }) |
| 69 |
type TRunFunc func(name string, doFn TDoFunc) |
| 70 |
|
| 71 |
// WithWithoutOpenat2 runs a given test with and without openat2 (by forcefully |
| 72 |
// disabling its usage). |
| 73 |
func WithWithoutOpenat2(doAuto bool, tRunFn TRunFunc, doFn TDoFunc) { |
| 74 |
if doAuto { |
| 75 |
tRunFn("openat2=auto", doFn) |
| 76 |
} |
| 77 |
for _, useOpenat2 := range []bool{true, false} { |
| 78 |
useOpenat2 := useOpenat2 // copy iterator |
| 79 |
tRunFn(fmt.Sprintf("openat2=%v", useOpenat2), func(t TestingT) { |
| 80 |
if useOpenat2 && !linux.HasOpenat2() { |
| 81 |
t.Skip("no openat2 support") |
| 82 |
} |
| 83 |
|
| 84 |
origHasOpenat2 := linux.HasOpenat2 |
| 85 |
linux.HasOpenat2 = func() bool { return useOpenat2 } |
| 86 |
defer func() { linux.HasOpenat2 = origHasOpenat2 }() |
| 87 |
|
| 88 |
if !useOpenat2 { |
| 89 |
origOpenat2 := fd.Openat2 |
| 90 |
fd.Openat2 = func(_ fd.Fd, _ string, _ *unix.OpenHow) (*os.File, error) { |
| 91 |
return nil, fmt.Errorf("INTERNAL ERROR THAT SHOULD NEVER BE SEEN: %w", unix.ENOSYS) |
| 92 |
} |
| 93 |
defer func() { fd.Openat2 = origOpenat2 }() |
| 94 |
} |
| 95 |
|
| 96 |
doFn(t) |
| 97 |
}) |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
// CreateInTree creates a given inode inside the root directory. |
| 102 |
// |
| 103 |
// Format: |
| 104 |
// |
| 105 |
// dir <name> <?uid:gid:mode> |
| 106 |
// file <name> <?content> <?uid:gid:mode> |
| 107 |
// symlink <name> <target> |
| 108 |
// char <name> <major> <minor> <?uid:gid:mode> |
| 109 |
// block <name> <major> <minor> <?uid:gid:mode> |
| 110 |
// fifo <name> <?uid:gid:mode> |
| 111 |
// sock <name> <?uid:gid:mode> |
| 112 |
func CreateInTree(t TestingT, root, spec string) { |
| 113 |
f := strings.Fields(spec) |
| 114 |
if len(f) < 2 { |
| 115 |
t.Fatalf("invalid spec %q", spec) |
| 116 |
} |
| 117 |
inoType, subPath, f := f[0], f[1], f[2:] |
| 118 |
fullPath := filepath.Join(root, subPath) |
| 119 |
|
| 120 |
var setOwnerMode *string |
| 121 |
switch inoType { |
| 122 |
case "dir": |
| 123 |
if len(f) >= 1 { |
| 124 |
setOwnerMode = &f[0] |
| 125 |
} |
| 126 |
MkdirAll(t, fullPath, 0o755) |
| 127 |
case "file": |
| 128 |
var contents []byte |
| 129 |
if len(f) >= 1 { |
| 130 |
contents = []byte(f[0]) |
| 131 |
} |
| 132 |
if len(f) >= 2 { |
| 133 |
setOwnerMode = &f[1] |
| 134 |
} |
| 135 |
WriteFile(t, fullPath, contents, 0o644) |
| 136 |
case "symlink": |
| 137 |
if len(f) < 1 { |
| 138 |
t.Fatalf("invalid spec %q", spec) |
| 139 |
} |
| 140 |
target := f[0] |
| 141 |
Symlink(t, target, fullPath) |
| 142 |
case "char", "block": |
| 143 |
if len(f) < 2 { |
| 144 |
t.Fatalf("invalid spec %q", spec) |
| 145 |
} |
| 146 |
if len(f) >= 3 { |
| 147 |
setOwnerMode = &f[2] |
| 148 |
} |
| 149 |
|
| 150 |
major, err := strconv.Atoi(f[0]) |
| 151 |
require.NoErrorf(t, err, "mknod %s: parse major", subPath) |
| 152 |
minor, err := strconv.Atoi(f[1]) |
| 153 |
require.NoErrorf(t, err, "mknod %s: parse minor", subPath) |
| 154 |
dev := unix.Mkdev(uint32(major), uint32(minor)) |
| 155 |
|
| 156 |
var mode uint32 = 0o644 |
| 157 |
switch inoType { |
| 158 |
case "char": |
| 159 |
mode |= unix.S_IFCHR |
| 160 |
case "block": |
| 161 |
mode |= unix.S_IFBLK |
| 162 |
} |
| 163 |
err = unix.Mknod(fullPath, mode, int(dev)) |
| 164 |
require.NoErrorf(t, err, "mknod (%s %d:%d) %s", inoType, major, minor, fullPath) |
| 165 |
case "fifo", "sock": |
| 166 |
if len(f) >= 1 { |
| 167 |
setOwnerMode = &f[0] |
| 168 |
} |
| 169 |
var mode uint32 = 0o644 |
| 170 |
switch inoType { |
| 171 |
case "fifo": |
| 172 |
mode |= unix.S_IFIFO |
| 173 |
case "sock": |
| 174 |
mode |= unix.S_IFSOCK |
| 175 |
} |
| 176 |
err := unix.Mknod(fullPath, mode, 0) |
| 177 |
require.NoErrorf(t, err, "mk%s %s", inoType, fullPath) |
| 178 |
} |
| 179 |
if setOwnerMode != nil { |
| 180 |
// <?uid>:<?gid>:<?mode> |
| 181 |
fields := strings.Split(*setOwnerMode, ":") |
| 182 |
require.Lenf(t, fields, 3, "set owner-mode format uid:gid:mode") |
| 183 |
uidStr, gidStr, modeStr := fields[0], fields[1], fields[2] |
| 184 |
|
| 185 |
if uidStr != "" && gidStr != "" { |
| 186 |
uid, err := strconv.Atoi(uidStr) |
| 187 |
require.NoErrorf(t, err, "chown %s: parse uid", fullPath) |
| 188 |
gid, err := strconv.Atoi(gidStr) |
| 189 |
require.NoErrorf(t, err, "chown %s: parse gid", fullPath) |
| 190 |
err = unix.Chown(fullPath, uid, gid) |
| 191 |
require.NoErrorf(t, err, "chown %s", fullPath) |
| 192 |
} |
| 193 |
|
| 194 |
if modeStr != "" { |
| 195 |
mode, err := strconv.ParseUint(modeStr, 8, 32) |
| 196 |
require.NoErrorf(t, err, "chmod %s: parse mode", fullPath) |
| 197 |
err = unix.Chmod(fullPath, uint32(mode)) |
| 198 |
require.NoErrorf(t, err, "chmod %s", fullPath) |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
// CreateTree creates a rootfs tree using spec entries (as documented in |
| 204 |
// [CreateInTree]). The returned path is the path to the root of the new tree. |
| 205 |
func CreateTree(t TestingT, specs ...string) string { |
| 206 |
root := t.TempDir() |
| 207 |
|
| 208 |
// Put the root in a subdir. |
| 209 |
treeRoot := filepath.Join(root, "tree") |
| 210 |
MkdirAll(t, treeRoot, 0o755) |
| 211 |
|
| 212 |
for _, spec := range specs { |
| 213 |
CreateInTree(t, treeRoot, spec) |
| 214 |
} |
| 215 |
return treeRoot |
| 216 |
} |