Skip to content

Commit

Permalink
added some more tests and improved C error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Dahlmanns committed May 15, 2014
1 parent a8eaf5a commit b05aca9
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
44 changes: 44 additions & 0 deletions uinput_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,47 @@ func TestBasicVKeyboard(t *testing.T) {
t.Fatalf("Failed to close device. Last error was: %s\n", err)
}
}

// This test will confirm that a proper error code is returned if an invalid uinput path is
// passed to the library
func TestInvalidDevicePath(t *testing.T) {
devId, err := CreateVKeyboardDevice("/invalid/path")

if err == nil {
// this usually shouldn't happen, but if the device is created, we need to close it
CloseDevice(devId)
t.Fatalf("Expected error code,but received %s instead.\n", err)
}
}

// This test will confirm that a proper error code is returned if an invalid keycode is
// passed to the library
func TestInvalidKeycode(t *testing.T) {
devId, err := CreateVKeyboardDevice("/dev/uinput")

if err != nil {
t.Fatalf("Failed to create the virtual keyboard. Last error was: %s\n", err)
}

err = SendBtnEvent(devId, 4711, 1)

if err == nil {
t.Fatalf("Sending an invalid keycode did not trigger an error. Got: %d.\n")
}
}

// This test will confirm that a proper error code is returned if an invalid button state is
// passed to the library
func TestInvalidBtnState(t *testing.T) {
devId, err := CreateVKeyboardDevice("/dev/uinput")

if err != nil {
t.Fatalf("Failed to create the virtual keyboard. Last error was: %s\n", err)
}

err = SendBtnEvent(devId, KEY_1, 3)

if err == nil {
t.Fatalf("Sending an invalid btnState did not trigger an error. Got: %d.\n")
}
}
14 changes: 12 additions & 2 deletions uinputwrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ int initVKeyboardDevice(char* uinputPath) {
deviceHandle = -1;
}
} else {
// register key events
for(i=0; i<255; i++) {
// register key events - only values from 1 to 255 are valid
for(i=1; i<256; i++) {
ioctl(deviceHandle, UI_SET_KEYBIT, i);
}

Expand Down Expand Up @@ -59,6 +59,16 @@ int initVKeyboardDevice(char* uinputPath) {
}

int sendBtnEvent(int deviceHandle, int key, int btnState) {
// check whether the keycode is valid and return -1 on error
if (key < 1 || key > 255) {
return -1;
}

// btnState should only be either 0 or 1
if (btnState < 0 || btnState > 1) {
return -1;
}

struct input_event ev;
memset(&ev, 0, sizeof(ev));

Expand Down

0 comments on commit b05aca9

Please sign in to comment.