Skip to content

Commit

Permalink
(WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
Godin committed May 3, 2017
1 parent 30bc8e1 commit 7e7c2c8
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 13 deletions.
105 changes: 103 additions & 2 deletions org.jacoco.core.test/src/org/jacoco/core/instr/InstrumenterTest.java
Expand Up @@ -23,6 +23,7 @@
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.Arrays;
import java.util.jar.JarInputStream;
import java.util.jar.JarOutputStream;
import java.util.jar.Pack200;
Expand Down Expand Up @@ -169,6 +170,72 @@ public void testInstrumentAll_Zip() throws IOException {
assertNull(zipin.getNextEntry());
}

/**
* Triggers exception in
* {@link Instrumenter#instrumentAll(InputStream, OutputStream, String)}.
*/
@Test
public void testInstrumentAll_Broken() {
try {
instrumenter.instrumentAll(new InputStream() {
@Override
public int read() throws IOException {
throw new IOException();
}
}, new ByteArrayOutputStream(), "Test.class");
fail("exception expected");
} catch (IOException e) {
assertEquals("Error while instrumenting Test.class.",
e.getMessage());
}
}

/**
* Triggers exception in
* {@link Instrumenter#instrumentZip(InputStream, OutputStream, String)}.
*/
@Test
public void testInstrumentAll_BrokenZip() {
final byte[] buffer = new byte[30];
buffer[0] = 0x50;
buffer[1] = 0x4b;
buffer[2] = 0x03;
buffer[3] = 0x04;
Arrays.fill(buffer, 4, buffer.length, (byte) 0x42);

try {
instrumenter.instrumentAll(new ByteArrayInputStream(buffer),
new ByteArrayOutputStream(), "Test.zip");
fail("exception expected");
} catch (IOException e) {
assertEquals("Error while instrumenting Test.zip.", e.getMessage());
}
}

/**
* Triggers exception in
* {@link Instrumenter#instrumentAll(InputStream, OutputStream, String)}.
*/
@Test
public void testInstrumentAll_BrokenZipEntry() throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ZipOutputStream zip = new ZipOutputStream(out);
zip.putNextEntry(new ZipEntry("brokenentry.txt"));
out.write(0x23); // Unexpected data here
zip.close();

try {
instrumenter.instrumentAll(
new ByteArrayInputStream(out.toByteArray()),
new ByteArrayOutputStream(), "broken.zip");
fail("exception expected");
} catch (IOException e) {
assertEquals(
"Error while instrumenting broken.zip@brokenentry.txt.",
e.getMessage());
}
}

@Test
public void testInstrumentAll_BrokenClassFileInZip() throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
Expand All @@ -186,12 +253,28 @@ public void testInstrumentAll_BrokenClassFileInZip() throws IOException {
"test.zip");
fail();
} catch (IOException e) {
assertEquals(
"Error while instrumenting class test.zip@Test.class.",
assertEquals("Error while instrumenting class test.zip@Test.class.",
e.getMessage());
}
}

/**
* Triggers exception in
* {@link Instrumenter#instrumentGzip(InputStream, OutputStream, String)}.
*/
@Test
public void testInstrumentAll_BrokenGZ() {
final byte[] buffer = new byte[] { 0x1f, (byte) 0x8b, 0x00, 0x00 };

try {
instrumenter.instrumentAll(new ByteArrayInputStream(buffer),
new ByteArrayOutputStream(), "Test.gz");
fail("exception expected");
} catch (IOException e) {
assertEquals("Error while instrumenting Test.gz.", e.getMessage());
}
}

@Test
public void testInstrumentAll_Pack200() throws IOException {
ByteArrayOutputStream jarbuffer = new ByteArrayOutputStream();
Expand Down Expand Up @@ -223,6 +306,24 @@ public void testInstrumentAll_Pack200() throws IOException {
assertNull(zipin.getNextEntry());
}

/**
* Triggers exception in
* {@link Instrumenter#instrumentPack200(InputStream, OutputStream, String)}.
*/
@Test
public void testInstrumentAll_BrokenPack200() {
final byte[] buffer = new byte[] { (byte) 0xca, (byte) 0xfe,
(byte) 0xd0, 0x0d };

try {
instrumenter.instrumentAll(new ByteArrayInputStream(buffer),
new ByteArrayOutputStream(), "Test.pack200");
} catch (IOException e) {
assertEquals("Error while instrumenting Test.pack200.",
e.getMessage());
}
}

@Test
public void testInstrumentAll_Other() throws IOException {
InputStream in = new ByteArrayInputStream("text".getBytes());
Expand Down
47 changes: 36 additions & 11 deletions org.jacoco.core/src/org/jacoco/core/instr/Instrumenter.java
Expand Up @@ -114,7 +114,7 @@ public byte[] instrument(final byte[] buffer, final String name)
return instrument(new ClassReader(buffer));
}
} catch (final RuntimeException e) {
throw instrumentError(name, e);
throw instrumentError("class " + name, e);
}
}

Expand All @@ -135,7 +135,7 @@ public byte[] instrument(final InputStream input, final String name)
try {
return instrument(Java9Support.readFully(input), name);
} catch (final RuntimeException e) {
throw instrumentError(name, e);
throw instrumentError("class " + name, e);
}
}

Expand All @@ -157,14 +157,14 @@ public void instrument(final InputStream input, final OutputStream output,
try {
output.write(instrument(Java9Support.readFully(input), name));
} catch (final RuntimeException e) {
throw instrumentError(name, e);
throw instrumentError("class " + name, e);
}
}

private IOException instrumentError(final String name,
final RuntimeException cause) {
final IOException ex = new IOException(String.format(
"Error while instrumenting class %s.", name));
final Exception cause) {
final IOException ex = new IOException(
String.format("Error while instrumenting %s.", name));
ex.initCause(cause);
return ex;
}
Expand All @@ -187,7 +187,12 @@ private IOException instrumentError(final String name,
*/
public int instrumentAll(final InputStream input,
final OutputStream output, final String name) throws IOException {
final ContentTypeDetector detector = new ContentTypeDetector(input);
final ContentTypeDetector detector;
try {
detector = new ContentTypeDetector(input);
} catch (IOException e) {
throw instrumentError(name, e);
}
switch (detector.getType()) {
case ContentTypeDetector.CLASSFILE:
instrument(detector.getInputStream(), output, name);
Expand All @@ -210,7 +215,7 @@ private int instrumentZip(final InputStream input,
final ZipOutputStream zipout = new ZipOutputStream(output);
ZipEntry entry;
int count = 0;
while ((entry = zipin.getNextEntry()) != null) {
while ((entry = nextEntry(zipin, name)) != null) {
final String entryName = entry.getName();
if (signatureRemover.removeEntry(entryName)) {
continue;
Expand All @@ -226,19 +231,39 @@ private int instrumentZip(final InputStream input,
return count;
}

private ZipEntry nextEntry(ZipInputStream input, String location)
throws IOException {
try {
return input.getNextEntry();
} catch (IOException e) {
throw instrumentError(location, e);
}
}

private int instrumentGzip(final InputStream input,
final OutputStream output, final String name) throws IOException {
final GZIPInputStream gzipInputStream;
try {
gzipInputStream = new GZIPInputStream(input);
} catch (IOException e) {
throw instrumentError(name, e);
}
final GZIPOutputStream gzout = new GZIPOutputStream(output);
final int count = instrumentAll(new GZIPInputStream(input), gzout, name);
final int count = instrumentAll(gzipInputStream, gzout, name);
gzout.finish();
return count;
}

private int instrumentPack200(final InputStream input,
final OutputStream output, final String name) throws IOException {
final InputStream unpackedInput;
try {
unpackedInput = Pack200Streams.unpack(input);
} catch (IOException e) {
throw instrumentError(name, e);
}
final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
final int count = instrumentAll(Pack200Streams.unpack(input), buffer,
name);
final int count = instrumentAll(unpackedInput, buffer, name);
Pack200Streams.pack(buffer.toByteArray(), output);
return count;
}
Expand Down

0 comments on commit 7e7c2c8

Please sign in to comment.