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		// well known major/minor numbers according to /dev/MAKEDEV on
23		// OpenBSD 6.0
24		{"/dev/null", 2, 2},
25		{"/dev/zero", 2, 12},
26		{"/dev/ttyp0", 5, 0},
27		{"/dev/ttyp1", 5, 1},
28		{"/dev/random", 45, 0},
29		{"/dev/srandom", 45, 1},
30		{"/dev/urandom", 45, 2},
31		{"/dev/arandom", 45, 3},
32	}
33	for _, tc := range testCases {
34		t.Run(fmt.Sprintf("%s %v:%v", tc.path, tc.major, tc.minor), func(t *testing.T) {
35			var stat unix.Stat_t
36			err := unix.Stat(tc.path, &stat)
37			if err != nil {
38				t.Errorf("failed to stat device: %v", err)
39				return
40			}
41
42			dev := uint64(stat.Rdev)
43			if unix.Major(dev) != tc.major {
44				t.Errorf("for %s Major(%#x) == %d, want %d", tc.path, dev, unix.Major(dev), tc.major)
45			}
46			if unix.Minor(dev) != tc.minor {
47				t.Errorf("for %s Minor(%#x) == %d, want %d", tc.path, dev, unix.Minor(dev), tc.minor)
48			}
49			if unix.Mkdev(tc.major, tc.minor) != dev {
50				t.Errorf("for %s Mkdev(%d, %d) == %#x, want %#x", tc.path, tc.major, tc.minor, unix.Mkdev(tc.major, tc.minor), dev)
51			}
52		})
53	}
54}
55