Source file src/runtime/runtime_linux_test.go

     1  // Copyright 2012 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  package runtime_test
     6  
     7  import (
     8  	. "runtime"
     9  	"syscall"
    10  	"testing"
    11  	"time"
    12  	"unsafe"
    13  )
    14  
    15  var pid, tid int
    16  
    17  func init() {
    18  	// Record pid and tid of init thread for use during test.
    19  	// The call to LockOSThread is just to exercise it;
    20  	// we can't test that it does anything.
    21  	// Instead we're testing that the conditions are good
    22  	// for how it is used in init (must be on main thread).
    23  	pid, tid = syscall.Getpid(), syscall.Gettid()
    24  	LockOSThread()
    25  
    26  	sysNanosleep = func(d time.Duration) {
    27  		// Invoke a blocking syscall directly; calling time.Sleep()
    28  		// would deschedule the goroutine instead.
    29  		ts := syscall.NsecToTimespec(d.Nanoseconds())
    30  		for {
    31  			if err := syscall.Nanosleep(&ts, &ts); err != syscall.EINTR {
    32  				return
    33  			}
    34  		}
    35  	}
    36  }
    37  
    38  func TestLockOSThread(t *testing.T) {
    39  	if pid != tid {
    40  		t.Fatalf("pid=%d but tid=%d", pid, tid)
    41  	}
    42  }
    43  
    44  // Test that error values are negative.
    45  // Use a misaligned pointer to get -EINVAL.
    46  func TestMincoreErrorSign(t *testing.T) {
    47  	var dst byte
    48  	v := Mincore(unsafe.Add(unsafe.Pointer(new(int32)), 1), 1, &dst)
    49  
    50  	const EINVAL = 0x16
    51  	if v != -EINVAL {
    52  		t.Errorf("mincore = %v, want %v", v, -EINVAL)
    53  	}
    54  }
    55  
    56  func TestParseRelease(t *testing.T) {
    57  	tests := []struct {
    58  		in                  string
    59  		major, minor, patch int
    60  		ok                  bool
    61  	}{
    62  		{"6.1.0", 6, 1, 0, true},
    63  		{"5.15.0-91-generic", 5, 15, 0, true},
    64  		{"4.19.0+", 4, 19, 0, true},
    65  		{"6.6.0-rc1", 6, 6, 0, true},
    66  		// Synology embedded Linux appends a platform identifier
    67  		// after an underscore.
    68  		{"3.4.35_hi3535", 3, 4, 35, true},
    69  		{"2.6.32_synology", 2, 6, 32, true},
    70  		{"3.10", 3, 10, 0, true},
    71  		// A single component is not enough; major+minor required.
    72  		{"3", 0, 0, 0, false},
    73  		{"3-rc1", 0, 0, 0, false},
    74  		{"", 0, 0, 0, false},
    75  		{"bogus", 0, 0, 0, false},
    76  	}
    77  	for _, tt := range tests {
    78  		major, minor, patch, ok := ParseRelease(tt.in)
    79  		if major != tt.major || minor != tt.minor || patch != tt.patch || ok != tt.ok {
    80  			t.Errorf("ParseRelease(%q) = (%d, %d, %d, %v); want (%d, %d, %d, %v)",
    81  				tt.in, major, minor, patch, ok, tt.major, tt.minor, tt.patch, tt.ok)
    82  		}
    83  	}
    84  }
    85  
    86  func TestKernelStructSize(t *testing.T) {
    87  	// Check that the Go definitions of structures exchanged with the kernel are
    88  	// the same size as what the kernel defines.
    89  	if have, want := unsafe.Sizeof(Siginfo{}), uintptr(SiginfoMaxSize); have != want {
    90  		t.Errorf("Go's siginfo struct is %d bytes long; kernel expects %d", have, want)
    91  	}
    92  	if have, want := unsafe.Sizeof(Sigevent{}), uintptr(SigeventMaxSize); have != want {
    93  		t.Errorf("Go's sigevent struct is %d bytes long; kernel expects %d", have, want)
    94  	}
    95  }
    96  

View as plain text