dev_darwin_test.go 1.3 KB

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