Skip to content

Commit

Permalink
refactor, rename
Browse files Browse the repository at this point in the history
  • Loading branch information
siyuniu-ms committed Feb 24, 2023
1 parent bf171da commit 1008aef
Show file tree
Hide file tree
Showing 15 changed files with 36 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

class InjectionTest {
class OutputStreamSnippetInjectionHelperTest {

@Test
void testInjectionForStringContainHeadTag() throws IOException {
String testSnippet = "\n <script type=\"text/javascript\"> Test </script>";
String original = readFile("staticHtmlOrigin.html");
String correct = readFile("staticHtmlAfter.html");
String original = readFile("beforeSnippetInjection.html");
String correct = readFile("afterSnippetInjection.html");
byte[] originalBytes = original.getBytes(StandardCharsets.UTF_8);
SnippetInjectingResponseWrapper response = mock(SnippetInjectingResponseWrapper.class);
when(response.isCommitted()).thenReturn(false);
Expand All @@ -40,7 +40,7 @@ public void write(int b) throws IOException {
}
};
OutputStreamSnippetInjectionHelper helper = new OutputStreamSnippetInjectionHelper(testSnippet);
boolean injected = helper.handleWrite(originalBytes, 0, originalBytes.length, obj, sp);
boolean injected = helper.handleWrite(obj, sp, originalBytes, 0, originalBytes.length);
assertThat(obj.getHeadTagBytesSeen()).isEqualTo(-1);
assertThat(injected).isEqualTo(true);
writer.flush();
Expand All @@ -54,8 +54,8 @@ public void write(int b) throws IOException {
@Disabled
void testInjectionForChinese() throws IOException {
String testSnippet = "\n <script type=\"text/javascript\"> Test </script>";
String original = readFile("staticHtmlChineseOrigin.html");
String correct = readFile("staticHtmlChineseAfter.html");
String original = readFile("beforeSnippetInjectionChinese.html");
String correct = readFile("afterSnippetInjectionChinese.html");
byte[] originalBytes = original.getBytes(StandardCharsets.UTF_8);

SnippetInjectingResponseWrapper response = mock(SnippetInjectingResponseWrapper.class);
Expand All @@ -73,7 +73,7 @@ public void write(int b) throws IOException {
}
};
OutputStreamSnippetInjectionHelper helper = new OutputStreamSnippetInjectionHelper(testSnippet);
boolean injected = helper.handleWrite(originalBytes, 0, originalBytes.length, obj, sp);
boolean injected = helper.handleWrite(obj, sp, originalBytes, 0, originalBytes.length);
assertThat(obj.getHeadTagBytesSeen()).isEqualTo(-1);
assertThat(injected).isEqualTo(true);
writer.flush();
Expand Down Expand Up @@ -103,7 +103,7 @@ public void write(int b) throws IOException {
}
};
OutputStreamSnippetInjectionHelper helper = new OutputStreamSnippetInjectionHelper(testSnippet);
boolean injected = helper.handleWrite(originalBytes, 0, originalBytes.length, obj, sp);
boolean injected = helper.handleWrite(obj, sp, originalBytes, 0, originalBytes.length);
assertThat(obj.getHeadTagBytesSeen()).isEqualTo(0);
assertThat(injected).isEqualTo(false);
writer.flush();
Expand Down Expand Up @@ -132,7 +132,7 @@ public void write(int b) throws IOException {
};
OutputStreamSnippetInjectionHelper helper = new OutputStreamSnippetInjectionHelper(testSnippet);
boolean injected =
helper.handleWrite(originalFirstPartBytes, 0, originalFirstPartBytes.length, obj, sp);
helper.handleWrite(obj, sp, originalFirstPartBytes, 0, originalFirstPartBytes.length);

writer.flush();
String result = writer.toString();
Expand All @@ -150,7 +150,7 @@ public void write(int b) throws IOException {
+ "</html>";
byte[] originalSecondPartBytes = originalSecondPart.getBytes(StandardCharsets.UTF_8);
injected =
helper.handleWrite(originalSecondPartBytes, 0, originalSecondPartBytes.length, obj, sp);
helper.handleWrite(obj, sp, originalSecondPartBytes, 0, originalSecondPartBytes.length);
assertThat(obj.getHeadTagBytesSeen()).isEqualTo(-1);
assertThat(injected).isEqualTo(true);
String correctSecondPart =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class SnippetInjectingResponseWrapperTest {
@Test
void testInjectToTextHtml() throws IOException {

String original = readFile("staticHtmlOrigin.html");
String correct = readFile("staticHtmlAfter.html");
String original = readFile("beforeSnippetInjection.html");
String correct = readFile("afterSnippetInjection.html");
HttpServletResponse response = mock(HttpServletResponse.class);
when(response.getContentType()).thenReturn("text/html");
when(response.getStatus()).thenReturn(200);
Expand All @@ -47,8 +47,8 @@ void testInjectToTextHtml() throws IOException {
@Disabled
void testInjectToChineseTextHtml() throws IOException {

String original = readFile("staticHtmlChineseOrigin.html");
String correct = readFile("staticHtmlChineseAfter.html");
String original = readFile("beforeSnippetInjectionChinese.html");
String correct = readFile("afterSnippetInjectionChinese.html");
HttpServletResponse response = mock(HttpServletResponse.class);
when(response.getContentType()).thenReturn("text/html");

Expand All @@ -69,7 +69,7 @@ void testInjectToChineseTextHtml() throws IOException {
@Test
void shouldNotInjectToTextHtml() throws IOException {

String original = readFile("staticHtmlOrigin.html");
String original = readFile("beforeSnippetInjection.html");

StringWriter writer = new StringWriter();
HttpServletResponse response = mock(HttpServletResponse.class);
Expand All @@ -94,8 +94,8 @@ void shouldNotInjectToTextHtml() throws IOException {
@Test
void testWriteInt() throws IOException {

String original = readFile("staticHtmlOrigin.html");
String correct = readFile("staticHtmlAfter.html");
String original = readFile("beforeSnippetInjection.html");
String correct = readFile("afterSnippetInjection.html");
HttpServletResponse response = mock(HttpServletResponse.class);
when(response.getContentType()).thenReturn("text/html");

Expand All @@ -120,8 +120,8 @@ void testWriteInt() throws IOException {
@Test
void testWriteCharArray() throws IOException {

String original = readFile("staticHtmlChineseOrigin.html");
String correct = readFile("staticHtmlChineseAfter.html");
String original = readFile("beforeSnippetInjectionChinese.html");
String correct = readFile("afterSnippetInjectionChinese.html");
HttpServletResponse response = mock(HttpServletResponse.class);
when(response.getContentType()).thenReturn("text/html");
when(response.getStatus()).thenReturn(200);
Expand All @@ -145,8 +145,8 @@ void testWriteCharArray() throws IOException {
@Test
void testWriteWithOffset() throws IOException {

String original = readFile("staticHtmlChineseOrigin.html");
String correct = readFile("staticHtmlChineseAfter.html");
String original = readFile("beforeSnippetInjectionChinese.html");
String correct = readFile("afterSnippetInjectionChinese.html");
String extraBuffer = "this buffer should not be print out";
original = extraBuffer + original;
HttpServletResponse response = mock(HttpServletResponse.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ dependencies {

testLibrary("org.eclipse.jetty:jetty-server:8.0.0.v20110901")
testLibrary("org.eclipse.jetty:jetty-servlet:8.0.0.v20110901")
// TODO (trask?) test against tomcat 7 (servlet 3.0)
testLibrary("org.apache.tomcat.embed:tomcat-embed-core:8.0.41")
testLibrary("org.apache.tomcat.embed:tomcat-embed-jasper:8.0.41")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ public static boolean methodEnter(
// if it returns false, then it means nothing was written to the servletOutputStream and the
// original method call should be executed
return !getSnippetInjectionHelper()
.handleWrite(write, 0, write.length, state, servletOutputStream);
.handleWrite(state, servletOutputStream, write, 0, write.length);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ public static boolean methodEnter(
// call (see skipOn above)
// if it returns false, then it means nothing was written to the servletOutputStream and the
// original method call should be executed
return !getSnippetInjectionHelper().handleWrite(write, off, len, state, servletOutputStream);
return !getSnippetInjectionHelper().handleWrite(state, servletOutputStream, write, off, len);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public OutputStreamSnippetInjectionHelper(String snippet) {
* true, and would write the original bytes when the return value is false.
*/
public boolean handleWrite(
byte[] original, int off, int length, InjectionState state, OutputStream out)
InjectionState state, OutputStream out, byte[] original, int off, int length)
throws IOException {
if (state.isHeadTagWritten()) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ abstract class AbstractServlet3Test<SERVER, CONTEXT> extends HttpServerTest<SERV

expect:
response.status().code() == HTML_SERVLET_OUTPUT_STREAM.status
// check response content-length header
String result = "<!DOCTYPE html>\n" +
"<html lang=\"en\">\n" +
"<head>\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,11 @@ class TestServlet3 {
resp.writer.print(endpoint.body)
throw new ServletException(endpoint.body)
case HTML_PRINT_WRITER:
// intentionally testing HTML_PRINT_WRITER and HTML_SERVLET_OUTPUT_STREAM with different order of setting status and contentType
resp.status = endpoint.status
// intentionally testing setting status before contentType here to cover that case somewhere resp.status = endpoint.status
resp.contentType = "text/html"
resp.writer.print(endpoint.body)
break
case HTML_SERVLET_OUTPUT_STREAM:
// intentionally testing HTML_PRINT_WRITER and HTML_SERVLET_OUTPUT_STREAM with different order of setting status and contentType
resp.contentType = "text/html"
resp.status = endpoint.status
resp.getOutputStream().print(endpoint.body)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public class ExperimentalSnippetHolder {

private static String snippet = "";
private static volatile String snippet = "";

public static void setSnippet(String snippet) {
ExperimentalSnippetHolder.snippet = snippet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@

public class ServletOutputStreamInstrumentation implements TypeInstrumentation {
private final String basePackageName;
private final String writeBytesAndOffsetClassName;
private final String writeBytesClassName;
private final String writeBytesAndOffsetAdviceClassName;
private final String writeBytesAdviceClassName;
private final String writeIntAdviceClassName;

public ServletOutputStreamInstrumentation(
String basePackageName,
String writeBytesAndOffsetClassName,
String writeBytesClassName,
String writeBytesAndOffsetAdviceClassName,
String writeBytesAdviceClassName,
String writeIntAdviceClassName) {
this.basePackageName = basePackageName;
this.writeBytesAndOffsetClassName = writeBytesAndOffsetClassName;
this.writeBytesClassName = writeBytesClassName;
this.writeBytesAndOffsetAdviceClassName = writeBytesAndOffsetAdviceClassName;
this.writeBytesAdviceClassName = writeBytesAdviceClassName;
this.writeIntAdviceClassName = writeIntAdviceClassName;
}

Expand All @@ -54,10 +54,10 @@ public void transform(TypeTransformer transformer) {
.and(takesArgument(1, int.class))
.and(takesArgument(2, int.class))
.and(isPublic()),
writeBytesAndOffsetClassName);
writeBytesAndOffsetAdviceClassName);
transformer.applyAdviceToMethod(
named("write").and(takesArguments(1)).and(takesArgument(0, byte[].class)).and(isPublic()),
writeBytesClassName);
writeBytesAdviceClassName);
transformer.applyAdviceToMethod(
named("write").and(takesArguments(1)).and(takesArgument(0, int.class)).and(isPublic()),
writeIntAdviceClassName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public enum ServerEndpoint {
AUTH_ERROR("basicsecured/endpoint", 401, null),
INDEXED_CHILD("child", 200, ""),
HTML_PRINT_WRITER(
"HTML_PRINT_WRITER",
"htmlPrintWriter",
200,
"<!DOCTYPE html>\n"
+ "<html lang=\"en\">\n"
Expand All @@ -46,7 +46,7 @@ public enum ServerEndpoint {
+ "</body>\n"
+ "</html>"),
HTML_SERVLET_OUTPUT_STREAM(
"HTML_SERVLET_OUTPUT_STREAM",
"htmlServletOutputStream",
200,
"<!DOCTYPE html>\n"
+ "<html lang=\"en\">\n"
Expand Down

0 comments on commit 1008aef

Please sign in to comment.