Example #1
0
func (s *realSender) SendMessage(msg []byte) error {
	if len(msg) > MaxMessageLength {
		return util.AssertionErrorf("message length exceeds maximum: %d", len(msg))
	}

	lengthAndMsg := fmt.Sprintf("%04x%s", len(msg), msg)
	return writeFully(s.writer, []byte(lengthAndMsg))
}
Example #2
0
func (s *realSyncSender) SendOctetString(str string) error {
	if len(str) != 4 {
		return util.AssertionErrorf("octet string must be exactly 4 bytes: '%s'", str)
	}

	wrappedErr := util.WrapErrorf(writeFully(s.Writer, []byte(str)),
		util.NetworkError, "error sending octet string on sync sender")

	return wrappedErr
}
Example #3
0
func RequireOctetString(s SyncScanner, expected string) error {
	actual, err := s.ReadOctetString()
	if err != nil {
		return util.WrapErrorf(err, util.NetworkError, "expected to read '%s'", expected)
	}
	if actual != expected {
		return util.AssertionErrorf("expected to read '%s', got '%s'", expected, actual)
	}
	return nil
}
Example #4
0
func newDevice(serial string, attrs map[string]string) (*DeviceInfo, error) {
	if serial == "" {
		return nil, util.AssertionErrorf("device serial cannot be blank")
	}

	return &DeviceInfo{
		Serial:     serial,
		Product:    attrs["product"],
		Model:      attrs["model"],
		DeviceInfo: attrs["device"],
		Usb:        attrs["usb"],
	}, nil
}
Example #5
0
func (s *realSyncSender) SendString(str string) error {
	length := len(str)
	if length > MaxChunkSize {
		// This limit might not apply to filenames, but it's big enough
		// that I don't think it will be a problem.
		return util.AssertionErrorf("str must be <= %d in length", MaxChunkSize)
	}

	if err := s.SendInt32(int32(length)); err != nil {
		return util.WrapErrorf(err, util.NetworkError, "error sending string length on sync sender")
	}
	return util.WrapErrorf(writeFully(s.Writer, []byte(str)),
		util.NetworkError, "error sending string on sync sender")
}
Example #6
0
// prepareCommandLine validates the command and argument strings, quotes
// arguments if required, and joins them into a valid adb command string.
func prepareCommandLine(cmd string, args ...string) (string, error) {
	if isBlank(cmd) {
		return "", util.AssertionErrorf("command cannot be empty")
	}

	for i, arg := range args {
		if strings.ContainsRune(arg, '"') {
			return "", util.Errorf(util.ParseError, "arg at index %d contains an invalid double quote: %s", i, arg)
		}
		if containsWhitespace(arg) {
			args[i] = fmt.Sprintf("\"%s\"", arg)
		}
	}

	// Prepend the command to the args array.
	if len(args) > 0 {
		cmd = fmt.Sprintf("%s %s", cmd, strings.Join(args, " "))
	}

	return cmd, nil
}
Example #7
0
func TestSyncSendOctetStringTooLong(t *testing.T) {
	var buf bytes.Buffer
	s := NewSyncSender(&buf)
	err := s.SendOctetString("hello")
	assert.Equal(t, util.AssertionErrorf("octet string must be exactly 4 bytes: 'hello'"), err)
}