-
Notifications
You must be signed in to change notification settings - Fork 114
/
stegify_test.go
228 lines (207 loc) · 7.18 KB
/
stegify_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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package main_test
import (
"bytes"
"fmt"
"github.com/DimitarPetrov/stegify/steg"
"io/ioutil"
"os"
"os/exec"
"strings"
"testing"
)
func TestMain(m *testing.M) {
cmd := exec.Command("go", "build")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
fmt.Fprintf(os.Stderr, "Error building stegify binary: %v\n", err)
os.Exit(1)
}
exitCode := m.Run()
_ = os.Remove("stegify")
os.Exit(exitCode)
}
func TestEncode(t *testing.T) {
tests := []struct {
name string
args []string
data string
results []string
shouldFail bool
}{
{
name: "Basic encode",
args: []string{"encode", "--carrier", "examples/street.jpeg", "--data", "examples/lake.jpeg", "--result", "result.jpeg"},
data: "examples/lake.jpeg",
results: []string{"result.jpeg"},
},
{
name: "Encode with multiple carriers using --carrier flag and --result flag",
args: []string{"encode", "--carrier", "examples/street.jpeg", "--carrier", "examples/lake.jpeg", "--data", "examples/video.mp4", "--result", "result1.jpeg", "--result", "result2.jpeg"},
data: "examples/video.mp4",
results: []string{"result1.jpeg", "result2.jpeg"},
},
{
name: "Encode with multiple carriers using --carriers flag and --results flag",
args: []string{"encode", "--carriers", "examples/street.jpeg examples/lake.jpeg", "--data", "examples/video.mp4", "--results", "result3.jpeg result4.jpeg"},
data: "examples/video.mp4",
results: []string{"result3.jpeg", "result4.jpeg"},
},
{
name: "Encode with multiple carriers using --carriers flag and --result flag",
args: []string{"encode", "--carriers", "examples/street.jpeg examples/lake.jpeg", "--data", "examples/video.mp4", "--result", "result5.jpeg", "--result", "result6.jpeg"},
data: "examples/video.mp4",
results: []string{"result5.jpeg", "result6.jpeg"},
},
{
name: "Encode with multiple carriers using --carrier flag and --results flag",
args: []string{"encode", "--carrier", "examples/street.jpeg", "--carrier", "examples/lake.jpeg", "--data", "examples/video.mp4", "--results", "result7.jpeg result8.jpeg"},
data: "examples/video.mp4",
results: []string{"result7.jpeg", "result8.jpeg"},
},
{
name: "Encode with multiple carriers using mixed flags",
args: []string{"encode", "--carriers", "examples/street.jpeg", "--carrier", "examples/lake.jpeg", "--data", "examples/video.mp4", "--results", "result9.jpeg", "--result", "result10.jpeg"},
data: "examples/video.mp4",
results: []string{"result10.jpeg", "result9.jpeg"}, // --carrier/--result is with priority over --carriers/--results
},
{
name: "Encode with single carrier should add default result name",
args: []string{"encode", "--carrier", "examples/street.jpeg", "--data", "examples/lake.jpeg"},
data: "examples/lake.jpeg",
results: []string{"result0"},
},
{
name: "Encode with multiple carriers using --carriers should add default result names",
args: []string{"encode", "--carriers", "examples/street.jpeg examples/lake.jpeg", "--data", "examples/video.mp4"},
data: "examples/video.mp4",
results: []string{"result0", "result1"},
},
{
name: "Encode carriers count does not match results count should return an error",
args: []string{"encode", "--carriers", "examples/street.jpeg examples/lake.jpeg", "--data", "examples/video.mp4", "--results", "result1.jpeg"},
shouldFail: true,
},
{
name: "Encode without data file should fail",
args: []string{"encode", "--carrier", "examples/street.jpeg", "--result", "result.jpeg"},
shouldFail: true,
},
{
name: "Encode without carrier file should fail",
args: []string{"encode", "--data", "examples/lake.jpeg"},
shouldFail: true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Logf("Executing: stegify %s", strings.Join(test.args, " "))
cmd := exec.Command("./stegify", test.args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
if test.shouldFail {
return
} else {
t.Fatalf("Unexpected error: %v", err)
}
}
for _, resultName := range test.results {
defer os.Remove(resultName)
}
err = steg.MultiCarrierDecodeByFileNames(test.results, "decode_result")
if err != nil {
t.Fatalf("Error decoding results: %v", err)
}
defer os.Remove("decode_result")
assertEqualFiles(t, test.data, "decode_result")
})
}
}
func TestDecode(t *testing.T) {
tests := []struct {
name string
args []string
expected string
result string
shouldFail bool
}{
{
name: "Basic decode",
args: []string{"decode", "--carrier", "examples/test_decode.jpeg", "--result", "result.jpeg"},
expected: "examples/lake.jpeg",
result: "result.jpeg",
},
{
name: "Decode from multiple carriers using --carier flag",
args: []string{"decode", "--carrier", "examples/test_multi_carrier_decode1.jpeg", "--carrier", "examples/test_multi_carrier_decode2.jpeg", "--result", "result1.mp4"},
expected: "examples/video.mp4",
result: "result1.mp4",
},
{
name: "Decode from multiple carriers using --cariers flag",
args: []string{"decode", "--carriers", "examples/test_multi_carrier_decode1.jpeg examples/test_multi_carrier_decode2.jpeg", "--result", "result2.mp4"},
expected: "examples/video.mp4",
result: "result2.mp4",
},
{
name: "Decode without result flag should add default",
args: []string{"decode", "--carrier", "examples/test_decode.jpeg"},
expected: "examples/lake.jpeg",
result: "result",
},
{
name: "Decode with multiple results should fail",
args: []string{"decode", "--carriers", "examples/test_multi_carrier_decode1.jpeg examples/test_multi_carrier_decode2.jpeg", "--result", "result1.mp4", "--result", "result2.mp4"},
shouldFail: true,
},
{
name: "Decode without carrier file should fail",
args: []string{"decode", "--result", "result"},
shouldFail: true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Logf("Executing: stegify %s", strings.Join(test.args, " "))
cmd := exec.Command("./stegify", test.args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
if test.shouldFail {
return
} else {
t.Fatalf("Unexpected error: %v", err)
}
}
defer os.Remove(test.result)
assertEqualFiles(t, test.expected, test.result)
})
}
}
func assertEqualFiles(t *testing.T, expected string, given string) {
expectedReader, err := os.Open(expected)
if err != nil {
t.Fatalf("Error opening data file %s:%v", expected, err)
}
defer expectedReader.Close()
decodeResult, err := os.Open(given)
if err != nil {
t.Fatalf("Error opening decode result file:%v", err)
}
defer decodeResult.Close()
wantedBytes, err := ioutil.ReadAll(expectedReader)
if err != nil {
t.Fatalf("Error reading data file: %v", err)
}
resultBytes, err := ioutil.ReadAll(decodeResult)
if err != nil {
t.Fatalf("Error reading decode result file: %v", err)
}
if !bytes.Equal(wantedBytes, resultBytes) {
t.Error("Assertion failed!")
}
}