Skip to content

Commit

Permalink
Test possible keyboard creation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
bendahl committed May 2, 2018
1 parent 2657730 commit dd4a9c5
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion keyboard_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package uinput

import "testing"
import (
"fmt"
"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
Expand Down Expand Up @@ -50,6 +53,50 @@ func TestKeysInValidRangeWork(t *testing.T) {
}
}

func TestKeyboardCreationFailsOnEmptyPath(t *testing.T) {
expected := "device path must not be empty"
defer func() {
if r := recover(); r != nil {
actual := r.(string)
if actual != expected {
t.Fatalf("Expected: %s\nActual: %s", expected, actual)
}
}
}()
CreateKeyboard("", []byte("KeyboardDevice"))
t.Fatalf("Empty path did not yield a panic")
}

func TestKeyboardCreationFailsOnNonExistentPathName(t *testing.T) {
path := "/some/bogus/path"
expected := "device path '" + path + "' does not exist"
defer func() {
if r := recover(); r != nil {
actual := r.(string)
if actual != expected {
t.Fatalf("Expected: %s\nActual: %s", expected, actual)
}
}
}()
CreateKeyboard(path, []byte("KeyboardDevice"))
t.Fatalf("Invalid path did not yield a panic")
}

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)
defer func() {
if r := recover(); r != nil {
actual := r.(string)
if actual != expected {
t.Fatalf("Expected: %s\nActual: %s", expected, actual)
}
}
}()
CreateKeyboard("/dev/uinput", []byte(name))
t.Fatalf("Invalid name did not yield a panic")
}

func TestKeyOutsideOfRangeKeyPressFails(t *testing.T) {
vk, err := CreateKeyboard("/dev/uinput", []byte("Test Basic Keyboard"))
if err != nil {
Expand Down

0 comments on commit dd4a9c5

Please sign in to comment.