dev_linux_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. package unix_test
  5. import (
  6. "fmt"
  7. "testing"
  8. "golang.org/x/sys/unix"
  9. )
  10. func TestDevices(t *testing.T) {
  11. testCases := []struct {
  12. path string
  13. major uint32
  14. minor uint32
  15. }{
  16. // well known major/minor numbers according to
  17. // https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/admin-guide/devices.txt
  18. {"/dev/null", 1, 3},
  19. {"/dev/zero", 1, 5},
  20. {"/dev/random", 1, 8},
  21. {"/dev/full", 1, 7},
  22. {"/dev/urandom", 1, 9},
  23. {"/dev/tty", 5, 0},
  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. }