forked from bendahl/uinput
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dial_test.go
77 lines (66 loc) · 2.17 KB
/
dial_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package uinput
import (
"fmt"
"io/ioutil"
"os"
"testing"
)
func TestDialWheel(t *testing.T) {
relDev, err := CreateDial("/dev/uinput", []byte("Test Dial"))
if err != nil {
t.Fatalf("Failed to create the virtual dial. Last error was: %s\n", err)
}
err = relDev.Turn(1)
if err != nil {
t.Fatalf("Failed to perform wheel movement. Last error was: %s\n", err)
}
err = relDev.Close()
if err != nil {
t.Fatalf("Failed to close device. Last error was: %s\n", err)
}
}
func TestDialFailsIfDeviceIsClosed(t *testing.T) {
relDev, err := CreateDial("/dev/uinput", []byte("Test Dial"))
if err != nil {
t.Fatalf("Failed to create the virtual dial. Last error was: %s\n", err)
}
relDev.Close()
err = relDev.Turn(1)
if err == nil {
t.Fatalf("Expected error due to closed device, but no error was returned.")
}
}
func TestDialCreationFailsOnEmptyPath(t *testing.T) {
expected := "device path must not be empty"
_, err := CreateDial("", []byte("DialDevice"))
if err.Error() != expected {
t.Fatalf("Expected: %s\nActual: %s", expected, err)
}
}
func TestDialCreationFailsOnNonExistentPathName(t *testing.T) {
path := "/some/bogus/path"
_, err := CreateDial(path, []byte("DialDevice"))
if !os.IsNotExist(err) {
t.Fatalf("Expected: os.IsNotExist error\nActual: %s", err)
}
}
func TestDialCreationFailsOnWrongPathName(t *testing.T) {
file, err := ioutil.TempFile(os.TempDir(), "uinput-dial-test-")
if err != nil {
t.Fatalf("Failed to setup test. Unable to create tempfile: %v", err)
}
defer file.Close()
expected := "failed to register dial input device: failed to close device: inappropriate ioctl for device"
_, err = CreateDial(file.Name(), []byte("DialDevice"))
if err == nil || !(expected == err.Error()) {
t.Fatalf("Expected: %s\nActual: %s", expected, err)
}
}
func TestDialCreationFailsIfNameIsTooLong(t *testing.T) {
name := "adsfdsferqewoirueworiuejdsfjdfa;ljoewrjeworiewuoruew;rj;kdlfjoeai;jfewoaifjef;das"
expected := fmt.Sprintf("device name %s is too long (maximum of %d characters allowed)", name, uinputMaxNameSize)
_, err := CreateDial("/dev/uinput", []byte(name))
if err.Error() != expected {
t.Fatalf("Expected: %s\nActual: %s", expected, err)
}
}