Skip to content

Commit

Permalink
add synth test for metadata block
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 548120163
  • Loading branch information
eustas authored and Copybara-Service committed Jul 14, 2023
1 parent de52bc7 commit c2848d5
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 6 deletions.
51 changes: 49 additions & 2 deletions java/org/brotli/dec/SynthTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -2640,6 +2641,41 @@ public void testOneInsert() {
);
}

@Test
public void testPeculiarWrap() {
byte[] compressed = {
(byte) 0x21, (byte) 0xfc, (byte) 0x1f, (byte) 0x00, (byte) 0x00, (byte) 0xa1, (byte) 0x12,
(byte) 0x82, (byte) 0x04, (byte) 0x60, (byte) 0x1d, (byte) 0x00, (byte) 0xca, (byte) 0xfe,
(byte) 0xba, (byte) 0xbe, (byte) 0xde, (byte) 0xad, (byte) 0xbe, (byte) 0xef, (byte) 0x21,
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x38, (byte) 0x4e,
(byte) 0xdb, (byte) 0x00, (byte) 0x00, (byte) 0x70, (byte) 0xb0, (byte) 0x65, (byte) 0x12,
(byte) 0x03, (byte) 0x24, (byte) 0x00, (byte) 0x00, (byte) 0xee, (byte) 0xb4, (byte) 0x91,
(byte) 0x61, (byte) 0x68, (byte) 0x64, (byte) 0x0c
};
checkSynth(
/*
* main_header: 10
* // See ZeroCostCommand
* metablock_header_begin: 0, 0, 2048, 0
* metablock_header_trivial_context
* huffman_simple: 0,1,256, 42
* huffman_simple: 0,1,704, 130
* huffman_simple: 0,1,64, 0
* // Metadata block; at least 8 bytes long
* bits: "0", "11", "0", "01", "00000111"
* byte_boundary
* bits: "11001010", "11111110", "10111010", "10111110"
* bits: "11011110", "10101101", "10111110", "11101111"
* metablock_header_easy: 3, 1
* command_easy: 0, "abc", 0
*/
compressed,
true,
times(512, "left")
+ "abc"
);
}

@Test
public void testSimplePrefix() {
byte[] compressed = {
Expand Down Expand Up @@ -2712,22 +2748,33 @@ public void testSimplePrefixOutOfRangeSymbols() {
);
}

/* DISABLED: Java decoder does not tolerate extra input after the brotli stream.
@Test
public void testSimplePrefixPlusExtraData() {
assumeTrue(false);
byte[] compressed = {
(byte) 0x1b, (byte) 0x03, (byte) 0x00, (byte) 0x00, (byte) 0xa0, (byte) 0xc3, (byte) 0xc4,
(byte) 0xc6, (byte) 0xc8, (byte) 0x02, (byte) 0x00, (byte) 0x70, (byte) 0xb0, (byte) 0x65,
(byte) 0x12, (byte) 0x03, (byte) 0x24, (byte) 0x00, (byte) 0x00, (byte) 0xee, (byte) 0xb4,
(byte) 0x51, (byte) 0xa0, (byte) 0x1d, (byte) 0x55, (byte) 0xaa
};
checkSynth(
/*
* main_header
* metablock_header_begin: 1, 0, 4, 0
* metablock_header_trivial_context
* huffman_simple: 1,4,256, 97,98,99,100 // ASCII codes for a, b, c, d
* huffman_fixed: 704
* huffman_fixed: 64
* command_inscopy_easy: 4, 0
* command_literal_bits: 0, 10, 110, 111 // a, b, c, d
* byte_boundary
* bits: "01010101", "10101010"
*/
compressed,
true,
"abcd"
);
}
*/

@Test
public void testStressReadDistanceExtraBits() {
Expand Down
34 changes: 32 additions & 2 deletions js/decode_synth_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1988,6 +1988,37 @@ testOneInsert() {
);
},

testPeculiarWrap() {
const compressed = [
0x21, 0xfc, 0x1f, 0x00, 0x00, 0xa1, 0x12, 0x82, 0x04, 0x60, 0x1d, 0x00,
0xca, 0xfe, 0xba, 0xbe, 0xde, 0xad, 0xbe, 0xef, 0x21, 0x00, 0x00, 0x00,
0x00, 0x00, 0x38, 0x4e, 0xdb, 0x00, 0x00, 0x70, 0xb0, 0x65, 0x12, 0x03,
0x24, 0x00, 0x00, 0xee, 0xb4, 0x91, 0x61, 0x68, 0x64, 0x0c
];
checkSynth(
/*
* main_header: 10
* // See ZeroCostCommand
* metablock_header_begin: 0, 0, 2048, 0
* metablock_header_trivial_context
* huffman_simple: 0,1,256, 42
* huffman_simple: 0,1,704, 130
* huffman_simple: 0,1,64, 0
* // Metadata block; at least 8 bytes long
* bits: "0", "11", "0", "01", "00000111"
* byte_boundary
* bits: "11001010", "11111110", "10111010", "10111110"
* bits: "11011110", "10101101", "10111110", "11101111"
* metablock_header_easy: 3, 1
* command_easy: 0, "abc", 0
*/
compressed,
true,
times(512, 'left')
+ 'abc'
);
},

testSimplePrefix() {
const compressed = [
0x1b, 0x03, 0x00, 0x00, 0xa0, 0xc3, 0xc4, 0xc6, 0xc8, 0x02, 0x00, 0x70,
Expand Down Expand Up @@ -2053,8 +2084,7 @@ testSimplePrefixOutOfRangeSymbols() {
},

testSimplePrefixPlusExtraData() {
// SKIP: JS decoder does not tolerate extra input after the brotli stream.
if ({}.toString() == {}) return; // same as 'if (true) return'
if (pending) pending(); else return;
const compressed = [
0x1b, 0x03, 0x00, 0x00, 0xa0, 0xc3, 0xc4, 0xc6, 0xc8, 0x02, 0x00, 0x70,
0xb0, 0x65, 0x12, 0x03, 0x24, 0x00, 0x00, 0xee, 0xb4, 0x51, 0xa0, 0x1d,
Expand Down
34 changes: 32 additions & 2 deletions js/decode_synth_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1978,6 +1978,37 @@ testOneInsert() {
);
},

testPeculiarWrap() {
const compressed = [
0x21, 0xfc, 0x1f, 0x00, 0x00, 0xa1, 0x12, 0x82, 0x04, 0x60, 0x1d, 0x00,
0xca, 0xfe, 0xba, 0xbe, 0xde, 0xad, 0xbe, 0xef, 0x21, 0x00, 0x00, 0x00,
0x00, 0x00, 0x38, 0x4e, 0xdb, 0x00, 0x00, 0x70, 0xb0, 0x65, 0x12, 0x03,
0x24, 0x00, 0x00, 0xee, 0xb4, 0x91, 0x61, 0x68, 0x64, 0x0c
];
checkSynth(
/*
* main_header: 10
* // See ZeroCostCommand
* metablock_header_begin: 0, 0, 2048, 0
* metablock_header_trivial_context
* huffman_simple: 0,1,256, 42
* huffman_simple: 0,1,704, 130
* huffman_simple: 0,1,64, 0
* // Metadata block; at least 8 bytes long
* bits: "0", "11", "0", "01", "00000111"
* byte_boundary
* bits: "11001010", "11111110", "10111010", "10111110"
* bits: "11011110", "10101101", "10111110", "11101111"
* metablock_header_easy: 3, 1
* command_easy: 0, "abc", 0
*/
compressed,
true,
times(512, 'left')
+ 'abc'
);
},

testSimplePrefix() {
const compressed = [
0x1b, 0x03, 0x00, 0x00, 0xa0, 0xc3, 0xc4, 0xc6, 0xc8, 0x02, 0x00, 0x70,
Expand Down Expand Up @@ -2043,8 +2074,7 @@ testSimplePrefixOutOfRangeSymbols() {
},

testSimplePrefixPlusExtraData() {
// SKIP: JS decoder does not tolerate extra input after the brotli stream.
if (2 * 2 === 4) return;
if (pending) pending(); else return;
const compressed = [
0x1b, 0x03, 0x00, 0x00, 0xa0, 0xc3, 0xc4, 0xc6, 0xc8, 0x02, 0x00, 0x70,
0xb0, 0x65, 0x12, 0x03, 0x24, 0x00, 0x00, 0xee, 0xb4, 0x51, 0xa0, 0x1d,
Expand Down
2 changes: 2 additions & 0 deletions js/jasmine-polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ goog.require('goog.testing.asserts');
goog.require('goog.testing.jsunit');

let test_case_;
/** @type{?function(): void} */
let pending = null;

function describe(caseName, callback) {
test_case_ = new goog.testing.TestCase(caseName);
Expand Down

0 comments on commit c2848d5

Please sign in to comment.