|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.tika.parser.mp3; |
| 18 | + |
| 19 | +import static java.nio.charset.StandardCharsets.ISO_8859_1; |
| 20 | +import static java.nio.charset.StandardCharsets.UTF_16BE; |
| 21 | +import static java.nio.charset.StandardCharsets.UTF_16LE; |
| 22 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 23 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 24 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 25 | + |
| 26 | +import java.io.ByteArrayOutputStream; |
| 27 | +import java.io.IOException; |
| 28 | +import java.nio.charset.Charset; |
| 29 | + |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | + |
| 32 | +import org.apache.tika.parser.mp3.ID3Tags.ID3Comment; |
| 33 | + |
| 34 | +/** |
| 35 | + * Tests the shared ID3v2 text decoding used by every ID3v2.2/2.3/2.4 text frame. |
| 36 | + */ |
| 37 | +public class ID3v2FrameTest { |
| 38 | + |
| 39 | + private static final byte ISO_8859_1_FLAG = 0; |
| 40 | + private static final byte UTF_16_BOM_FLAG = 1; |
| 41 | + private static final byte UTF_16BE_FLAG = 2; |
| 42 | + private static final byte UTF_8_FLAG = 3; |
| 43 | + |
| 44 | + private static final byte[] BOM_LE = {(byte) 0xff, (byte) 0xfe}; |
| 45 | + private static final byte[] BOM_BE = {(byte) 0xfe, (byte) 0xff}; |
| 46 | + |
| 47 | + // LATIN has a NUL per char in UTF-16 (a byte-order signal); CJK does not |
| 48 | + private static final String LATIN = "Test Copyright"; |
| 49 | + private static final String CJK = "日本語"; |
| 50 | + |
| 51 | + private static byte[] frame(byte encodingFlag, byte[]... parts) { |
| 52 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 53 | + out.write(encodingFlag); |
| 54 | + for (byte[] part : parts) { |
| 55 | + out.write(part, 0, part.length); |
| 56 | + } |
| 57 | + return out.toByteArray(); |
| 58 | + } |
| 59 | + |
| 60 | + private static byte[] bytes(String text, Charset charset) { |
| 61 | + return text.getBytes(charset); |
| 62 | + } |
| 63 | + |
| 64 | + private static String tagString(byte[] data) { |
| 65 | + return ID3v2Frame.getTagString(data, 0, data.length); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testSingleByteEncodings() { |
| 70 | + assertEquals(LATIN, tagString(frame(ISO_8859_1_FLAG, bytes(LATIN, ISO_8859_1)))); |
| 71 | + assertEquals(CJK, tagString(frame(UTF_8_FLAG, bytes(CJK, UTF_8)))); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testUTF16WithBOM() { |
| 76 | + assertEquals(LATIN, tagString(frame(UTF_16_BOM_FLAG, BOM_LE, bytes(LATIN, UTF_16LE)))); |
| 77 | + assertEquals(LATIN, tagString(frame(UTF_16_BOM_FLAG, BOM_BE, bytes(LATIN, UTF_16BE)))); |
| 78 | + assertEquals(CJK, tagString(frame(UTF_16_BOM_FLAG, BOM_LE, bytes(CJK, UTF_16LE)))); |
| 79 | + assertEquals(CJK, tagString(frame(UTF_16_BOM_FLAG, BOM_BE, bytes(CJK, UTF_16BE)))); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testUTF16BEWithoutBOMFlag() { |
| 84 | + assertEquals(LATIN, tagString(frame(UTF_16BE_FLAG, bytes(LATIN, UTF_16BE)))); |
| 85 | + assertEquals(CJK, tagString(frame(UTF_16BE_FLAG, bytes(CJK, UTF_16BE)))); |
| 86 | + } |
| 87 | + |
| 88 | + // $01 promises a BOM; omitting it used to decode LE as BE mojibake ('T' 0x54 0x00 -> U+5400) |
| 89 | + @Test |
| 90 | + public void testUTF16WithoutBOMRecoversByteOrder() { |
| 91 | + assertEquals(LATIN, tagString(frame(UTF_16_BOM_FLAG, bytes(LATIN, UTF_16LE)))); |
| 92 | + assertEquals(LATIN, tagString(frame(UTF_16_BOM_FLAG, bytes(LATIN, UTF_16BE)))); |
| 93 | + } |
| 94 | + |
| 95 | + // no NUL bytes (all chars above U+00FF) means no signal, so keep the big-endian default |
| 96 | + @Test |
| 97 | + public void testUTF16WithoutBOMKeepsBigEndianDefaultWhenNoSignal() { |
| 98 | + assertEquals(CJK, tagString(frame(UTF_16_BOM_FLAG, bytes(CJK, UTF_16BE)))); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void testNullTerminationIsTrimmed() { |
| 103 | + byte[] doubleNul = {0, 0}; |
| 104 | + byte[] singleNul = {0}; |
| 105 | + assertEquals(LATIN, tagString(frame(UTF_16_BOM_FLAG, bytes(LATIN, UTF_16LE), doubleNul))); |
| 106 | + assertEquals(LATIN, |
| 107 | + tagString(frame(UTF_16_BOM_FLAG, BOM_LE, bytes(LATIN, UTF_16LE), doubleNul))); |
| 108 | + assertEquals(LATIN, tagString(frame(ISO_8859_1_FLAG, bytes(LATIN, ISO_8859_1), singleNul))); |
| 109 | + } |
| 110 | + |
| 111 | + // TIKA-1024: a frame holding nothing but a BOM decodes to the empty string |
| 112 | + @Test |
| 113 | + public void testNakedBOM() { |
| 114 | + assertEquals("", tagString(frame(UTF_16_BOM_FLAG, BOM_LE))); |
| 115 | + assertEquals("", tagString(frame(UTF_16_BOM_FLAG, BOM_BE))); |
| 116 | + } |
| 117 | + |
| 118 | + // COMM decodes description and text separately, so each recovers byte order on its own |
| 119 | + @Test |
| 120 | + public void testCommentWithoutBOMRecoversByteOrder() throws IOException { |
| 121 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 122 | + out.write(UTF_16_BOM_FLAG); |
| 123 | + out.write(bytes("eng", ISO_8859_1)); |
| 124 | + out.write(bytes("Desc", UTF_16LE)); |
| 125 | + out.write(new byte[]{0, 0}); |
| 126 | + out.write(bytes(LATIN, UTF_16LE)); |
| 127 | + byte[] data = out.toByteArray(); |
| 128 | + |
| 129 | + ID3Comment comment = ID3v2Frame.getComment(data, 0, data.length); |
| 130 | + assertEquals("eng", comment.getLanguage()); |
| 131 | + assertEquals("Desc", comment.getDescription()); |
| 132 | + assertEquals(LATIN, comment.getText()); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + public void testCommentWithBOM() throws IOException { |
| 137 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 138 | + out.write(UTF_16_BOM_FLAG); |
| 139 | + out.write(bytes("eng", ISO_8859_1)); |
| 140 | + out.write(BOM_LE); |
| 141 | + out.write(bytes("Desc", UTF_16LE)); |
| 142 | + out.write(new byte[]{0, 0}); |
| 143 | + out.write(BOM_LE); |
| 144 | + out.write(bytes(LATIN, UTF_16LE)); |
| 145 | + byte[] data = out.toByteArray(); |
| 146 | + |
| 147 | + ID3Comment comment = ID3v2Frame.getComment(data, 0, data.length); |
| 148 | + assertEquals("eng", comment.getLanguage()); |
| 149 | + assertEquals("Desc", comment.getDescription()); |
| 150 | + assertEquals(LATIN, comment.getText()); |
| 151 | + } |
| 152 | + |
| 153 | + // too short for flag + 3 byte language, or an unknown flag, decodes to null (no overrun) |
| 154 | + @Test |
| 155 | + public void testMalformedCommentsReturnNull() { |
| 156 | + assertNull(ID3v2Frame.getComment(new byte[0], 0, 0)); |
| 157 | + assertNull(ID3v2Frame.getComment(new byte[]{UTF_16_BOM_FLAG}, 0, 1)); |
| 158 | + assertNull(ID3v2Frame.getComment(new byte[]{ISO_8859_1_FLAG, 'e', 'n'}, 0, 3)); |
| 159 | + // 0x05 is not a defined ID3v2 text encoding |
| 160 | + byte[] badFlag = {5, 'e', 'n', 'g', 'D', 'e', 's', 'c', 0, 'T'}; |
| 161 | + assertNull(ID3v2Frame.getComment(badFlag, 0, badFlag.length)); |
| 162 | + } |
| 163 | + |
| 164 | + // a double byte comment ending on a lone NUL must not overrun looking for a terminator |
| 165 | + @Test |
| 166 | + public void testCommentTruncatedOnTerminatorDoesNotOverrun() { |
| 167 | + byte[] data = {UTF_16_BOM_FLAG, 'e', 'n', 'g', 0}; |
| 168 | + ID3Comment comment = ID3v2Frame.getComment(data, 0, data.length); |
| 169 | + assertEquals("eng", comment.getLanguage()); |
| 170 | + } |
| 171 | +} |
0 commit comments