-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommand_line_test.py
More file actions
290 lines (255 loc) · 12.7 KB
/
Copy pathcommand_line_test.py
File metadata and controls
290 lines (255 loc) · 12.7 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
import os
import subprocess
import tempfile
import unittest
from vmaf import ExternalProgram, run_process
from vmaf.config import VmafConfig
from vmaf.tools.misc import MyTestCase
__copyright__ = "Copyright 2016-2020, Netflix, Inc."
__license__ = "BSD+Patent"
class RunProcessTest(MyTestCase):
def test_run_process(self):
ret = run_process("echo hello", shell=True)
self.assertEqual(ret, 0)
def test_run_process_false_cmd(self):
with self.assertRaises(AssertionError) as e:
run_process("echoo hello", shell=True)
self.assertTrue("Process returned 127, cmd: echoo hello" in e.exception.args[0])
self.assertTrue("not found" in e.exception.args[0])
class CommandLineTest(MyTestCase):
def setUp(self):
super().setUp()
self.dataset_filename = VmafConfig.test_resource_path("example_dataset.py")
self.raw_dataset_filename = VmafConfig.test_resource_path("example_raw_dataset.py")
self.out_model_filepath = VmafConfig.workdir_path("tmp.json")
self.param_filename = VmafConfig.test_resource_path("vmaf_v4.py")
self.batch_filename = VmafConfig.workdir_path("test_batch_input")
def tearDown(self):
if os.path.exists(self.out_model_filepath):
os.remove(self.out_model_filepath)
if os.path.exists(self.out_model_filepath + ".model"):
os.remove(self.out_model_filepath + ".model")
if os.path.exists(self.batch_filename):
os.remove(self.batch_filename)
super().tearDown()
def test_run_testing_vmaf(self):
exe = VmafConfig.root_path("python", "vmaf", "script", "run_testing.py")
cmd = "{exe} VMAF {dataset} --parallelize --suppress-plot".format(
exe=exe, dataset=self.dataset_filename
)
ret = run_process(cmd, shell=True)
self.assertEqual(ret, 0)
def test_run_testing_vmaf_raw_dataset(self):
exe = VmafConfig.root_path("python", "vmaf", "script", "run_testing.py")
cmd = "{exe} VMAF {dataset} --parallelize --suppress-plot".format(
exe=exe, dataset=self.raw_dataset_filename
)
ret = run_process(cmd, shell=True)
self.assertEqual(ret, 0)
def test_run_testing_psnr(self):
exe = VmafConfig.root_path("python", "vmaf", "script", "run_testing.py")
cmd = "{exe} PSNR {dataset} --parallelize --suppress-plot".format(
exe=exe, dataset=self.dataset_filename
)
ret = run_process(cmd, shell=True)
self.assertEqual(ret, 0)
def test_run_testing_proccesses0(self):
exe = VmafConfig.root_path("python", "vmaf", "script", "run_testing.py")
cmd = "{exe} PSNR {dataset} --parallelize --suppress-plot --processes 0".format(
exe=exe, dataset=self.dataset_filename
)
with self.assertRaises(AssertionError):
run_process(cmd, shell=True)
def test_run_testing_proccesses2_without_parallelize(self):
exe = VmafConfig.root_path("python", "vmaf", "script", "run_testing.py")
cmd = "{exe} PSNR {dataset} --suppress-plot --processes 2".format(
exe=exe, dataset=self.dataset_filename
)
with self.assertRaises(AssertionError):
run_process(cmd, shell=True)
def test_run_vmaf_training(self):
exe = VmafConfig.root_path("python", "vmaf", "script", "run_vmaf_training.py")
cmd = "{exe} {dataset} {param} {param} {output} --parallelize --suppress-plot".format(
exe=exe,
dataset=self.dataset_filename,
param=self.param_filename,
output=self.out_model_filepath,
)
ret = run_process(cmd, shell=True)
self.assertEqual(ret, 0)
def test_run_vmaf_training_processes0(self):
exe = VmafConfig.root_path("python", "vmaf", "script", "run_vmaf_training.py")
cmd = "{exe} {dataset} {param} {param} {output} --parallelize --suppress-plot --processes 0".format(
exe=exe,
dataset=self.dataset_filename,
param=self.param_filename,
output=self.out_model_filepath,
)
with self.assertRaises(AssertionError):
run_process(cmd, shell=True)
def test_run_vmaf_training_processes2_without_parallelize(self):
exe = VmafConfig.root_path("python", "vmaf", "script", "run_vmaf_training.py")
cmd = "{exe} {dataset} {param} {param} {output} --suppress-plot --processes 2".format(
exe=exe,
dataset=self.dataset_filename,
param=self.param_filename,
output=self.out_model_filepath,
)
with self.assertRaises(AssertionError):
run_process(cmd, shell=True)
def test_run_vmaf_training_raw_dataset(self):
exe = VmafConfig.root_path("python", "vmaf", "script", "run_vmaf_training.py")
cmd = "{exe} {dataset} {param} {param} {output} --parallelize --suppress-plot".format(
exe=exe,
dataset=self.raw_dataset_filename,
param=self.param_filename,
output=self.out_model_filepath,
)
ret = run_process(cmd, shell=True)
self.assertEqual(ret, 0)
def test_run_vmaf(self):
exe = VmafConfig.root_path("python", "vmaf", "script", "run_vmaf.py")
line = (
"yuv420p 576 324 {root}/python/test/resource/yuv/src01_hrc00_576x324.yuv "
"{root}/python/test/resource/yuv/src01_hrc01_576x324.yuv".format(
root=VmafConfig.root_path()
)
)
cmd = "{exe} {line} >/dev/null 2>&1".format(line=line, exe=exe)
ret = run_process(cmd, shell=True)
self.assertEqual(ret, 0)
def test_run_vmaf_ci(self):
exe = VmafConfig.root_path("python", "vmaf", "script", "run_vmaf.py")
line = (
"yuv420p 576 324 {root}/python/test/resource/yuv/src01_hrc00_576x324.yuv "
"{root}/python/test/resource/yuv/src01_hrc01_576x324.yuv".format(
root=VmafConfig.root_path()
)
)
cmd = "{exe} {line} --ci >/dev/null 2>&1".format(line=line, exe=exe)
ret = run_process(cmd, shell=True)
self.assertEqual(ret, 0)
def test_run_vmaf_both_local_explain_and_ci(self):
exe = VmafConfig.root_path("python", "vmaf", "script", "run_vmaf.py")
line = (
"yuv420p 576 324 {root}/python/test/resource/yuv/src01_hrc00_576x324.yuv "
"{root}/python/test/resource/yuv/src01_hrc01_576x324.yuv".format(
root=VmafConfig.root_path()
)
)
cmd = "{exe} {line} --local-explain --ci >/dev/null 2>&1".format(line=line, exe=exe)
# `cmd` is built from VmafConfig.root_path() + hardcoded test-fixture
# paths under python/test/resource/yuv/. No attacker-controlled string
# flows in. shell=True is needed for `>/dev/null 2>&1` redirection /
# CLI argument quoting. See Research-0090, F13–F17.
# nosemgrep: python.lang.security.audit.subprocess-shell-true.subprocess-shell-true
ret = subprocess.call(cmd, shell=True)
self.assertEqual(ret, 2)
def test_run_psnr(self):
exe = VmafConfig.root_path("python", "vmaf", "script", "run_psnr.py")
line = (
"yuv420p 576 324 {root}/python/test/resource/yuv/src01_hrc00_576x324.yuv "
"{root}/python/test/resource/yuv/src01_hrc01_576x324.yuv".format(
root=VmafConfig.root_path()
)
)
cmd = "{exe} {line} >/dev/null 2>&1".format(line=line, exe=exe)
ret = run_process(cmd, shell=True)
self.assertEqual(ret, 0)
def test_run_cleaning_cache_psnr(self):
exe = VmafConfig.root_path("python", "vmaf", "script", "run_testing.py")
cmd = "{exe} PSNR {dataset} --parallelize --cache-result --suppress-plot".format(
exe=exe, dataset=self.dataset_filename
)
ret = run_process(cmd, shell=True)
self.assertEqual(ret, 0)
exe = VmafConfig.root_path("python", "vmaf", "script", "run_cleaning_cache.py")
cmd = "{exe} PSNR {dataset}".format(exe=exe, dataset=self.dataset_filename)
ret = run_process(cmd, shell=True)
self.assertEqual(ret, 0)
class VmafexecCommandLineTest(MyTestCase):
RC_SUCCESS = 0
def setUp(self) -> None:
super().setUp()
self.output_file_path = tempfile.NamedTemporaryFile().name
def tearDown(self) -> None:
if os.path.exists(self.output_file_path):
os.remove(self.output_file_path)
super().tearDown()
def test_run_vmafexec(self):
exe = ExternalProgram.vmafexec
cmd = (
"{exe} --reference {ref} --distorted {dis} --width 576 --height 324 --pixel_format 420 --bitdepth 8 --xml --feature psnr "
"--model path={model} --quiet --output {output}".format(
exe=exe,
ref=VmafConfig.test_resource_path("yuv", "src01_hrc00_576x324.yuv"),
dis=VmafConfig.test_resource_path("yuv", "src01_hrc01_576x324.yuv"),
model=VmafConfig.model_path("other_models", "vmaf_v0.6.0.json"),
output=self.output_file_path,
)
)
# `cmd` is built from VmafConfig.root_path() + hardcoded test-fixture
# paths under python/test/resource/yuv/. No attacker-controlled string
# flows in. shell=True is needed for `>/dev/null 2>&1` redirection /
# CLI argument quoting. See Research-0090, F13–F17.
# nosemgrep: python.lang.security.audit.subprocess-shell-true.subprocess-shell-true
ret = subprocess.call(cmd, shell=True)
self.assertEqual(ret, self.RC_SUCCESS)
with open(self.output_file_path, "rt") as fo:
fc = fo.read()
self.assertTrue(
'<metric name="psnr_y" min="29.640688" max="34.760779" mean="30.755064" harmonic_mean="30.727905" />'
in fc
)
def test_run_vmafexec_with_frame_skipping(self):
exe = ExternalProgram.vmafexec
cmd = (
"{exe} --reference {ref} --distorted {dis} --width 576 --height 324 --pixel_format 420 --bitdepth 8 --xml --feature psnr "
"--model path={model} --quiet --output {output} --frame_skip_ref 2 --frame_skip_dist 2".format(
exe=exe,
ref=VmafConfig.test_resource_path("yuv", "src01_hrc00_576x324.yuv"),
dis=VmafConfig.test_resource_path("yuv", "src01_hrc01_576x324.yuv"),
model=VmafConfig.model_path("other_models", "vmaf_v0.6.0.json"),
output=self.output_file_path,
)
)
# `cmd` is built from VmafConfig.root_path() + hardcoded test-fixture
# paths under python/test/resource/yuv/. No attacker-controlled string
# flows in. shell=True is needed for `>/dev/null 2>&1` redirection /
# CLI argument quoting. See Research-0090, F13–F17.
# nosemgrep: python.lang.security.audit.subprocess-shell-true.subprocess-shell-true
ret = subprocess.call(cmd, shell=True)
self.assertEqual(ret, self.RC_SUCCESS)
with open(self.output_file_path, "rt") as fo:
fc = fo.read()
self.assertTrue(
'<metric name="psnr_y" min="29.640688" max="33.788213" mean="30.643458" harmonic_mean="30.626214" />'
in fc
)
def test_run_vmafexec_with_frame_skipping_unequal(self):
exe = ExternalProgram.vmafexec
cmd = (
"{exe} --reference {ref} --distorted {dis} --width 576 --height 324 --pixel_format 420 --bitdepth 8 --xml --feature psnr "
"--model path={model} --quiet --output {output} --frame_skip_ref 2 --frame_skip_dist 5".format(
exe=exe,
ref=VmafConfig.test_resource_path("yuv", "src01_hrc00_576x324.yuv"),
dis=VmafConfig.test_resource_path("yuv", "src01_hrc01_576x324.yuv"),
model=VmafConfig.model_path("other_models", "vmaf_v0.6.0.json"),
output=self.output_file_path,
)
)
# `cmd` is built from VmafConfig.root_path() + hardcoded test-fixture
# paths under python/test/resource/yuv/. No attacker-controlled string
# flows in. shell=True is needed for `>/dev/null 2>&1` redirection /
# CLI argument quoting. See Research-0090, F13–F17.
# nosemgrep: python.lang.security.audit.subprocess-shell-true.subprocess-shell-true
ret = subprocess.call(cmd, shell=True)
self.assertEqual(ret, self.RC_SUCCESS)
with open(self.output_file_path, "rt") as fo:
fc = fo.read()
self.assertTrue(
'<metric name="psnr_y" min="19.019327" max="21.084954" mean="20.269606" harmonic_mean="20.258113" />'
in fc
)
if __name__ == "__main__":
unittest.main(verbosity=2)