Skip to content

Commit

Permalink
Add dumpsys param to dump users running backup service
Browse files Browse the repository at this point in the history
This is required in CTS tests to know when UserBackupManagerService init is finished.

Bug: 131307343
Test: 1) atest RunBackupFrameworksServicesRoboTests
      2) atest CtsBackupTestCases
      3) atest CtsBackupHostTestCases
      4) atest GtsBackupTestCases
      5) atest GtsBackupHostTestCases

Change-Id: Ia220f32b5be793b3b36efb2985604e29b0015e53
  • Loading branch information
rtkhakokhov committed May 10, 2019
1 parent 7b0194c commit e039932
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import android.util.SparseArray;

import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.DumpUtils;
import com.android.server.SystemConfig;
import com.android.server.SystemService;

Expand All @@ -71,6 +72,9 @@ public class BackupManagerService {
public static final boolean MORE_DEBUG = false;
public static final boolean DEBUG_SCHEDULING = true;

@VisibleForTesting
static final String DUMP_RUNNING_USERS_MESSAGE = "Backup Manager is running for users:";

// The published binder is a singleton Trampoline object that calls through to the proper code.
// This indirection lets us turn down the heavy implementation object on the fly without
// disturbing binders that have been cached elsewhere in the system.
Expand Down Expand Up @@ -849,6 +853,23 @@ public void acknowledgeAdbBackupOrRestore(

/** Prints service state for 'dumpsys backup'. */
public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
if (!DumpUtils.checkDumpAndUsageStatsPermission(mContext, TAG, pw)) {
return;
}

if (args != null) {
for (String arg : args) {
if ("users".equals(arg.toLowerCase())) {
pw.print(DUMP_RUNNING_USERS_MESSAGE);
for (int i = 0; i < mServiceUsers.size(); i++) {
pw.print(" " + mServiceUsers.keyAt(i));
}
pw.println();
return;
}
}
}

UserBackupManagerService userBackupManagerService =
getServiceForUserIfCallerHasPermission(UserHandle.USER_SYSTEM, "dump()");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.backup.IBackupTransport;
import com.android.internal.util.DumpUtils;
import com.android.internal.util.Preconditions;
import com.android.server.AppWidgetBackupBridge;
import com.android.server.EventLogTags;
Expand Down Expand Up @@ -3470,8 +3469,6 @@ public String[] filterAppsEligibleForBackup(String[] packages) {

/** Prints service state for 'dumpsys backup'. */
public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
if (!DumpUtils.checkDumpAndUsageStatsPermission(mContext, TAG, pw)) return;

long identityToken = Binder.clearCallingIdentity();
try {
if (args != null) {
Expand All @@ -3480,6 +3477,8 @@ public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
pw.println("'dumpsys backup' optional arguments:");
pw.println(" -h : this help text");
pw.println(" a[gents] : dump information about defined backup agents");
pw.println(" users : dump the list of users for which backup service "
+ "is running");
return;
} else if ("agents".startsWith(arg)) {
dumpAgents(pw);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package com.android.server.backup;

import static android.Manifest.permission.BACKUP;
import static android.Manifest.permission.DUMP;
import static android.Manifest.permission.INTERACT_ACROSS_USERS_FULL;
import static android.Manifest.permission.PACKAGE_USAGE_STATS;

import static com.android.server.backup.testing.BackupManagerServiceTestUtils.startBackupThread;
import static com.android.server.backup.testing.TransportData.backupTransport;
Expand Down Expand Up @@ -64,6 +66,7 @@
import java.io.FileDescriptor;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;

/** Tests for the user-aware backup/restore system service {@link BackupManagerService}. */
@RunWith(RobolectricTestRunner.class)
Expand Down Expand Up @@ -1515,6 +1518,8 @@ public void testAcknowledgeAdbBackupOrRestore_onUnknownUser_doesNotPropagateCall
/** Test that the backup service routes methods correctly to the user that requests it. */
@Test
public void testDump_onRegisteredUser_callsMethodForUser() throws Exception {
grantDumpPermissions();

BackupManagerService backupManagerService =
createServiceAndRegisterUser(UserHandle.USER_SYSTEM, mUserOneService);
File testFile = createTestFile();
Expand All @@ -1530,6 +1535,8 @@ public void testDump_onRegisteredUser_callsMethodForUser() throws Exception {
/** Test that the backup service does not route methods for non-registered users. */
@Test
public void testDump_onUnknownUser_doesNotPropagateCall() throws Exception {
grantDumpPermissions();

BackupManagerService backupManagerService = createService();
File testFile = createTestFile();
FileDescriptor fileDescriptor = new FileDescriptor();
Expand All @@ -1541,6 +1548,31 @@ public void testDump_onUnknownUser_doesNotPropagateCall() throws Exception {
verify(mUserOneService, never()).dump(fileDescriptor, printWriter, args);
}

/** Test that 'dumpsys backup users' dumps the list of users registered in backup service*/
@Test
public void testDump_users_dumpsListOfRegisteredUsers() {
grantDumpPermissions();

BackupManagerService backupManagerService = createServiceAndRegisterUser(mUserOneId,
mUserOneService);
StringWriter out = new StringWriter();
PrintWriter writer = new PrintWriter(out);
String[] args = {"users"};

backupManagerService.dump(null, writer, args);

writer.flush();
assertEquals(
String.format("%s %d\n", BackupManagerService.DUMP_RUNNING_USERS_MESSAGE,
mUserOneId),
out.toString());
}

private void grantDumpPermissions() {
mShadowContext.grantPermissions(DUMP);
mShadowContext.grantPermissions(PACKAGE_USAGE_STATS);
}

private File createTestFile() throws IOException {
File testFile = new File(mContext.getFilesDir(), "test");
testFile.createNewFile();
Expand Down

0 comments on commit e039932

Please sign in to comment.