Add --no-encrypt with the same idea as the previous commit.
[dcpomatic.git] / test / create_cli_test.cc
1 /*
2     Copyright (C) 2019-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 #include "lib/config.h"
23 #include "lib/create_cli.h"
24 #include "lib/film.h"
25 #include "lib/ratio.h"
26 #include "lib/dcp_content_type.h"
27 #include "test.h"
28 #include <boost/test/unit_test.hpp>
29 #include <boost/tokenizer.hpp>
30 #include <boost/algorithm/string/predicate.hpp>
31 #include <iostream>
32
33
34 using std::string;
35
36
37 static CreateCLI
38 run (string cmd)
39 {
40         /* This approximates the logic which splits command lines up into argc/argv */
41
42         boost::escaped_list_separator<char> els ("", " ", "\"\'");
43         boost::tokenizer<boost::escaped_list_separator<char> > tok (cmd, els);
44
45         std::vector<char*> argv(256);
46         int argc = 0;
47
48         for (boost::tokenizer<boost::escaped_list_separator<char> >::iterator i = tok.begin(); i != tok.end(); ++i) {
49                 argv[argc++] = strdup (i->c_str());
50         }
51
52         CreateCLI cc (argc, argv.data());
53
54         for (int i = 0; i < argc; ++i) {
55                 free (argv[i]);
56         }
57
58         return cc;
59 }
60
61 BOOST_AUTO_TEST_CASE (create_cli_test)
62 {
63         CreateCLI cc = run ("dcpomatic2_create --version");
64         BOOST_CHECK (!cc.error);
65         BOOST_CHECK (cc.version);
66
67         cc = run ("dcpomatic2_create --versionX");
68         BOOST_REQUIRE (cc.error);
69         BOOST_CHECK (boost::algorithm::starts_with(*cc.error, "dcpomatic2_create: unrecognised option '--versionX'"));
70
71         cc = run ("dcpomatic2_create --help");
72         BOOST_REQUIRE (cc.error);
73
74         cc = run ("dcpomatic2_create -h");
75         BOOST_REQUIRE (cc.error);
76
77         cc = run ("dcpomatic2_create x --name frobozz --template bar");
78         BOOST_CHECK (!cc.error);
79         BOOST_CHECK_EQUAL(cc._name, "frobozz");
80         BOOST_REQUIRE(cc._template_name);
81         BOOST_CHECK_EQUAL(*cc._template_name, "bar");
82
83         cc = run ("dcpomatic2_create x --dcp-content-type FTR");
84         BOOST_CHECK (!cc.error);
85         BOOST_CHECK_EQUAL(cc._dcp_content_type, DCPContentType::from_isdcf_name("FTR"));
86
87         cc = run ("dcpomatic2_create x --dcp-frame-rate 30");
88         BOOST_CHECK (!cc.error);
89         BOOST_REQUIRE (cc.dcp_frame_rate);
90         BOOST_CHECK_EQUAL (*cc.dcp_frame_rate, 30);
91
92         cc = run ("dcpomatic2_create x --container-ratio 185");
93         BOOST_CHECK (!cc.error);
94         BOOST_CHECK_EQUAL(cc._container_ratio, Ratio::from_id("185"));
95
96         cc = run ("dcpomatic2_create x --container-ratio XXX");
97         BOOST_CHECK (cc.error);
98
99         cc = run ("dcpomatic2_create x --still-length 42");
100         BOOST_CHECK (!cc.error);
101         BOOST_CHECK_EQUAL(cc.still_length.get_value_or(0), 42);
102
103         cc = run ("dcpomatic2_create x --standard SMPTE");
104         BOOST_CHECK (!cc.error);
105         BOOST_CHECK_EQUAL(cc._standard, dcp::Standard::SMPTE);
106
107         cc = run ("dcpomatic2_create x --standard interop");
108         BOOST_CHECK (!cc.error);
109         BOOST_CHECK_EQUAL(cc._standard, dcp::Standard::INTEROP);
110
111         cc = run ("dcpomatic2_create x --standard SMPTEX");
112         BOOST_CHECK (cc.error);
113
114         cc = run("dcpomatic2_create x --no-encrypt");
115         BOOST_CHECK(cc._no_encrypt);
116
117         cc = run("dcpomatic2_create x --encrypt");
118         BOOST_CHECK(cc._encrypt);
119
120         cc = run("dcpomatic2_create x --no-encrypt --encrypt");
121         BOOST_CHECK(cc.error);
122
123         cc = run("dcpomatic2_create x --twod");
124         BOOST_CHECK(cc._twod);
125
126         cc = run("dcpomatic2_create x --threed");
127         BOOST_CHECK(cc._threed);
128
129         cc = run("dcpomatic2_create x --twod --threed");
130         BOOST_CHECK(cc.error);
131
132         cc = run ("dcpomatic2_create x --config foo/bar");
133         BOOST_CHECK (!cc.error);
134         BOOST_REQUIRE (cc.config_dir);
135         BOOST_CHECK_EQUAL (*cc.config_dir, "foo/bar");
136
137         cc = run ("dcpomatic2_create x --output fred/jim");
138         BOOST_CHECK (!cc.error);
139         BOOST_REQUIRE (cc.output_dir);
140         BOOST_CHECK_EQUAL (*cc.output_dir, "fred/jim");
141
142         cc = run ("dcpomatic2_create x --outputX fred/jim");
143         BOOST_CHECK (cc.error);
144
145         cc = run ("dcpomatic2_create --config foo/bar --still-length 42 --output flaps fred jim sheila");
146         BOOST_CHECK (!cc.error);
147         BOOST_REQUIRE (cc.config_dir);
148         BOOST_CHECK_EQUAL (*cc.config_dir, "foo/bar");
149         BOOST_CHECK_EQUAL(cc.still_length.get_value_or(0), 42);
150         BOOST_REQUIRE (cc.output_dir);
151         BOOST_CHECK_EQUAL (*cc.output_dir, "flaps");
152         BOOST_REQUIRE_EQUAL (cc.content.size(), 3U);
153         BOOST_CHECK_EQUAL (cc.content[0].path, "fred");
154         BOOST_CHECK_EQUAL (cc.content[0].frame_type, VideoFrameType::TWO_D);
155         BOOST_CHECK_EQUAL (cc.content[1].path, "jim");
156         BOOST_CHECK_EQUAL (cc.content[1].frame_type, VideoFrameType::TWO_D);
157         BOOST_CHECK_EQUAL (cc.content[2].path, "sheila");
158         BOOST_CHECK_EQUAL (cc.content[2].frame_type, VideoFrameType::TWO_D);
159
160         cc = run ("dcpomatic2_create --left-eye left.mp4 --right-eye right.mp4");
161         BOOST_REQUIRE_EQUAL (cc.content.size(), 2U);
162         BOOST_CHECK_EQUAL (cc.content[0].path, "left.mp4");
163         BOOST_CHECK_EQUAL (cc.content[0].frame_type, VideoFrameType::THREE_D_LEFT);
164         BOOST_CHECK_EQUAL (cc.content[1].path, "right.mp4");
165         BOOST_CHECK_EQUAL (cc.content[1].frame_type, VideoFrameType::THREE_D_RIGHT);
166         BOOST_CHECK_EQUAL(cc._fourk, false);
167
168         cc = run ("dcpomatic2_create --twok foo.mp4");
169         BOOST_REQUIRE_EQUAL (cc.content.size(), 1U);
170         BOOST_CHECK_EQUAL (cc.content[0].path, "foo.mp4");
171         BOOST_CHECK_EQUAL(cc._twok, true);
172         BOOST_CHECK (!cc.error);
173
174         cc = run ("dcpomatic2_create --fourk foo.mp4");
175         BOOST_REQUIRE_EQUAL (cc.content.size(), 1U);
176         BOOST_CHECK_EQUAL (cc.content[0].path, "foo.mp4");
177         BOOST_CHECK_EQUAL(cc._fourk, true);
178         BOOST_CHECK (!cc.error);
179
180         cc = run ("dcpomatic2_create --j2k-bandwidth 120 foo.mp4");
181         BOOST_REQUIRE_EQUAL (cc.content.size(), 1U);
182         BOOST_CHECK_EQUAL (cc.content[0].path, "foo.mp4");
183         BOOST_REQUIRE(cc._j2k_bandwidth);
184         BOOST_CHECK_EQUAL(*cc._j2k_bandwidth, 120000000);
185         BOOST_CHECK (!cc.error);
186
187         cc = run ("dcpomatic2_create --channel L fred.wav --channel R jim.wav sheila.wav");
188         BOOST_REQUIRE_EQUAL (cc.content.size(), 3U);
189         BOOST_CHECK_EQUAL (cc.content[0].path, "fred.wav");
190         BOOST_CHECK (cc.content[0].channel);
191         BOOST_CHECK (*cc.content[0].channel == dcp::Channel::LEFT);
192         BOOST_CHECK_EQUAL (cc.content[1].path, "jim.wav");
193         BOOST_CHECK (cc.content[1].channel);
194         BOOST_CHECK (*cc.content[1].channel == dcp::Channel::RIGHT);
195         BOOST_CHECK_EQUAL (cc.content[2].path, "sheila.wav");
196         BOOST_CHECK (!cc.content[2].channel);
197
198         cc = run ("dcpomatic2_create --channel foo fred.wav");
199         BOOST_REQUIRE (cc.error);
200         BOOST_CHECK (boost::algorithm::starts_with(*cc.error, "dcpomatic2_create: foo is not valid for --channel"));
201
202         cc = run ("dcpomatic2_create fred.wav --gain -6 jim.wav --gain 2 sheila.wav");
203         BOOST_REQUIRE_EQUAL (cc.content.size(), 3U);
204         BOOST_CHECK_EQUAL (cc.content[0].path, "fred.wav");
205         BOOST_CHECK (!cc.content[0].gain);
206         BOOST_CHECK_EQUAL (cc.content[1].path, "jim.wav");
207         BOOST_CHECK_CLOSE (*cc.content[1].gain, -6, 0.001);
208         BOOST_CHECK_EQUAL (cc.content[2].path, "sheila.wav");
209         BOOST_CHECK_CLOSE (*cc.content[2].gain, 2, 0.001);
210
211         cc = run("dcpomatic2_create --cpl 123456-789-0 dcp");
212         BOOST_REQUIRE_EQUAL(cc.content.size(), 1U);
213         BOOST_CHECK_EQUAL(cc.content[0].path, "dcp");
214         BOOST_REQUIRE(static_cast<bool>(cc.content[0].cpl));
215         BOOST_CHECK_EQUAL(*cc.content[0].cpl, "123456-789-0");
216
217         cc = run("dcpomatic2_create -s SMPTE sheila.wav");
218         BOOST_CHECK(!cc.still_length);
219         BOOST_CHECK(cc.error);
220 }
221
222
223 BOOST_AUTO_TEST_CASE(create_cli_template_test)
224 {
225         ConfigRestorer cr;
226
227         Config::override_path = "test/data";
228
229         auto cc = run("dcpomatic2_create test/data/flat_red.png");
230         auto film = cc.make_film();
231         BOOST_CHECK(!film->three_d());
232
233         cc = run("dcpomatic2_create test/data/flat_red.png --template 2d");
234         film = cc.make_film();
235         BOOST_CHECK(!film->three_d());
236
237         cc = run("dcpomatic2_create test/data/flat_red.png --template 2d --threed");
238         film = cc.make_film();
239         BOOST_CHECK(film->three_d());
240
241         cc = run("dcpomatic2_create test/data/flat_red.png --template 3d");
242         film = cc.make_film();
243         BOOST_CHECK(film->three_d());
244
245         cc = run("dcpomatic2_create test/data/flat_red.png --template 3d --twod");
246         film = cc.make_film();
247         BOOST_CHECK(!film->three_d());
248
249         cc = run("dcpomatic2_create test/data/flat_red.png");
250         film = cc.make_film();
251         BOOST_CHECK(!film->encrypted());
252
253         cc = run("dcpomatic2_create test/data/flat_red.png --template unencrypted");
254         film = cc.make_film();
255         BOOST_CHECK(!film->encrypted());
256
257         cc = run("dcpomatic2_create test/data/flat_red.png --template unencrypted --encrypt");
258         film = cc.make_film();
259         BOOST_CHECK(film->encrypted());
260
261         cc = run("dcpomatic2_create test/data/flat_red.png --template encrypted");
262         film = cc.make_film();
263         BOOST_CHECK(film->encrypted());
264
265         cc = run("dcpomatic2_create test/data/flat_red.png --template encrypted --no-encrypt");
266         film = cc.make_film();
267         BOOST_CHECK(!film->encrypted());
268 }
269