-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SimpleHttpResponse adds deserialization whitelist
- Loading branch information
1 parent
af70d95
commit 76fef20
Showing
3 changed files
with
85 additions
and
23 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
59 changes: 59 additions & 0 deletions
59
tunnel-common/src/test/java/com/alibaba/arthas/tunnel/common/SimpleHttpResponseTest.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,59 @@ | ||
package com.alibaba.arthas.tunnel.common; | ||
|
||
import static org.junit.Assert.assertArrayEquals; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.InvalidClassException; | ||
import java.io.ObjectOutputStream; | ||
import java.util.Date; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.junit.Test; | ||
|
||
public class SimpleHttpResponseTest { | ||
|
||
@Test | ||
public void testSerialization() throws IOException, ClassNotFoundException { | ||
SimpleHttpResponse response = new SimpleHttpResponse(); | ||
response.setStatus(200); | ||
|
||
Map<String, String> headers = new HashMap<String, String>(); | ||
headers.put("Content-Type", "text/plain"); | ||
response.setHeaders(headers); | ||
|
||
String content = "Hello, world!"; | ||
response.setContent(content.getBytes()); | ||
|
||
byte[] bytes = SimpleHttpResponse.toBytes(response); | ||
|
||
SimpleHttpResponse deserializedResponse = SimpleHttpResponse.fromBytes(bytes); | ||
|
||
assertEquals(response.getStatus(), deserializedResponse.getStatus()); | ||
assertEquals(response.getHeaders(), deserializedResponse.getHeaders()); | ||
assertArrayEquals(response.getContent(), deserializedResponse.getContent()); | ||
} | ||
|
||
private static byte[] toBytes(Object object) throws IOException { | ||
ByteArrayOutputStream bos = new ByteArrayOutputStream(); | ||
try (ObjectOutputStream out = new ObjectOutputStream(bos)) { | ||
out.writeObject(object); | ||
out.flush(); | ||
return bos.toByteArray(); | ||
} | ||
} | ||
|
||
@Test(expected = InvalidClassException.class) | ||
public void testDeserializationWithUnauthorizedClass() throws IOException, ClassNotFoundException { | ||
Date date = new Date(); | ||
|
||
byte[] bytes = toBytes(date); | ||
|
||
// Try to deserialize the object with an unauthorized class | ||
// This should throw an InvalidClassException | ||
SimpleHttpResponse.fromBytes(bytes); | ||
} | ||
|
||
} |