You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DoPrivilegedTest.java 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.user;
  21. import org.junit.Before;
  22. import org.junit.Test;
  23. import org.sonar.db.component.ComponentDto;
  24. import org.sonar.server.tester.MockUserSession;
  25. import static org.assertj.core.api.Assertions.assertThat;
  26. import static org.assertj.core.api.Assertions.fail;
  27. public class DoPrivilegedTest {
  28. private static final String LOGIN = "dalailaHidou!";
  29. private ThreadLocalUserSession threadLocalUserSession = new ThreadLocalUserSession();
  30. private MockUserSession session = new MockUserSession(LOGIN);
  31. @Before
  32. public void setUp() {
  33. threadLocalUserSession.set(session);
  34. }
  35. @Test
  36. public void allow_everything_in_privileged_block_only() {
  37. UserSessionCatcherTask catcher = new UserSessionCatcherTask();
  38. DoPrivileged.execute(catcher);
  39. // verify the session used inside Privileged task
  40. assertThat(catcher.userSession.isLoggedIn()).isFalse();
  41. assertThat(catcher.userSession.hasComponentPermission("any permission", new ComponentDto())).isTrue();
  42. assertThat(catcher.userSession.isSystemAdministrator()).isTrue();
  43. // verify session in place after task is done
  44. assertThat(threadLocalUserSession.get()).isSameAs(session);
  45. }
  46. @Test
  47. public void loose_privileges_on_exception() {
  48. UserSessionCatcherTask catcher = new UserSessionCatcherTask() {
  49. @Override
  50. protected void doPrivileged() {
  51. super.doPrivileged();
  52. throw new RuntimeException("Test to lose privileges");
  53. }
  54. };
  55. try {
  56. DoPrivileged.execute(catcher);
  57. fail("An exception should have been raised!");
  58. } catch (Throwable ignored) {
  59. // verify session in place after task is done
  60. assertThat(threadLocalUserSession.get()).isSameAs(session);
  61. // verify the session used inside Privileged task
  62. assertThat(catcher.userSession.isLoggedIn()).isFalse();
  63. assertThat(catcher.userSession.hasComponentPermission("any permission", new ComponentDto())).isTrue();
  64. }
  65. }
  66. private class UserSessionCatcherTask extends DoPrivileged.Task {
  67. UserSession userSession;
  68. public UserSessionCatcherTask() {
  69. super(DoPrivilegedTest.this.threadLocalUserSession);
  70. }
  71. @Override
  72. protected void doPrivileged() {
  73. userSession = threadLocalUserSession.get();
  74. }
  75. }
  76. }