Skip to content

Commit

Permalink
Add unit test for sizelimitinginputstream for more coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
bvasseur-urw committed Sep 19, 2024
1 parent ec1660f commit e654911
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@

import org.junit.Test;

import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.core.Application;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.client.Invocation.Builder;

import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import com.coreoz.plume.jersey.security.control.ContentControlFeature;

Expand All @@ -35,14 +33,22 @@ public void checkContentSize_withBody_whenWithinDefaultLimit_shouldReturn200() {
}

@Test
public void checkContentSize_withBody_whenBeyondDefaultLimit_shouldThrow() {
public void checkContentSize_withBody_whenBeyondDefaultLimit_shouldReturn413() {
// Generate a byte array of ContentControlFeature.DEFAULT_MAX_SIZE + 1
byte[] data = new byte[ContentControlFeature.DEFAULT_MAX_SIZE + 1];
Builder request = target("/test/upload-default").request();
Entity<byte[]> entity = Entity.entity(data, MediaType.APPLICATION_OCTET_STREAM);
assertEquals(Response.Status.REQUEST_ENTITY_TOO_LARGE.getStatusCode(), request.post(entity).getStatus());
}

@Test
public void checkContentSize_withBody_whenContentLengthIsWrong_shouldReturn411() {
// Generate a byte array of ContentControlFeature.DEFAULT_MAX_SIZE + 1
Builder request = target("/test/upload-default").request();
request.header(HttpHeaders.CONTENT_LENGTH, null);
assertEquals(Response.Status.LENGTH_REQUIRED.getStatusCode(), request.post(null).getStatus());
}

@Test
public void checkContentSize_withBody_whenWithinCustomLimit_shouldReturn200() {
byte[] data = "12345".getBytes();
Expand All @@ -51,12 +57,14 @@ public void checkContentSize_withBody_whenWithinCustomLimit_shouldReturn200() {
}

@Test
public void checkContentSize_withBody_whenBeyondCustomLimit_shouldThrow() {
public void checkContentSize_withBody_whenBeyondCustomLimit_shouldReturn413() {
// Generate a byte array of CUSTOM_MAX_SIZE + 1
byte[] data = new byte[TestContentSizeResource.CUSTOM_MAX_SIZE + 1];
Builder request = target("/test/upload-custom").request();
Entity<byte[]> entity = Entity.entity(data, MediaType.APPLICATION_OCTET_STREAM);
assertEquals(Response.Status.REQUEST_ENTITY_TOO_LARGE.getStatusCode(), request.post(entity).getStatus());
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,49 @@ public void testRead_whenEmpty_shouldSuccessWithoutReading() throws IOException
assertEquals(-1, bytesRead);
}

@Test
public void testRead_withoutOffsetAndLength_shouldSuccess() throws IOException {
byte[] data = "1234567890".getBytes(); // 10 bytes, exactly at the limit
byteArrayInputStream = new ByteArrayInputStream(data);
sizeLimitingInputStream = new SizeLimitingInputStream(byteArrayInputStream, LIMIT);
int result = sizeLimitingInputStream.read(data, 0, 5);
assertEquals(5, result);
}

@Test
public void testRead_withoutOffsetAndWithoutLength_shouldSuccess() throws IOException {
byte[] data = "1234567890".getBytes(); // 10 bytes, exactly at the limit
byteArrayInputStream = new ByteArrayInputStream(data);
sizeLimitingInputStream = new SizeLimitingInputStream(byteArrayInputStream, LIMIT);
int result = sizeLimitingInputStream.read();
assertEquals('1', result);
}

@Test
public void testRead_withOffsetAndLength_shouldSuccess() throws IOException {
byte[] data = "1234567890".getBytes(); // 10 bytes, exactly at the limit
byteArrayInputStream = new ByteArrayInputStream(data);
sizeLimitingInputStream = new SizeLimitingInputStream(byteArrayInputStream, LIMIT);
int result = sizeLimitingInputStream.read(data, 3, 5);
assertEquals(5, result);
}

@Test
public void testSkip_withinLength_shouldSuccess() throws IOException {
byte[] data = "1234567890".getBytes(); // 10 bytes, exactly at the limit
byteArrayInputStream = new ByteArrayInputStream(data);
sizeLimitingInputStream = new SizeLimitingInputStream(byteArrayInputStream, LIMIT);
long result = sizeLimitingInputStream.skip(3);
assertEquals(3, result);
}

@Test
public void testSkip_beyondLength_shouldSuccess() throws IOException {
byte[] data = "1234567890".getBytes(); // 10 bytes, exactly at the limit
byteArrayInputStream = new ByteArrayInputStream(data);
sizeLimitingInputStream = new SizeLimitingInputStream(byteArrayInputStream, LIMIT);
long result = sizeLimitingInputStream.skip(11);
assertEquals(10, result);
}

}

0 comments on commit e654911

Please sign in to comment.