|
| 1 | +/* |
| 2 | + * Protocol-Attacker - A Framework to create Protocol Analysis Tools |
| 3 | + * |
| 4 | + * Copyright 2023-2025 Ruhr University Bochum, Paderborn University, Technology Innovation Institute, and Hackmanit GmbH |
| 5 | + * |
| 6 | + * Licensed under Apache License, Version 2.0 |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0.txt |
| 8 | + */ |
| 9 | +package de.rub.nds.protocol.util; |
| 10 | + |
| 11 | +import static org.junit.jupiter.api.Assertions.*; |
| 12 | + |
| 13 | +import java.io.ByteArrayOutputStream; |
| 14 | +import java.nio.charset.StandardCharsets; |
| 15 | +import org.junit.jupiter.api.BeforeEach; |
| 16 | +import org.junit.jupiter.api.Test; |
| 17 | + |
| 18 | +class SilentByteArrayOutputStreamTest { |
| 19 | + |
| 20 | + private SilentByteArrayOutputStream stream; |
| 21 | + |
| 22 | + @BeforeEach |
| 23 | + public void setUp() { |
| 24 | + stream = new SilentByteArrayOutputStream(); |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + public void testDefaultConstructor() { |
| 29 | + assertNotNull(stream); |
| 30 | + assertEquals(0, stream.size()); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void testSizedConstructor() { |
| 35 | + SilentByteArrayOutputStream sizedStream = new SilentByteArrayOutputStream(64); |
| 36 | + assertNotNull(sizedStream); |
| 37 | + assertEquals(0, sizedStream.size()); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void testWriteSingleByte() { |
| 42 | + stream.write(0x04); |
| 43 | + assertEquals(1, stream.size()); |
| 44 | + assertEquals((byte) 0x04, stream.toByteArray()[0]); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testWriteByteArray() { |
| 49 | + byte[] data = {0x01, 0x02, 0x03, 0x04}; |
| 50 | + stream.write(data); |
| 51 | + assertEquals(4, stream.size()); |
| 52 | + assertArrayEquals(data, stream.toByteArray()); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testWriteByteArrayWithOffsetAndLength() { |
| 57 | + byte[] data = {0x01, 0x02, 0x03, 0x04}; |
| 58 | + stream.write(data, 1, 2); |
| 59 | + assertArrayEquals(new byte[] {0x02, 0x03}, stream.toByteArray()); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testWriteBytes() { |
| 64 | + byte[] data = {0x01, 0x02, 0x03, 0x04}; |
| 65 | + stream.writeBytes(data); |
| 66 | + assertArrayEquals(data, stream.toByteArray()); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testWriteTo() { |
| 71 | + byte[] data = {0x01, 0x02, 0x03, 0x04}; |
| 72 | + stream.write(data); |
| 73 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 74 | + stream.writeTo(out); |
| 75 | + assertArrayEquals(data, out.toByteArray()); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testReset() { |
| 80 | + byte[] data = {0x01, 0x02, 0x03, 0x04}; |
| 81 | + stream.write(data); |
| 82 | + assertEquals(4, stream.size()); |
| 83 | + stream.reset(); |
| 84 | + assertEquals(0, stream.size()); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testToByteArray() { |
| 89 | + stream.write(new byte[] {0x01, 0x02}); |
| 90 | + byte[] result = stream.toByteArray(); |
| 91 | + assertArrayEquals(new byte[] {0x01, 0x02}, result); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void testToStringDefaultCharset() { |
| 96 | + String testString = "Test"; |
| 97 | + stream.write(testString.getBytes(StandardCharsets.UTF_8)); |
| 98 | + assertEquals(testString, stream.toString()); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void testToStringWithCharsetName() { |
| 103 | + String testString = "Test"; |
| 104 | + stream.write(testString.getBytes(StandardCharsets.UTF_8)); |
| 105 | + assertEquals(testString, stream.toString("UTF-8")); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void testToStringWithCharset() { |
| 110 | + String testString = "Test"; |
| 111 | + stream.write(testString.getBytes(StandardCharsets.UTF_8)); |
| 112 | + assertEquals(testString, stream.toString(StandardCharsets.UTF_8)); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + @SuppressWarnings("deprecation") |
| 117 | + public void testToStringWithHighByte() { |
| 118 | + stream.write(new byte[] {0x54, 0x65, 0x73, 0x74}); // Test |
| 119 | + String result = stream.toString(0); |
| 120 | + assertEquals(4, result.length()); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void testCloseDoesNothing() { |
| 125 | + stream.write(0x01); |
| 126 | + assertDoesNotThrow(() -> stream.close()); |
| 127 | + stream.write(0x02); |
| 128 | + assertEquals(2, stream.size()); |
| 129 | + } |
| 130 | +} |
0 commit comments