aboutsummaryrefslogtreecommitdiff
path: root/prod-tests/src/com/android/framework/tests/PackageManagerHostTestUtils.java
blob: 4b31dc0bb449eb448df4cac801c2e74ff8269698 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
/*
 * Copyright (C) 2010 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 com.android.framework.tests;

import com.android.ddmlib.testrunner.IRemoteAndroidTestRunner;
import com.android.ddmlib.testrunner.RemoteAndroidTestRunner;
import com.android.tradefed.device.DeviceNotAvailableException;
import com.android.tradefed.device.IFileEntry;
import com.android.tradefed.device.ITestDevice;
import com.android.tradefed.log.LogUtil.CLog;
import com.android.tradefed.result.CollectingTestListener;

import junit.framework.Assert;

import java.io.File;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Set of tests that verify host side install cases
 */
public class PackageManagerHostTestUtils extends Assert {
    private ITestDevice mDevice = null;
    private boolean mEmulatedExternalStorage = false;

    // TODO: get this value from Android Environment instead of hard coding
    public static final String PRE_JB_APP_PRIVATE_PATH = "/data/app-private/";
    public static final String JB_APP_PRIVATE_PATH = "/mnt/asec/";
    public static final String JB_ASEC_PRIVATE_PATH = "/data/app-asec/";
    public static final String DEVICE_APP_PATH = "/data/app/";
    public static final String SDCARD_APP_PATH = "/mnt/secure/asec/";

    private static String mAppPrivatePath = PRE_JB_APP_PRIVATE_PATH;

    private static final int MAX_WAIT_FOR_DEVICE_TIME = 120 * 1000;

    // Install preference on the device-side
    public static enum InstallLocPreference {
        AUTO,
        INTERNAL,
        EXTERNAL
    }

    // Actual install location
    public static enum InstallLocation {
        DEVICE,
        SDCARD
    }

    /**
     * Constructor.
     *
     * @param device the {@link ITestDevice} to use when performing operations.
     * @throws DeviceNotAvailableException
     */
    public PackageManagerHostTestUtils(ITestDevice device)
            throws DeviceNotAvailableException {
        mDevice = device;
        determineExternalStorageEmulation();
    }

    /**
     * Returns the path on the device of forward-locked apps.
     *
     * @return path of forward-locked apps on the device
     */
    public static String getAppPrivatePath() {
        return mAppPrivatePath;
    }

    public static void setAppPrivatePath(String path) {
        mAppPrivatePath = path;
    }

    /**
     * Returns the path on the device of normal apps.
     *
     * @return path of forward-locked apps on the device
     */
    public static String getDeviceAppPath() {
        return DEVICE_APP_PATH;
    }

    /**
     * Returns the path of apps installed on the SD card.
     *
     * @return path of forward-locked apps on the device
     */
    public static String getSDCardAppPath() {
        return SDCARD_APP_PATH;
    }

    /**
     * Helper method to run tests and return the listener that collected the
     * results. For the optional params, pass null to use the default values.
     *
     * @param pkgName Android application package for tests
     * @param className (optional) The class containing the method to test
     * @param methodName (optional) The method in the class of which to test
     * @param runnerName (optional) The name of the TestRunner of the test on
     *            the device to be run
     * @param params (optional) Any additional parameters to pass into the Test
     *            Runner
     * @return the {@link CollectingTestRunListener}
     * @throws DeviceNotAvailableException
     */
    private CollectingTestListener doRunTests(String pkgName, String className,
            String methodName, String runnerName, Map<String, String> params)
            throws DeviceNotAvailableException {
        IRemoteAndroidTestRunner testRunner = new RemoteAndroidTestRunner(pkgName, runnerName,
                mDevice.getIDevice());

        if (className != null && methodName != null) {
            testRunner.setMethodName(className, methodName);
        }

        // Add in any additional args to pass into the test
        if (params != null) {
            for (Entry<String, String> argPair : params.entrySet()) {
                testRunner.addInstrumentationArg(argPair.getKey(), argPair.getValue());
            }
        }

        CollectingTestListener listener = new CollectingTestListener();
        mDevice.runInstrumentationTests(testRunner, listener);
        return listener;
    }

    /**
     * Runs the specified packages tests, and returns whether all tests passed
     * or not.
     *
     * @param pkgName Android application package for tests
     * @param className The class containing the method to test
     * @param methodName The method in the class of which to test
     * @param runnerName The name of the TestRunner of the test on the device to
     *            be run
     * @param params Any additional parameters to pass into the Test Runner
     * @return true if test passed, false otherwise.
     * @throws DeviceNotAvailableException
     */
    public boolean runDeviceTestsDidAllTestsPass(String pkgName, String className,
            String methodName, String runnerName, Map<String, String> params)
            throws DeviceNotAvailableException {
        CollectingTestListener listener = doRunTests(pkgName, className, methodName,
                runnerName, params);
        return !listener.hasFailedTests();
    }

    /**
     * Runs the specified packages tests, and returns whether all tests passed
     * or not.
     *
     * @param pkgName Android application package for tests
     * @return true if every test passed, false otherwise.
     * @throws DeviceNotAvailableException
     */
    public boolean runDeviceTestsDidAllTestsPass(String pkgName)
            throws DeviceNotAvailableException {
        CollectingTestListener listener = doRunTests(pkgName, null, null, null, null);
        return !listener.hasFailedTests();
    }

    /**
     * Helper method to install a file
     *
     * @param localFile the {@link File} to install
     * @param reinstall set to <code>true</code> if re-install of app should be
     *            performed
     * @throws DeviceNotAvailableException
     */
    public void installFile(final File localFile, final boolean replace)
            throws DeviceNotAvailableException {
        String result = mDevice.installPackage(localFile, replace);
        assertEquals(null, result);
    }

    /**
     * Helper method to install a file to device as forward locked.
     *
     * @param apkFile the {@link File} to install
     * @param replace set to <code>true</code> if re-install of app should be
     *            performed
     * @throws DeviceNotAvailableException if communication with device is lost
     */
    public String installFileForwardLocked(final File apkFile, final boolean replace)
            throws DeviceNotAvailableException {
        return mDevice.installPackage(apkFile, replace, "-l");
    }

    /**
     * Helper method to install an encrypted apk to the device.
     *
     * @param apkFile the {@link File} to install
     * @param replace set to <code>true</code> if re-install of app should be
     *            performed
     * @param algorithm the encryption algorithm used for the encrypted apk
     * @param iv the initialization vector used for the encrypted apk
     * @param encryptionKey the encryption key used for the encrypted apk
     * @param macAlgorithm the mac algorithm used
     * @param macKey the mac key used
     * @param macTag the mac tag used
     * @return
     * @throws DeviceNotAvailableException
     */
    public String installFileEncrypted(final File apkFile, final boolean replace,
            final String algorithm, final String iv, final String encryptionKey,
            final String macAlgorithm, final String macKey, final String macTag)
            throws DeviceNotAvailableException {
        return mDevice.installPackage(apkFile, replace, "-l", "--algo", algorithm, "--iv", iv,
                "--key", encryptionKey, "--macalgo", macAlgorithm,
                "--mackey", macKey, "--tag", macTag);
    }

    /**
     * Helper method to determine if file exists on the device containing a
     * given string.
     *
     * @param destPath the absolute path of the file
     * @return <code>true</code> if file exists containing given string,
     *         <code>false</code> otherwise.
     * @throws DeviceNotAvailableException
     */
    public boolean doesRemoteFileExistContainingString(String destPath, String searchString)
            throws DeviceNotAvailableException {
        String lsResult = mDevice.executeShellCommand(String.format("ls %s", destPath));
        return lsResult.contains(searchString);
    }

    /**
     * Helper method to determine if package on device exists.
     *
     * @param packageName the Android manifest package to check.
     * @return <code>true</code> if package exists, <code>false</code> otherwise
     * @throws DeviceNotAvailableException
     */
    public boolean doesPackageExist(String packageName) throws DeviceNotAvailableException {
        String pkgGrep = mDevice.executeShellCommand(String.format("pm path %s", packageName));
        return pkgGrep.contains("package:");
    }

    /**
     * Determines if app was installed on device.
     *
     * @param packageName package name to check for
     * @return <code>true</code> if file exists, <code>false</code> otherwise.
     * @throws DeviceNotAvailableException
     */
    public boolean doesAppExistOnDevice(String packageName) throws DeviceNotAvailableException {
        return doesRemoteFileExistContainingString(DEVICE_APP_PATH, packageName);
    }

    /**
     * Determines if app was installed on SD card.
     *
     * @param packageName package name to check for
     * @return <code>true</code> if file exists, <code>false</code> otherwise.
     * @throws DeviceNotAvailableException
     */
    public boolean doesAppExistOnSDCard(String packageName) throws DeviceNotAvailableException {

        // if we're using emulated storage, the SDcard path is actually the
        // device's normal app path
        if (getIsExternalStorageEmulated()) {
            return doesRemoteFileExistContainingString(DEVICE_APP_PATH, packageName);
        }
        else {
            return doesRemoteFileExistContainingString(SDCARD_APP_PATH, packageName);
        }
    }

    /**
     * Helper method to determine if app was installed as forward locked.
     *
     * @param packageName package name to check for
     * @return <code>true</code> if file exists, <code>false</code> otherwise.
     * @throws DeviceNotAvailableException
     */
    public boolean doesAppExistAsForwardLocked(String packageName)
            throws DeviceNotAvailableException {
        return doesRemoteFileExistContainingString(mAppPrivatePath, packageName);
    }

    /**
     * Waits for device's package manager to respond.
     *
     * @throws DeviceNotAvailableException
     */
    public void waitForPackageManager() throws DeviceNotAvailableException {
        CLog.i("waiting for device");
        mDevice.waitForDeviceAvailable(MAX_WAIT_FOR_DEVICE_TIME);
    }

    /**
     * Helper method for installing an app to wherever is specified in its
     * manifest, and then verifying the app was installed onto SD Card.
     * <p/>
     * Assumes adb is running as root in device under test.
     *
     * @param the path of the apk to install
     * @param the name of the package
     * @param <code>true</code> if the app should be overwritten,
     *        <code>false</code> otherwise
     * @throws DeviceNotAvailableException
     */
    public void installAppAndVerifyExistsOnSDCard(File apkPath, String pkgName, boolean overwrite)
            throws DeviceNotAvailableException {
        // Start with a clean slate if we're not overwriting
        if (!overwrite) {
            // cleanup test app just in case it already exists
            mDevice.uninstallPackage(pkgName);
            // grep for package to make sure its not installed
            assertFalse(doesPackageExist(pkgName));
        }

        installFile(apkPath, overwrite);
        assertTrue(doesAppExistOnSDCard(pkgName));
        // TODO: is this necessary?
        waitForPackageManager();

        // grep for package to make sure it is installed
        assertTrue(doesPackageExist(pkgName));
    }

    /**
     * Helper method for installing an app to wherever is specified in its
     * manifest, and then verifying the app was installed onto device.
     * <p/>
     * Assumes adb is running as root in device under test.
     *
     * @param apkFile the {@link File} of the apk to install
     * @param the name of the package
     * @param <code>true</code> if the app should be overwritten,
     *        <code>false</code> otherwise
     * @throws DeviceNotAvailableException
     */
    public void installAppAndVerifyExistsOnDevice(File apkFile, String pkgName, boolean overwrite)
            throws DeviceNotAvailableException {
        // Start with a clean slate if we're not overwriting
        if (!overwrite) {
            // cleanup test app just in case it already exists
            mDevice.uninstallPackage(pkgName);
            // grep for package to make sure its not installed
            assertFalse(doesPackageExist(pkgName));
        }

        installFile(apkFile, overwrite);
        assertTrue(doesAppExistOnDevice(pkgName));
        // TODO: is this necessary?
        waitForPackageManager();

        // grep for package to make sure it is installed
        assertTrue(doesPackageExist(pkgName));
    }

    /**
     * Helper method for installing an app as forward-locked, and then verifying
     * the app was installed in the proper forward-locked location.
     * <p/>
     * Assumes adb is running as root in device under test.
     *
     * @param apkFile the {@link File} of the apk to install
     * @param pkgName the name of the package
     * @param overwrite <code>true</code> if the app should be overwritten,
     *            <code>false</code> otherwise
     * @throws Exception if failed to install app
     */
    public void installFwdLockedAppAndVerifyExists(File apkFile,
            String pkgName, boolean overwrite) throws Exception {
        // Start with a clean slate if we're not overwriting
        if (!overwrite) {
            // cleanup test app just in case it already exists
            mDevice.uninstallPackage(pkgName);
            // grep for package to make sure its not installed
            assertFalse(doesPackageExist(pkgName));
        }

        String result = installFileForwardLocked(apkFile, overwrite);
        assertEquals(null, result);
        assertTrue(doesAppExistAsForwardLocked(pkgName));
        waitForPackageManager();

        // grep for package to make sure it is installed
        assertTrue(doesPackageExist(pkgName));
    }

    /**
     * Helper method for installing an encrypted apk and then verifies the
     * package exists.
     * <p/>
     * Assumes adb is running as root in device under test.
     *
     * @param apkFile the {@link File} of the apk to install
     * @param pkgName the name of the package
     * @param overwrite <code>true</code> if the app should be overwritten,
     *            <code>false</code> otherwise
     * @param algorithm the encryption algorithm used for the encrypted apk
     * @param iv the initialization vector used for the encrypted apk
     * @param encryptionKey the encryption key used for the encrypted apk
     * @param macAlgorithm the mac algorithm used
     * @param macKey the mac key used
     * @param macTag the mac tag used
     * @throws DeviceNotAvailableException
     */
    public void installEncryptedAppAndVerifyExists(File apkFile, String pkgName, boolean overwrite,
            final String algorithm, final String iv, final String encryptionKey,
            final String macAlgorithm, final String macKey, final String macTag)
            throws DeviceNotAvailableException {
        // Start with a clean slate if we're not overwriting
        if (!overwrite) {
            // cleanup test app just in case it already exists
            mDevice.uninstallPackage(pkgName);
            // grep for package to make sure its not installed
            assertFalse(doesPackageExist(pkgName));
        }
        String result = installFileEncrypted(apkFile, overwrite, algorithm, iv, encryptionKey,
                macAlgorithm, macKey, macTag);
        assertEquals(null, result);
        waitForPackageManager();

        // grep for package to make sure it is installed
        assertTrue(doesPackageExist(pkgName));
    }

    /**
     * Helper method to ensure that the encrypted apk and asec file are stored
     * in the correct location.
     * <p/>
     * Assumes adb is running as root in device under test.
     *
     * @param packageName the name of the package
     * @param externalStorage {@link boolean} whether the file is stored to
     *            external sd card.
     * @return true if the app is indeed encrypted, false otherwise.
     * @throws DeviceNotAvailableException
     */
    public boolean appExistsAsEncrypted(String packageName, boolean externalStorage)
            throws DeviceNotAvailableException {
        // getIsExternalStorageEmulated()
        if (externalStorage) {
            if (doesRemoteFileExistContainingString(JB_APP_PRIVATE_PATH, packageName) &&
                    doesRemoteFileExistContainingString(SDCARD_APP_PATH, packageName)) {
                return true;
            }
        } else {
            if (doesRemoteFileExistContainingString(JB_APP_PRIVATE_PATH, packageName) &&
                    doesRemoteFileExistContainingString(JB_ASEC_PRIVATE_PATH, packageName)) {
                return true;
            }
        }
        return false;
    }

    /**
     * Helper method for uninstalling an app.
     * <p/>
     * Assumes adb is running as root in device under test.
     *
     * @param pkgName package name to uninstall
     * @throws DeviceNotAvailableException
     */
    public void uninstallApp(String pkgName) throws DeviceNotAvailableException {
        mDevice.uninstallPackage(pkgName);
        waitForPackageManager();
        // make sure its not installed anymore
        assertFalse(doesPackageExist(pkgName));
    }

    /**
     * Sets the device's install location preference.
     * <p/>
     * Assumes adb is running as root in device under test.
     *
     * @throws DeviceNotAvailableException
     */
    public void setDevicePreferredInstallLocation(InstallLocPreference pref)
            throws DeviceNotAvailableException {
        String command = "pm set-install-location %d";
        int locValue = 0;
        switch (pref) {
            case INTERNAL:
                locValue = 1;
                break;
            case EXTERNAL:
                locValue = 2;
                break;
            default: // AUTO
                locValue = 0;
                break;
        }
        mDevice.executeShellCommand(String.format(command, locValue));
    }

    /**
     * Gets the device's install location preference.
     * <p/>
     * Assumes adb is running as root in device under test.
     *
     * @throws DeviceNotAvailableException
     */
    public InstallLocPreference getDevicePreferredInstallLocation()
            throws DeviceNotAvailableException {
        String result = mDevice.executeShellCommand("pm get-install-location");
        if (result.indexOf('0') != -1) {
            return InstallLocPreference.AUTO;
        }
        else if (result.indexOf('1') != -1) {
            return InstallLocPreference.INTERNAL;
        }
        else {
            return InstallLocPreference.EXTERNAL;
        }
    }

    /**
     * Determines whether the device is using emulated external storage.
     * <p/>
     * Sets mEmulatedExternalStorage based on the result Assumes adb is running
     * as root in device under test.
     *
     * @throws DeviceNotAvailableException
     */
    private void determineExternalStorageEmulation()
            throws DeviceNotAvailableException {
        String result = mDevice.executeShellCommand("dumpsys mount");
        if (result.indexOf("mEmulated=true") != -1) {
            CLog.i("Device is using emulated external storage.");
            mEmulatedExternalStorage = true;
        }
        else if (result.indexOf("mEmulated=false") != -1) {
            CLog.i("Device is using actual external storage.");
            mEmulatedExternalStorage = false;
        }
        else {
            // older devices will not have mEmulated flag in the output
            CLog.i("Unable to precisely determine external storage emulation; assuming false.");
            mEmulatedExternalStorage = false;
        }
    }

    /**
     * Determine the location of the app private path.
     *
     * @param apkFile the {@link File} of test apk to determine packages'
     *            install path.
     * @param pkgName the {@link String} pkgName of the test apk.
     * @throws DeviceNotAvailableException
     */
    public void determinePrivateAppPath(File apkFile, String pkgName)
            throws DeviceNotAvailableException {
        String result = installFileForwardLocked(apkFile, true);
        assertEquals(null, result);
        waitForPackageManager();

        // grep for package to make sure it is installed
        assertTrue(doesPackageExist(pkgName));

        // Determine path of secret path.
        result = mDevice.executeShellCommand("pm path " + pkgName);
        if (result.indexOf(PRE_JB_APP_PRIVATE_PATH) != -1) {
            setAppPrivatePath(PRE_JB_APP_PRIVATE_PATH);
        } else if (result.indexOf(JB_APP_PRIVATE_PATH) != -1) {
            setAppPrivatePath(JB_APP_PRIVATE_PATH);
        } else {
            Assert.fail("Failed to locate private app path on device.");
        }
        CLog.d("Device private app path is: %s", getAppPrivatePath());
        uninstallApp(pkgName);
    }

    /**
     * Returns whether the external storage is emulated or not.
     *
     * @return <code>true</code> if external storage is emulated,
     *         <code>false</code> otherwise.
     */
    public boolean getIsExternalStorageEmulated() {
        return mEmulatedExternalStorage;
    }

    /**
     * Connect device to wifi.
     *
     * @param device
     * @param wifiNetwork
     * @param wifiPsk
     * @param connectionAttempts
     * @return true if able to connect to wifi.
     * @throws DeviceNotAvailableException
     */
    public static boolean connectToWifi(ITestDevice device, String wifiNetwork, String wifiPsk,
            int connectionAttempts) throws DeviceNotAvailableException {
        if (wifiNetwork != null) {
            for (int i = 0; i < connectionAttempts; i++) {
                device.disconnectFromWifi();
                if (device.connectToWifiNetwork(wifiNetwork, wifiPsk)) {
                    CLog.i("Connected to wifi network %s", wifiNetwork);
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * Ensure that the file's permissions matches expectation.
     *
     * @param remoteFilePath {@link String} the remote path for the file to check.
     * @param expectedPerms {@link String} expected permissions.
     * @return true if the permissions for a given file matches, false otherwise.
     * @throws DeviceNotAvailableException
     */
    public boolean checkFilePermissions(String remoteFilePath, String expectedPerms)
            throws DeviceNotAvailableException {
        IFileEntry file = mDevice.getFileEntry(remoteFilePath);
        return file.getPermissions().equals(expectedPerms);
    }

    /**
     * Ensure that the file's owner matches expectation.
     *
     * @param remoteFilePath {@link String} the remote path for the file to check.
     * @param expectedOwner {@link String} the expected owner.
     * @return true if the owner for a given file matches, false otherwise.
     * @throws DeviceNotAvailableException
     */
    public boolean checkFileOwnerName(String remoteFilePath, String expectedOwner)
            throws DeviceNotAvailableException {
        //TODO: uncomment this, when we have support from ddmlib.
        //IFileEntry file = mDevice.getFileEntry(remoteFilePath);
        // return file.getOwner().equals(expectedOwner)
        return true;
    }

    /**
     * Ensure that the file's group matches expectation
     *
     * @param remoteFilePath {@link String} the remote path for the file to check.
     * @param expectedGroup {@link String} the expected group.
     * @return true if the group for a given file matches, false otherwise.
     * @throws DeviceNotAvailableException
     */
    public boolean checkFileGroupName(String remoteFilePath, String expectedGroup)
            throws DeviceNotAvailableException {
        //TODO: uncomment this, when we have support from ddmlib.
        //IFileEntry file = mDevice.getFileEntry(remoteFilePath);
        // return file.getGroup().equals(expectedOwner)
        return true;
    }

    /**
     * Returns the uid of the installed package.
     * @param pkgName package name of the test apk.
     * @return uid of the installed package
     * @throws DeviceNotAvailableException
     */
    public Integer getUid(String pkgName) throws DeviceNotAvailableException {
        String out = mDevice.executeShellCommand(String.format("dumpsys package %s", pkgName));
        Matcher m = Pattern.compile("userId=(\\d+)").matcher(out);
        assertTrue(m.find());

        Integer uid = Integer.parseInt(m.group(1));
        CLog.v("package %s has uid %d", pkgName, uid);
        return uid;
    }

    public String getAbi(String pkgName) throws DeviceNotAvailableException {
        String out = mDevice.executeShellCommand(String.format("dumpsys package %s", pkgName));
        Matcher m = Pattern.compile("primaryCpuAbi=(.+)").matcher(out);
        assertTrue(m.find());

        String abi = m.group(1);
        CLog.i("package %s has abi %s", pkgName, abi);
        return abi;
    }
}