Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 60803039 authored by Sudheer Shanka's avatar Sudheer Shanka
Browse files

Return randomly generated session ids from createSession().

Bug: 149324538
Test: atest cts/tests/BlobStore/src/com/android/cts/blob/BlobStoreManagerTest.java
Test: atest ./services/tests/mockingservicestests/src/com/android/server/blob/BlobStoreManagerServiceTest.java
Change-Id: I889c0cc0ebe8baef65f741a2f15c1e3201aec7d2
parent 251c21df
Loading
Loading
Loading
Loading
+46 −15
Original line number Diff line number Diff line
@@ -96,10 +96,12 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.Set;

/**
@@ -122,8 +124,15 @@ public class BlobStoreManagerService extends SystemService {

    // Contains all ids that are currently in use.
    @GuardedBy("mBlobsLock")
    private final ArraySet<Long> mActiveBlobIds = new ArraySet<>();
    // Contains all ids that are currently in use and those that were in use but got deleted in the
    // current boot session.
    @GuardedBy("mBlobsLock")
    private final ArraySet<Long> mKnownBlobIds = new ArraySet<>();

    // Random number generator for new session ids.
    private final Random mRandom = new SecureRandom();

    private final Context mContext;
    private final Handler mHandler;
    private final Injector mInjector;
@@ -181,7 +190,16 @@ public class BlobStoreManagerService extends SystemService {

    @GuardedBy("mBlobsLock")
    private long generateNextSessionIdLocked() {
        return ++mCurrentMaxSessionId;
        // Logic borrowed from PackageInstallerService.
        int n = 0;
        long sessionId;
        do {
            sessionId = Math.abs(mRandom.nextLong());
            if (mKnownBlobIds.indexOf(sessionId) < 0 && sessionId != 0) {
                return sessionId;
            }
        } while (n++ < 32);
        throw new IllegalStateException("Failed to allocate session ID");
    }

    private void registerReceivers() {
@@ -228,11 +246,18 @@ public class BlobStoreManagerService extends SystemService {
    }

    @VisibleForTesting
    void addKnownIdsForTest(long... knownIds) {
    void addActiveIdsForTest(long... activeIds) {
        synchronized (mBlobsLock) {
            for (long id : knownIds) {
                mKnownBlobIds.add(id);
            for (long id : activeIds) {
                addActiveBlobIdLocked(id);
            }
        }
    }

    @VisibleForTesting
    Set<Long> getActiveIdsForTest() {
        synchronized (mBlobsLock) {
            return mActiveBlobIds;
        }
    }

@@ -246,7 +271,7 @@ public class BlobStoreManagerService extends SystemService {
    @GuardedBy("mBlobsLock")
    private void addSessionForUserLocked(BlobStoreSession session, int userId) {
        getUserSessionsLocked(userId).put(session.getSessionId(), session);
        mKnownBlobIds.add(session.getSessionId());
        addActiveBlobIdLocked(session.getSessionId());
    }

    @GuardedBy("mBlobsLock")
@@ -258,7 +283,13 @@ public class BlobStoreManagerService extends SystemService {
    private void addBlobForUserLocked(BlobMetadata blobMetadata,
            ArrayMap<BlobHandle, BlobMetadata> userBlobs) {
        userBlobs.put(blobMetadata.getBlobHandle(), blobMetadata);
        mKnownBlobIds.add(blobMetadata.getBlobId());
        addActiveBlobIdLocked(blobMetadata.getBlobId());
    }

    @GuardedBy("mBlobsLock")
    private void addActiveBlobIdLocked(long id) {
        mActiveBlobIds.add(id);
        mKnownBlobIds.add(id);
    }

    private long createSessionInternal(BlobHandle blobHandle,
@@ -391,7 +422,7 @@ public class BlobStoreManagerService extends SystemService {
                    session.getSessionFile().delete();
                    getUserSessionsLocked(UserHandle.getUserId(session.getOwnerUid()))
                            .remove(session.getSessionId());
                    mKnownBlobIds.remove(session.getSessionId());
                    mActiveBlobIds.remove(session.getSessionId());
                    if (LOGV) {
                        Slog.v(TAG, "Session is invalid; deleted " + session);
                    }
@@ -698,7 +729,7 @@ public class BlobStoreManagerService extends SystemService {
                if (session.getOwnerUid() == uid
                        && session.getOwnerPackageName().equals(packageName)) {
                    session.getSessionFile().delete();
                    mKnownBlobIds.remove(session.getSessionId());
                    mActiveBlobIds.remove(session.getSessionId());
                    indicesToRemove.add(i);
                }
            }
@@ -718,7 +749,7 @@ public class BlobStoreManagerService extends SystemService {
                // Delete the blob if it doesn't have any active leases.
                if (!blobMetadata.hasLeases()) {
                    blobMetadata.getBlobFile().delete();
                    mKnownBlobIds.remove(blobMetadata.getBlobId());
                    mActiveBlobIds.remove(blobMetadata.getBlobId());
                    indicesToRemove.add(i);
                }
            }
@@ -741,7 +772,7 @@ public class BlobStoreManagerService extends SystemService {
                for (int i = 0, count = userSessions.size(); i < count; ++i) {
                    final BlobStoreSession session = userSessions.valueAt(i);
                    session.getSessionFile().delete();
                    mKnownBlobIds.remove(session.getSessionId());
                    mActiveBlobIds.remove(session.getSessionId());
                }
            }

@@ -751,7 +782,7 @@ public class BlobStoreManagerService extends SystemService {
                for (int i = 0, count = userBlobs.size(); i < count; ++i) {
                    final BlobMetadata blobMetadata = userBlobs.valueAt(i);
                    blobMetadata.getBlobFile().delete();
                    mKnownBlobIds.remove(blobMetadata.getBlobId());
                    mActiveBlobIds.remove(blobMetadata.getBlobId());
                }
            }
            if (LOGV) {
@@ -771,7 +802,7 @@ public class BlobStoreManagerService extends SystemService {
            for (File file : blobsDir.listFiles()) {
                try {
                    final long id = Long.parseLong(file.getName());
                    if (mKnownBlobIds.indexOf(id) < 0) {
                    if (mActiveBlobIds.indexOf(id) < 0) {
                        filesToDelete.add(file);
                        deletedBlobIds.add(id);
                    }
@@ -806,7 +837,7 @@ public class BlobStoreManagerService extends SystemService {

                if (shouldRemove) {
                    blobMetadata.getBlobFile().delete();
                    mKnownBlobIds.remove(blobMetadata.getBlobId());
                    mActiveBlobIds.remove(blobMetadata.getBlobId());
                    deletedBlobIds.add(blobMetadata.getBlobId());
                }
                return shouldRemove;
@@ -836,7 +867,7 @@ public class BlobStoreManagerService extends SystemService {

                if (shouldRemove) {
                    blobStoreSession.getSessionFile().delete();
                    mKnownBlobIds.remove(blobStoreSession.getSessionId());
                    mActiveBlobIds.remove(blobStoreSession.getSessionId());
                    indicesToRemove.add(j);
                    deletedBlobIds.add(blobStoreSession.getSessionId());
                }
@@ -883,7 +914,7 @@ public class BlobStoreManagerService extends SystemService {
            }
            blobMetadata.getBlobFile().delete();
            userBlobs.remove(blobHandle);
            mKnownBlobIds.remove(blobMetadata.getBlobId());
            mActiveBlobIds.remove(blobMetadata.getBlobId());
            writeBlobsInfoAsync();
        }
    }
+13 −8
Original line number Diff line number Diff line
@@ -149,7 +149,7 @@ public class BlobStoreManagerServiceTest {
        final BlobMetadata blobMetadata2 = createBlobMetadataMock(blobId2, blobFile2, false);
        mUserBlobs.put(blobHandle2, blobMetadata2);

        mService.addKnownIdsForTest(sessionId1, sessionId2, sessionId3, sessionId4,
        mService.addActiveIdsForTest(sessionId1, sessionId2, sessionId3, sessionId4,
                blobId1, blobId2);

        // Invoke test method
@@ -180,8 +180,10 @@ public class BlobStoreManagerServiceTest {
        assertThat(mUserBlobs.get(blobHandle1)).isNotNull();
        assertThat(mUserBlobs.get(blobHandle2)).isNull();

        assertThat(mService.getKnownIdsForTest()).containsExactly(
        assertThat(mService.getActiveIdsForTest()).containsExactly(
                sessionId2, sessionId3, blobId1);
        assertThat(mService.getKnownIdsForTest()).containsExactly(
                sessionId1, sessionId2, sessionId3, sessionId4, blobId1, blobId2);
    }

    @Test
@@ -198,12 +200,12 @@ public class BlobStoreManagerServiceTest {
        doReturn(String.valueOf(testId3)).when(file3).getName();

        doReturn(new File[] {file1, file2, file3}).when(mBlobsDir).listFiles();
        mService.addKnownIdsForTest(testId1, testId3);
        mService.addActiveIdsForTest(testId1, testId3);

        // Invoke test method
        mService.handleIdleMaintenanceLocked();

        // Verify unknown blobs are delete
        // Verify unknown blobs are deleted
        verify(file1, never()).delete();
        verify(file2).delete();
        verify(file3, never()).delete();
@@ -242,7 +244,7 @@ public class BlobStoreManagerServiceTest {
                sessionId3, sessionFile3, blobHandle3);
        mUserSessions.append(sessionId3, session3);

        mService.addKnownIdsForTest(sessionId1, sessionId2, sessionId3);
        mService.addActiveIdsForTest(sessionId1, sessionId2, sessionId3);

        // Invoke test method
        mService.handleIdleMaintenanceLocked();
@@ -255,7 +257,9 @@ public class BlobStoreManagerServiceTest {
        assertThat(mUserSessions.size()).isEqualTo(1);
        assertThat(mUserSessions.get(sessionId2)).isNotNull();

        assertThat(mService.getKnownIdsForTest()).containsExactly(sessionId2);
        assertThat(mService.getActiveIdsForTest()).containsExactly(sessionId2);
        assertThat(mService.getKnownIdsForTest()).containsExactly(
                sessionId1, sessionId2, sessionId3);
    }

    @Test
@@ -282,7 +286,7 @@ public class BlobStoreManagerServiceTest {
        final BlobMetadata blobMetadata3 = createBlobMetadataMock(blobId3, blobFile3, false);
        mUserBlobs.put(blobHandle3, blobMetadata3);

        mService.addKnownIdsForTest(blobId1, blobId2, blobId3);
        mService.addActiveIdsForTest(blobId1, blobId2, blobId3);

        // Invoke test method
        mService.handleIdleMaintenanceLocked();
@@ -295,7 +299,8 @@ public class BlobStoreManagerServiceTest {
        assertThat(mUserBlobs.size()).isEqualTo(1);
        assertThat(mUserBlobs.get(blobHandle2)).isNotNull();

        assertThat(mService.getKnownIdsForTest()).containsExactly(blobId2);
        assertThat(mService.getActiveIdsForTest()).containsExactly(blobId2);
        assertThat(mService.getKnownIdsForTest()).containsExactly(blobId1, blobId2, blobId3);
    }

    private BlobStoreSession createBlobStoreSessionMock(String ownerPackageName, int ownerUid,