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"
|
2015-07-12 06:18:58 +00:00
|
|
|
"github.com/zach-klippenstein/goadb/util"
|
2015-04-11 21:45:29 +00:00
|
|
|
"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)
|
|
|
|
}
|
|
|
|
|
2015-05-17 19:33:41 +00:00
|
|
|
// MockServer implements Dialer, Scanner, and Sender.
|
2015-04-11 21:45:29 +00:00
|
|
|
type MockServer struct {
|
2015-05-17 19:33:41 +00:00
|
|
|
// Each time an operation is performed, if this slice is non-empty, the head element
|
|
|
|
// of this slice is returned and removed from the slice. If the head is nil, it is removed
|
|
|
|
// but not returned.
|
|
|
|
Errs []error
|
|
|
|
|
2015-04-12 20:34:20 +00:00
|
|
|
Status wire.StatusCode
|
|
|
|
|
2015-05-17 19:33:41 +00:00
|
|
|
// Messages are returned from read calls in order, each preceded by a length header.
|
|
|
|
Messages []string
|
|
|
|
nextMsgIndex int
|
2015-04-11 21:45:29 +00:00
|
|
|
|
2015-05-17 19:33:41 +00:00
|
|
|
// Each message passed to a send call is appended to this slice.
|
2015-04-12 20:34:20 +00:00
|
|
|
Requests []string
|
|
|
|
|
2015-05-17 19:33:41 +00:00
|
|
|
// Each time an operaiton is performed, its name is appended to this slice.
|
|
|
|
Trace []string
|
2015-04-11 21:45:29 +00:00
|
|
|
}
|
|
|
|
|
2015-04-12 20:34:20 +00:00
|
|
|
func (s *MockServer) Dial() (*wire.Conn, error) {
|
2015-05-17 19:33:41 +00:00
|
|
|
s.logMethod("Dial")
|
|
|
|
if err := s.getNextErrToReturn(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
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) {
|
2015-05-17 19:33:41 +00:00
|
|
|
s.logMethod("ReadStatus")
|
|
|
|
if err := s.getNextErrToReturn(); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-04-11 21:45:29 +00:00
|
|
|
return s.Status, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MockServer) ReadMessage() ([]byte, error) {
|
2015-05-17 19:33:41 +00:00
|
|
|
s.logMethod("ReadMessage")
|
|
|
|
if err := s.getNextErrToReturn(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-04-11 21:45:29 +00:00
|
|
|
if s.nextMsgIndex >= len(s.Messages) {
|
2015-07-12 06:18:58 +00:00
|
|
|
return nil, util.WrapErrorf(io.EOF, util.NetworkError, "")
|
2015-04-11 21:45:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s.nextMsgIndex++
|
|
|
|
return []byte(s.Messages[s.nextMsgIndex-1]), nil
|
|
|
|
}
|
|
|
|
|
2015-04-12 20:34:20 +00:00
|
|
|
func (s *MockServer) ReadUntilEof() ([]byte, error) {
|
2015-05-17 19:33:41 +00:00
|
|
|
s.logMethod("ReadUntilEof")
|
|
|
|
if err := s.getNextErrToReturn(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-04-12 20:34:20 +00:00
|
|
|
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-05-17 19:33:41 +00:00
|
|
|
s.logMethod("SendMessage")
|
|
|
|
if err := s.getNextErrToReturn(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-04-12 20:34:20 +00:00
|
|
|
s.Requests = append(s.Requests, string(msg))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MockServer) NewSyncScanner() wire.SyncScanner {
|
2015-05-17 19:33:41 +00:00
|
|
|
s.logMethod("NewSyncScanner")
|
2015-04-12 20:34:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MockServer) NewSyncSender() wire.SyncSender {
|
2015-05-17 19:33:41 +00:00
|
|
|
s.logMethod("NewSyncSender")
|
2015-04-11 21:45:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MockServer) Close() error {
|
2015-05-17 19:33:41 +00:00
|
|
|
s.logMethod("Close")
|
|
|
|
if err := s.getNextErrToReturn(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-04-11 21:45:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-05-17 19:33:41 +00:00
|
|
|
|
|
|
|
func (s *MockServer) getNextErrToReturn() (err error) {
|
|
|
|
if len(s.Errs) > 0 {
|
|
|
|
err = s.Errs[0]
|
|
|
|
s.Errs = s.Errs[1:]
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MockServer) logMethod(name string) {
|
|
|
|
s.Trace = append(s.Trace, name)
|
|
|
|
}
|