2015-04-11 21:45:29 +00:00
|
|
|
package goadb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2015-04-12 20:34:20 +00:00
|
|
|
"strings"
|
2015-04-11 21:45:29 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/zach-klippenstein/goadb/wire"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetServerVersion(t *testing.T) {
|
2015-04-12 20:34:20 +00:00
|
|
|
s := &MockServer{
|
2015-04-11 21:45:29 +00:00
|
|
|
Status: wire.StatusSuccess,
|
|
|
|
Messages: []string{"000a"},
|
2015-04-12 20:34:20 +00:00
|
|
|
}
|
2015-07-11 21:32:04 +00:00
|
|
|
client := NewHostClient(ClientConfig{
|
|
|
|
Dialer: s,
|
|
|
|
})
|
2015-04-11 21:45:29 +00:00
|
|
|
|
|
|
|
v, err := client.GetServerVersion()
|
2015-04-12 20:34:20 +00:00
|
|
|
assert.Equal(t, "host:version", s.Requests[0])
|
2015-04-11 21:45:29 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 10, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
type MockServer struct {
|
2015-04-12 20:34:20 +00:00
|
|
|
Status wire.StatusCode
|
|
|
|
|
|
|
|
// Messages are sent in order, each preceded by a length header.
|
2015-04-11 21:45:29 +00:00
|
|
|
Messages []string
|
|
|
|
|
2015-04-12 20:34:20 +00:00
|
|
|
// Each request is appended to this slice.
|
|
|
|
Requests []string
|
|
|
|
|
2015-04-11 21:45:29 +00:00
|
|
|
nextMsgIndex int
|
|
|
|
}
|
|
|
|
|
2015-04-12 20:34:20 +00:00
|
|
|
func (s *MockServer) Dial() (*wire.Conn, error) {
|
2015-04-24 13:20:47 +00:00
|
|
|
return wire.NewConn(s, s), nil
|
2015-04-12 20:34:20 +00:00
|
|
|
}
|
|
|
|
|
2015-04-11 21:45:29 +00:00
|
|
|
func (s *MockServer) ReadStatus() (wire.StatusCode, error) {
|
|
|
|
return s.Status, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MockServer) ReadMessage() ([]byte, error) {
|
|
|
|
if s.nextMsgIndex >= len(s.Messages) {
|
|
|
|
return nil, io.EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
s.nextMsgIndex++
|
|
|
|
return []byte(s.Messages[s.nextMsgIndex-1]), nil
|
|
|
|
}
|
|
|
|
|
2015-04-12 20:34:20 +00:00
|
|
|
func (s *MockServer) ReadUntilEof() ([]byte, error) {
|
|
|
|
var data []string
|
|
|
|
for ; s.nextMsgIndex < len(s.Messages); s.nextMsgIndex++ {
|
|
|
|
data = append(data, s.Messages[s.nextMsgIndex])
|
|
|
|
}
|
|
|
|
return []byte(strings.Join(data, "")), nil
|
|
|
|
}
|
|
|
|
|
2015-04-11 21:45:29 +00:00
|
|
|
func (s *MockServer) SendMessage(msg []byte) error {
|
2015-04-12 20:34:20 +00:00
|
|
|
s.Requests = append(s.Requests, string(msg))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MockServer) NewSyncScanner() wire.SyncScanner {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MockServer) NewSyncSender() wire.SyncSender {
|
2015-04-11 21:45:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MockServer) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|