hc
2024-03-22 a0752693d998599af469473b8dc239ef973a012f
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
/* Copyright 2015 Google Inc. All Rights Reserved.
 
   Distributed under MIT license.
   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
*/
 
package org.brotli.dec;
 
import static org.junit.Assert.fail;
 
import java.io.ByteArrayInputStream;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
 
/**
 * Tests for {@link BitReader}.
 */
@RunWith(JUnit4.class)
public class BitReaderTest {
 
  @Test
  public void testReadAfterEos() {
    State reader = new State();
    Decode.initState(reader, new ByteArrayInputStream(new byte[1]));
    BitReader.readBits(reader, 9);
    try {
      BitReader.checkHealth(reader, 0);
    } catch (BrotliRuntimeException ex) {
      // This exception is expected.
      return;
    }
    fail("BrotliRuntimeException should have been thrown by BitReader.checkHealth");
  }
 
  @Test
  public void testAccumulatorUnderflowDetected() {
    State reader = new State();
    Decode.initState(reader, new ByteArrayInputStream(new byte[8]));
    // 65 bits is enough for both 32 and 64 bit systems.
    BitReader.readBits(reader, 13);
    BitReader.readBits(reader, 13);
    BitReader.readBits(reader, 13);
    BitReader.readBits(reader, 13);
    BitReader.readBits(reader, 13);
    try {
      BitReader.fillBitWindow(reader);
    } catch (IllegalStateException ex) {
      // This exception is expected.
      return;
    }
    fail("IllegalStateException should have been thrown by 'broken' BitReader");
  }
}