Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
[JENKINS-8856]
Added additional diagnostic code that will try to read ahead and provide what's ahead in the input stream.
- Loading branch information
Showing
with
153 additions
and 7 deletions.
@@ -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(); | ||
} | ||
} |
@@ -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()); | ||
} | ||
} | ||
} |