hid_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package hid
  2. import (
  3. "testing"
  4. )
  5. func TestParseHex(t *testing.T) {
  6. cases := []struct {
  7. name string
  8. input string
  9. want uint16
  10. err bool
  11. }{
  12. {"0x36B0", "36b0", 0x36b0, false},
  13. {"0x6666", "6666", 0x6666, false},
  14. {"0x0001", "0001", 0x0001, false},
  15. {"0xFFFF", "ffff", 0xffff, false},
  16. {"0x0000", "0000", 0x0000, false},
  17. {"uppercase", "ABCD", 0xabcd, false},
  18. {"mixed case", "AbCd", 0xabcd, false},
  19. {"6 chars", "0036b0", 0x36b0, false},
  20. {"invalid hex", "12gh", 0, true},
  21. }
  22. for _, tc := range cases {
  23. t.Run(tc.name, func(t *testing.T) {
  24. got, err := parseHex(tc.input)
  25. if tc.err {
  26. if err == nil {
  27. t.Errorf("parseHex(%q) expected error, got nil", tc.input)
  28. }
  29. return
  30. }
  31. if err != nil {
  32. t.Errorf("parseHex(%q) unexpected error: %v", tc.input, err)
  33. return
  34. }
  35. if got != tc.want {
  36. t.Errorf("parseHex(%q) = %d, want %d", tc.input, got, tc.want)
  37. }
  38. })
  39. }
  40. }
  41. func TestSplitLines(t *testing.T) {
  42. cases := []struct {
  43. name string
  44. input string
  45. want int
  46. }{
  47. {"single line", "hello", 1},
  48. {"two lines", "hello\nworld", 2},
  49. {"trailing newline", "hello\n", 1},
  50. {"empty", "", 0},
  51. {"single newline", "\n", 1},
  52. {"double newline", "\n\n", 2},
  53. {"with content and newlines", "foo\nbar\n", 2},
  54. }
  55. for _, tc := range cases {
  56. t.Run(tc.name, func(t *testing.T) {
  57. got := splitLines(tc.input)
  58. if len(got) != tc.want {
  59. t.Errorf("splitLines(%q) = %d lines, want %d lines", tc.input, len(got), tc.want)
  60. }
  61. })
  62. }
  63. }
  64. func TestParseUEVENT(t *testing.T) {
  65. ueventData := `DRIVER=hid-generic
  66. HID_ID=0003:000036B0:0000309F
  67. HID_NAME=RDMCTMZT Impact 80
  68. HID_PHYS=usb-0000:10:00.0-5.4.1.4.4.1/input0
  69. HID_UNIQ=
  70. MODALIAS=hid:b0003g0001v000036B0p0000309F
  71. `
  72. vid, pid := parseUEVENT(ueventData)
  73. if vid != 0x36B0 {
  74. t.Errorf("parseUEVENT() vid = 0x%04X, want 0x36B0", vid)
  75. }
  76. if pid != 0x309F {
  77. t.Errorf("parseUEVENT() pid = 0x%04X, want 0x309F", pid)
  78. }
  79. // Mouse
  80. mouse := `DRIVER=hid-generic
  81. HID_ID=0003:0000046D:0000C041
  82. HID_NAME=Logitech USB Gaming Mouse
  83. `
  84. vid, pid = parseUEVENT(mouse)
  85. if vid != 0x046D {
  86. t.Errorf("parseUEVENT() mouse vid = 0x%04X, want 0x046D", vid)
  87. }
  88. if pid != 0xC041 {
  89. t.Errorf("parseUEVENT() mouse pid = 0x%04X, want 0xC041", pid)
  90. }
  91. }