Merge pull request #4945 from ByteHamster/more-parser-tests

Added more id3 parser tests
This commit is contained in:
ByteHamster 2021-02-17 17:35:50 +01:00 committed by GitHub
commit 986a62de75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 66 additions and 8 deletions

View File

@ -180,14 +180,13 @@ public class ID3Reader {
private String readEncodedString2(Charset charset, int max) throws IOException { private String readEncodedString2(Charset charset, int max) throws IOException {
ByteArrayOutputStream bytes = new ByteArrayOutputStream(); ByteArrayOutputStream bytes = new ByteArrayOutputStream();
int bytesRead = 0; int bytesRead = 0;
while (bytesRead < max) { while (bytesRead + 1 < max) {
byte c1 = readByte(); byte c1 = readByte();
bytesRead++; byte c2 = readByte();
if (c1 == 0) { if (c1 == 0 && c2 == 0) {
break; break;
} }
byte c2 = readByte(); bytesRead += 2;
bytesRead++;
bytes.write(c1); bytes.write(c1);
bytes.write(c2); bytes.write(c2);
} }

View File

@ -11,6 +11,7 @@ import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
public class Id3ReaderTest { public class Id3ReaderTest {
@ -27,16 +28,74 @@ public class Id3ReaderTest {
} }
@Test @Test
public void testReadUtf16WithBom() throws IOException { public void testReadMultipleStrings() throws IOException {
byte[] data = {
ID3Reader.ENCODING_ISO,
'F', 'o', 'o',
0, // Null-terminated
ID3Reader.ENCODING_ISO,
'B', 'a', 'r',
0 // Null-terminated
};
CountingInputStream inputStream = new CountingInputStream(new ByteArrayInputStream(data));
ID3Reader reader = new ID3Reader(inputStream);
assertEquals("Foo", reader.readEncodingAndString(1000));
assertEquals("Bar", reader.readEncodingAndString(1000));
}
@Test
public void testReadingLimit() throws IOException {
byte[] data = {
ID3Reader.ENCODING_ISO,
'A', 'B', 'C', 'D'
};
CountingInputStream inputStream = new CountingInputStream(new ByteArrayInputStream(data));
ID3Reader reader = new ID3Reader(inputStream);
assertEquals("ABC", reader.readEncodingAndString(4)); // Includes encoding
assertEquals('D', reader.readByte());
}
@Test
public void testReadUtf16RespectsBom() throws IOException {
byte[] data = {
ID3Reader.ENCODING_UTF16_WITH_BOM,
(byte) 0xff, (byte) 0xfe, // BOM: Little-endian
'A', 0, 'B', 0, 'C', 0,
0, 0, // Null-terminated
ID3Reader.ENCODING_UTF16_WITH_BOM,
(byte) 0xfe, (byte) 0xff, // BOM: Big-endian
0, 'D', 0, 'E', 0, 'F',
0, 0, // Null-terminated
};
CountingInputStream inputStream = new CountingInputStream(new ByteArrayInputStream(data));
ID3Reader reader = new ID3Reader(inputStream);
assertEquals("ABC", reader.readEncodingAndString(1000));
assertEquals("DEF", reader.readEncodingAndString(1000));
}
@Test
public void testReadUtf16NullPrefix() throws IOException {
byte[] data = { byte[] data = {
ID3Reader.ENCODING_UTF16_WITH_BOM, ID3Reader.ENCODING_UTF16_WITH_BOM,
(byte) 0xff, (byte) 0xfe, // BOM (byte) 0xff, (byte) 0xfe, // BOM
'A', 0, 'B', 0, 'C', 0, 0x00, 0x01, // Latin Capital Letter A with macron (Ā)
0, 0, // Null-terminated 0, 0, // Null-terminated
}; };
CountingInputStream inputStream = new CountingInputStream(new ByteArrayInputStream(data)); CountingInputStream inputStream = new CountingInputStream(new ByteArrayInputStream(data));
String string = new ID3Reader(inputStream).readEncodingAndString(1000); String string = new ID3Reader(inputStream).readEncodingAndString(1000);
assertEquals("ABC", string); assertEquals("Ā", string);
}
@Test
public void testReadingLimitUtf16() throws IOException {
byte[] data = {
ID3Reader.ENCODING_UTF16_WITHOUT_BOM,
'A', 0, 'B', 0, 'C', 0, 'D', 0
};
CountingInputStream inputStream = new CountingInputStream(new ByteArrayInputStream(data));
ID3Reader reader = new ID3Reader(inputStream);
reader.readEncodingAndString(6); // Includes encoding, produces broken string
assertTrue("Should respect limit even if it breaks a symbol", reader.getPosition() <= 6);
} }
@Test @Test