|
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; |
4 | 4 | import static org.junit.jupiter.api.Assertions.assertFalse; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
5 | 6 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
6 | 7 | import static org.junit.jupiter.api.Assertions.assertNull; |
7 | 8 | import static org.junit.jupiter.api.Assertions.assertTrue; |
8 | 9 |
|
| 10 | +import java.io.IOException; |
| 11 | +import java.nio.file.Files; |
| 12 | +import java.nio.file.Path; |
9 | 13 | import java.time.OffsetDateTime; |
| 14 | +import java.util.Base64; |
10 | 15 | import java.util.List; |
11 | 16 | import java.util.Map; |
12 | 17 |
|
13 | 18 | import io.a2a.spec.Artifact; |
14 | 19 | import io.a2a.spec.DataPart; |
| 20 | +import io.a2a.spec.FileContent; |
15 | 21 | import io.a2a.spec.FilePart; |
16 | 22 | import io.a2a.spec.FileWithBytes; |
17 | 23 | import io.a2a.spec.FileWithUri; |
|
22 | 28 | import io.a2a.spec.TaskStatus; |
23 | 29 | import io.a2a.spec.TextPart; |
24 | 30 | import org.junit.jupiter.api.Test; |
| 31 | +import org.junit.jupiter.api.io.TempDir; |
25 | 32 |
|
26 | 33 | /** |
27 | 34 | * Tests for Task serialization and deserialization using Gson. |
@@ -706,4 +713,74 @@ void testTaskWithMixedPartTypes() throws JsonProcessingException { |
706 | 713 | assertTrue(parts.get(2) instanceof DataPart); |
707 | 714 | assertTrue(parts.get(3) instanceof FilePart); |
708 | 715 | } |
| 716 | + |
| 717 | + // ========== FileContentTypeAdapter tests ========== |
| 718 | + |
| 719 | + @TempDir |
| 720 | + Path tempDir; |
| 721 | + |
| 722 | + @Test |
| 723 | + void testFileWithBytesSerializationDoesNotLeakInternalFields() throws Exception { |
| 724 | + FileWithBytes fwb = new FileWithBytes("application/pdf", "doc.pdf", "base64data"); |
| 725 | + |
| 726 | + String json = JsonUtil.toJson(fwb); |
| 727 | + |
| 728 | + // Must contain the three protocol fields |
| 729 | + assertTrue(json.contains("\"mimeType\""), "missing mimeType: " + json); |
| 730 | + assertTrue(json.contains("\"name\""), "missing name: " + json); |
| 731 | + assertTrue(json.contains("\"bytes\""), "missing bytes: " + json); |
| 732 | + // Must NOT contain internal implementation fields |
| 733 | + assertFalse(json.contains("\"source\""), "internal source field leaked: " + json); |
| 734 | + assertFalse(json.contains("\"cachedBytes\""), "internal cachedBytes field leaked: " + json); |
| 735 | + } |
| 736 | + |
| 737 | + @Test |
| 738 | + void testFileWithBytesRoundTripViaFileContentTypeAdapter() throws Exception { |
| 739 | + FileWithBytes original = new FileWithBytes("image/png", "photo.png", "abc123"); |
| 740 | + |
| 741 | + String json = JsonUtil.toJson(original); |
| 742 | + FileContent deserialized = JsonUtil.fromJson(json, FileContent.class); |
| 743 | + |
| 744 | + assertInstanceOf(FileWithBytes.class, deserialized); |
| 745 | + FileWithBytes result = (FileWithBytes) deserialized; |
| 746 | + assertEquals("image/png", result.mimeType()); |
| 747 | + assertEquals("photo.png", result.name()); |
| 748 | + assertEquals("abc123", result.bytes()); |
| 749 | + } |
| 750 | + |
| 751 | + @Test |
| 752 | + void testPathBackedFileWithBytesDoesNotLeakFilePath() throws Exception { |
| 753 | + byte[] content = "hello".getBytes(); |
| 754 | + Path file = tempDir.resolve("secret.txt"); |
| 755 | + Files.write(file, content); |
| 756 | + |
| 757 | + FileWithBytes fwb = new FileWithBytes("text/plain", file); |
| 758 | + |
| 759 | + String json = JsonUtil.toJson(fwb); |
| 760 | + |
| 761 | + // File path must not appear in the serialized JSON |
| 762 | + assertFalse(json.contains(file.toString()), "file path leaked in JSON: " + json); |
| 763 | + assertFalse(json.contains(tempDir.toString()), "temp dir path leaked in JSON: " + json); |
| 764 | + // Must contain the three protocol fields, not internal implementation fields |
| 765 | + assertTrue(json.contains("\"bytes\""), "missing bytes field: " + json); |
| 766 | + assertFalse(json.contains("\"source\""), "internal source field leaked: " + json); |
| 767 | + } |
| 768 | + |
| 769 | + @Test |
| 770 | + void testPathBackedFileWithBytesRoundTrip() throws Exception { |
| 771 | + byte[] content = "round-trip".getBytes(); |
| 772 | + Path file = tempDir.resolve("data.bin"); |
| 773 | + Files.write(file, content); |
| 774 | + |
| 775 | + FileWithBytes original = new FileWithBytes("application/octet-stream", file); |
| 776 | + |
| 777 | + String json = JsonUtil.toJson(original); |
| 778 | + FileContent deserialized = JsonUtil.fromJson(json, FileContent.class); |
| 779 | + |
| 780 | + assertInstanceOf(FileWithBytes.class, deserialized); |
| 781 | + FileWithBytes result = (FileWithBytes) deserialized; |
| 782 | + assertEquals("application/octet-stream", result.mimeType()); |
| 783 | + assertEquals("data.bin", result.name()); |
| 784 | + assertEquals(Base64.getEncoder().encodeToString(content), result.bytes()); |
| 785 | + } |
709 | 786 | } |
0 commit comments