-
Notifications
You must be signed in to change notification settings - Fork 6
/
false_positive_test.go
90 lines (76 loc) · 2.17 KB
/
false_positive_test.go
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
// Copyright 2021 Cosmos Nicolaou. All rights reserved.
// Use of this source code is governed by the Apache-2.0
// license that can be found in the LICENSE file.
package pbzip2_test
import (
"bytes"
"context"
"fmt"
"io"
"testing"
"github.com/cosnicolaou/pbzip2"
"github.com/cosnicolaou/pbzip2/internal/bitstream"
)
func TestHandlingFalsePositives(t *testing.T) {
ctx := context.Background()
filename := bzip2Files["300KB1"]
rd := openBzipFile(t, filename)
origData, err := io.ReadAll(rd)
if err != nil {
t.Fatal(err)
}
godata := readBzipFile(t, filename)
defer pbzip2.ResetBlockMagic()
// Fake a false positive by finding some sequences that occur as
// data and then changing the block magic values to be these
// naturally ocurring sequences.
for i, falsePositiveRange := range [][8]byte{
{0xae, 0x91, 0xff, 0x6b, 0x72, 0xb1, 0xa4, 0x7a},
{0xed, 0xbb, 0x7a, 0x1b, 0xda, 0xf7, 0x27, 0x57},
} {
// Test with shifted values of the magic numbers above.
for s := 0; s < 8; s++ {
data := make([]byte, len(origData))
copy(data, origData)
var (
falsePositive [6]byte
tmp [8]byte
)
copy(tmp[:], falsePositiveRange[:])
for i := 0; i < s; i++ {
bitstream.ShiftRight(tmp[:])
}
copy(falsePositive[:], tmp[1:7])
fmt.Printf("magic: %08b\n", falsePositive)
// Block offsets in bits are from the output of gentestdata.go
for _, offset := range []int{32, 806286, 1612607, 2418837} {
bitstream.OverwriteAtBitOffset(data, offset, falsePositive[:])
}
pbzip2.SetCustomBlockMagic(falsePositive)
brd := pbzip2.NewReader(ctx, bytes.NewBuffer(data))
buf := bytes.NewBuffer(make([]byte, 0, 1000*1024))
_, err = io.Copy(buf, brd)
if err != nil {
t.Error(err)
}
if got, want := buf.Bytes(), godata; !bytes.Equal(got, want) {
if testing.Verbose() {
fmt.Printf("got\n")
prettyPrintBlock(got)
fmt.Printf("want\n")
prettyPrintBlock(want)
}
t.Errorf("%v: got %v, want %v", i, len(got), len(want))
}
}
}
}
func prettyPrintBlock(block []byte) {
for i := 0; i < len(block); i++ {
if i > 0 && (i%32 == 0) {
fmt.Println()
}
fmt.Printf("%02x ", block[i])
}
fmt.Println()
}