-
Notifications
You must be signed in to change notification settings - Fork 86
/
main_test.go
147 lines (141 loc) · 4.52 KB
/
main_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
package main
import (
"bytes"
"fmt"
"os"
"path"
"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 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",
},
}
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 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 = decryptFile(inBuf, nil, outBuf, key)
if err != nil {
b.Error(err)
}
}
}