Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #5444: fix StringIndexOutOfBoundsException on corrupted state param #5447

Merged
merged 4 commits into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,11 @@ public Object getState(FacesContext ctx, String viewId) {
}

int sep = compoundId.indexOf(':');
assert sep != -1;
assert sep < compoundId.length();

if (sep == -1) {
LOGGER.log(FINE, "Unable to restore server side state for view ID {0} as no state ID is available", viewId);
return null;
}

String idInLogicalMap = compoundId.substring(0, sep);
String idInActualMap = compoundId.substring(sep + 1);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package com.sun.faces.renderkit.html_basic;

import static jakarta.faces.render.ResponseStateManager.VIEW_STATE_PARAM;
import static java.util.Collections.emptyEnumeration;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;

import java.util.Map;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;

import com.sun.faces.renderkit.ServerSideStateHelper;

import jakarta.faces.component.UIViewRoot;
import jakarta.faces.context.ExternalContext;
import jakarta.faces.context.FacesContext;
import jakarta.servlet.ServletContext;

public class TestServerSideStateHelper {

private FacesContext mockedFacesContext;
private ExternalContext mockedExternalContext;
private MockedStatic<FacesContext> mockedStaticFacesContext;

@BeforeEach
public void setup() {
ServletContext mockedServletContext = mock(ServletContext.class);
when(mockedServletContext.getInitParameterNames()).thenReturn(emptyEnumeration());
mockedExternalContext = mock(ExternalContext.class);
when(mockedExternalContext.getContext()).thenReturn(mockedServletContext);
mockedFacesContext = mock(FacesContext.class);
when(mockedFacesContext.getExternalContext()).thenReturn(mockedExternalContext);
when(mockedFacesContext.getViewRoot()).thenReturn(new UIViewRoot());
mockedStaticFacesContext = mockStatic(FacesContext.class);
mockedStaticFacesContext.when(FacesContext::getCurrentInstance).thenReturn(mockedFacesContext);
}

@AfterEach
public void teardown() {
mockedStaticFacesContext.close();
}

private void prepareViewStateParam(String viewStateParam) {
when(mockedExternalContext.getRequestParameterMap()).thenReturn(Map.of(VIEW_STATE_PARAM, viewStateParam));
}

@Test
void testViewStateParam1() {
prepareViewStateParam("1");
assertDoesNotThrow(() -> new ServerSideStateHelper().getState(mockedFacesContext, null));
}

@Test
void testViewStateParam2() {
prepareViewStateParam("-1");
assertDoesNotThrow(() -> new ServerSideStateHelper().getState(mockedFacesContext, null));
}

@Test
void testViewStateParam3() {
prepareViewStateParam("");
assertDoesNotThrow(() -> new ServerSideStateHelper().getState(mockedFacesContext, null));
}

@Test
void testViewStateParam4() {
prepareViewStateParam("1:");
assertDoesNotThrow(() -> new ServerSideStateHelper().getState(mockedFacesContext, null));
}

@Test
void testViewStateParam5() {
prepareViewStateParam(":");
assertDoesNotThrow(() -> new ServerSideStateHelper().getState(mockedFacesContext, null));
}

@Test
void testViewStateParam6() {
prepareViewStateParam(":1");
assertDoesNotThrow(() -> new ServerSideStateHelper().getState(mockedFacesContext, null));
}

@Test
void testViewStateParam7() {
prepareViewStateParam("1:1");
assertDoesNotThrow(() -> new ServerSideStateHelper().getState(mockedFacesContext, null));
}

@Test
void testViewStateParam8() {
prepareViewStateParam("stateless");
assertDoesNotThrow(() -> new ServerSideStateHelper().getState(mockedFacesContext, null));
}

}
Loading