|
| 1 | +/* |
| 2 | + * Copyright (C) 2026 Dominik Schadow, dominikschadow@gmail.com |
| 3 | + * |
| 4 | + * This file is part of the Java Security project. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package de.dominikschadow.javasecurity.csrf; |
| 19 | + |
| 20 | +import org.junit.jupiter.api.BeforeEach; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import org.mockito.Mock; |
| 23 | +import org.mockito.MockitoAnnotations; |
| 24 | + |
| 25 | +import javax.servlet.ServletException; |
| 26 | +import javax.servlet.http.HttpServletRequest; |
| 27 | +import javax.servlet.http.HttpSession; |
| 28 | +import java.security.NoSuchAlgorithmException; |
| 29 | +import java.security.NoSuchProviderException; |
| 30 | + |
| 31 | +import static org.junit.jupiter.api.Assertions.*; |
| 32 | +import static org.mockito.Mockito.*; |
| 33 | + |
| 34 | +/** |
| 35 | + * Tests for the CSRFTokenHandler class. |
| 36 | + * |
| 37 | + * @author Dominik Schadow |
| 38 | + */ |
| 39 | +class CSRFTokenHandlerTest { |
| 40 | + @Mock |
| 41 | + private HttpServletRequest request; |
| 42 | + |
| 43 | + @Mock |
| 44 | + private HttpSession session; |
| 45 | + |
| 46 | + @BeforeEach |
| 47 | + void setUp() { |
| 48 | + MockitoAnnotations.openMocks(this); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void getToken_withNullSession_throwsServletException() { |
| 53 | + assertThrows(ServletException.class, () -> CSRFTokenHandler.getToken(null)); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void getToken_withValidSessionWithoutToken_generatesNewToken() throws Exception { |
| 58 | + when(session.getAttribute(CSRFTokenHandler.CSRF_TOKEN)).thenReturn(null); |
| 59 | + |
| 60 | + String token = CSRFTokenHandler.getToken(session); |
| 61 | + |
| 62 | + assertNotNull(token); |
| 63 | + assertFalse(token.isEmpty()); |
| 64 | + verify(session).setAttribute(eq(CSRFTokenHandler.CSRF_TOKEN), anyString()); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void getToken_withValidSessionWithEmptyToken_generatesNewToken() throws Exception { |
| 69 | + when(session.getAttribute(CSRFTokenHandler.CSRF_TOKEN)).thenReturn(""); |
| 70 | + |
| 71 | + String token = CSRFTokenHandler.getToken(session); |
| 72 | + |
| 73 | + assertNotNull(token); |
| 74 | + assertFalse(token.isEmpty()); |
| 75 | + verify(session).setAttribute(eq(CSRFTokenHandler.CSRF_TOKEN), anyString()); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void getToken_withValidSessionWithExistingToken_returnsExistingToken() throws Exception { |
| 80 | + String existingToken = "existingToken123"; |
| 81 | + when(session.getAttribute(CSRFTokenHandler.CSRF_TOKEN)).thenReturn(existingToken); |
| 82 | + |
| 83 | + String token = CSRFTokenHandler.getToken(session); |
| 84 | + |
| 85 | + assertEquals(existingToken, token); |
| 86 | + verify(session, never()).setAttribute(anyString(), anyString()); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + void isValid_withNullSession_throwsServletException() { |
| 91 | + when(request.getSession(false)).thenReturn(null); |
| 92 | + |
| 93 | + assertThrows(ServletException.class, () -> CSRFTokenHandler.isValid(request)); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void isValid_withMatchingToken_returnsTrue() throws Exception { |
| 98 | + String csrfToken = "validToken123"; |
| 99 | + when(request.getSession(false)).thenReturn(session); |
| 100 | + when(session.getAttribute(CSRFTokenHandler.CSRF_TOKEN)).thenReturn(csrfToken); |
| 101 | + when(request.getParameter(CSRFTokenHandler.CSRF_TOKEN)).thenReturn(csrfToken); |
| 102 | + |
| 103 | + boolean result = CSRFTokenHandler.isValid(request); |
| 104 | + |
| 105 | + assertTrue(result); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + void isValid_withNonMatchingToken_returnsFalse() throws Exception { |
| 110 | + when(request.getSession(false)).thenReturn(session); |
| 111 | + when(session.getAttribute(CSRFTokenHandler.CSRF_TOKEN)).thenReturn("sessionToken"); |
| 112 | + when(request.getParameter(CSRFTokenHandler.CSRF_TOKEN)).thenReturn("differentToken"); |
| 113 | + |
| 114 | + boolean result = CSRFTokenHandler.isValid(request); |
| 115 | + |
| 116 | + assertFalse(result); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + void isValid_withNullRequestToken_returnsFalse() throws Exception { |
| 121 | + when(request.getSession(false)).thenReturn(session); |
| 122 | + when(session.getAttribute(CSRFTokenHandler.CSRF_TOKEN)).thenReturn("sessionToken"); |
| 123 | + when(request.getParameter(CSRFTokenHandler.CSRF_TOKEN)).thenReturn(null); |
| 124 | + |
| 125 | + boolean result = CSRFTokenHandler.isValid(request); |
| 126 | + |
| 127 | + assertFalse(result); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + void isValid_withNullSessionToken_returnsFalse() throws Exception { |
| 132 | + when(request.getSession(false)).thenReturn(session); |
| 133 | + when(session.getAttribute(CSRFTokenHandler.CSRF_TOKEN)).thenReturn(null); |
| 134 | + when(request.getParameter(CSRFTokenHandler.CSRF_TOKEN)).thenReturn("requestToken"); |
| 135 | + |
| 136 | + boolean result = CSRFTokenHandler.isValid(request); |
| 137 | + |
| 138 | + assertFalse(result); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + void isValid_withBothTokensNull_returnsTrue() throws Exception { |
| 143 | + when(request.getSession(false)).thenReturn(session); |
| 144 | + when(session.getAttribute(CSRFTokenHandler.CSRF_TOKEN)).thenReturn(null); |
| 145 | + when(request.getParameter(CSRFTokenHandler.CSRF_TOKEN)).thenReturn(null); |
| 146 | + |
| 147 | + boolean result = CSRFTokenHandler.isValid(request); |
| 148 | + |
| 149 | + // When session has no token, getToken() generates a new one |
| 150 | + // So the tokens won't match |
| 151 | + assertFalse(result); |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + void getToken_generatesUniqueTokens() throws Exception { |
| 156 | + HttpSession session1 = mock(HttpSession.class); |
| 157 | + HttpSession session2 = mock(HttpSession.class); |
| 158 | + when(session1.getAttribute(CSRFTokenHandler.CSRF_TOKEN)).thenReturn(null); |
| 159 | + when(session2.getAttribute(CSRFTokenHandler.CSRF_TOKEN)).thenReturn(null); |
| 160 | + |
| 161 | + String token1 = CSRFTokenHandler.getToken(session1); |
| 162 | + String token2 = CSRFTokenHandler.getToken(session2); |
| 163 | + |
| 164 | + assertNotNull(token1); |
| 165 | + assertNotNull(token2); |
| 166 | + // Tokens should be different (with very high probability) |
| 167 | + assertNotEquals(token1, token2); |
| 168 | + } |
| 169 | +} |
0 commit comments