From 81043d7260e1682f8e852c679a39dc0fa47242a8 Mon Sep 17 00:00:00 2001 From: Zach Klippenstein Date: Sat, 12 Sep 2015 15:17:16 -0700 Subject: [PATCH] Handle newer adb servers' device not found error messages. --- wire/util.go | 10 ++++++++- wire/util_test.go | 56 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 wire/util_test.go diff --git a/wire/util.go b/wire/util.go index 89c7bb6..b65a881 100644 --- a/wire/util.go +++ b/wire/util.go @@ -3,6 +3,7 @@ package wire import ( "fmt" "io" + "regexp" "github.com/zach-klippenstein/goadb/util" ) @@ -13,6 +14,13 @@ type ErrorResponseDetails struct { ServerMsg string } +// deviceNotFoundMessagePattern matches all possible error messages returned by adb servers to +// report that a matching device was not found. Used to set the util.DeviceNotFound error code on +// error values. +// +// Old servers send "device not found", and newer ones "device 'serial' not found". +var deviceNotFoundMessagePattern = regexp.MustCompile(`device( '.*')? not found`) + // Reads the status, and if failure, reads the message and returns it as an error. // If the status is success, doesn't read the message. // req is just used to populate the AdbError, and can be nil. @@ -44,7 +52,7 @@ func adbServerError(request string, serverMsg string) error { } errCode := util.AdbError - if serverMsg == "device not found" { + if deviceNotFoundMessagePattern.MatchString(serverMsg) { errCode = util.DeviceNotFound } diff --git a/wire/util_test.go b/wire/util_test.go new file mode 100644 index 0000000..9e4fdb4 --- /dev/null +++ b/wire/util_test.go @@ -0,0 +1,56 @@ +package wire + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/zach-klippenstein/goadb/util" +) + +func TestAdbServerError_NoRequest(t *testing.T) { + err := adbServerError("", "fail") + assert.Equal(t, util.Err{ + Code: util.AdbError, + Message: "server error: fail", + Details: ErrorResponseDetails{ + Request: "", + ServerMsg: "fail", + }, + }, *(err.(*util.Err))) +} + +func TestAdbServerError_WithRequest(t *testing.T) { + err := adbServerError("polite", "fail") + assert.Equal(t, util.Err{ + Code: util.AdbError, + Message: "server error for polite request: fail", + Details: ErrorResponseDetails{ + Request: "polite", + ServerMsg: "fail", + }, + }, *(err.(*util.Err))) +} + +func TestAdbServerError_DeviceNotFound(t *testing.T) { + err := adbServerError("", "device not found") + assert.Equal(t, util.Err{ + Code: util.DeviceNotFound, + Message: "server error: device not found", + Details: ErrorResponseDetails{ + Request: "", + ServerMsg: "device not found", + }, + }, *(err.(*util.Err))) +} + +func TestAdbServerError_DeviceSerialNotFound(t *testing.T) { + err := adbServerError("", "device 'LGV4801c74eccd' not found") + assert.Equal(t, util.Err{ + Code: util.DeviceNotFound, + Message: "server error: device 'LGV4801c74eccd' not found", + Details: ErrorResponseDetails{ + Request: "", + ServerMsg: "device 'LGV4801c74eccd' not found", + }, + }, *(err.(*util.Err))) +}