-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathmain_test.go
More file actions
389 lines (359 loc) · 10.9 KB
/
Copy pathmain_test.go
File metadata and controls
389 lines (359 loc) · 10.9 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package main
import (
"bytes"
"encoding/hex"
"fmt"
"os"
"path"
"strings"
"testing"
"github.com/Eyevinn/mp4ff/mp4"
)
func TestNonRunningOptionCases(t *testing.T) {
infile := "../../mp4/testdata/cbcs_audio.mp4"
nonEncryptedFile := "../../mp4/testdata/prog_8s_dec_dashinit.mp4"
key := "00112233445566778899aabbccddeeff"
badKey := "00112233445566778899aabbccddeefx"
tmpDir := t.TempDir()
outFile := path.Join(tmpDir, "outfile.mp4")
cases := []struct {
desc string
args []string
err bool
}{
{desc: "no args", args: []string{"mp4ff-decrypt"}, err: true},
{desc: "unknown args", args: []string{"mp4ff-decrypt", "-x"}, err: true},
{desc: "no outfile", args: []string{"mp4ff-decrypt", "infile.mp4"}, err: true},
{desc: "no key", args: []string{"mp4ff-decrypt", "infile.mp4", outFile}, err: true},
{desc: "non-existing infile", args: []string{"mp4ff-decrypt", "-key", key, "infile.mp4", outFile}, err: true},
{desc: "non-existing initfile", args: []string{"mp4ff-decrypt", "-init", "init.mp4", "-key", key, infile, outFile}, err: true},
{desc: "bad infile", args: []string{"mp4ff-decrypt", "-key", key, "main.go", outFile}, err: true},
{desc: "short key", args: []string{"mp4ff-decrypt", "-key", "ab", infile, outFile}, err: true},
{desc: "bad key", args: []string{"mp4ff-decrypt", "-key", badKey, infile, outFile}, err: true},
{desc: "non-encrypted file", args: []string{"mp4ff-decrypt", "-key", key, nonEncryptedFile, outFile}, err: false},
{desc: "version", args: []string{"mp4ff-decrypt", "-version"}, err: false},
{desc: "help", args: []string{"mp4ff-decrypt", "-h"}, err: false},
}
for _, c := range cases {
t.Run(c.desc, func(t *testing.T) {
err := run(c.args)
if c.err && err == nil {
t.Error("expected error but got nil")
}
if !c.err && err != nil {
t.Errorf("unexpected error: %s", err)
}
})
}
}
func TestDecryptWithSeparateInit(t *testing.T) {
// Split a combined init+segments file into separate init and segment files,
// then decrypt using the separate init path and compare with golden output.
combinedFile := "../../mp4/testdata/prog_8s_enc_dashinit.mp4"
key := "63cb5f7184dd4b689a5c5ff11ee6a328"
combined, err := mp4.ReadMP4File(combinedFile)
if err != nil {
t.Fatal(err)
}
tmpDir := t.TempDir()
initPath := path.Join(tmpDir, "init.mp4")
segPath := path.Join(tmpDir, "seg.m4s")
outPath := path.Join(tmpDir, "out.mp4")
// Write init segment
initFh, err := os.Create(initPath)
if err != nil {
t.Fatal(err)
}
err = combined.Init.Encode(initFh)
initFh.Close()
if err != nil {
t.Fatal(err)
}
// Write all media segments
segFh, err := os.Create(segPath)
if err != nil {
t.Fatal(err)
}
for _, seg := range combined.Segments {
err = seg.Encode(segFh)
if err != nil {
segFh.Close()
t.Fatal(err)
}
}
segFh.Close()
// Decrypt using separate init
args := []string{"mp4ff-decrypt", "-init", initPath, "-key", key, segPath, outPath}
err = run(args)
if err != nil {
t.Fatalf("decrypt with separate init failed: %v", err)
}
// Decrypt the combined file the normal way for comparison
combinedOutPath := path.Join(tmpDir, "combined_out.mp4")
args = []string{"mp4ff-decrypt", "-key", key, combinedFile, combinedOutPath}
err = run(args)
if err != nil {
t.Fatal(err)
}
// The separate-init decrypted segments should match those from the combined decrypted file
combinedDec, err := mp4.ReadMP4File(combinedOutPath)
if err != nil {
t.Fatal(err)
}
var expectedSegs bytes.Buffer
for _, seg := range combinedDec.Segments {
err = seg.Encode(&expectedSegs)
if err != nil {
t.Fatal(err)
}
}
actualSegs, err := os.ReadFile(outPath)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(expectedSegs.Bytes(), actualSegs) {
t.Errorf("decrypted segments from separate init (%d bytes) differ from combined decrypt (%d bytes)",
len(actualSegs), expectedSegs.Len())
}
}
func TestDecodeFiles(t *testing.T) {
testCases := []struct {
desc string
initFile string
inFile string
expectedOutFile string
keyHexOrBase64 string
}{
{
desc: "cenc",
inFile: "../../mp4/testdata/prog_8s_enc_dashinit.mp4",
expectedOutFile: "../../mp4/testdata/prog_8s_dec_dashinit.mp4",
keyHexOrBase64: "63cb5f7184dd4b689a5c5ff11ee6a328",
},
{
desc: "cenc with base64 key",
inFile: "../../mp4/testdata/prog_8s_enc_dashinit.mp4",
expectedOutFile: "../../mp4/testdata/prog_8s_dec_dashinit.mp4",
keyHexOrBase64: "Y8tfcYTdS2iaXF/xHuajKA==",
},
{
desc: "cbcs",
inFile: "../../mp4/testdata/cbcs.mp4",
expectedOutFile: "../../mp4/testdata/cbcsdec.mp4",
keyHexOrBase64: "22bdb0063805260307ee5045c0f3835a",
},
{
desc: "cbcs audio",
inFile: "../../mp4/testdata/cbcs_audio.mp4",
expectedOutFile: "../../mp4/testdata/cbcs_audiodec.mp4",
keyHexOrBase64: "5ffd93861fa776e96cccd934898fc1c8",
},
{
desc: "PIFF audio",
initFile: "testdata/PIFF/audio/init.mp4",
inFile: "testdata/PIFF/audio/segment-1.0001.m4s",
expectedOutFile: "testdata/PIFF/audio/segment-1.0001_dec.m4s",
keyHexOrBase64: "602a9289bfb9b1995b75ac63f123fc86",
},
{
desc: "PIFF video",
inFile: "testdata/PIFF/video/complseg-1.0001.mp4",
expectedOutFile: "testdata/PIFF/video/complseg-1.0001_dec.mp4",
keyHexOrBase64: "602a9289bfb9b1995b75ac63f123fc86",
},
{
desc: "PIFF scheme WMA audio",
inFile: "testdata/PIFF/wma_piff_scheme/audio.mp4",
expectedOutFile: "testdata/PIFF/wma_piff_scheme/audio_dec.mp4",
keyHexOrBase64: "0b17cd8bfc86557341c77bbc6e4fe9a3",
},
}
tmpDir := t.TempDir()
for nr, c := range testCases {
t.Run(c.desc, func(t *testing.T) {
outFile := path.Join(tmpDir, fmt.Sprintf("out%d.mp4", nr))
args := []string{"mp4ff-decrypt"}
if c.initFile != "" {
args = append(args, "-init", c.initFile)
}
args = append(args, "-key", c.keyHexOrBase64, c.inFile, outFile)
err := run(args)
if err != nil {
t.Error(err)
}
expectedOut, err := os.ReadFile(c.expectedOutFile)
if err != nil {
t.Error(err)
}
out, err := os.ReadFile(outFile)
if err != nil {
t.Error(err)
}
if !bytes.Equal(expectedOut, out) {
t.Error("output file does not match expected")
}
})
}
}
func TestParseKeys(t *testing.T) {
legacyKey := "00112233445566778899aabbccddeeff"
kidWithDash := "855ca997-b201-5736-f3d6-a59c9eff84d9"
kidNoDash := "855ca997b2015736f3d6a59c9eff84d9"
t.Run("legacy key", func(t *testing.T) {
key, keysByKID, strictMode, err := parseKeys([]string{legacyKey})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if strictMode {
t.Fatal("expected non-strict mode")
}
if len(key) != 16 {
t.Fatalf("unexpected key length: %d", len(key))
}
if len(keysByKID) != 0 {
t.Fatalf("expected no kid map, got %d", len(keysByKID))
}
})
t.Run("duplicate kid fails", func(t *testing.T) {
_, _, _, err := parseKeys([]string{kidWithDash + ":" + legacyKey, kidNoDash + ":" + legacyKey})
if err == nil {
t.Fatal("expected duplicate kid error")
}
if !strings.Contains(err.Error(), "duplicate kid") {
t.Fatalf("unexpected error: %v", err)
}
})
t.Run("single kid:key pair", func(t *testing.T) {
_, keysByKID, strictMode, err := parseKeys([]string{kidNoDash + ":" + legacyKey})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !strictMode {
t.Fatal("expected strict mode")
}
if len(keysByKID) != 1 {
t.Fatalf("expected 1 kid map entry, got %d", len(keysByKID))
}
})
t.Run("multiple legacy keys fails", func(t *testing.T) {
_, _, _, err := parseKeys([]string{legacyKey, legacyKey})
if err == nil {
t.Fatal("expected error")
}
if !strings.Contains(err.Error(), "multiple legacy keys") {
t.Fatalf("unexpected error: %v", err)
}
})
t.Run("bad kid:key format", func(t *testing.T) {
_, _, _, err := parseKeys([]string{":"})
if err == nil {
t.Fatal("expected error")
}
if !strings.Contains(err.Error(), "bad kid:key format") {
t.Fatalf("unexpected error: %v", err)
}
})
t.Run("bad kid in pair", func(t *testing.T) {
_, _, _, err := parseKeys([]string{"badkid:" + legacyKey})
if err == nil {
t.Fatal("expected error")
}
if !strings.Contains(err.Error(), "unpacking kid") {
t.Fatalf("unexpected error: %v", err)
}
})
t.Run("bad key in pair", func(t *testing.T) {
_, _, _, err := parseKeys([]string{kidNoDash + ":badkey"})
if err == nil {
t.Fatal("expected error")
}
if !strings.Contains(err.Error(), "unpacking key for kid") {
t.Fatalf("unexpected error: %v", err)
}
})
t.Run("mixed mode fails", func(t *testing.T) {
_, _, _, err := parseKeys([]string{legacyKey, kidNoDash + ":" + legacyKey})
if err == nil {
t.Fatal("expected strict mixed mode error")
}
if !strings.Contains(err.Error(), "cannot mix") {
t.Fatalf("unexpected error: %v", err)
}
})
}
func TestStrictKIDKeySelection(t *testing.T) {
inFile := "../../mp4/testdata/cbcs_audio.mp4"
expectedOutFile := "../../mp4/testdata/cbcs_audiodec.mp4"
rawKey := "5ffd93861fa776e96cccd934898fc1c8"
tmpDir := t.TempDir()
outFile := path.Join(tmpDir, "outfile.mp4")
input, err := mp4.ReadMP4File(inFile)
if err != nil {
t.Fatal(err)
}
decInfo, err := mp4.DecryptInit(input.Init)
if err != nil {
t.Fatal(err)
}
if len(decInfo.TrackInfos) == 0 || decInfo.TrackInfos[0].Sinf == nil {
t.Fatal("missing encrypted track info")
}
kid := decInfo.TrackInfos[0].Sinf.Schi.Tenc.DefaultKID
kidHex := hex.EncodeToString(kid)
t.Run("matching kid decrypts", func(t *testing.T) {
args := []string{"mp4ff-decrypt", "-key", kidHex + ":" + rawKey, inFile, outFile}
err := run(args)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
expectedOut, err := os.ReadFile(expectedOutFile)
if err != nil {
t.Fatal(err)
}
out, err := os.ReadFile(outFile)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(expectedOut, out) {
t.Fatal("output file does not match expected")
}
})
t.Run("missing kid fails", func(t *testing.T) {
missingKID := kidHex
if missingKID[0] == '0' {
missingKID = "1" + missingKID[1:]
} else {
missingKID = "0" + missingKID[1:]
}
args := []string{"mp4ff-decrypt", "-key", missingKID + ":" + rawKey, inFile, outFile}
err := run(args)
if err == nil {
t.Fatal("expected missing kid error")
}
if !strings.Contains(err.Error(), "requested key was not found") {
t.Fatalf("unexpected error: %v", err)
}
})
}
func BenchmarkDecodeCenc(b *testing.B) {
inFile := "../../mp4/testdata/prog_8s_enc_dashinit.mp4"
hexKey := "63cb5f7184dd4b689a5c5ff11ee6a328"
raw, err := os.ReadFile(inFile)
if err != nil {
b.Error(err)
}
outData := make([]byte, 0, len(raw))
outBuf := bytes.NewBuffer(outData)
for i := 0; i < b.N; i++ {
inBuf := bytes.NewBuffer(raw)
outBuf.Reset()
key, err := mp4.UnpackKey(hexKey)
if err != nil {
b.Error(err)
}
err = decryptFileWithKeyMap(inBuf, nil, outBuf, key, nil, false)
if err != nil {
b.Error(err)
}
}
}