-
Notifications
You must be signed in to change notification settings - Fork 36
/
jsteg_test.go
125 lines (113 loc) · 2.27 KB
/
jsteg_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
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
package jsteg
import (
"bytes"
"image/jpeg"
"os"
"strings"
"testing"
)
func loadTestImages(t *testing.T) []string {
testdata, err := os.Open("testdata")
if err != nil {
t.Fatal(err)
}
names, err := testdata.Readdirnames(-1)
if err != nil {
t.Fatal(err)
}
return names
}
func TestHideReveal(t *testing.T) {
for _, name := range loadTestImages(t) {
// load test jpeg
f, err := os.Open("testdata/" + name)
if err != nil {
t.Fatal(err)
}
defer f.Close()
img, err := jpeg.Decode(f)
if err != nil {
t.Fatal(err)
}
// hide data in img
var buf bytes.Buffer
data := []byte("foo bar baz quux")
err = Hide(&buf, img, data, nil)
if err != nil {
t.Fatal(err)
}
// reveal data
revealed, err := Reveal(&buf)
if err != nil {
t.Fatal(err)
}
revealed = revealed[:len(data)]
if !bytes.Equal(data, revealed) {
t.Fatal("revealed bytes do not match original")
}
}
}
// Test that jpegs post-Hide can still be decoded normally
func TestHideDecode(t *testing.T) {
for _, name := range loadTestImages(t) {
// load test jpeg
f, err := os.Open("testdata/" + name)
if err != nil {
t.Fatal(err)
}
defer f.Close()
img, err := jpeg.Decode(f)
if err != nil {
t.Fatal(err)
}
// hide data in img
var buf bytes.Buffer
data := []byte("foo bar baz quux")
err = Hide(&buf, img, data, nil)
if err != nil {
t.Fatal(err)
}
// decode img
_, err = jpeg.Decode(&buf)
if err != nil {
t.Fatal(err)
}
}
}
// Progressive JPEGs are not supported
func TestRevealProgressive(t *testing.T) {
for _, name := range loadTestImages(t) {
if !strings.Contains(name, "progressive") {
continue
}
// load test jpeg
f, err := os.Open("testdata/" + name)
if err != nil {
t.Fatal(err)
}
defer f.Close()
_, err = Reveal(f)
if _, ok := err.(jpeg.UnsupportedError); !ok {
t.Fatal("expected UnsupportedError, got", err)
}
}
}
func TestTooSmall(t *testing.T) {
// load test jpeg
f, err := os.Open("testdata/video-001.jpeg")
if err != nil {
t.Fatal(err)
}
defer f.Close()
img, err := jpeg.Decode(f)
if err != nil {
t.Fatal(err)
}
// attempt hide data in img
var buf bytes.Buffer
data := make([]byte, 10e6)
err = Hide(&buf, img, data, nil)
if err != ErrTooSmall {
t.Fatal("expected ErrTooSmall, got", err)
}
}