PageRenderTime 3660ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/gerrit-server/src/test/java/com/google/gerrit/server/project/ProjectControlTest.java

https://gitlab.com/GerritCodeReview/gerrit
Java | 195 lines | 152 code | 29 blank | 14 comment | 6 complexity | 3d32c57cc4c18186ee9e563d9f248e24 MD5 | raw file
  1. // Copyright (C) 2014 The Android Open Source Project
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package com.google.gerrit.server.project;
  15. import static com.google.gerrit.common.data.Permission.READ;
  16. import static com.google.gerrit.server.group.SystemGroupBackend.REGISTERED_USERS;
  17. import static com.google.gerrit.server.project.Util.allow;
  18. import static com.google.gerrit.server.project.Util.deny;
  19. import static org.junit.Assert.assertFalse;
  20. import static org.junit.Assert.assertTrue;
  21. import com.google.gerrit.lifecycle.LifecycleManager;
  22. import com.google.gerrit.reviewdb.client.Account;
  23. import com.google.gerrit.reviewdb.client.Project;
  24. import com.google.gerrit.reviewdb.server.ReviewDb;
  25. import com.google.gerrit.server.CurrentUser;
  26. import com.google.gerrit.server.IdentifiedUser;
  27. import com.google.gerrit.server.account.AccountManager;
  28. import com.google.gerrit.server.account.AuthRequest;
  29. import com.google.gerrit.server.git.ProjectConfig;
  30. import com.google.gerrit.server.schema.SchemaCreator;
  31. import com.google.gerrit.server.util.RequestContext;
  32. import com.google.gerrit.server.util.ThreadLocalRequestContext;
  33. import com.google.gerrit.testutil.InMemoryDatabase;
  34. import com.google.gerrit.testutil.InMemoryModule;
  35. import com.google.gerrit.testutil.InMemoryRepositoryManager;
  36. import com.google.inject.Guice;
  37. import com.google.inject.Inject;
  38. import com.google.inject.Injector;
  39. import com.google.inject.Provider;
  40. import com.google.inject.util.Providers;
  41. import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
  42. import org.eclipse.jgit.junit.TestRepository;
  43. import org.eclipse.jgit.lib.ObjectId;
  44. import org.eclipse.jgit.revwalk.RevCommit;
  45. import org.eclipse.jgit.revwalk.RevWalk;
  46. import org.junit.After;
  47. import org.junit.Before;
  48. import org.junit.Test;
  49. /** Unit tests for {@link ProjectControl}. */
  50. public class ProjectControlTest {
  51. @Inject private AccountManager accountManager;
  52. @Inject private IdentifiedUser.GenericFactory userFactory;
  53. @Inject private InMemoryDatabase schemaFactory;
  54. @Inject private InMemoryRepositoryManager repoManager;
  55. @Inject private ProjectControl.GenericFactory projectControlFactory;
  56. @Inject private SchemaCreator schemaCreator;
  57. @Inject private ThreadLocalRequestContext requestContext;
  58. private LifecycleManager lifecycle;
  59. private ReviewDb db;
  60. private TestRepository<InMemoryRepository> repo;
  61. private ProjectConfig project;
  62. private IdentifiedUser user;
  63. @Before
  64. public void setUp() throws Exception {
  65. Injector injector = Guice.createInjector(new InMemoryModule());
  66. injector.injectMembers(this);
  67. lifecycle = new LifecycleManager();
  68. lifecycle.add(injector);
  69. lifecycle.start();
  70. db = schemaFactory.open();
  71. schemaCreator.create(db);
  72. Account.Id userId = accountManager.authenticate(AuthRequest.forUser("user"))
  73. .getAccountId();
  74. user = userFactory.create(Providers.of(db), userId);
  75. Project.NameKey name = new Project.NameKey("project");
  76. InMemoryRepository inMemoryRepo = repoManager.createRepository(name);
  77. project = new ProjectConfig(name);
  78. project.load(inMemoryRepo);
  79. repo = new TestRepository<>(inMemoryRepo);
  80. requestContext.setContext(new RequestContext() {
  81. @Override
  82. public CurrentUser getUser() {
  83. return user;
  84. }
  85. @Override
  86. public Provider<ReviewDb> getReviewDbProvider() {
  87. return Providers.of(db);
  88. }
  89. });
  90. }
  91. @After
  92. public void tearDown() {
  93. if (repo != null) {
  94. repo.getRepository().close();
  95. }
  96. if (lifecycle != null) {
  97. lifecycle.stop();
  98. }
  99. requestContext.setContext(null);
  100. if (db != null) {
  101. db.close();
  102. }
  103. InMemoryDatabase.drop(schemaFactory);
  104. }
  105. @Test
  106. public void canReadCommitWhenAllRefsVisible() throws Exception {
  107. allow(project, READ, REGISTERED_USERS, "refs/*");
  108. ObjectId id = repo.branch("master").commit().create();
  109. ProjectControl pc = newProjectControl();
  110. RevWalk rw = repo.getRevWalk();
  111. assertTrue(pc.canReadCommit(db, rw, rw.parseCommit(id)));
  112. }
  113. @Test
  114. public void canReadCommitIfRefVisible() throws Exception {
  115. allow(project, READ, REGISTERED_USERS, "refs/heads/branch1");
  116. deny(project, READ, REGISTERED_USERS, "refs/heads/branch2");
  117. ObjectId id1 = repo.branch("branch1").commit().create();
  118. ObjectId id2 = repo.branch("branch2").commit().create();
  119. ProjectControl pc = newProjectControl();
  120. RevWalk rw = repo.getRevWalk();
  121. assertTrue(pc.canReadCommit(db, rw, rw.parseCommit(id1)));
  122. assertFalse(pc.canReadCommit(db, rw, rw.parseCommit(id2)));
  123. }
  124. @Test
  125. public void canReadCommitIfReachableFromVisibleRef() throws Exception {
  126. allow(project, READ, REGISTERED_USERS, "refs/heads/branch1");
  127. deny(project, READ, REGISTERED_USERS, "refs/heads/branch2");
  128. RevCommit parent1 = repo.commit().create();
  129. repo.branch("branch1").commit().parent(parent1).create();
  130. RevCommit parent2 = repo.commit().create();
  131. repo.branch("branch2").commit().parent(parent2).create();
  132. ProjectControl pc = newProjectControl();
  133. RevWalk rw = repo.getRevWalk();
  134. assertTrue(pc.canReadCommit(db, rw, rw.parseCommit(parent1)));
  135. assertFalse(pc.canReadCommit(db, rw, rw.parseCommit(parent2)));
  136. }
  137. @Test
  138. public void cannotReadAfterRollbackWithRestrictedRead() throws Exception {
  139. allow(project, READ, REGISTERED_USERS, "refs/heads/branch1");
  140. RevCommit parent1 = repo.commit().create();
  141. ObjectId id1 = repo.branch("branch1").commit().parent(parent1).create();
  142. ProjectControl pc = newProjectControl();
  143. RevWalk rw = repo.getRevWalk();
  144. assertTrue(pc.canReadCommit(db, rw, rw.parseCommit(parent1)));
  145. assertTrue(pc.canReadCommit(db, rw, rw.parseCommit(id1)));
  146. repo.branch("branch1").update(parent1);
  147. assertTrue(pc.canReadCommit(db, rw, rw.parseCommit(parent1)));
  148. assertFalse(pc.canReadCommit(db, rw, rw.parseCommit(id1)));
  149. }
  150. @Test
  151. public void canReadAfterRollbackWithAllRefsVisible() throws Exception {
  152. allow(project, READ, REGISTERED_USERS, "refs/*");
  153. RevCommit parent1 = repo.commit().create();
  154. ObjectId id1 = repo.branch("branch1").commit().parent(parent1).create();
  155. ProjectControl pc = newProjectControl();
  156. RevWalk rw = repo.getRevWalk();
  157. assertTrue(pc.canReadCommit(db, rw, rw.parseCommit(parent1)));
  158. assertTrue(pc.canReadCommit(db, rw, rw.parseCommit(id1)));
  159. repo.branch("branch1").update(parent1);
  160. assertTrue(pc.canReadCommit(db, rw, rw.parseCommit(parent1)));
  161. assertFalse(pc.canReadCommit(db, rw, rw.parseCommit(id1)));
  162. }
  163. private ProjectControl newProjectControl() throws Exception {
  164. return projectControlFactory.controlFor(project.getName(), user);
  165. }
  166. }