dev_linux_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. // well known major/minor numbers according to
  18. // https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/admin-guide/devices.txt
  19. {"/dev/null", 1, 3},
  20. {"/dev/zero", 1, 5},
  21. {"/dev/random", 1, 8},
  22. {"/dev/full", 1, 7},
  23. {"/dev/urandom", 1, 9},
  24. {"/dev/tty", 5, 0},
  25. }
  26. for _, tc := range testCases {
  27. t.Run(fmt.Sprintf("%s %v:%v", tc.path, tc.major, tc.minor), func(t *testing.T) {
  28. var stat unix.Stat_t
  29. err := unix.Stat(tc.path, &stat)
  30. if err != nil {
  31. if err == unix.EACCES {
  32. t.Skip("no permission to stat device, skipping test")
  33. }
  34. t.Errorf("failed to stat device: %v", err)
  35. return
  36. }
  37. dev := uint64(stat.Rdev)
  38. if unix.Major(dev) != tc.major {
  39. t.Errorf("for %s Major(%#x) == %d, want %d", tc.path, dev, unix.Major(dev), tc.major)
  40. }
  41. if unix.Minor(dev) != tc.minor {
  42. t.Errorf("for %s Minor(%#x) == %d, want %d", tc.path, dev, unix.Minor(dev), tc.minor)
  43. }
  44. if unix.Mkdev(tc.major, tc.minor) != dev {
  45. t.Errorf("for %s Mkdev(%d, %d) == %#x, want %#x", tc.path, tc.major, tc.minor, unix.Mkdev(tc.major, tc.minor), dev)
  46. }
  47. })
  48. }
  49. }