goadb/host_client_test.go

81 lines
1.6 KiB
Go
Raw Normal View History

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