This repository has been archived by the owner on Mar 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
keyboard_test.go
194 lines (162 loc) · 5.7 KB
/
keyboard_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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package uinput
import (
"fmt"
"io/ioutil"
"os"
"testing"
)
// This test will confirm that basic key events are working.
// Note that only Key1 is used here, as the purpose of this test is to ensure that the event handling for
// keyboard devices is working. All other keys, defined in keycodes.go should work as well if this test passes.
// Another thing to keep in mind is that there are certain key codes that might not be great candidates for
// unit testing, as they may create unwanted side effects, like logging out the current user, etc...
func TestKeysInValidRangeWork(t *testing.T) {
vk, err := CreateKeyboard("/dev/uinput", []byte("Test Basic Keyboard"))
if err != nil {
t.Fatalf("Failed to create the virtual keyboard. Last error was: %s\n", err)
}
for _, key := range []int{keyReserved, keyMax} {
err = vk.KeyPress(key)
if err != nil {
t.Fatalf("Failed to send key press. Last error was: %s\n", err)
}
err = vk.KeyDown(key)
if err != nil {
t.Fatalf("Failed to send key down event. Last error was: %s\n", err)
}
err = vk.KeyUp(key)
if err != nil {
t.Fatalf("Failed to send key up event. Last error was: %s\n", err)
}
}
err = vk.Close()
if err != nil {
t.Fatalf("Failed to close device. Last error was: %s\n", err)
}
}
func TestKeyboardCreationFailsOnEmptyPath(t *testing.T) {
expected := "device path must not be empty"
_, err := CreateKeyboard("", []byte("KeyboardDevice"))
if err.Error() != expected {
t.Fatalf("Expected: %s\nActual: %s", expected, err)
}
}
func TestKeyboardCreationFailsOnNonExistentPathName(t *testing.T) {
path := "/some/bogus/path"
_, err := CreateKeyboard(path, []byte("KeyboardDevice"))
if !os.IsNotExist(err) {
t.Fatalf("Expected: os.IsNotExist error\nActual: %s", err)
}
}
func TestKeyboardCreationFailsOnWrongPathName(t *testing.T) {
file, err := ioutil.TempFile(os.TempDir(), "uinput-keyboard-test-")
if err != nil {
t.Fatalf("Failed to setup test. Unable to create tempfile: %v", err)
}
defer file.Close()
expected := "failed to register virtual keyboard device: failed to close device: inappropriate ioctl for device"
_, err = CreateKeyboard(file.Name(), []byte("DialDevice"))
if err == nil || !(expected == err.Error()) {
t.Fatalf("Expected: %s\nActual: %s", expected, err)
}
}
func TestKeyboardCreationFailsIfNameIsTooLong(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 := CreateKeyboard("/dev/uinput", []byte(name))
if err.Error() != expected {
t.Fatalf("Expected: %s\nActual: %s", expected, err)
}
}
func TestKeyOutsideOfRangeKeyPressFails(t *testing.T) {
vk, err := CreateKeyboard("/dev/uinput", []byte("Test Basic Keyboard"))
if err != nil {
t.Fatalf("Failed to create the virtual keyboard. Last error was: %s\n", err)
}
defer vk.Close()
err = vk.KeyPress(keyMax + 1)
if err == nil {
t.Fatalf("Expected key press to fail due to invalid key code, but got no error.")
}
err = vk.KeyPress(-1)
if err == nil {
t.Fatalf("Expected key press to fail due to invalid key code, but got no error.")
}
}
func TestKeyOutsideOfRangeKeyUpFails(t *testing.T) {
vk, err := CreateKeyboard("/dev/uinput", []byte("Test Basic Keyboard"))
if err != nil {
t.Fatalf("Failed to create the virtual keyboard. Last error was: %s\n", err)
}
defer vk.Close()
err = vk.KeyUp(keyMax + 1)
if err == nil {
t.Fatalf("Expected key press to fail due to invalid key code, but got no error.")
}
err = vk.KeyUp(-1)
if err == nil {
t.Fatalf("Expected key press to fail due to invalid key code, but got no error.")
}
}
func TestKeyOutsideOfRangeKeyDownFails(t *testing.T) {
vk, err := CreateKeyboard("/dev/uinput", []byte("Test Basic Keyboard"))
if err != nil {
t.Fatalf("Failed to create the virtual keyboard. Last error was: %s\n", err)
}
defer vk.Close()
err = vk.KeyDown(keyMax + 1)
if err == nil {
t.Fatalf("Expected key press to fail due to invalid key code, but got no error.")
}
err = vk.KeyDown(-1)
if err == nil {
t.Fatalf("Expected key press to fail due to invalid key code, but got no error.")
}
}
func TestKeyPressFailsIfDeviceIsClosed(t *testing.T) {
vk, err := CreateKeyboard("/dev/uinput", []byte("Test Basic Keyboard"))
if err != nil {
t.Fatalf("Failed to create the virtual keyboard. Last error was: %s\n", err)
}
vk.Close()
err = vk.KeyPress(Key1)
if err == nil {
t.Fatalf("Expected KeyPress to fail, but no error was returned.")
}
}
func TestKeyUpFailsIfDeviceIsClosed(t *testing.T) {
vk, err := CreateKeyboard("/dev/uinput", []byte("Test Basic Keyboard"))
if err != nil {
t.Fatalf("Failed to create the virtual keyboard. Last error was: %s\n", err)
}
vk.Close()
err = vk.KeyUp(Key1)
if err == nil {
t.Fatalf("Expected KeyPress to fail, but no error was returned.")
}
}
func TestKeyDownFailsIfDeviceIsClosed(t *testing.T) {
vk, err := CreateKeyboard("/dev/uinput", []byte("Test Basic Keyboard"))
if err != nil {
t.Fatalf("Failed to create the virtual keyboard. Last error was: %s\n", err)
}
vk.Close()
err = vk.KeyDown(Key1)
if err == nil {
t.Fatalf("Expected KeyPress to fail, but no error was returned.")
}
}
func TestKeyboardSyspath(t *testing.T) {
vk, err := CreateKeyboard("/dev/uinput", []byte("Test Basic Keyboard"))
if err != nil {
t.Fatalf("Failed to create the virtual keyboard. Last error was: %s\n", err)
}
sysPath, err := vk.FetchSyspath()
if err != nil {
t.Fatalf("Failed to fetch syspath. Last error was: %s\n", err)
}
if sysPath[:32] != "/sys/devices/virtual/input/input" {
t.Fatalf("Expected syspath to start with /sys/devices/virtual/input/input, but got %s", sysPath)
}
t.Logf("Syspath: %s", sysPath)
}