Skip to content

Commit

Permalink
Add test case for empty device name
Browse files Browse the repository at this point in the history
  • Loading branch information
bendahl committed Nov 13, 2018
1 parent 516316c commit 1dfe568
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions uinput.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ func validateDevicePath(path string) {
}

func validateUinputName(name []byte) {
if name == nil || len(name) == 0 {
panic(fmt.Sprintf("device name may not be empty"))
}
if len(name) > uinputMaxNameSize {
panic(fmt.Sprintf("device name %s is too long (maximum of %d characters allowed)", name, uinputMaxNameSize))
}
Expand Down
14 changes: 14 additions & 0 deletions uinput_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,17 @@ func TestValidateDevicePathInvalidPathPanics(t *testing.T) {
validateDevicePath(path)
t.Fatalf("Invalid path did not yield a panic")
}

func TestValidateUinputNameEmptyNamePanics(t *testing.T) {
expected := "device name may not be empty"
defer func() {
if r := recover(); r != nil {
actual := r.(string)
if actual != expected {
t.Fatalf("Expected: %s\nActual: %s", expected, actual)
}
}
}()
validateUinputName(nil)
t.Fatalf("expected panic due to validation error, but nothing happened...")
}

0 comments on commit 1dfe568

Please sign in to comment.