summaryrefslogtreecommitdiff
path: root/parser/media/src/test/java/de/danoeh/antennapod/parser/media/id3/Id3ReaderTest.java
blob: 4b0bb21134673fb085dd88d943412c274eadf70b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package de.danoeh.antennapod.parser.media.id3;

import de.danoeh.antennapod.parser.media.id3.model.FrameHeader;
import de.danoeh.antennapod.parser.media.id3.model.TagHeader;
import org.apache.commons.io.input.CountingInputStream;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public class Id3ReaderTest {
    @Test
    public void testReadString() throws IOException {
        byte[] data = {
            ID3Reader.ENCODING_ISO,
            'T', 'e', 's', 't',
            0 // Null-terminated
        };
        CountingInputStream inputStream = new CountingInputStream(new ByteArrayInputStream(data));
        String string = new ID3Reader(inputStream).readEncodingAndString(1000);
        assertEquals("Test", string);
    }

    @Test
    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 = {
            ID3Reader.ENCODING_UTF16_WITH_BOM,
            (byte) 0xff, (byte) 0xfe, // BOM
            0x00, 0x01, // Latin Capital Letter A with macron (Ā)
            0, 0, // Null-terminated
        };
        CountingInputStream inputStream = new CountingInputStream(new ByteArrayInputStream(data));
        String string = new ID3Reader(inputStream).readEncodingAndString(1000);
        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
    public void testReadTagHeader() throws IOException, ID3ReaderException {
        byte[] data = generateId3Header(23);
        CountingInputStream inputStream = new CountingInputStream(new ByteArrayInputStream(data));
        TagHeader header = new ID3Reader(inputStream).readTagHeader();
        assertEquals("ID3", header.getId());
        assertEquals(42, header.getVersion());
        assertEquals(23, header.getSize());
    }

    @Test
    public void testReadFrameHeader() throws IOException {
        byte[] data = generateFrameHeader("CHAP", 42);
        CountingInputStream inputStream = new CountingInputStream(new ByteArrayInputStream(data));
        FrameHeader header = new ID3Reader(inputStream).readFrameHeader();
        assertEquals("CHAP", header.getId());
        assertEquals(42, header.getSize());
    }

    public static byte[] generateFrameHeader(String id, int size) {
        return concat(
            id.getBytes(StandardCharsets.ISO_8859_1), // Frame ID
            new byte[] {
                (byte) (size >> 24), (byte) (size >> 16),
                (byte) (size >> 8), (byte) (size), // Size
                0, 0 // Flags
            });
    }

    static byte[] generateId3Header(int size) {
        return new byte[] {
                'I', 'D', '3', // Identifier
                0, 42, // Version
                0, // Flags
                (byte) (size >> 24), (byte) (size >> 16),
                (byte) (size >> 8), (byte) (size), // Size
        };
    }

    static byte[] concat(byte[]... arrays) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try {
            for (byte[] array : arrays) {
                outputStream.write(array);
            }
        } catch (IOException e) {
            fail(e.getMessage());
        }
        return outputStream.toByteArray();
    }
}