Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added additional diagnostic code that will try to read ahead and provide what's ahead in the input stream.
- Loading branch information
Showing
4 changed files
with
153 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/main/java/hudson/remoting/DiagnosedStreamCorruptionException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package hudson.remoting; | ||
|
||
import java.io.PrintWriter; | ||
import java.io.StreamCorruptedException; | ||
import java.io.StringWriter; | ||
|
||
/** | ||
* Signals a {@link StreamCorruptedException} with some additional diagnostic information. | ||
* | ||
* @author Kohsuke Kawaguchi | ||
*/ | ||
public class DiagnosedStreamCorruptionException extends StreamCorruptedException { | ||
private final Exception diagnoseFailure; | ||
private final byte[] readAhead; | ||
|
||
public DiagnosedStreamCorruptionException(StreamCorruptedException cause, Exception diagnoseFailure, byte[] readAhead) { | ||
initCause(cause); | ||
this.diagnoseFailure = diagnoseFailure; | ||
this.readAhead = readAhead; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
StringBuilder buf = new StringBuilder(); | ||
buf.append(super.toString()).append("\n"); | ||
buf.append("Read ahead: "+HexDump.toHex(readAhead)); | ||
if (diagnoseFailure!=null) { | ||
StringWriter w = new StringWriter(); | ||
PrintWriter p = new PrintWriter(w); | ||
diagnoseFailure.printStackTrace(p); | ||
p.flush(); | ||
|
||
buf.append("Diagnosis problem:\n "); | ||
buf.append(w.toString().trim().replace("\n","\n ")); | ||
} | ||
return buf.toString(); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/test/java/hudson/remoting/DiagnosedStreamCorruptionExceptionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package hudson.remoting; | ||
|
||
import hudson.remoting.Channel.Mode; | ||
import org.apache.commons.io.output.NullOutputStream; | ||
import org.junit.Test; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.PrintWriter; | ||
import java.io.StreamCorruptedException; | ||
import java.io.StringWriter; | ||
|
||
import static junit.framework.Assert.*; | ||
|
||
/** | ||
* @author Kohsuke Kawaguchi | ||
*/ | ||
public class DiagnosedStreamCorruptionExceptionTest { | ||
@Test | ||
public void exercise() throws Exception { | ||
byte[] payload = { | ||
0,0,0,0, /* binary stream preamble*/ | ||
(byte)0xAC, (byte)0xED, 0x00, 0x05, /* object input stream header */ | ||
1, 2, 3, 4, 5 /* bogus data */ | ||
}; | ||
|
||
ClassicCommandTransport ct = (ClassicCommandTransport) ClassicCommandTransport.create(Mode.BINARY, new ByteArrayInputStream(payload), new NullOutputStream(), new NullOutputStream(), getClass().getClassLoader(), new Capability()); | ||
|
||
try { | ||
ct.read(); | ||
fail(); | ||
} catch (DiagnosedStreamCorruptionException e) { | ||
StringWriter s = new StringWriter(); | ||
PrintWriter w = new PrintWriter(s); | ||
e.printStackTrace(w); | ||
w.close(); | ||
|
||
String msg = s.toString(); | ||
assertTrue(msg.contains("Read ahead: 02030405")); | ||
assertTrue(msg.contains("invalid type code: 01")); | ||
assertSame(StreamCorruptedException.class, e.getCause().getClass()); | ||
} | ||
} | ||
} |