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// +build go1.7
6
7package unix_test
8
9import (
10	"fmt"
11	"testing"
12
13	"golang.org/x/sys/unix"
14)
15
16func TestDevices(t *testing.T) {
17	testCases := []struct {
18		path  string
19		major uint32
20		minor uint32
21	}{
22		// Most of the device major/minor numbers on Darwin are
23		// dynamically generated by devfs. These are some well-known
24		// static numbers.
25		{"/dev/ttyp0", 4, 0},
26		{"/dev/ttys0", 4, 48},
27		{"/dev/ptyp0", 5, 0},
28		{"/dev/ptyr0", 5, 32},
29	}
30	for _, tc := range testCases {
31		t.Run(fmt.Sprintf("%s %v:%v", tc.path, tc.major, tc.minor), func(t *testing.T) {
32			var stat unix.Stat_t
33			err := unix.Stat(tc.path, &stat)
34			if err != nil {
35				t.Errorf("failed to stat device: %v", err)
36				return
37			}
38
39			dev := uint64(stat.Rdev)
40			if unix.Major(dev) != tc.major {
41				t.Errorf("for %s Major(%#x) == %d, want %d", tc.path, dev, unix.Major(dev), tc.major)
42			}
43			if unix.Minor(dev) != tc.minor {
44				t.Errorf("for %s Minor(%#x) == %d, want %d", tc.path, dev, unix.Minor(dev), tc.minor)
45			}
46			if unix.Mkdev(tc.major, tc.minor) != dev {
47				t.Errorf("for %s Mkdev(%d, %d) == %#x, want %#x", tc.path, tc.major, tc.minor, unix.Mkdev(tc.major, tc.minor), dev)
48			}
49		})
50	}
51}
52