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

     1  // Copyright 2017 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 unix_test
     6  
     7  import (
     8  	"fmt"
     9  	"testing"
    10  
    11  	"golang.org/x/sys/unix"
    12  )
    13  
    14  func TestDevices(t *testing.T) {
    15  	testCases := []struct {
    16  		path  string
    17  		major uint32
    18  		minor uint32
    19  	}{
    20  		// well known major/minor numbers according to
    21  		// https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/admin-guide/devices.txt
    22  		{"/dev/null", 1, 3},
    23  		{"/dev/zero", 1, 5},
    24  		{"/dev/random", 1, 8},
    25  		{"/dev/full", 1, 7},
    26  		{"/dev/urandom", 1, 9},
    27  		{"/dev/tty", 5, 0},
    28  	}
    29  	for _, tc := range testCases {
    30  		t.Run(fmt.Sprintf("%s %v:%v", tc.path, tc.major, tc.minor), func(t *testing.T) {
    31  			var stat unix.Stat_t
    32  			err := unix.Stat(tc.path, &stat)
    33  			if err != nil {
    34  				if err == unix.EACCES {
    35  					t.Skip("no permission to stat device, skipping test")
    36  				}
    37  				t.Errorf("failed to stat device: %v", err)
    38  				return
    39  			}
    40  
    41  			dev := uint64(stat.Rdev)
    42  			if unix.Major(dev) != tc.major {
    43  				t.Errorf("for %s Major(%#x) == %d, want %d", tc.path, dev, unix.Major(dev), tc.major)
    44  			}
    45  			if unix.Minor(dev) != tc.minor {
    46  				t.Errorf("for %s Minor(%#x) == %d, want %d", tc.path, dev, unix.Minor(dev), tc.minor)
    47  			}
    48  			if unix.Mkdev(tc.major, tc.minor) != dev {
    49  				t.Errorf("for %s Mkdev(%d, %d) == %#x, want %#x", tc.path, tc.major, tc.minor, unix.Mkdev(tc.major, tc.minor), dev)
    50  			}
    51  		})
    52  
    53  	}
    54  }