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