golang.org/x/sys@v0.20.1-0.20240517151509-673e0f94c16d/unix/dev_zos_test.go (about)

     1  // Copyright 2020 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 zos && s390x
     6  
     7  package unix_test
     8  
     9  // Modified from Linux tests for device numbers.
    10  
    11  import (
    12  	"fmt"
    13  	"testing"
    14  
    15  	"golang.org/x/sys/unix"
    16  )
    17  
    18  func TestDevices(t *testing.T) {
    19  	testCases := []struct {
    20  		path  string
    21  		major uint32
    22  		minor uint32
    23  	}{
    24  		// Device nums found using ls -l on z/OS
    25  		{"/dev/null", 4, 0},
    26  		{"/dev/zero", 4, 1},
    27  		{"/dev/random", 4, 2},
    28  		{"/dev/urandom", 4, 2},
    29  		{"/dev/tty", 3, 0},
    30  	}
    31  	for _, tc := range testCases {
    32  		t.Run(fmt.Sprintf("%s %v:%v", tc.path, tc.major, tc.minor), func(t *testing.T) {
    33  			var stat unix.Stat_t
    34  			err := unix.Stat(tc.path, &stat)
    35  			if err != nil {
    36  				if err == unix.EACCES {
    37  					t.Skip("no permission to stat device, skipping test")
    38  				}
    39  				t.Errorf("failed to stat device: %v", err)
    40  				return
    41  			}
    42  
    43  			dev := uint64(stat.Rdev)
    44  			if unix.Major(dev) != tc.major {
    45  				t.Errorf("for %s Major(%#x) == %d, want %d", tc.path, dev, unix.Major(dev), tc.major)
    46  			}
    47  			if unix.Minor(dev) != tc.minor {
    48  				t.Errorf("for %s Minor(%#x) == %d, want %d", tc.path, dev, unix.Minor(dev), tc.minor)
    49  			}
    50  			if unix.Mkdev(tc.major, tc.minor) != dev {
    51  				t.Errorf("for %s Mkdev(%d, %d) == %#x, want %#x", tc.path, tc.major, tc.minor, unix.Mkdev(tc.major, tc.minor), dev)
    52  			}
    53  		})
    54  
    55  	}
    56  }