| // Copyright (C) 2016 The Android Open Source Project |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| package adb_test |
| |
| import ( |
| "fmt" |
| |
| "android.googlesource.com/platform/tools/gpu/client/adb" |
| "android.googlesource.com/platform/tools/gpu/client/shell" |
| "android.googlesource.com/platform/tools/gpu/client/shell/stub" |
| "android.googlesource.com/platform/tools/gpu/framework/assert" |
| "android.googlesource.com/platform/tools/gpu/framework/log" |
| ) |
| |
| var ( |
| validDevices = stub.RespondTo(`adb devices`, ` |
| List of devices attached |
| adb server version (36) doesn't match this client (35); killing... |
| * daemon not running. starting it now on port 5037 * |
| * daemon started successfully * |
| production_device unauthorized |
| `) |
| emptyDevices = stub.RespondTo(`adb devices`, ` |
| List of devices attached |
| * daemon not running. starting it now on port 5037 * |
| * daemon started successfully * |
| `) |
| invalidDevices = stub.RespondTo(`adb devices`, ` |
| List of devices attached |
| * daemon not running. starting it now on port 5037 * |
| * daemon started successfully * |
| production_device unauthorized invalid |
| `) |
| invalidStatus = stub.RespondTo(`adb devices`, ` |
| List of devices attached |
| * daemon not running. starting it now on port 5037 * |
| * daemon started successfully * |
| production_device invalid |
| `) |
| notDevices = stub.RespondTo(`adb devices`, ``) |
| devices = &stub.Delegate{Handlers: []shell.Target{validDevices}} |
| ) |
| |
| func init() { |
| adb.ADB = "adb" |
| |
| shell.LocalTarget = stub.OneOf( |
| devices, |
| stub.RespondTo(`adb -s dumpsys_device shell dumpsys package`, ` |
| Activity Resolver Table: |
| Non-Data Actions: |
| android.intent.action.MAIN: |
| 43178558 com.google.foo/.FooActivity filter 4327f110 |
| 12345678 com.google.qux/.QuxActivity filter 1256e899 |
| com.google.android.FOO: |
| 43178558 com.google.foo/.FooActivity filter 431d7db8 |
| android.intent.action.SEARCH: |
| 43178558 com.google.foo/.FooActivity filter 4327cc40 |
| |
| Packages: |
| Package [com.google.foo] (ffffffc): |
| userId=12345 |
| primaryCpuAbi=armeabi-v7a |
| secondaryCpuAbi=null |
| versionCode=902107 targetSdk=15 |
| flags=[ HAS_CODE ALLOW_CLEAR_USER_DATA ALLOW_BACKUP ] |
| Package [com.google.qux] (cafe0000): |
| userId=34567 |
| primaryCpuAbi=armeabi-v7a |
| secondaryCpuAbi=null |
| versionCode=123456 targetSdk=15 |
| flags=[ DEBUGGABLE HAS_CODE ALLOW_CLEAR_USER_DATA ALLOW_BACKUP ] |
| `), |
| |
| // Screen on / off queries. |
| stub.RespondTo(`adb -s screen_off_device shell dumpsys window policy`, ` |
| WINDOW MANAGER POLICY STATE (dumpsys window policy) |
| mSafeMode=false mSystemReady=true mSystemBooted=true |
| mAwake=false |
| mScreenOnEarly=false mScreenOnFully=false |
| mKeyguardDrawComplete=false mWindowManagerDrawComplete=false |
| mOrientationSensorEnabled=false`), |
| stub.RespondTo(`adb -s screen_on_device shell dumpsys window policy`, ` |
| WINDOW MANAGER POLICY STATE (dumpsys window policy) |
| mSafeMode=false mSystemReady=true mSystemBooted=true |
| mAwake=true |
| mScreenOnEarly=true mScreenOnFully=true |
| mKeyguardDrawComplete=false mWindowManagerDrawComplete=false |
| mOrientationSensorEnabled=false`), |
| |
| // Lockscreen on / off queries. |
| stub.RespondTo(`adb -s lockscreen_off_device shell dumpsys window policy`, ` |
| WINDOW MANAGER POLICY STATE (dumpsys window policy) |
| mSafeMode=false mSystemReady=true mSystemBooted=true |
| mDockLayer=268435456 mStatusBarLayer=151000 |
| mShowingLockscreen=false mShowingDream=false mDreamingLockscreen=false |
| mStatusBar=Window{5aa43c7 u0 StatusBar}`), |
| stub.RespondTo(`adb -s lockscreen_on_device shell dumpsys window policy`, ` |
| WINDOW MANAGER POLICY STATE (dumpsys window policy) |
| mSafeMode=false mSystemReady=true mSystemBooted=true |
| mDockLayer=268435456 mStatusBarLayer=151000 |
| mShowingLockscreen=true mShowingDream=false mDreamingLockscreen=true |
| mStatusBar=Window{5aa43c7 u0 StatusBar}`), |
| |
| // Pid queries. |
| stub.Regex(`adb -s ok_pgrep_\S*device shell pgrep .* com.google.foo`, stub.Respond("")), |
| stub.Regex(`adb -s ok_pgrep\S*device shell pgrep -o -f com.google.bar`, stub.Respond("2778")), |
| stub.RespondTo(`adb -s no_pgrep_ok_ps_device shell ps`, ` |
| u0_a11 21926 5061 1976096 42524 SyS_epoll_ 0000000000 S com.google.android.gms |
| u0_a111 2778 5062 1990796 59268 SyS_epoll_ 0000000000 S com.google.bar |
| u0_a69 22841 5062 1255788 88672 SyS_epoll_ 0000000000 S com.example.meh`), |
| stub.Regex(`adb -s \S*no_ps\S*device shell ps`, stub.Respond("/system/bin/sh: ps: not found")), |
| stub.Regex(`adb -s \S*no_pgrep\S*device shell pgrep \S+`, stub.Respond("/system/bin/sh: pgrep: not found")), |
| |
| stub.RespondTo(`adb -s invalid_device shell dumpsys window policy`, `not a normal response`), |
| |
| // Root command responses |
| stub.RespondTo(`adb -s production_device root`, `adbd cannot run as root in production builds`), |
| &stub.Sequence{ |
| stub.RespondTo(`adb -s debug_device root`, `restarting adbd as root`), |
| stub.RespondTo(`adb -s debug_device root`, `some random output`), |
| stub.RespondTo(`adb -s debug_device root`, `adbd is already running as root`), |
| }, |
| stub.RespondTo(`adb -s rooted_device root`, `adbd is already running as root`), |
| stub.RespondTo(`adb -s invalid_device root`, `not a normal response`), |
| stub.Match(`adb -s error_device root`, &stub.Response{WaitErr: fmt.Errorf(`not a normal response`)}), |
| |
| // SELinuxEnforcing command responses |
| stub.RespondTo(`adb -s production_device shell getenforce`, `Enforcing`), |
| stub.RespondTo(`adb -s debug_device shell getenforce`, `Permissive`), |
| stub.Match(`adb -s error_device shell getenforce`, &stub.Response{WaitErr: fmt.Errorf(`not a normal response`)}), |
| |
| // Logcat command responses |
| stub.RespondTo(`adb -s logcat_device logcat -v long -T 0`, ` |
| [ 03-29 15:16:29.514 24153:24153 V/AndroidRuntime ] |
| >>>>>> START com.android.internal.os.RuntimeInit uid 0 <<<<<< |
| |
| |
| [ 03-29 15:16:29.518 24153:24153 D/AndroidRuntime ] |
| CheckJNI is OFF |
| |
| |
| [ 03-29 15:16:29.761 31608:31608 I/Finsky ] |
| [1] PackageVerificationReceiver.onReceive: Verification requested, id = 331 |
| |
| [ 03-29 15:16:32.205 31608:31655 W/qtaguid ] |
| Failed write_ctrl(u 48) res=-1 errno=22 |
| |
| [ 03-29 15:16:32.205 31608:31655 E/NetworkManagementSocketTagger ] |
| untagSocket(48) failed with errno -22 |
| |
| [ 03-29 15:16:32.219 31608:31608 F/Finsky ] |
| [1] PackageVerificationReceiver.onReceive: Verification requested, id = 331 |
| `), |
| |
| // Common responses to all devices |
| stub.Regex(`adb -s .* shell getprop ro\.build\.product`, stub.Respond("hammerhead")), |
| stub.Regex(`adb -s .* shell getprop ro\.build\.version\.release`, stub.Respond("6.0.1")), |
| stub.Regex(`adb -s .* shell getprop ro\.build\.description`, stub.Respond("hammerhead-user 6.0.1 MMB29Q 2480792 release-keys")), |
| stub.Regex(`adb -s .* shell getprop ro\.product\.cpu\.abi`, stub.Respond("armeabi-v7a")), |
| stub.Regex(`adb -s .* shell input .*`, stub.Respond("")), |
| ) |
| } |
| |
| // expectedCommand uses the standard response for an unexpected command to the stub in order to check the command itself |
| // was as expected. |
| func expectedCommand(ctx log.Context, expect string, err error) { |
| assert.For(ctx, "Expected an unmatched command"). |
| ThatError(err).HasMessage(fmt.Sprintf("Error:Failed:Start<unmatched:%s>", expect)) |
| } |