|
23 | 23 | import static org.junit.Assert.assertTrue; |
24 | 24 | import static org.junit.Assert.fail; |
25 | 25 |
|
| 26 | +import android.content.BroadcastReceiver; |
26 | 27 | import android.content.Context; |
| 28 | +import android.content.Intent; |
| 29 | +import android.content.IntentFilter; |
| 30 | +import android.content.pm.PackageManager; |
| 31 | +import android.debug.AdbManager; |
| 32 | +import android.debug.IAdbManager; |
| 33 | +import android.os.ServiceManager; |
27 | 34 | import android.provider.Settings; |
28 | 35 | import android.util.Log; |
29 | 36 |
|
@@ -105,6 +112,7 @@ public void setUp() throws Exception { |
105 | 112 | public void tearDown() throws Exception { |
106 | 113 | mKeyStore.deleteKeyStore(); |
107 | 114 | setAllowedConnectionTime(mOriginalAllowedConnectionTime); |
| 115 | + dropShellPermissionIdentity(); |
108 | 116 | } |
109 | 117 |
|
110 | 118 | /** |
@@ -813,6 +821,108 @@ private boolean isValidMdnsServiceName(String name) { |
813 | 821 | return hasAtLeastOneLetter; |
814 | 822 | } |
815 | 823 |
|
| 824 | + CountDownLatch mAdbActionLatch = new CountDownLatch(1); |
| 825 | + private final BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 826 | + @Override |
| 827 | + public void onReceive(Context context, Intent intent) { |
| 828 | + String action = intent.getAction(); |
| 829 | + Log.i(TAG, "Received intent action=" + action); |
| 830 | + if (AdbManager.WIRELESS_DEBUG_PAIRED_DEVICES_ACTION.equals(action)) { |
| 831 | + assertEquals("Received broadcast without MANAGE_DEBUGGING permission.", |
| 832 | + context.checkSelfPermission(android.Manifest.permission.MANAGE_DEBUGGING), |
| 833 | + PackageManager.PERMISSION_GRANTED); |
| 834 | + Log.i(TAG, "action=" + action + " paired_device=" + intent.getSerializableExtra( |
| 835 | + AdbManager.WIRELESS_DEVICES_EXTRA).toString()); |
| 836 | + mAdbActionLatch.countDown(); |
| 837 | + } else if (AdbManager.WIRELESS_DEBUG_STATE_CHANGED_ACTION.equals(action)) { |
| 838 | + assertEquals("Received broadcast without MANAGE_DEBUGGING permission.", |
| 839 | + context.checkSelfPermission(android.Manifest.permission.MANAGE_DEBUGGING), |
| 840 | + PackageManager.PERMISSION_GRANTED); |
| 841 | + int status = intent.getIntExtra(AdbManager.WIRELESS_STATUS_EXTRA, |
| 842 | + AdbManager.WIRELESS_STATUS_DISCONNECTED); |
| 843 | + Log.i(TAG, "action=" + action + " status=" + status); |
| 844 | + mAdbActionLatch.countDown(); |
| 845 | + } else if (AdbManager.WIRELESS_DEBUG_PAIRING_RESULT_ACTION.equals(action)) { |
| 846 | + assertEquals("Received broadcast without MANAGE_DEBUGGING permission.", |
| 847 | + context.checkSelfPermission(android.Manifest.permission.MANAGE_DEBUGGING), |
| 848 | + PackageManager.PERMISSION_GRANTED); |
| 849 | + Integer res = intent.getIntExtra( |
| 850 | + AdbManager.WIRELESS_STATUS_EXTRA, |
| 851 | + AdbManager.WIRELESS_STATUS_FAIL); |
| 852 | + Log.i(TAG, "action=" + action + " result=" + res); |
| 853 | + |
| 854 | + if (res.equals(AdbManager.WIRELESS_STATUS_PAIRING_CODE)) { |
| 855 | + String pairingCode = intent.getStringExtra( |
| 856 | + AdbManager.WIRELESS_PAIRING_CODE_EXTRA); |
| 857 | + Log.i(TAG, "pairingCode=" + pairingCode); |
| 858 | + } else if (res.equals(AdbManager.WIRELESS_STATUS_CONNECTED)) { |
| 859 | + int port = intent.getIntExtra(AdbManager.WIRELESS_DEBUG_PORT_EXTRA, 0); |
| 860 | + Log.i(TAG, "port=" + port); |
| 861 | + } |
| 862 | + mAdbActionLatch.countDown(); |
| 863 | + } |
| 864 | + } |
| 865 | + }; |
| 866 | + |
| 867 | + private void adoptShellPermissionIdentity() { |
| 868 | + InstrumentationRegistry.getInstrumentation().getUiAutomation() |
| 869 | + .adoptShellPermissionIdentity(android.Manifest.permission.MANAGE_DEBUGGING); |
| 870 | + } |
| 871 | + |
| 872 | + private void dropShellPermissionIdentity() { |
| 873 | + InstrumentationRegistry.getInstrumentation().getUiAutomation() |
| 874 | + .dropShellPermissionIdentity(); |
| 875 | + } |
| 876 | + |
| 877 | + @Test |
| 878 | + public void testBroadcastReceiverWithPermissions() throws Exception { |
| 879 | + adoptShellPermissionIdentity(); |
| 880 | + final IAdbManager mAdbManager = IAdbManager.Stub.asInterface( |
| 881 | + ServiceManager.getService(Context.ADB_SERVICE)); |
| 882 | + IntentFilter intentFilter = |
| 883 | + new IntentFilter(AdbManager.WIRELESS_DEBUG_PAIRED_DEVICES_ACTION); |
| 884 | + intentFilter.addAction(AdbManager.WIRELESS_DEBUG_STATE_CHANGED_ACTION); |
| 885 | + intentFilter.addAction(AdbManager.WIRELESS_DEBUG_PAIRING_RESULT_ACTION); |
| 886 | + assertEquals("Context does not have MANAGE_DEBUGGING permission.", |
| 887 | + mContext.checkSelfPermission(android.Manifest.permission.MANAGE_DEBUGGING), |
| 888 | + PackageManager.PERMISSION_GRANTED); |
| 889 | + try { |
| 890 | + mContext.registerReceiver(mReceiver, intentFilter); |
| 891 | + mAdbManager.enablePairingByPairingCode(); |
| 892 | + if (!mAdbActionLatch.await(TIMEOUT, TIMEOUT_TIME_UNIT)) { |
| 893 | + fail("Receiver did not receive adb intent action within the timeout duration"); |
| 894 | + } |
| 895 | + } finally { |
| 896 | + mContext.unregisterReceiver(mReceiver); |
| 897 | + } |
| 898 | + } |
| 899 | + |
| 900 | + @Test |
| 901 | + public void testBroadcastReceiverWithoutPermissions() throws Exception { |
| 902 | + adoptShellPermissionIdentity(); |
| 903 | + final IAdbManager mAdbManager = IAdbManager.Stub.asInterface( |
| 904 | + ServiceManager.getService(Context.ADB_SERVICE)); |
| 905 | + IntentFilter intentFilter = |
| 906 | + new IntentFilter(AdbManager.WIRELESS_DEBUG_PAIRED_DEVICES_ACTION); |
| 907 | + intentFilter.addAction(AdbManager.WIRELESS_DEBUG_STATE_CHANGED_ACTION); |
| 908 | + intentFilter.addAction(AdbManager.WIRELESS_DEBUG_PAIRING_RESULT_ACTION); |
| 909 | + mAdbManager.enablePairingByPairingCode(); |
| 910 | + |
| 911 | + dropShellPermissionIdentity(); |
| 912 | + assertEquals("Context has MANAGE_DEBUGGING permission.", |
| 913 | + mContext.checkSelfPermission(android.Manifest.permission.MANAGE_DEBUGGING), |
| 914 | + PackageManager.PERMISSION_DENIED); |
| 915 | + try { |
| 916 | + mContext.registerReceiver(mReceiver, intentFilter); |
| 917 | + |
| 918 | + if (mAdbActionLatch.await(TIMEOUT, TIMEOUT_TIME_UNIT)) { |
| 919 | + fail("Broadcast receiver received adb action intent without debug permissions"); |
| 920 | + } |
| 921 | + } finally { |
| 922 | + mContext.unregisterReceiver(mReceiver); |
| 923 | + } |
| 924 | + } |
| 925 | + |
816 | 926 | /** |
817 | 927 | * Runs an adb test with the provided configuration. |
818 | 928 | * |
|
0 commit comments