Tree @debian/0.0_git20201101.da20708-1 (Download .tar.gz)
- ..
- linux
- .gitignore
- affinity_linux.go
- aliases.go
- asm_aix_ppc64.s
- asm_darwin_386.s
- asm_darwin_amd64.s
- asm_darwin_arm.s
- asm_darwin_arm64.s
- asm_dragonfly_amd64.s
- asm_freebsd_386.s
- asm_freebsd_amd64.s
- asm_freebsd_arm.s
- asm_freebsd_arm64.s
- asm_linux_386.s
- asm_linux_amd64.s
- asm_linux_arm.s
- asm_linux_arm64.s
- asm_linux_mips64x.s
- asm_linux_mipsx.s
- asm_linux_ppc64x.s
- asm_linux_riscv64.s
- asm_linux_s390x.s
- asm_netbsd_386.s
- asm_netbsd_amd64.s
- asm_netbsd_arm.s
- asm_netbsd_arm64.s
- asm_openbsd_386.s
- asm_openbsd_amd64.s
- asm_openbsd_arm.s
- asm_openbsd_arm64.s
- asm_openbsd_mips64.s
- asm_solaris_amd64.s
- bluetooth_linux.go
- cap_freebsd.go
- constants.go
- creds_test.go
- darwin_test.go
- dev_aix_ppc.go
- dev_aix_ppc64.go
- dev_darwin.go
- dev_dragonfly.go
- dev_freebsd.go
- dev_linux.go
- dev_linux_test.go
- dev_netbsd.go
- dev_openbsd.go
- dirent.go
- dirent_test.go
- endian_big.go
- endian_little.go
- env_unix.go
- errors_freebsd_386.go
- errors_freebsd_amd64.go
- errors_freebsd_arm.go
- errors_freebsd_arm64.go
- example_exec_test.go
- example_flock_test.go
- export_test.go
- fcntl.go
- fcntl_darwin.go
- fcntl_linux_32bit.go
- fdset.go
- fdset_test.go
- gccgo.go
- gccgo_c.c
- gccgo_linux_amd64.go
- getdirentries_test.go
- ioctl.go
- mkall.sh
- mkasm_darwin.go
- mkerrors.sh
- mkmerge.go
- mkmerge_test.go
- mkpost.go
- mksyscall.go
- mksyscall_aix_ppc.go
- mksyscall_aix_ppc64.go
- mksyscall_solaris.go
- mksysctl_openbsd.go
- mksysnum.go
- mmap_unix_test.go
- openbsd_test.go
- pagesize_unix.go
- pledge_openbsd.go
- race.go
- race0.go
- readdirent_getdents.go
- readdirent_getdirentries.go
- README.md
- sendfile_test.go
- sockcmsg_dragonfly.go
- sockcmsg_linux.go
- sockcmsg_unix.go
- sockcmsg_unix_other.go
- str.go
- syscall.go
- syscall_aix.go
- syscall_aix_ppc.go
- syscall_aix_ppc64.go
- syscall_aix_test.go
- syscall_bsd.go
- syscall_bsd_test.go
- syscall_darwin.1_12.go
- syscall_darwin.1_13.go
- syscall_darwin.go
- syscall_darwin_386.go
- syscall_darwin_amd64.go
- syscall_darwin_arm.go
- syscall_darwin_arm64.go
- syscall_darwin_libSystem.go
- syscall_darwin_test.go
- syscall_dragonfly.go
- syscall_dragonfly_amd64.go
- syscall_freebsd.go
- syscall_freebsd_386.go
- syscall_freebsd_amd64.go
- syscall_freebsd_arm.go
- syscall_freebsd_arm64.go
- syscall_freebsd_test.go
- syscall_illumos.go
- syscall_internal_bsd_test.go
- syscall_internal_darwin_test.go
- syscall_internal_linux_test.go
- syscall_linux.go
- syscall_linux_386.go
- syscall_linux_amd64.go
- syscall_linux_amd64_gc.go
- syscall_linux_arm.go
- syscall_linux_arm64.go
- syscall_linux_gc.go
- syscall_linux_gc_386.go
- syscall_linux_gc_arm.go
- syscall_linux_gccgo_386.go
- syscall_linux_gccgo_arm.go
- syscall_linux_mips64x.go
- syscall_linux_mipsx.go
- syscall_linux_ppc64x.go
- syscall_linux_riscv64.go
- syscall_linux_s390x.go
- syscall_linux_sparc64.go
- syscall_linux_test.go
- syscall_netbsd.go
- syscall_netbsd_386.go
- syscall_netbsd_amd64.go
- syscall_netbsd_arm.go
- syscall_netbsd_arm64.go
- syscall_netbsd_test.go
- syscall_openbsd.go
- syscall_openbsd_386.go
- syscall_openbsd_amd64.go
- syscall_openbsd_arm.go
- syscall_openbsd_arm64.go
- syscall_openbsd_mips64.go
- syscall_openbsd_test.go
- syscall_solaris.go
- syscall_solaris_amd64.go
- syscall_solaris_test.go
- syscall_test.go
- syscall_unix.go
- syscall_unix_gc.go
- syscall_unix_gc_ppc64x.go
- syscall_unix_test.go
- timestruct.go
- timestruct_test.go
- types_aix.go
- types_darwin.go
- types_dragonfly.go
- types_freebsd.go
- types_netbsd.go
- types_openbsd.go
- types_solaris.go
- unveil_openbsd.go
- xattr_bsd.go
- xattr_test.go
- zerrors_aix_ppc.go
- zerrors_aix_ppc64.go
- zerrors_darwin_386.go
- zerrors_darwin_amd64.go
- zerrors_darwin_arm.go
- zerrors_darwin_arm64.go
- zerrors_dragonfly_amd64.go
- zerrors_freebsd_386.go
- zerrors_freebsd_amd64.go
- zerrors_freebsd_arm.go
- zerrors_freebsd_arm64.go
- zerrors_linux.go
- zerrors_linux_386.go
- zerrors_linux_amd64.go
- zerrors_linux_arm.go
- zerrors_linux_arm64.go
- zerrors_linux_mips.go
- zerrors_linux_mips64.go
- zerrors_linux_mips64le.go
- zerrors_linux_mipsle.go
- zerrors_linux_ppc64.go
- zerrors_linux_ppc64le.go
- zerrors_linux_riscv64.go
- zerrors_linux_s390x.go
- zerrors_linux_sparc64.go
- zerrors_netbsd_386.go
- zerrors_netbsd_amd64.go
- zerrors_netbsd_arm.go
- zerrors_netbsd_arm64.go
- zerrors_openbsd_386.go
- zerrors_openbsd_amd64.go
- zerrors_openbsd_arm.go
- zerrors_openbsd_arm64.go
- zerrors_openbsd_mips64.go
- zerrors_solaris_amd64.go
- zptrace_armnn_linux.go
- zptrace_linux_arm64.go
- zptrace_mipsnn_linux.go
- zptrace_mipsnnle_linux.go
- zptrace_x86_linux.go
- zsyscall_aix_ppc.go
- zsyscall_aix_ppc64.go
- zsyscall_aix_ppc64_gc.go
- zsyscall_aix_ppc64_gccgo.go
- zsyscall_darwin_386.1_13.go
- zsyscall_darwin_386.1_13.s
- zsyscall_darwin_386.go
- zsyscall_darwin_386.s
- zsyscall_darwin_amd64.1_13.go
- zsyscall_darwin_amd64.1_13.s
- zsyscall_darwin_amd64.go
- zsyscall_darwin_amd64.s
- zsyscall_darwin_arm.1_13.go
- zsyscall_darwin_arm.1_13.s
- zsyscall_darwin_arm.go
- zsyscall_darwin_arm.s
- zsyscall_darwin_arm64.1_13.go
- zsyscall_darwin_arm64.1_13.s
- zsyscall_darwin_arm64.go
- zsyscall_darwin_arm64.s
- zsyscall_dragonfly_amd64.go
- zsyscall_freebsd_386.go
- zsyscall_freebsd_amd64.go
- zsyscall_freebsd_arm.go
- zsyscall_freebsd_arm64.go
- zsyscall_illumos_amd64.go
- zsyscall_linux.go
- zsyscall_linux_386.go
- zsyscall_linux_amd64.go
- zsyscall_linux_arm.go
- zsyscall_linux_arm64.go
- zsyscall_linux_mips.go
- zsyscall_linux_mips64.go
- zsyscall_linux_mips64le.go
- zsyscall_linux_mipsle.go
- zsyscall_linux_ppc64.go
- zsyscall_linux_ppc64le.go
- zsyscall_linux_riscv64.go
- zsyscall_linux_s390x.go
- zsyscall_linux_sparc64.go
- zsyscall_netbsd_386.go
- zsyscall_netbsd_amd64.go
- zsyscall_netbsd_arm.go
- zsyscall_netbsd_arm64.go
- zsyscall_openbsd_386.go
- zsyscall_openbsd_amd64.go
- zsyscall_openbsd_arm.go
- zsyscall_openbsd_arm64.go
- zsyscall_openbsd_mips64.go
- zsyscall_solaris_amd64.go
- zsysctl_openbsd_386.go
- zsysctl_openbsd_amd64.go
- zsysctl_openbsd_arm.go
- zsysctl_openbsd_arm64.go
- zsysctl_openbsd_mips64.go
- zsysnum_darwin_386.go
- zsysnum_darwin_amd64.go
- zsysnum_darwin_arm.go
- zsysnum_darwin_arm64.go
- zsysnum_dragonfly_amd64.go
- zsysnum_freebsd_386.go
- zsysnum_freebsd_amd64.go
- zsysnum_freebsd_arm.go
- zsysnum_freebsd_arm64.go
- zsysnum_linux_386.go
- zsysnum_linux_amd64.go
- zsysnum_linux_arm.go
- zsysnum_linux_arm64.go
- zsysnum_linux_mips.go
- zsysnum_linux_mips64.go
- zsysnum_linux_mips64le.go
- zsysnum_linux_mipsle.go
- zsysnum_linux_ppc64.go
- zsysnum_linux_ppc64le.go
- zsysnum_linux_riscv64.go
- zsysnum_linux_s390x.go
- zsysnum_linux_sparc64.go
- zsysnum_netbsd_386.go
- zsysnum_netbsd_amd64.go
- zsysnum_netbsd_arm.go
- zsysnum_netbsd_arm64.go
- zsysnum_openbsd_386.go
- zsysnum_openbsd_amd64.go
- zsysnum_openbsd_arm.go
- zsysnum_openbsd_arm64.go
- zsysnum_openbsd_mips64.go
- ztypes_aix_ppc.go
- ztypes_aix_ppc64.go
- ztypes_darwin_386.go
- ztypes_darwin_amd64.go
- ztypes_darwin_arm.go
- ztypes_darwin_arm64.go
- ztypes_dragonfly_amd64.go
- ztypes_freebsd_386.go
- ztypes_freebsd_amd64.go
- ztypes_freebsd_arm.go
- ztypes_freebsd_arm64.go
- ztypes_linux.go
- ztypes_linux_386.go
- ztypes_linux_amd64.go
- ztypes_linux_arm.go
- ztypes_linux_arm64.go
- ztypes_linux_mips.go
- ztypes_linux_mips64.go
- ztypes_linux_mips64le.go
- ztypes_linux_mipsle.go
- ztypes_linux_ppc64.go
- ztypes_linux_ppc64le.go
- ztypes_linux_riscv64.go
- ztypes_linux_s390x.go
- ztypes_linux_sparc64.go
- ztypes_netbsd_386.go
- ztypes_netbsd_amd64.go
- ztypes_netbsd_arm.go
- ztypes_netbsd_arm64.go
- ztypes_openbsd_386.go
- ztypes_openbsd_amd64.go
- ztypes_openbsd_arm.go
- ztypes_openbsd_arm64.go
- ztypes_openbsd_mips64.go
- ztypes_solaris_amd64.go
syscall_freebsd_test.go @debian/0.0_git20201101.da20708-1 — raw · history · blame
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | // Copyright 2014 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // +build freebsd package unix_test import ( "flag" "fmt" "io/ioutil" "os" "os/exec" "path" "path/filepath" "runtime" "testing" "golang.org/x/sys/unix" ) func TestSysctlUint64(t *testing.T) { _, err := unix.SysctlUint64("vm.swap_total") if err != nil { t.Fatal(err) } } // FIXME: Infrastructure for launching tests in subprocesses stolen from openbsd_test.go - refactor? // testCmd generates a proper command that, when executed, runs the test // corresponding to the given key. type testProc struct { fn func() // should always exit instead of returning arg func(t *testing.T) string // generate argument for test cleanup func(arg string) error // for instance, delete coredumps from testing pledge success bool // whether zero-exit means success or failure } var ( testProcs = map[string]testProc{} procName = "" procArg = "" ) const ( optName = "sys-unix-internal-procname" optArg = "sys-unix-internal-arg" ) func init() { flag.StringVar(&procName, optName, "", "internal use only") flag.StringVar(&procArg, optArg, "", "internal use only") } func testCmd(procName string, procArg string) (*exec.Cmd, error) { exe, err := filepath.Abs(os.Args[0]) if err != nil { return nil, err } cmd := exec.Command(exe, "-"+optName+"="+procName, "-"+optArg+"="+procArg) cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr return cmd, nil } // ExitsCorrectly is a comprehensive, one-line-of-use wrapper for testing // a testProc with a key. func ExitsCorrectly(t *testing.T, procName string) { s := testProcs[procName] arg := "-" if s.arg != nil { arg = s.arg(t) } c, err := testCmd(procName, arg) defer func(arg string) { if err := s.cleanup(arg); err != nil { t.Fatalf("Failed to run cleanup for %s %s %#v", procName, err, err) } }(arg) if err != nil { t.Fatalf("Failed to construct command for %s", procName) } if (c.Run() == nil) != s.success { result := "succeed" if !s.success { result = "fail" } t.Fatalf("Process did not %s when it was supposed to", result) } } func TestMain(m *testing.M) { flag.Parse() if procName != "" { t := testProcs[procName] t.fn() os.Stderr.WriteString("test function did not exit\n") if t.success { os.Exit(1) } else { os.Exit(0) } } os.Exit(m.Run()) } // end of infrastructure const testfile = "gocapmodetest" const testfile2 = testfile + "2" func CapEnterTest() { _, err := os.OpenFile(path.Join(procArg, testfile), os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666) if err != nil { panic(fmt.Sprintf("OpenFile: %s", err)) } err = unix.CapEnter() if err != nil { panic(fmt.Sprintf("CapEnter: %s", err)) } _, err = os.OpenFile(path.Join(procArg, testfile2), os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666) if err == nil { panic("OpenFile works!") } if err.(*os.PathError).Err != unix.ECAPMODE { panic(fmt.Sprintf("OpenFile failed wrong: %s %#v", err, err)) } os.Exit(0) } func makeTempDir(t *testing.T) string { d, err := ioutil.TempDir("", "go_openat_test") if err != nil { t.Fatalf("TempDir failed: %s", err) } return d } func removeTempDir(arg string) error { err := os.RemoveAll(arg) if err != nil && err.(*os.PathError).Err == unix.ENOENT { return nil } return err } func init() { testProcs["cap_enter"] = testProc{ CapEnterTest, makeTempDir, removeTempDir, true, } } func TestCapEnter(t *testing.T) { if runtime.GOARCH != "amd64" { t.Skipf("skipping test on %s", runtime.GOARCH) } ExitsCorrectly(t, "cap_enter") } func OpenatTest() { f, err := os.Open(procArg) if err != nil { panic(err) } err = unix.CapEnter() if err != nil { panic(fmt.Sprintf("CapEnter: %s", err)) } fxx, err := unix.Openat(int(f.Fd()), "xx", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666) if err != nil { panic(err) } unix.Close(fxx) // The right to open BASE/xx is not ambient _, err = os.OpenFile(procArg+"/xx", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666) if err == nil { panic("OpenFile succeeded") } if err.(*os.PathError).Err != unix.ECAPMODE { panic(fmt.Sprintf("OpenFile failed wrong: %s %#v", err, err)) } // Can't make a new directory either err = os.Mkdir(procArg+"2", 0777) if err == nil { panic("MKdir succeeded") } if err.(*os.PathError).Err != unix.ECAPMODE { panic(fmt.Sprintf("Mkdir failed wrong: %s %#v", err, err)) } // Remove all caps except read and lookup. r, err := unix.CapRightsInit([]uint64{unix.CAP_READ, unix.CAP_LOOKUP}) if err != nil { panic(fmt.Sprintf("CapRightsInit failed: %s %#v", err, err)) } err = unix.CapRightsLimit(f.Fd(), r) if err != nil { panic(fmt.Sprintf("CapRightsLimit failed: %s %#v", err, err)) } // Check we can get the rights back again r, err = unix.CapRightsGet(f.Fd()) if err != nil { panic(fmt.Sprintf("CapRightsGet failed: %s %#v", err, err)) } b, err := unix.CapRightsIsSet(r, []uint64{unix.CAP_READ, unix.CAP_LOOKUP}) if err != nil { panic(fmt.Sprintf("CapRightsIsSet failed: %s %#v", err, err)) } if !b { panic(fmt.Sprintf("Unexpected rights")) } b, err = unix.CapRightsIsSet(r, []uint64{unix.CAP_READ, unix.CAP_LOOKUP, unix.CAP_WRITE}) if err != nil { panic(fmt.Sprintf("CapRightsIsSet failed: %s %#v", err, err)) } if b { panic(fmt.Sprintf("Unexpected rights (2)")) } // Can no longer create a file _, err = unix.Openat(int(f.Fd()), "xx2", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666) if err == nil { panic("Openat succeeded") } if err != unix.ENOTCAPABLE { panic(fmt.Sprintf("OpenFileAt failed wrong: %s %#v", err, err)) } // But can read an existing one _, err = unix.Openat(int(f.Fd()), "xx", os.O_RDONLY, 0666) if err != nil { panic(fmt.Sprintf("Openat failed: %s %#v", err, err)) } os.Exit(0) } func init() { testProcs["openat"] = testProc{ OpenatTest, makeTempDir, removeTempDir, true, } } func TestOpenat(t *testing.T) { if runtime.GOARCH != "amd64" { t.Skipf("skipping test on %s", runtime.GOARCH) } ExitsCorrectly(t, "openat") } func TestCapRightsSetAndClear(t *testing.T) { r, err := unix.CapRightsInit([]uint64{unix.CAP_READ, unix.CAP_WRITE, unix.CAP_PDWAIT}) if err != nil { t.Fatalf("CapRightsInit failed: %s", err) } err = unix.CapRightsSet(r, []uint64{unix.CAP_EVENT, unix.CAP_LISTEN}) if err != nil { t.Fatalf("CapRightsSet failed: %s", err) } b, err := unix.CapRightsIsSet(r, []uint64{unix.CAP_READ, unix.CAP_WRITE, unix.CAP_PDWAIT, unix.CAP_EVENT, unix.CAP_LISTEN}) if err != nil { t.Fatalf("CapRightsIsSet failed: %s", err) } if !b { t.Fatalf("Wrong rights set") } err = unix.CapRightsClear(r, []uint64{unix.CAP_READ, unix.CAP_PDWAIT}) if err != nil { t.Fatalf("CapRightsClear failed: %s", err) } b, err = unix.CapRightsIsSet(r, []uint64{unix.CAP_WRITE, unix.CAP_EVENT, unix.CAP_LISTEN}) if err != nil { t.Fatalf("CapRightsIsSet failed: %s", err) } if !b { t.Fatalf("Wrong rights set") } } // stringsFromByteSlice converts a sequence of attributes to a []string. // On FreeBSD, each entry consists of a single byte containing the length // of the attribute name, followed by the attribute name. // The name is _not_ NULL-terminated. func stringsFromByteSlice(buf []byte) []string { var result []string i := 0 for i < len(buf) { next := i + 1 + int(buf[i]) result = append(result, string(buf[i+1:next])) i = next } return result } |