When performing security (or regular) tests on Android applications, we sometimes need to emulate or fake mobile data or actions; making/receiving calls, sending SMS or setting the exact geo-location are some commands that can be done, using the Emulator Console.  Here are a few tricks that will help you through Android application testing using the emulator:

  • First, connect to the emu, using telnet:

telnet localhost 5554

  • To change geo-locations:

geo fix <longtitude value> <latitude value>

  • To make a phone call to the emulator:

gsm call <callerPhoneNumber>

  • To send an sms to the emulator:

sms send <senderePhoneNumber> <textMessage>

  • To scale the emulator window:

window scale <value from 0 to 1>

Monkey

The Monkey is a command-line tool that runs on the emulator instance or on a device. When the Monkey runs, it generates pseudo-random events and sends them to the system.

The Monkey includes a number of options, such as:

  • basic configuration options, such as setting the number of events to attempt
  • operational constraints, such as restricting the test to a single package
  • event types and frequencies
  • debugging options

Monkey also watches three types of events in the system:

  • when running on a specific package, it blocks attempts to navigate to any other packages
  • when the application crashes, Monkey will stop and report the error
  • when the application generates an application not responding error, Monkey will stop and report the error

Monkey runs in the emulator/device environment, therefore it must be launched from a shell in that environment, usingadb shell or by entering the shell and entering Monkey commands directly.

The basic syntax is:

$ adb shell monkey [options] <event-count>

With no options specified, the Monkey will launch in a non-verbose mode, and will send events to any of the packages installed on the device/emulator.

To launch the Monkey on a specific application with 500 pseudo-random events:

$ adb shell monkey -p your.package.name -v 500

The table with the list of all options included on the Monkey command line, can be found in the Command Options Reference, here: http://developer.android.com/tools/help/monkey.html

monkeyrunner

For users who wants to control the Android device or emulator from outside of Android code, Android provides the monkeyrunner tool, which provides an API for writing Python programs that install apps, test packages, send keystrokes, take snapshots and more.

Here is a simple program that connects to a device. The program installs an Android application package, runs one of its activities, and sends key events to the activity. The program then takes a screenshot of the result:

# Installs the package (returns a boolean, to see if the installation worked). device.installPackage(‘myproject/bin/MyApplication.apk’) # sets a variable with the package’s internal name package = ‘com.example.android.myapplication’ # sets a variable with the name of an Activity in the package activity = ‘com.example.android.myapplication.MainActivity’ # sets the name of the component to start runComponent = package + ‘/’ + activity # Runs the component device.startActivity(component=runComponent) # Presses the Menu button device.press(‘KEYCODE_MENU’, MonkeyDevice.DOWN_AND_UP) # Takes a screenshot result = device.takeSnapshot() # Writes the screenshot to a file result.writeToFile(‘myproject/shot1.png’,‘png’)

The monkeyrunner API is contained in three modules in the package com.android.monkeyrunner:

  • MonkeyRunner: A class that provides a method for connecting monkeyrunner to a device or emulator
  • MonkeyDevice: A class that provides methods for installing and uninstalling packages, starting an Activity, and sending keyboard or touch events to an application
  • MonkeyImage: A class that provides methods for capturing screens, converting bitmap images to various formats, comparing two MonkeyImage objects, and writing an image to a file

To import a module, use the Python fromstatement:

from com.android.monkeyrunner import <module>

For more information about monkeyrunner, available features and commands, visit:http://developer.android.com/tools/help/monkeyrunner_concepts.html

Tags: adb, android, emulator, mobile

Share with your friends

appsec-labs-logo-007

No comments yet.

Leave a Reply

You must be logged in to post a comment.