Search in sources :

Example 1 with ChangeNotesCommit

use of com.google.gerrit.server.notedb.ChangeNotesCommit in project gerrit by GerritCodeReview.

the class AbstractDaemonTest method verifyNoAccountDetailsInChangeNotes.

/**
 * Verify that NoteDB commits do not persist user-sensitive information, by running checks for all
 * commits in {@link RefNames#changeMetaRef} for all changes, created during the test.
 *
 * <p>These tests prevent regression, assuming appropriate test coverage for new features. The
 * verification is disabled by default and can be enabled using {@link VerifyNoPiiInChangeNotes}
 * annotation either on test class or method.
 */
protected void verifyNoAccountDetailsInChangeNotes() throws RestApiException, IOException {
    List<ChangeInfo> allChanges = gApi.changes().query().get();
    List<AccountState> allAccounts = accounts.all();
    for (ChangeInfo change : allChanges) {
        try (Repository repo = repoManager.openRepository(Project.nameKey(change.project))) {
            String metaRefName = RefNames.changeMetaRef(Change.Id.tryParse(change._number.toString()).get());
            ObjectId metaTip = repo.getRefDatabase().exactRef(metaRefName).getObjectId();
            ChangeNotesRevWalk revWalk = ChangeNotesCommit.newRevWalk(repo);
            revWalk.reset();
            revWalk.markStart(revWalk.parseCommit(metaTip));
            ChangeNotesCommit commit;
            while ((commit = revWalk.next()) != null) {
                String fullMessage = commit.getFullMessage();
                for (AccountState accountState : allAccounts) {
                    Account account = accountState.account();
                    assertThat(fullMessage).doesNotContain(account.getName());
                    if (account.fullName() != null) {
                        assertThat(fullMessage).doesNotContain(account.fullName());
                    }
                    if (account.displayName() != null) {
                        assertThat(fullMessage).doesNotContain(account.displayName());
                    }
                    if (account.preferredEmail() != null) {
                        assertThat(fullMessage).doesNotContain(account.preferredEmail());
                    }
                    if (accountState.userName().isPresent()) {
                        assertThat(fullMessage).doesNotContain(accountState.userName().get());
                    }
                    List<String> allEmails = accountState.externalIds().stream().map(ExternalId::email).filter(Objects::nonNull).collect(toImmutableList());
                    for (String email : allEmails) {
                        assertThat(fullMessage).doesNotContain(email);
                    }
                }
            }
        }
    }
}
Also used : Account(com.google.gerrit.entities.Account) TestRepository(org.eclipse.jgit.junit.TestRepository) Repository(org.eclipse.jgit.lib.Repository) InMemoryRepository(org.eclipse.jgit.internal.storage.dfs.InMemoryRepository) ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) ObjectId(org.eclipse.jgit.lib.ObjectId) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) ChangeNotesRevWalk(com.google.gerrit.server.notedb.ChangeNotesCommit.ChangeNotesRevWalk) AccountState(com.google.gerrit.server.account.AccountState) IdString(com.google.gerrit.extensions.restapi.IdString) ChangeNotesCommit(com.google.gerrit.server.notedb.ChangeNotesCommit)

Aggregations

Account (com.google.gerrit.entities.Account)1 ChangeInfo (com.google.gerrit.extensions.common.ChangeInfo)1 IdString (com.google.gerrit.extensions.restapi.IdString)1 AccountState (com.google.gerrit.server.account.AccountState)1 ExternalId (com.google.gerrit.server.account.externalids.ExternalId)1 ChangeNotesCommit (com.google.gerrit.server.notedb.ChangeNotesCommit)1 ChangeNotesRevWalk (com.google.gerrit.server.notedb.ChangeNotesCommit.ChangeNotesRevWalk)1 InMemoryRepository (org.eclipse.jgit.internal.storage.dfs.InMemoryRepository)1 TestRepository (org.eclipse.jgit.junit.TestRepository)1 ObjectId (org.eclipse.jgit.lib.ObjectId)1 Repository (org.eclipse.jgit.lib.Repository)1