forked from zach-klippenstein/goadb
-
Notifications
You must be signed in to change notification settings - Fork 2
/
device_test.go
185 lines (161 loc) · 5.3 KB
/
device_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
package adb
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/jt6562/go-adb/internal/errors"
"github.com/jt6562/go-adb/wire"
)
func TestGetAttribute(t *testing.T) {
s := &MockServer{
Status: wire.StatusSuccess,
Messages: []string{"value"},
}
client := (&Adb{s}).Device(DeviceWithSerial("serial"))
v, err := client.getAttribute("attr")
assert.Equal(t, "host-serial:serial:attr", s.Requests[0])
assert.NoError(t, err)
assert.Equal(t, "value", v)
}
func TestGetDeviceInfo(t *testing.T) {
deviceLister := func() ([]*DeviceInfo, error) {
return []*DeviceInfo{
&DeviceInfo{
Serial: "abc",
Product: "Foo",
},
&DeviceInfo{
Serial: "def",
Product: "Bar",
},
}, nil
}
client := newDeviceClientWithDeviceLister("abc", deviceLister)
device, err := client.DeviceInfo()
assert.NoError(t, err)
assert.Equal(t, "Foo", device.Product)
client = newDeviceClientWithDeviceLister("def", deviceLister)
device, err = client.DeviceInfo()
assert.NoError(t, err)
assert.Equal(t, "Bar", device.Product)
client = newDeviceClientWithDeviceLister("serial", deviceLister)
device, err = client.DeviceInfo()
assert.True(t, HasErrCode(err, DeviceNotFound))
assert.EqualError(t, err.(*errors.Err).Cause,
"DeviceNotFound: device list doesn't contain serial serial")
assert.Nil(t, device)
}
func newDeviceClientWithDeviceLister(serial string, deviceLister func() ([]*DeviceInfo, error)) *Device {
client := (&Adb{&MockServer{
Status: wire.StatusSuccess,
Messages: []string{serial},
}}).Device(DeviceWithSerial(serial))
client.deviceListFunc = deviceLister
return client
}
func TestRunCommandNoArgs(t *testing.T) {
s := &MockServer{
Status: wire.StatusSuccess,
Messages: []string{"output"},
}
client := (&Adb{s}).Device(AnyDevice())
v, err := client.RunCommand("cmd")
assert.Equal(t, "host:transport-any", s.Requests[0])
assert.Equal(t, "shell:cmd", s.Requests[1])
assert.NoError(t, err)
assert.Equal(t, "output", v)
}
func TestForward(t *testing.T) {
s := &MockServer{
Status: wire.StatusSuccess,
Messages: []string{""},
}
client := (&Adb{s}).Device(DeviceWithSerial("abc"))
err := client.Forward(ForwardSpec{"tcp", "8999"}, ForwardSpec{"localabstract", "demo"})
assert.Equal(t, "host-serial:abc:forward:tcp:8999;localabstract:demo", s.Requests[0])
assert.NoError(t, err)
}
func TestForwardList(t *testing.T) {
s := &MockServer{
Status: wire.StatusSuccess,
Messages: []string{"serial tcp:8999 tcp:d1\nabc tcp:8994 udp:d2\nabc tcp:8995 udp:d3"},
}
client := (&Adb{s}).Device(DeviceWithSerial("abc"))
fws, err := client.ForwardList()
assert.NoError(t, err)
assert.Equal(t, "host-serial:abc:list-forward", s.Requests[0])
assert.Equal(t, 2, len(fws))
assert.Equal(t, fws[0].Serial, "abc")
assert.Equal(t, fws[0].Local.Protocol, "tcp")
assert.Equal(t, fws[0].Local.PortOrName, "8994")
assert.Equal(t, fws[0].Remote.Protocol, "udp")
assert.Equal(t, fws[0].Remote.PortOrName, "d2")
assert.Equal(t, fws[1].Remote.PortOrName, "d3")
}
func TestForwardRemove(t *testing.T) {
s := &MockServer{
Status: wire.StatusSuccess,
Messages: []string{""},
}
client := (&Adb{s}).Device(DeviceWithSerial("abc"))
err := client.ForwardRemove(ForwardSpec{"tcp", "8999"})
assert.Equal(t, "host-serial:abc:killforward:tcp:8999", s.Requests[0])
assert.NoError(t, err)
}
func TestForwardRemoveAll(t *testing.T) {
s := &MockServer{
Status: wire.StatusSuccess,
Messages: []string{""},
}
client := (&Adb{s}).Device(DeviceWithSerial("abc"))
err := client.ForwardRemoveAll()
assert.Equal(t, "host-serial:abc:killforward-all", s.Requests[0])
assert.NoError(t, err)
}
func TestProperties(t *testing.T) {
s := &MockServer{
Status: wire.StatusSuccess,
Messages: []string{"[wifi.interface]: [wlan0]\r\n[wlan.driver.ath]: [0]\r\n"},
}
client := (&Adb{s}).Device(AnyDevice())
props, err := client.Properties()
assert.NoError(t, err)
assert.Equal(t, len(props), 2)
assert.Equal(t, props["wifi.interface"], "wlan0")
assert.Equal(t, props["wlan.driver.ath"], "0")
}
func TestPrepareCommandLineNoArgs(t *testing.T) {
result, err := prepareCommandLine("cmd")
assert.NoError(t, err)
assert.Equal(t, "cmd", result)
}
func TestPrepareCommandLineEmptyCommand(t *testing.T) {
_, err := prepareCommandLine("")
assert.Equal(t, errors.AssertionError, code(err))
assert.Equal(t, "command cannot be empty", message(err))
}
func TestPrepareCommandLineBlankCommand(t *testing.T) {
_, err := prepareCommandLine(" ")
assert.Equal(t, errors.AssertionError, code(err))
assert.Equal(t, "command cannot be empty", message(err))
}
func TestPrepareCommandLineCleanArgs(t *testing.T) {
result, err := prepareCommandLine("cmd", "arg1", "arg2")
assert.NoError(t, err)
assert.Equal(t, "cmd arg1 arg2", result)
}
func TestPrepareCommandLineArgWithWhitespaceQuotes(t *testing.T) {
result, err := prepareCommandLine("cmd", "arg with spaces")
assert.NoError(t, err)
assert.Equal(t, "cmd \"arg with spaces\"", result)
}
func TestPrepareCommandLineArgWithDoubleQuoteFails(t *testing.T) {
_, err := prepareCommandLine("cmd", "quoted\"arg")
assert.Equal(t, errors.ParseError, code(err))
assert.Equal(t, "arg at index 0 contains an invalid double quote: quoted\"arg", message(err))
}
func code(err error) errors.ErrCode {
return err.(*errors.Err).Code
}
func message(err error) string {
return err.(*errors.Err).Message
}