github.com/etecs-ru/go-sys-wineventlog@v0.0.0-20210227233244-4c3abb794018/unix/darwin_test.go (about)

     1  // Copyright 2018 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build (darwin && go1.12 && amd64) || (darwin && go1.12 && 386)
     6  // +build darwin,go1.12,amd64 darwin,go1.12,386
     7  
     8  package unix
     9  
    10  import (
    11  	"os"
    12  	"os/exec"
    13  	"strings"
    14  	"testing"
    15  )
    16  
    17  type darwinTest struct {
    18  	name string
    19  	f    func()
    20  }
    21  
    22  // TODO(khr): decide whether to keep this test enabled permanently or
    23  // only temporarily.
    24  func TestDarwinLoader(t *testing.T) {
    25  	// Make sure the Darwin dynamic loader can actually resolve
    26  	// all the system calls into libSystem.dylib. Unfortunately
    27  	// there is no easy way to test this at compile time. So we
    28  	// implement a crazy hack here, calling into the syscall
    29  	// function with all its arguments set to junk, and see what
    30  	// error we get. We are happy with any error (or none) except
    31  	// an error from the dynamic loader.
    32  	//
    33  	// We have to run each test in a separate subprocess for fault isolation.
    34  	//
    35  	// Hopefully the junk args won't accidentally ask the system to do "rm -fr /".
    36  	//
    37  	// In an ideal world each syscall would have its own test, so this test
    38  	// would be unnecessary. Unfortunately, we do not live in that world.
    39  	for _, test := range darwinTests {
    40  		// Call the test binary recursively, giving it a magic argument
    41  		// (see init below) and the name of the test to run.
    42  		cmd := exec.Command(os.Args[0], "testDarwinLoader", test.name)
    43  
    44  		// Run subprocess, collect results. Note that we expect the subprocess
    45  		// to fail somehow, so the error is irrelevant.
    46  		out, _ := cmd.CombinedOutput()
    47  
    48  		if strings.Contains(string(out), "dyld: Symbol not found:") {
    49  			t.Errorf("can't resolve %s in libSystem.dylib", test.name)
    50  		}
    51  		if !strings.Contains(string(out), "success") {
    52  			// Not really an error. Might be a syscall that never returns,
    53  			// like exit, or one that segfaults, like gettimeofday.
    54  			t.Logf("test never finished: %s: %s", test.name, string(out))
    55  		}
    56  	}
    57  }
    58  
    59  func init() {
    60  	// The test binary execs itself with the "testDarwinLoader" argument.
    61  	// Run the test specified by os.Args[2], then panic.
    62  	if len(os.Args) >= 3 && os.Args[1] == "testDarwinLoader" {
    63  		for _, test := range darwinTests {
    64  			if test.name == os.Args[2] {
    65  				test.f()
    66  			}
    67  		}
    68  		// Panic with a "success" label, so the parent process can check it.
    69  		panic("success")
    70  	}
    71  }
    72  
    73  // All the _trampoline functions in zsyscall_darwin_$ARCH.s
    74  var darwinTests = [...]darwinTest{
    75  	{"getgroups", libc_getgroups_trampoline},
    76  	{"setgroups", libc_setgroups_trampoline},
    77  	{"wait4", libc_wait4_trampoline},
    78  	{"accept", libc_accept_trampoline},
    79  	{"bind", libc_bind_trampoline},
    80  	{"connect", libc_connect_trampoline},
    81  	{"socket", libc_socket_trampoline},
    82  	{"getsockopt", libc_getsockopt_trampoline},
    83  	{"setsockopt", libc_setsockopt_trampoline},
    84  	{"getpeername", libc_getpeername_trampoline},
    85  	{"getsockname", libc_getsockname_trampoline},
    86  	{"shutdown", libc_shutdown_trampoline},
    87  	{"socketpair", libc_socketpair_trampoline},
    88  	{"recvfrom", libc_recvfrom_trampoline},
    89  	{"sendto", libc_sendto_trampoline},
    90  	{"recvmsg", libc_recvmsg_trampoline},
    91  	{"sendmsg", libc_sendmsg_trampoline},
    92  	{"kevent", libc_kevent_trampoline},
    93  	{"sysctl", libc_sysctl_trampoline},
    94  	{"utimes", libc_utimes_trampoline},
    95  	{"futimes", libc_futimes_trampoline},
    96  	{"fcntl", libc_fcntl_trampoline},
    97  	{"poll", libc_poll_trampoline},
    98  	{"madvise", libc_madvise_trampoline},
    99  	{"mlock", libc_mlock_trampoline},
   100  	{"mlockall", libc_mlockall_trampoline},
   101  	{"mprotect", libc_mprotect_trampoline},
   102  	{"msync", libc_msync_trampoline},
   103  	{"munlock", libc_munlock_trampoline},
   104  	{"munlockall", libc_munlockall_trampoline},
   105  	{"ptrace", libc_ptrace_trampoline},
   106  	{"pipe", libc_pipe_trampoline},
   107  	{"getxattr", libc_getxattr_trampoline},
   108  	{"fgetxattr", libc_fgetxattr_trampoline},
   109  	{"setxattr", libc_setxattr_trampoline},
   110  	{"fsetxattr", libc_fsetxattr_trampoline},
   111  	{"removexattr", libc_removexattr_trampoline},
   112  	{"fremovexattr", libc_fremovexattr_trampoline},
   113  	{"listxattr", libc_listxattr_trampoline},
   114  	{"flistxattr", libc_flistxattr_trampoline},
   115  	{"kill", libc_kill_trampoline},
   116  	{"ioctl", libc_ioctl_trampoline},
   117  	{"access", libc_access_trampoline},
   118  	{"adjtime", libc_adjtime_trampoline},
   119  	{"chdir", libc_chdir_trampoline},
   120  	{"chflags", libc_chflags_trampoline},
   121  	{"chmod", libc_chmod_trampoline},
   122  	{"chown", libc_chown_trampoline},
   123  	{"chroot", libc_chroot_trampoline},
   124  	{"close", libc_close_trampoline},
   125  	{"dup", libc_dup_trampoline},
   126  	{"dup2", libc_dup2_trampoline},
   127  	{"exchangedata", libc_exchangedata_trampoline},
   128  	{"exit", libc_exit_trampoline},
   129  	{"faccessat", libc_faccessat_trampoline},
   130  	{"fchdir", libc_fchdir_trampoline},
   131  	{"fchflags", libc_fchflags_trampoline},
   132  	{"fchmod", libc_fchmod_trampoline},
   133  	{"fchmodat", libc_fchmodat_trampoline},
   134  	{"fchown", libc_fchown_trampoline},
   135  	{"fchownat", libc_fchownat_trampoline},
   136  	{"flock", libc_flock_trampoline},
   137  	{"fpathconf", libc_fpathconf_trampoline},
   138  	{"fstat64", libc_fstat64_trampoline},
   139  	{"fstatat64", libc_fstatat64_trampoline},
   140  	{"fstatfs64", libc_fstatfs64_trampoline},
   141  	{"fsync", libc_fsync_trampoline},
   142  	{"ftruncate", libc_ftruncate_trampoline},
   143  	{"getdtablesize", libc_getdtablesize_trampoline},
   144  	{"getegid", libc_getegid_trampoline},
   145  	{"geteuid", libc_geteuid_trampoline},
   146  	{"getgid", libc_getgid_trampoline},
   147  	{"getpgid", libc_getpgid_trampoline},
   148  	{"getpgrp", libc_getpgrp_trampoline},
   149  	{"getpid", libc_getpid_trampoline},
   150  	{"getppid", libc_getppid_trampoline},
   151  	{"getpriority", libc_getpriority_trampoline},
   152  	{"getrlimit", libc_getrlimit_trampoline},
   153  	{"getrusage", libc_getrusage_trampoline},
   154  	{"getsid", libc_getsid_trampoline},
   155  	{"getuid", libc_getuid_trampoline},
   156  	{"issetugid", libc_issetugid_trampoline},
   157  	{"kqueue", libc_kqueue_trampoline},
   158  	{"lchown", libc_lchown_trampoline},
   159  	{"link", libc_link_trampoline},
   160  	{"linkat", libc_linkat_trampoline},
   161  	{"listen", libc_listen_trampoline},
   162  	{"lstat64", libc_lstat64_trampoline},
   163  	{"mkdir", libc_mkdir_trampoline},
   164  	{"mkdirat", libc_mkdirat_trampoline},
   165  	{"mkfifo", libc_mkfifo_trampoline},
   166  	{"mknod", libc_mknod_trampoline},
   167  	{"open", libc_open_trampoline},
   168  	{"openat", libc_openat_trampoline},
   169  	{"pathconf", libc_pathconf_trampoline},
   170  	{"pread", libc_pread_trampoline},
   171  	{"pwrite", libc_pwrite_trampoline},
   172  	{"read", libc_read_trampoline},
   173  	{"readlink", libc_readlink_trampoline},
   174  	{"readlinkat", libc_readlinkat_trampoline},
   175  	{"rename", libc_rename_trampoline},
   176  	{"renameat", libc_renameat_trampoline},
   177  	{"revoke", libc_revoke_trampoline},
   178  	{"rmdir", libc_rmdir_trampoline},
   179  	{"lseek", libc_lseek_trampoline},
   180  	{"select", libc_select_trampoline},
   181  	{"setegid", libc_setegid_trampoline},
   182  	{"seteuid", libc_seteuid_trampoline},
   183  	{"setgid", libc_setgid_trampoline},
   184  	{"setlogin", libc_setlogin_trampoline},
   185  	{"setpgid", libc_setpgid_trampoline},
   186  	{"setpriority", libc_setpriority_trampoline},
   187  	{"setprivexec", libc_setprivexec_trampoline},
   188  	{"setregid", libc_setregid_trampoline},
   189  	{"setreuid", libc_setreuid_trampoline},
   190  	{"setrlimit", libc_setrlimit_trampoline},
   191  	{"setsid", libc_setsid_trampoline},
   192  	{"settimeofday", libc_settimeofday_trampoline},
   193  	{"setuid", libc_setuid_trampoline},
   194  	{"stat64", libc_stat64_trampoline},
   195  	{"statfs64", libc_statfs64_trampoline},
   196  	{"symlink", libc_symlink_trampoline},
   197  	{"symlinkat", libc_symlinkat_trampoline},
   198  	{"sync", libc_sync_trampoline},
   199  	{"truncate", libc_truncate_trampoline},
   200  	{"umask", libc_umask_trampoline},
   201  	{"undelete", libc_undelete_trampoline},
   202  	{"unlink", libc_unlink_trampoline},
   203  	{"unlinkat", libc_unlinkat_trampoline},
   204  	{"unmount", libc_unmount_trampoline},
   205  	{"write", libc_write_trampoline},
   206  	{"mmap", libc_mmap_trampoline},
   207  	{"munmap", libc_munmap_trampoline},
   208  	{"gettimeofday", libc_gettimeofday_trampoline},
   209  	{"getfsstat64", libc_getfsstat64_trampoline},
   210  }