Monkey Source Analysis WindowManager injection events How to jump out of the inter-process security limit

Source: Internet
Author: User

In the analysis of Monkey source, some background knowledge is not clear, such as See Monkey is the use of WindowManager Injectkeyevent method injection events, the heart of a lump, this way is not only in the current application to inject events? Google has found a foreign Daniel has left clues to describe the problem, deliberately excerpt down and do the corresponding part of the translation, other parts of the people like to see, I do not translate.

How it works

Behind The scenes, Monkey uses several private interfaces to communicate with three essential system services:

  1. Package Manager:monkey uses the package Manager to get a list of activities for a given Intent. This enables Monkey to randomly switch between activities when testing an application.
  2. Activity Manager:monkey calls the very powerful Setactivitycontroller function on the activity Manager. This effectively gives Monkey complete control over the activity life-cycle for the duration of the test.
  3. window Manager:monkey calls a series of functions on the Windows Manager to inject events into the Application. This enables Monkey to simulate touches and key-presses. Because Monkey communicates at the this level, there is no obvious, difference between events which has arrived from Monkey an D events which has arrived from a actual user. In fact, the distinction are so seamless, it's sometimes necessary to manually check who's in control-hence the fam OUs Isuseramonkey () method in the Android
Window Manager:monkey injects events into the application by invoking a series of methods of WindowManager. This allows monkey to simulate user behavior such as touch and keystrokes. It is because monkey is interacting with the application at this level, so that the events that your application receives are from real users, which are not very clear boundaries from the monkey simulation. In fact it is precisely because of this kind of seamless distinction that we sometimes have to judge who is controlling our devices-that's why the Isuseramonkey () approach offered by Android has become so popular.

Monkey sends random events to any application you choose. In order to ensure the doesn ' t cause a security hole, Android uses several techniques to ensure the only monkey can Send events, and only when the phone's user is asking it to.

Monkey randomly sends random events to different apps. To prevent this behavior from causing Android's own security vulnerabilities, Android uses several techniques to ensure that only monkey can be used and to send events to different apps if the phone device user allows it.

Firstly, Monkey itself can only is run by root, or by someone in the "shell" Unix group. Normally, only "adb shell" runs as the "Shell group". This means, the only-to-run monkey is-to-do through "adb shell".

First, the monkey itself can only be run by Root, and the second is run by members belonging to the Shell group. In normal terms, only the "adb shell" is running under this group of shells. This means that the only way to run monkey is through the ' adb shell '.

secondly, the Monkey application, which is mostly written in Java, asks for both SP Ecial manifest permissions. The first, Set_activity_watcher, allows Monkey to take control of the ACTIVITY life-cycle. The second, inject_events, allows Monkey to simulate touches and key presses. Importantly, no normal Android application can request these permissions-they is only granted to applications supplied With the Android system. So there are little danger of a rogue APK taking control of an Android device using Monkey.

Second, Monkey, the Android app itself, is written mostly in Android's native language Java, which requests two manifest permissions from the system. The first is Set_activity_watcher, which allows monkey to have full control over the life cycle of the ACTIVITY. And the second one is inject_events this permission it allows monkey to simulate touch and keystroke events. It is important that the normal Android app is not able to request these permissions-only apps that the Android system agrees on will be allowed to get these permissions (the translator notes: The AOSP system signature that requires an Android system. Monkey is a tool for Android to maintain and, of course, is allowed.)

The following is an official explanation of the manifest option that I extracted from inject_events:

inject_events: Allows an application to INJECT user EVENTS (keys, Touch, trackball) into the event stream and deli ver them to any window.


Monkey Events

What's an event? In Android, events is sent in response to user input, or due to system events, such as power management. Monkey supports quite a few event types, but only three of them is of interest for automated testing:

  • Keyevent:these events is sent by the Windows Manager in response to hardware button presses, and also presses on the keyb Oard-whether hardware, or on-screen.
  • Motionevent:sent by the Windows Manager in response to presses on the touchscreen.
  • Flipevent:sent when the user flips out of the hardware keyboard on the HTC Dream. On the device, this would imply a orientation change. Unfortunately, Monkey does not simulate orientation changes on other devices.


Monkey Source Analysis WindowManager injection events How to jump out of the inter-process security limit

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.